[英]How to get the longest shortest path in my graph?
I'm trying to get all longest shortest paths on my graph and i did write this code:我试图在我的图表上获得所有最长的最短路径,并且我确实编写了以下代码:
for source in g.nodes:
for dist in g.nodes:
if source!=dist:
distination=len(nx.shortest_path(g,source,dist))
if distination>3:
print(source,dist,distination)
the out put of this code is like this这段代码的输出是这样的
0 1 7
0 2 7
0 3 7
0 4 6
0 5 6
0 6 5
0 7 4
0 8 7
0 9 6
0 10 7
0 11 8
0 12 5
0 13 7
0 14 8
0 15 5
0 16 8
0 18 5
0 19 6
0 20 6
0 21 6
0 22 8
0 23 5
0 26 7
0 27 8
0 28 5
0 29 8
0 30 4
0 31 8
it gave me the the required results but, is there is more professionals way to do it ?它给了我所需的结果,但是,是否有更专业的方法来做到这一点?
you can use all_pairs_dijkstra_path_length
wich will give you all the shortest path for each node and then you can loop with it你可以使用
all_pairs_dijkstra_path_length
会给你每个节点的所有最短路径,然后你可以用它循环
ppp=nx.all_pairs_dijkstra_path_length(g)
for i in ppp:
node=max(i[1], key=i[1].get)
print(i[0],node,len(nx.shortest_path(g,i[0],node)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.