Longest path in a DAG

前端 未结 3 964
小蘑菇
小蘑菇 2020-12-09 18:58

To find the longest path in a DAG, I\'m aware of 2 algorithms: algo 1: do a topological sort + use dynamic programming on the result of the sort ~ or ~ algo 2: enumerate all

3条回答
  •  孤街浪徒
    2020-12-09 19:44

    No DFS needed. Algorithm : takes a DAG G. Each arc holds one variable E

    for each node with no predecessor :
        for each of his leaving arcs, E=1.
    for each node whose predecessors have all been visited :
        for each of his leaving arcs, E=max(E(entering arcs))+1.
    

    max_path is the highest E within edges when all nodes have been processed.

提交回复
热议问题