2. Shortest paths in DAGs
General Problem. Given a digraph G = (V, E), edge lengths le ≥ 0, source
s ∈ V, and destination t ∈ V, find the shortest directed path from s to t.
Single-source shortest paths problem in DAG
Input Given a DAG, G = (V, E) and a source vertex s in V.
Output Find the shortest path from s to every other vertex in G, i.e, both
the sequence of nodes on the shortest path and its length.
Figure: Example Problem
(DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 93 / 101
3. How to proceed ?
■ STEP-1 OF 2 : linearize DAG, i.e, arrange nodes of G on a line so
that all edges go from left to right.
■ Find the topological sort of the following graphs:
(DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 94 / 101
5. Shortest paths in DAGs : data structures
• Formally, for the vth element of arrays:
• dist[v] stores the weight of a shortest path from s to v.
• pred[v] stores the predecessor of v on the so-far identified
shortest path.
Figure: Finding single source shortest path
(DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 96 / 101
6. Shortest paths in DAGs: Step-2
• STEP-2 OF 2
• RELAX sub-procedure.
(DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 97 / 101