I wanted to generate all the possible paths from one node to the other with this program:
graph = { "a" : ["b","c"],
"b" : ["a", "d"],
"c" : ["a", "d"],
"d" : ["e"],
"e" : ["b"]
}
def find_all_paths(graph, start, end, path=[]):
path = path + [start]
if start == end:
return [path]
paths = []
for node in graph[start]:
if node not in path:
newpaths = find_all_paths(graph, node, end, path)
for newpath in newpaths:
paths.append(newpath)
return paths
print(find_all_paths(graph, "b","e"))
But unfortunately I get the Error: UnboundLocalError: local variable 'newpaths' referenced before assignment
Can anyone help me?