all_pairs_shortest_path_length#

all_pairs_shortest_path_length(G, cutoff=None)[源代码]#

计算中所有节点之间的最短路径长度 G .

参数
G网络X图表
cutoff整数,可选

停止搜索的深度。仅限最多长度的路径 cutoff 都被退回了。

返回
lengths迭代器

(源,字典)迭代器,字典以目标为关键字,最短路径长度为关键字值。

笔记

返回的迭代器只有可访问的节点对。

实例

>>> G = nx.path_graph(5)
>>> length = dict(nx.all_pairs_shortest_path_length(G))
>>> for node in [0, 1, 2, 3, 4]:
...     print(f"1 - {node}: {length[1][node]}")
1 - 0: 1
1 - 1: 0
1 - 2: 1
1 - 3: 2
1 - 4: 3
>>> length[3][2]
1
>>> length[2][2]
0