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