Shortest and Longest Paths in a DAG
Solve shortest and longest paths with negative edge weights by evaluating one dependency order.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 2
- Reading time
- 1 min
In this article2 chapters
Predecessors are final before their successor is processed
Given a topological order, set the source label to zero and other distances to infinity. Relax outgoing edges as vertices occur in order. Every path to a vertex ends with an edge from an earlier vertex, so all predecessor labels are final when needed. Negative weight is harmless because no edge can return to an earlier state. The entire pass takes \(O(|V|+|E|)\) including the order construction.
Change the objective without changing the dependency graph
For longest paths, start unreachable labels at negative infinity and use maximum. For edges \(s\to a\) of length \(2\), \(a\to t\) of length \(3\), and \(s\to t\) of length \(4\), the longest length is \(5\). In a dependency scheduler this is a critical path under an unlimited-worker model. Resource constraints can still lengthen execution. Dynamic programming states explain the last-edge recurrence. Bellman–Ford uses repeated rounds where cyclic dependencies prevent a topological order.[1][2]