给定定向未加权图并且问题是找到最大长度的简单路径 (起始顶点和结束顶点不固定)。它显然可以在O(n ^ 2 * 2 ^ n)中解决,但我听说有O(n * 2 ^ n)算法,我不知道。那么如何在O(n * 2 ^ n)中解决它? // n = | V |
答案 0 :(得分:5)
如果问题确实是Longest Path Problem上的DAG,则维基百科的算法如下所示,并在O(| V | + | E |)中运行:
algorithm dag-longest-path is
input:
Directed acyclic graph G
output:
Length of the longest path
length_to = array with |V(G)| elements of type int with default value 0
for each vertex v in topOrder(G) do
for each edge (v, w) in E(G) do
if length_to[w] <= length_to[v] + weight(G,(v,w)) then
length_to[w] = length_to[v] + weight(G, (v,w))
return max(length_to[v] for v in V(G))