最大路径问题

Mik*_*leB 4 graph-algorithm

给定定向的未加权图并且问题是找到最大长度的简单路径(起始顶点和结束顶点不固定).它显然可以在O(n ^ 2*2 ^ n)中解决,但我听说有O(n*2 ^ n)算法,我不知道.那么如何在O(n*2 ^ n)中解决它?// n = | V |

jtd*_*ubs 5

如果您的问题确实是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))
Run Code Online (Sandbox Code Playgroud)