Python igraph: get all possible paths in a directed graph

后端 未结 6 1091
星月不相逢
星月不相逢 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:35

    Since you mentioned in your question that your ultimate goal is to get only the nodes that are in these paths and not the paths themselves, I think you don't even have to calculate the paths.

    The Graph object in igraph has a method called subcomponent. By default, it gives you all the nodes that are in the same (weakly connected) component as a given input node. However, it also has a mode argument. When you set mode to "out", it will give you all the nodes that are reachable from a certain node. When you set mode to "in", it will give you all the nodes from where you can reach a certain node. So, you'll probably need the intersection of the set of reachable nodes from your source vertex and the set of nodes that can reach your target vertex:

    s=set(graph.subcomponent(source, mode="out"))
    t=set(graph.subcomponent(target, mode="in"))
    s.intersection(t)
    

    This is probably way faster than calculating all the paths anyway.

提交回复
热议问题