给定一个起点a和一个最大距离D,从a开始的长度大于或等于D的最长路径是什么?这段代码有什么问题?在def find_paths(graph, start, path, allpaths, dmax):
if dmax == 0:
allpaths.append([start])
else:
path.append(start)
for edge in graph[start]:
d = graph[start][edge]
if dmax - d >= 0:
find_paths(graph, edge, path, allpaths, dmax - d)
else:
allpaths.append(path)
return allpaths
graph = {
1: {2: 33, 3: 15},
2: {1: 33, 4: 57, 5: 7},
3: {1: 15},
4: {2: 57},
5: {1: 89, 2: 7},
}
print find_paths(graph, 1, [], [], 50)
这样可以得到:
^{pr2}$
find_paths(graph, 1, [], [], 50)的输出应该是(1,3)和(1,2,5)。也就是说,第一条路径通过顶点1和3,第二条路径通过1、2和5。在
谢谢。在