Graph Theory37 sections · 1633 units
Open in Course

Shortest Path in DAGs

Better than Dijkstra

Finding shortest paths in a DAG is simpler than Dijkstra. No need for a priority queue or visited set. Process nodes in topological order. For each node uu, relax all outgoing edges (u,v)(u, v) by trying to update dp[v] = min(dp[v], dp[u] + w(u,v)).

When you reach node vv, all possible paths to vv have been considered. This runs in O(V+E)O(V + E), faster than Dijkstra's O((V+E)logV)O((V + E) \log V). The topological order guarantees you process nodes in the right sequence.

Space complexity is O(V)O(V) for the data structures used.