获取节点位置和边长

时间:2015-07-14 19:44:10

标签: python graph networkx

有人可以告诉我如何获取节点'位置和边缘的长度,而不是自己计算?

var myListOfStrings = listOfArrays.Select(a => a[1]).ToList();

1 个答案:

答案 0 :(得分:3)

节点没有任何位置,除非您为它们分配一个位置。幸运的是,Networkx有一些布局算法already implemented:

# Position nodes on a circle.
pos = circular_layout(G[, dim, scale])

# Position nodes uniformly at random in the unit square.
pos = random_layout(G[, dim])

# Position nodes in concentric circles.
pos = shell_layout(G[, nlist, dim, scale])  

# Position nodes using Fruchterman-Reingold force-directed algorithm.
pos = spring_layout(G[, dim, k, pos, fixed, ...])   

# Position nodes using the eigenvectors of the graph Laplacian.
pos = spectral_layout(G[, dim, weight, scale])

返回值pos是由节点索引的位置字典。

没有能够给出内置边长的函数,但是一旦你有了每个节点的坐标,就可以用简单的公式计算边长:

import math
dist_node12 = math.sqrt((pos[node1][0] - pos[node2][0])**2 + (pos[node1][1] - pos[node2][1])**2)