小编典典

有向无权图中的最长非循环路径

algorithm

可以使用哪种算法在未加权有向无环图中找到最长路径?


阅读 331

收藏
2020-07-28

共1个答案

小编典典

动态规划。鉴于它是DAG,在最长路径问题中也引用了它。

以下来自Wikipedia的代码:

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))
2020-07-28