Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Chapter 183: DAG Shortest & Longest Paths

For directed acyclic graphs (DAGs), shortest paths can be found in O(V + E) — faster than Dijkstra. The key insight: process vertices in topological order, guaranteeing that when we relax an edge, all incoming paths have already been finalized.


Algorithm

  1. Compute a topological ordering of the DAG.
  2. Initialize dist[s] = 0, all others = ∞.
  3. For each vertex u in topological order, relax every outgoing edge (u, v, w).
// O(V + E) shortest paths from source s in a DAG
void dag_shortest(int n, vector<tuple<int,int,int>>& edges, int s, vector<int>& dist) {
    vector<vector<pair<int,int>>> adj(n);
    for (auto& [u, v, w] : edges) adj[u].push_back({v, w});

    // Topological sort (Kahn's algorithm)
    vector<int> indeg(n, 0), order;
    for (auto& [u, v, w] : edges) indeg[v]++;
    queue<int> q;
    for (int i = 0; i < n; i++) if (!indeg[i]) q.push(i);
    while (!q.empty()) {
        int u = q.front(); q.pop();
        order.push_back(u);
        for (auto& [v, w] : adj[u]) if (--indeg[v] == 0) q.push(v);
    }

    // Relax in topological order
    dist.assign(n, INT_MAX);
    dist[s] = 0;
    for (int u : order) {
        if (dist[u] == INT_MAX) continue;
        for (auto& [v, w] : adj[u])
            if (dist[u] + w < dist[v]) dist[v] = dist[u] + w;
    }
}

For longest paths, simply negate all weights and run shortest paths, or flip the relaxation comparison.


Walkthrough

DAG with edges: 0→1 (3), 0→2 (6), 1→2 (2), 1→3 (4), 2→3 (1). Source = 0.

StepVertexdist[] beforeEdges relaxeddist[] after
10[0,∞,∞,∞]0→1:3, 0→2:6[0,3,6,∞]
21[0,3,6,∞]1→2:2, 1→3:4[0,3,5,7]
32[0,3,5,7]2→3:1 → 5+1=6 < 7[0,3,5,6]
43[0,3,5,6]No outgoing edges[0,3,5,6]

Shortest path 0→3: 6 (0→1→2→3).


Critical Path: Job Scheduling

Model tasks as a DAG (edges = dependencies, weights = durations). The longest path from start to end gives the critical path — the minimum project duration.


Complexity

PhaseTime
Topological sortO(V + E)
Relaxation passO(V + E)
TotalO(V + E)
SpaceO(V + E)

Common Mistakes

MistakeFix
Running on a graph with cyclesVerify DAG property first; otherwise use Bellman-Ford or Dijkstra
Forgetting longest-path negation trickNegate weights and run shortest, or use max instead of min
Using Dijkstra unnecessarily on a DAGDijkstra is O(E log V); topological sort is O(V + E)

Practice Problems

#ProblemHint
1Course Schedule II (LeetCode 210)Topological sort; longest chain = semesters needed
2Parallel Courses (LeetCode 1136)DAG longest path in terms of levels
3Alien Dictionary (LeetCode 269)Build DAG from ordering constraints, topological sort
4Critical Connections (LeetCode 1192)Related: find bridges, but topological order helps DAG problems
5Longest Increasing Path in Matrix (LeetCode 329)Build DAG where edge u→v exists if a[u]<a[v], find longest path
6Cheapest Flights Within K Stops — DAG variantIf stops form a DAG layer structure, use this approach

See Also