分析图形

基础处理

分析图形的结构 G 可以使用各种图论函数进行分析,例如:

1
2
3
4
5
6
7
8
9
G = nx.Graph()
G.add_edges_from([(1, 2), (1, 3)])
G.add_node("spam") # adds node "spam"
list(nx.connected_components(G))
[{1, 2, 3}, {'spam'}]
sorted(d for n, d in G.degree())
[0, 1, 1, 2]
nx.clustering(G)
{1: 0, 2: 0, 3: 0, 'spam': 0}

一些具有大输出的函数迭代(节点、值)2元组。这些很容易存储在 dict 结构,如果你愿意的话。

1
2
3
sp = dict(nx.all_pairs_shortest_path(G))
sp[3]
{3: [3], 1: [3, 1], 2: [3, 1, 2]}

图算法