热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Force-DirectLayoutofGraph力导向图MATLAB实现

•Wealreadyknow:–Themostcommongraphicalrepresentationofanetworkisanode-linkdiagram,

•We already know:
–The most common graphical representation of a network is a node-link diagram, where each node is shown as a point, circle, polygon, or some other small graphical object, and each edge is shown as a line segment or curve connecting two nodes.
•Force-Direct Layout idea:
–We imagine the nodes as physical particles that are initialized with random positions, but are gradually displaced under the effect of various forces, until they arrive at a final position. The forces are defined by the chosen algorithm, and typically seek to position adjacent nodes near each other, but not too near
•Specifically, imagine that we simulate two forces: a repulsive forcebetween all pairs of nodes, and a spring forcebetween all pairs of adjacent nodes.
•Let d be the current distance between two nodes, and define the repulsive force between them to be
=/^2
(a definition inspired by inverse-square laws such as Coulomb’s law), where ??is some constant.
•If the nodes are adjacent, let the spring force between them be
=(−)
(inspired by Hooke’s law), where Ks is the spring constant and L is the rest length of the spring (i.e., the length “preferred” by the edge, ignoring the repulsive force)
•Implementation
–To implement this force-directed layout, assume that the nodes are stored in an array nodes[],where each element of the array contains a position x, y and the net force force_x, force_yacting on the node.
–The forces are simulated in a loop that computes the net forces at each time step and updates the positions of the nodes, hopefully until the layout converges to some good distributed positions.
伪代码:
伪代码
源数据:链接:https://pan.baidu.com/s/1qYF0nT8l1QBonlpw0NXxDQ 密码:878o
MATLAB代码如下:

DATA = int32(xlsread('bus685.xlsx',1,'A1:B1282'))+1;
position =xlsread('position.xlsx',1,'A1:B685');

K_r = 0.1;
K_s = 0.025;
L = 2;
delta_t = 10;
MaxLength =8;
tic
for k = 1:100
force = zeros(685,2);
for m = 2:684
for n = m+1:685
dx = position(n,1)-position(m,1);
dy = position(n,2)-position(m,2);
if dx~=0 || dy~=0
dist2 = dx^2+dy^2;
dist = sqrt(dist2);
f = K_r/dist2;
fx = f*dx/dist;
fy = f*dy/dist;
force(m,1) = force(m,1)-fx;
force(m,2) = force(m,2)-fy;
force(n,1) = force(n,1)+fx;
force(n,2) = force(n,2)+fy;
end
end
end

for m = 2:685
ne=find(DATA(:,1)==m);
for n = 1:length(ne)

dx = position(DATA(ne(n),2),1)-position(m,1);
dy = position(DATA(ne(n),2),2)-position(m,2);
if dx~=0||dy~=0
dist = sqrt(dx^2+dy^2);
f = K_s*(dist-L);
fx = f*dx/dist;
fy = f*dy/dist;
force(m,1) = force(m,1)+fx;
force(m,2) = force(m,2)+fy;
force(DATA(ne(n),2),1) = force(DATA(ne(n),2),1)-fx;
force(DATA(ne(n),2),2) = force(DATA(ne(n),2),2)-fy;

end
end
end

for i = 2:685
dx = delta_t*force(i,1);
dy = delta_t*force(i,2);
displacement = dx^2+dy^2;
if(displacement>MaxLength)
s = sqrt(MaxLength/displacement);
dx = s*dx;
dy = s*dy;
end
position(i,1) = position(i,1)+dx;
position(i,2) = position(i,2)+dy;
end
end
toc
for i = 1:1282
s = DATA(i,1);
d = DATA(i,2);
x = [position(s,1);position(d,1)];
y = [position(s,2);position(d,2)];
plot(x,y,'o-b');
hold on;
end
sum = 0;
for j = 1:685
sum = sum+abs(force(j,1))+abs(force(j,2));
end
sum

图片如下:
迭代10次效果:
这里写图片描述
这里写图片描述
从图中我们可以看出,当N在一个相对较小的范围内时,随着N的增大,所绘制的力导向图变化较大,效果也变得越好,但是当N较大时,随着N的增大,力导向图的变化就不大了,但有收缩的趋势(变得扁平了),原因可能是此时引力的作用占据了主导地位。


推荐阅读
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • 3.223.28周学习总结中的贪心作业收获及困惑
    本文是对3.223.28周学习总结中的贪心作业进行总结,作者在解题过程中参考了他人的代码,但前提是要先理解题目并有解题思路。作者分享了自己在贪心作业中的收获,同时提到了一道让他困惑的题目,即input details部分引发的疑惑。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 本文讨论了一个数列求和问题,该数列按照一定规律生成。通过观察数列的规律,我们可以得出求解该问题的算法。具体算法为计算前n项i*f[i]的和,其中f[i]表示数列中有i个数字。根据参考的思路,我们可以将算法的时间复杂度控制在O(n),即计算到5e5即可满足1e9的要求。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • Java 11相对于Java 8,OptaPlanner性能提升有多大?
    本文通过基准测试比较了Java 11和Java 8对OptaPlanner的性能提升。测试结果表明,在相同的硬件环境下,Java 11相对于Java 8在垃圾回收方面表现更好,从而提升了OptaPlanner的性能。 ... [详细]
author-avatar
苏绿儿520
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有