
[英]In a directed, weighted graph, efficiently find the cost of the shortest path between two nodes A and B that traverses through a node X
[英]How to find a path that traverses through all the vertices of a directed graph [closed]
给定一个有向图,如何找到一条只遍历所有顶点一次的路径。
例如:
对于这个图表,一个答案是:
示例图的所有可能解决方案:
约束:2 <= V <= 2000
我的伪代码:
function find_path(u):
if length(path) == number_of_vertices:
return path
visited[u] = True
for v in childern(u):
if visited[v] is False:
x = find_path(v)
if x is not False:
return x
return False
for u in all vertices:
x = find_path(u)
if x is not False:
print(x)
break
但这对于较大的输入是不可行的。
谢谢你的帮助!
PS:python 3.x 中的代码将不胜感激,但算法或伪代码可以。
编辑:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.