Python: How to find if a path exists between 2 nodes in a graph?

后端 未结 5 2252
野性不改
野性不改 2021-01-01 17:55

I am using networkx package of Python.

5条回答
  •  梦谈多话
    2021-01-01 18:29

    Using a disjoint set data structure:

    Create a singleton set for every vertex in the graph, then union the sets containing each of the pair of vertices for every edge in the graph.

    Finally, you know a path exists between two vertices if they are in the same set.

    See the wikipedia page on the disjoint set data structure.

    This is much more efficient than using a path finding algorithm.

提交回复
热议问题