热门标签 | 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的增大,力导向图的变化就不大了,但有收缩的趋势(变得扁平了),原因可能是此时引力的作用占据了主导地位。


推荐阅读
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 深入解析JVM垃圾收集器
    本文基于《深入理解Java虚拟机:JVM高级特性与最佳实践》第二版,详细探讨了JVM中不同类型的垃圾收集器及其工作原理。通过介绍各种垃圾收集器的特性和应用场景,帮助读者更好地理解和优化JVM内存管理。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • [论文笔记] Crowdsourcing Translation: Professional Quality from Non-Professionals (ACL, 2011)
    Time:4hoursTimespan:Apr15–May3,2012OmarZaidan,ChrisCallison-Burch:CrowdsourcingTra ... [详细]
  • Docker的安全基准
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • 深入解析Android自定义View面试题
    本文探讨了Android Launcher开发中自定义View的重要性,并通过一道经典的面试题,帮助开发者更好地理解自定义View的实现细节。文章不仅涵盖了基础知识,还提供了实际操作建议。 ... [详细]
  • 探讨如何高效使用FastJSON进行JSON数据解析,特别是从复杂嵌套结构中提取特定字段值的方法。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文介绍如何利用动态规划算法解决经典的0-1背包问题。通过具体实例和代码实现,详细解释了在给定容量的背包中选择若干物品以最大化总价值的过程。 ... [详细]
  • 本文基于刘洪波老师的《英文词根词缀精讲》,深入探讨了多个重要词根词缀的起源及其相关词汇,帮助读者更好地理解和记忆英语单词。 ... [详细]
  • 深入理解 SQL 视图、存储过程与事务
    本文详细介绍了SQL中的视图、存储过程和事务的概念及应用。视图为用户提供了一种灵活的数据查询方式,存储过程则封装了复杂的SQL逻辑,而事务确保了数据库操作的完整性和一致性。 ... [详细]
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社区 版权所有