Weighted Shortest Path
Weighted Shortest Path
006
Massachusetts Institute of Technology
Instructors: Erik Demaine, Jason Ku, and Justin Solomon Lecture 11: Weighted Shortest Paths
Review
• Single-Source Shortest Paths with BFS in O(|V | + |E|) time (return distance per vertex)
• Single-Source Reachability with BFS or DFS in O(|E|) time (return only reachable vertices)
Weighted Graphs
• A weighted graph is a graph G = (V, E) together with a weight function w : E → Z
– Inside graph representation: store edge weight with each vertex in adjacency lists
– Store separate Set data structure mapping each edge to its weight
• We assume a representation that allows querying the weight of an edge in O(1) time
Examples
G1 G2
−5 −1 5 −5 −1 5
a b c d a b c d
6 9 6 9
7 −4 1 4 7 −4 1 4
8 8
e f g h e f g h
3 2 −2 3 2 −2
2 Lecture 11: Weighted Shortest Paths
Weighted Paths
• The weight w(π) of a path π in a weighted graph is the sum of weights of edges in the path
• (Often use “distance” for shortest-path weight in weighted graphs, not number of edges)
• Why infimum not minimum? Possible that no finite-length minimum-weight path exists
• When? Can occur if there is a negative-weight cycle in the graph, Ex: (b, f, g, c, b) in G1
• A negative-weight cycle is a path π starting and ending at same vertex with w(π) < 0
• If this occurs, don’t want a shortest path, but may want the negative-weight cycle
• (No parent pointers: can reconstruct shortest paths tree in linear time after. Next page!)
• Already know one algorithm: Breadth-First Search! Runs in O(|V | + |E|) time when, e.g.:
– graph has positive weights, and all weights are the same
– graph has positive weights, and sum of all weights at most O(|V | + |E|)
• For general weighted graphs, we don’t know how to solve SSSP in O(|V | + |E|) time
Shortest-Paths Tree
• For BFS, we kept track of parent pointers during search. Alternatively, compute them after!
• If know δ(s, v) for all vertices v ∈ V , can construct shortest-path tree in O(|V | + |E|) time
• For weighted shortest paths from s, only need parent pointers for vertices v with finite δ(s, v)
• Parent pointers may traverse cycles of zero weight. Mark each vertex in such a cycle.
– For each v ∈ Adj+ (u) where v is marked and δ(s, v) = δ(s, u) + w(u, v):
∗ Unmark vertices in cycle containing v by traversing parent pointers from v
∗ Set P (v) = u, breaking the cycle
• Exercise: Prove this algorithm correctly computes parent pointers in linear time
DAG Relaxation
• Idea! Maintain a distance estimate d(s, v) (initially ∞) for each vertex v ∈ V ,
that always upper bounds true distance δ(s, v), then gradually lowers until d(s, v) = δ(s, v)
• Triangle Inequality: the shortest-path weight from u to v cannot be greater than the shortest
path from u to v through another vertex x, i.e., δ(u, v) ≤ δ(u, x) + δ(x, v) for all u, v, x ∈ V
• If d(s, v) > d(s, u) + w(u, v) for some edge (u, v), then triangle inequality is violated :(
• Fix by lowering d(s, v) to d(s, u) + w(u, v), i.e., relax (u, v) to satisfy violated constraint
• Claim: Relaxation is safe: maintains that each d(s, v) is weight of a path to v (or ∞) ∀v ∈ V
• Proof: Assume d(s, v 0 ) is weight of a path (or ∞) for all v 0 ∈ V . Relaxing some edge (u, v)
sets d(s, v) to d(s, u) + w(u, v), which is the weight of a path from s to v through u.
4 Lecture 11: Weighted Shortest Paths
Correctness
• Claim: At end of DAG Relaxation: d(s, v) = δ(s, v) for all v ∈ V
• Proof: Induct on k: d(s, v) = δ(s, v) for all v in first k vertices in topological order
– Base case: Vertex s and every vertex before s in topological order satisfies claim at start
– Inductive step: Assume claim holds for first k 0 vertices, let v be the (k 0 + 1)th
– Consider a shortest path from s to v, and let u be the vertex preceding v on path
– u occurs before v in topological order, so d(s, u) = δ(s, u) by induction
– When processing u, d(s, v) is set to be no larger (≤) than δ(s, u) + w(u, v) = δ(s, v)
– But d(s, v) ≥ δ(s, v), since relaxation is safe, so d(s, v) = δ(s, v)
• Alternatively:
– For any vertex v, DAG relaxation sets d(s, v) = min{d(s, u) + w(u, v) | u ∈ Adj− (v)}
– Shortest path to v must pass through some incoming neighbor u of v
– So if d(s, u) = δ(s, u) for all u ∈ Adj− (v) by induction, then d(s, v) = δ(s, v)
Running Time
• Initialization takes O(|V |) time, and Topological Sort takes O(|V | + |E|) time
For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms