Path between two nodes

后端 未结 3 830
后悔当初
后悔当初 2020-12-01 13:31

I\'m using networkx to work with graphs. I have pretty large graph (it\'s near 200 nodes in it) and I try to find all possible paths between two nodes. But, as I understand,

3条回答
  •  既然无缘
    2020-12-01 13:50

    This one actually works with networkx, and it's non-recursive, which may be nice for large graphs.

    def find_all_paths(graph, start, end):
        path  = []
        paths = []
        queue = [(start, end, path)]
        while queue:
            start, end, path = queue.pop()
            print 'PATH', path
    
            path = path + [start]
            if start == end:
                paths.append(path)
            for node in set(graph[start]).difference(path):
                queue.append((node, end, path))
        return paths
    

提交回复
热议问题