Graph 编写一个算法,找到一条恰好遍历有向图G所有边一次的路径
…如有必要,您可以多次访问节点。显示算法的运行时复杂性。这个图不一定是强连通的,但从一个节点开始,应该存在这样一条路径 到目前为止,我的方法是在未访问的节点上重复DFS以查找具有最高post编号的节点,因为这将是元图中源节点的一部分 然后在反向图上重复DFS以查找接收器节点元图 然后运行欧拉路径算法,直到我们耗尽所有的边,如果一条路径导致死胡同,则进行回溯 我不知道如何从这里开始 嗨,我想到了这个。有人能核实一下吗Graph 编写一个算法,找到一条恰好遍历有向图G所有边一次的路径,graph,graph-algorithm,depth-first-search,Graph,Graph Algorithm,Depth First Search,…如有必要,您可以多次访问节点。显示算法的运行时复杂性。这个图不一定是强连通的,但从一个节点开始,应该存在这样一条路径 到目前为止,我的方法是在未访问的节点上重复DFS以查找具有最高post编号的节点,因为这将是元图中源节点的一部分 然后在反向图上重复DFS以查找接收器节点元图 然后运行欧拉路径算法,直到我们耗尽所有的边,如果一条路径导致死胡同,则进行回溯 我不知道如何从这里开始 嗨,我想到了这个。有人能核实一下吗 function edge_dfs(vertex u, graph, path,
function edge_dfs(vertex u, graph, path, num_edges) {
if num_edges == 0 { // Found a path.
return true;
}
for all neighbors n of u {
if exists((u, n)) == true {
num_edges--
exists((u, n)) = false
path.append((n))
if edge_dfs(n, g, p, num_edges) == true:
return true
else:
num_edges++ // Backtrack if this edge was unsuccessful.
exists((u, n)) = true
path.pop()
}
}
return false // No neighbors or No valid paths from this vertex.
}
repeatedly do dfs and fine a source component
node: call it s.
path = array{s}
exists((u, v)) = true for all edges (u, v) in graph
num_edges = number of edges in graph
if edge_dfs(s, graph, path, num_edges) == true:
Path is the elements in array 'path' in order.
else:
Such a path does not exist.
这是O(| E |+| V |),因为它只是所有边的DFS 我只发布了伪代码。