Force-Direct Layout of Graph力导向图MATLAB实现

•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://download.csdn.net/download/kswkly/10210735

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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值