python graph paths

Solutions on MaxInterview for python graph paths by the best coders in the world

showing results for - "python graph paths"
Layla
29 Jun 2020
1    def find_all_paths(graph, start, end, path=[]):
2        path = path + [start]
3        if start == end:
4            return [path]
5        if not graph.has_key(start):
6            return []
7        paths = []
8        for node in graph[start]:
9            if node not in path:
10                newpaths = find_all_paths(graph, node, end, path)
11                for newpath in newpaths:
12                    paths.append(newpath)
13        return paths
14