Chap 24
Chap 24
Single-Source
Shortest Paths
Shortest Paths
= w(vi 1 , vi )
i 1
• Variants
– Single-source: Find shortest paths
from a given source vertex s V to every vertex v V.
– Single-destination:
Find shortest paths to a given destination vertex.
– Single-pair: Find shortest path from u to v.
No way known that’s better in worst case than solving single-source.
– All-pairs: Find shortest path from u to v for all u, v V.
• Negative-weight edge
OK, as long as no negative-weight cycles are reachable from the source.
– If we have a negative-weight cycle, we can just keep going around it,
and get w(s, v) = -∞ for all v on the cycle.
– But OK if the negative-weight cycle is not reachable from the source.
– Some algorithms work only if there are no negative-weight edges in
the graph.
We’ll be clear when they’re allowed and not allowed.
.. continued
• Optimal Substructure
Lemma
Any subpath of a shortest path is a shortest path.
Proof Cut-and-paste
Then
w(p’) = w(pux ) + w(p’xy) + w(pyv)
< w(pux ) + w(pxy) + w(pyv)
= w(p) .
So p wasn’t a shortest path after all!
.. continued
• Cycles
Shortest paths can’t contain cycles:
– Already ruled out negative-weight cycles.
– Positive-weight we can get a shorter path by omitting the cycle.
– Zero-weight: no reason to use them assume that our solutions won’t
use them.
• Initialization
• No-path Property
If δ(s, v)=∞, then d[v]=∞ always.
Proof d[v] ≥ δ(s, v)=∞ d[v]=∞. ■
• Convergence Property
If → v is a shortest path, d [u] = δ(s, u), and we call RELAX(u,v,w),
then d [v] = δ(s, v) afterward.
Proof After relaxation:
d [v] ≤ d [u] + w(u, v) (RELAX code)
= δ(s, u) + w(u, v)
= δ(s, v) (lemma—optimal substructure)
Since d [v] ≥ δ(s, v), must have d [v] = δ(s, v). ■
.. continued
• Core: The first for loop relaxes all edges |V| -1 times.
• Time: (VE).
Example:
• Values you get on each pass and how quickly it converges depends on
order of relaxation.
• But guaranteed to converge after |V| -1 passes, assuming no negative-
weight cycles.
Proof Use path-relaxation property.
Let v be reachable from s, and let p = v0, v1, . . . , vk be a shortest path
from s to v, where v0 = s and vk = v. Since p is acyclic, it has ≤ |V| -1 edges,
so k ≤ |V| -1.
Each iteration of the for loop relaxes all edges:
– First iteration relaxes (v0, v1).
– Second iteration relaxes (v1, v2).
– kth iteration relaxes (vk-1, vk).
By the path-relaxation property, d[v] = d[vk ] = δ(s, vk ) = δ(s, v).
■
.. continued
How about the TRUE/FALSE return value?
• Suppose there is no negative-weight cycle reachable from s.
At termination, for all (u, v) E,
d[v] = δ(s, v)
≤ δ(s, u) + w(u,,v) (triangle inequality)
= d[u] + w(u, v) .
So BELLMAN-FORD returns TRUE.
• Now suppose there exists negative-weight cycle c = v0, v1, . . . , vk,
where v0 = vk , reachable from s.
Then
Each
k
vertex appears once in each summation
k k
d [vi ] and d [vi 1 ] 0 ≤ w[vi 1 , vi ]
i 1 i 1 i 1
• Example:
• Order of adding S: s, y, z, x.
• Correctness:
Loop invariant:
At the start of each iteration of the while loop, d[v] = δ(s, v) for all v S.
Initialization: Initially, S=, so trivially true.
Termination: At end, Q= S=V d[v]= (s, v) for all v V.
Maintenance:
Need to show that d[u]= (s, u) when u is added to S in each iteration.
Suppose there exists u such that d[u] δ(s, v). Without loss of generality,
let u be the first vertex for which d[u] δ(s, v) when u is added to S.
Observations:
• u s, since d[s]= (s, s) =0.
• Therefore, s S, so S .
• There must be some path ,
since otherwise d[u] = (s, u) = ∞ by no-path property.
So, there is a path . This means there is a shortest path
.. continued (maintenance of invariant)
Analysis: