我试图在 python 中遍历这个算法中的一个图。如果我想一一打印图表的所有元素或遍历整个图表,我应该做哪些更改。
任何帮助将不胜感激。谢谢。
grapth={'A': ['B', 10, 'B', 10, 'B', 10, 'C', 15], 'C': [1001, 'OUT'], 'B':
[1000, 'IN', 1000, 'IN']}
print "Path:",find_all_paths(Portdict1,'A','IN')
def find_all_paths(graph, start, end, path=[]):
path = path + [start]
if start == end:
return [path]
if not graph.has_key(start):
return []
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