Reputation: 788
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 ?
Upvotes: 1
Views: 1060
Reputation: 788
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
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)))
Upvotes: 1