all_pairs_shortest_path#
- all_pairs_shortest_path(G, cutoff=None)[source]#
计算所有节点之间的最短路径。
- Parameters:
- GNetworkX 图
- cutoff整数, 可选
停止搜索的深度。仅返回长度最多为
cutoff的路径。
- Returns:
- paths迭代器
按源和目标键控的最短路径字典。
See also
floyd_warshallall_pairs_all_shortest_paths
Notes
两个节点之间可能存在多个相同长度的最短路径。对于每一对节点,此函数仅返回其中一条路径。
Examples
>>> G = nx.path_graph(5) >>> path = dict(nx.all_pairs_shortest_path(G)) >>> print(path[0][4]) [0, 1, 2, 3, 4]
Additional backends implement this function
- parallelParallel backend for NetworkX algorithms
The parallel implementation first divides the nodes into chunks and then creates a generator to lazily compute shortest paths for each
node_chunk, and then employs joblib’sParallelfunction to execute these computations in parallel across all available CPU cores.- Additional parameters:
- get_chunksstr, function (default = “chunks”)
A function that takes in an iterable of all the nodes as input and returns an iterable
node_chunks. The default chunking is done by slicing theG.nodesintonchunks, wherenis the number of CPU cores.
[Source]