У меня есть следующая рекурсивная функция - функция хорошо работает для распечатки всех путей дерева / графа. Но попытка добавить ROUTES
в качестве глобальной переменной и добавить к ней приводит к куче пустых вложенных списков: [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [],
... et c
Лучшее решение для использования глобальной переменной и лучшее решение для хранения путей - это то, что я ищу, и это моя функция:
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