查找 NetworkX 中所有节点对之间的所有最短路径

Sha*_*Han 3 python graph-theory shortest-path networkx python-3.x

我试图获取无向未加权图中所有节点对之间的所有最短路径。我目前正在使用nx.all_pairs_shortest_path(),但我不明白为什么它只为每对节点返回一条最短路径。我的图中存在循环,因此某些节点之间应该存在多个最短路径。有什么建议么?

小智 5

迭代图中的所有节点:

results = []
for n1 in G.nodes():
    for n2 in G.nodes():
        shortest_path = nx.single_source_dijkstra(G, source=n1, target=n2, weight=f)
        results.append(shortest_path)
        
Run Code Online (Sandbox Code Playgroud)