Python igraph: get all possible paths in a directed graph

后端 未结 6 1106
星月不相逢
星月不相逢 2021-01-06 12:49

I am using igraph (Python) and would like to get all possible paths between two nodes in a directed graph. I am aware of the function get_all_shortest_paths, wh

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-06 13:52

    for this graph:

    import igraph
    G = ig.Graph()
    #ring
    G.add_vertices(4)
    G.add_edges([(0,1), (1,2),(2,3),(3,0)])
    G = G.as_directed()
    print G.is_directed()
    print G
    

    If I apply the function from above https://stackoverflow.com/a/29324009/2772305

    like

    for p in find_all_paths(G,0,0):
        print p
    

    I get only

    [0] as a result, whereas there should be a second path [0,1,2,3,0] imho

    How do i find all paths if there are such rings in a graph?

    in networkx, it is possible to get the desired result with all_simple_paths:

    import networkx as nx
    G = nx.MultiDiGraph()
    G.add_path(['a','b','c','d','a'])
    G.add_path(['a','e','f','g'])
    G.add_path(['a','a'])
    for p in  nx.all_simple_paths(G,'a','a'):
        print p
    

    Result:

    ['a', 'a']
    ['a', 'b', 'c', 'd', 'a']
    

    As said in above comments , the all_simple_paths function exists only in networkx, which is not suitable to process huge graphs due to performance issues. Is there any way to bring the all_simple_paths from networkx to igraph ?

提交回复
热议问题