Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get vertices in shortest path using python igraph? [duplicate]

I'm using igraph to generate a matrix of shortest path distances between pairs of vertices but I can't figure out how to return the vertices. So far I have:

path_length_matrix = ig_graph.shortest_paths_dijkstra(None,None,"distance", "ALL")

I'm looking for a function which returns a matrix of paths like the matrix of distances but I can't see anything in the igraph documentation which shows how to get the paths.

like image 463
Jamie Bull Avatar asked Nov 21 '22 15:11

Jamie Bull


2 Answers

The function you need is get_shortest_paths I believe. See https://igraph.org/python/api/latest/igraph.GraphBase.html#get_shortest_paths

You need to call it individually for each source vertex, and it will give you only a single (arbitrary) shortest path for each pair of nodes. If you need all shortest paths, then see get_all_shortest_paths: https://igraph.org/python/api/latest/igraph.GraphBase.html#get_all_shortest_paths

like image 119
Gabor Csardi Avatar answered Nov 24 '22 03:11

Gabor Csardi


I do this

from igraph import *
g = Graph([(0,1), (0,2), (2,3), (3,4), (4,2), (2,5), (5,0), (6,3), (5,6)])
g.vs["name"] = ["Alice", "Bob", "Claire", "Dennis", "Esther", "Frank", "George"]
#You could create Vertexes like g.add_vertex(name="Bill") 
path=g.get_shortest_paths("Alice",to="Frank",mode=OUT,output='vpath')
for n in path[0]:
    print("{}".format(g.vs[n]['name']))

Hope this helps

like image 30
Tim Seed Avatar answered Nov 24 '22 05:11

Tim Seed