I have the following recursive function - The function works well to print out all the paths of a tree/graph. But trying to add ROUTES as a global variable and appending to it results in a bunch of empty nested lists:
[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [],...etc
A better solution to using a global variable and a better solution to storing the paths is what I'm looking for and this is my function:
def printAllPathsUtil(self, u, d, visited, path):
# Mark the current node as visited and store in path
visited[u] = True
path.append(u)
# If current vertex is same as destination, then print
# current path[]
if u == d:
print(path)
ROUTES.append(path)
else:
# If current vertex is not destination
# Recur for all the vertices adjacent to this vertex
for i in self.graph[u]:
if visited[i] == False:
self.printAllPathsUtil(i, d, visited, path)
# Remove current vertex from path[] and mark it as unvisited
path.pop()
visited[u] = False
ROUTES.append(i for i in path)Seems to output[<generator object Graph.printAllPathsUtil.<locals>.<genexpr> at 0x03B27F30>, <generator object Graph.printAllPathsUtil.<locals>.<genexpr> at 0x03B27F70>, <generator object Graph.printAllPathsUtil.<locals>.