Abstract
Given a DAG with edge weights, find the shortest path from a given vertex to all other vertices.
- Category: Dynamic Programming / Graph Shortest Path
- Input: A DAG with edge weights (positive or negative), source vertex
- Output: for every vertex — the shortest distance from to
- Paradigm: Dynamic Programming over a topological order
- Typical use cases: shortest (or longest) path whenever the graph has no cycles — task/dependency DAGs, and as the underlying reduction target for other DP problems like Edit Distance and Longest Increasing Subsequence Example
Problem Specification
- Instance: A DAG with edge weights , and a source vertex .
- Solution Format: for every .
- Constraints: must equal the length of the shortest path from to (undefined/infinite if unreachable).
- Objective: Minimize path length, per vertex.
- Goal: Minimize.
Candidate Strategies / Approaches
Dijkstra’s Algorithm (general graphs) ✘ — works, but overkill
Dijkstra’s Algorithm solves this too, but pays for a priority queue it doesn’t need here, and can’t handle negative edge weights at all in the general case. It also doesn’t exploit the fact that the graph has no cycles.
DAG DP via Topological Order ✔
Since a DAG has no cycles, there’s a fixed order (a topological order) in which every edge points “forward.” Process vertices in that order, and every predecessor of a vertex is guaranteed already finalized by the time you reach it — no need to repeatedly search for “the next closest unfinished vertex” the way Dijkstra’s does.
Key Idea
A priority queue exists to answer “which unfinished vertex is currently closest?” — but in a DAG, topological order already answers that question for free, once and for all, before the algorithm even starts. That’s what turns this into a single pass instead of Dijkstra’s .
Dynamic Programming Solution
1. Subproblem
Let be the shortest distance from to .
2. Base Case
3. Recursion
4. Ordering
Topological Order. The trick: order the vertices topologically. Then, by the time you compute for a given vertex, every one of its predecessors (every with an edge ) has already been finalized — so a single left-to-right pass suffices.

5. Iterative Algorithm
Algorithm 19 Shortest Path in a DAG
procedure DAGDP()
Let be a list of all vertices after in topological ordering
for do
Variables & Data Structures
| Name | Type | Purpose |
|---|---|---|
dist | Array (vertex → number) | Shortest known distance from s; finalized in topological order, never revisited |
v_1,...,v_n | Topologically sorted vertex list | The processing order — guarantees every predecessor of v_i is already finalized when v_i is reached |
Helper Functions / Operations Used
- Topological sort — computed once up front; .
- Min over incoming edges — for each vertex, examine each incoming edge once; summed over all vertices, this is total across the whole algorithm.
Proof of Correctness / Optimality
Claim: upon termination, equals the true shortest-path distance from to , for every .
Proof (induction on position in the topological order):
- Base case: is correct — the shortest path from to itself has length 0.
- Inductive Hypothesis: every vertex processed before in the topological order has a correctly computed .
- Inductive Step: any shortest path to must arrive via its last edge, , from some predecessor . Since is a DAG and vertices are processed in topological order, every such predecessor appears before in the ordering — so by the Inductive Hypothesis, is already correct when is processed. Taking the minimum of over every incoming edge therefore considers every possible “last edge” of a shortest path to , and picks the best one — so is set correctly.
Time & Space Complexity Analysis
General Case
| Complexity | Notes | |
|---|---|---|
| Time | Topological sort is ; the main loop examines every incoming edge of every vertex exactly once, total | |
| Space | Adjacency structure for the graph, plus the dist array () |
Best / Worst / Average Case
- Best / Worst / Average case: all — every vertex and edge is examined exactly once regardless of the specific weights or graph shape.
Notable Properties
- Works for positive and negative weights. Being a DAG prevents the one thing that breaks shortest-path algorithms with negative weights in general graphs: negative cycles. Since there are no cycles at all, there’s nothing to loop around indefinitely.
- Longest path for free. To find the longest path instead, just negate every edge weight and run the same algorithm — safe here specifically because a DAG can never have a negative cycle to exploit, unlike general graphs (where longest path is NP-hard).
- Powers other DP solutions. Solving Edit Distance this way is time — matching that problem’s vertices and edges exactly. See Longest Increasing Subsequence for a worked example using the negation trick above.
Drawbacks / Constraints
- Only works on DAGs. If the graph has any cycle — even with all-positive weights — this algorithm doesn’t apply; use Dijkstra’s Algorithm (non-negative weights) or Bellman-Ford (negative weights, general graphs) instead.
- Requires a topological order to exist and be computed first, adding a real (if linear) upfront cost.
