此代码在python 官方图论论文中给出。这是代码:
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
我不擅长python,因为我还没有足够的练习和阅读它。您能否通过将其与 DFS 图中的子兄弟概念相关联来解释代码?谢谢。