Shortest pathsGiven: A single source vertex (given s) in a weighted, directed graph.
Want to compute a shortest path for each possible destination.
(Single Source Shortest Paths) SSSP.– Similar to BFS.
We will assume either– no negative-weight edges, or– no reachable negative-weight cycles.
Algorithms– Dijkstra’s (in comparison to MST prim)– Bellman-Ford– DAG Shortest Paths
The shortest paths between all pairs of vertices (All Pairs Shortest Paths). Floyd Warshall Algorithm (DP), Johnson’s.
where the length of a path is the sum of its edge weights.
propsTriangle inequality (Lemma 24.10)
For any edge (u, v) ² E, we have δ(s, v) ≤ δ(s, u) + w(u, v).
Upper-bound property (Lemma 24.11)
We always have d[v] ≥ δ(s, v) for all vertices v ² V, and once d[v] achieves
the value δ(s, v), it never changes.
No-path property (Corollary 24.12)
If there is no path from s to v, then we always have d[v] = δ(s, v)=∞.
Convergence property (Lemma 24.14)
If s u ❀ → v is a shortest path in G for some u, v ∈ V, and if d[u] = δ(s, u) at
any time prior to relaxing edge (u, v), then d[v] = δ(s, v) at all times afterward.
Path-relaxation property (Lemma 24.15)
If p = v0, v1, . . . , vk is a shortest path from s = v0 to vk , and the edges of p
are relaxed in the order (v0, v1), (v1, v2), . . . , (vk−1, vk ), then d[vk ] = δ(s, vk ).
This property holds regardless of any other relaxation steps that occur, even if
they are intermixed with relaxations of the edges of p.
Predecessor-subgraph property (Lemma 24.17)
Once d[v] = δ(s, v) for all v ∈ V, the predecessor subgraph, [u] is a shortest-paths.
A Fact About Shortest PathsTheorem: If p is a shortest path from u to v, then any subpath of p is also a shortest path.
Proof: Consider a subpath of p from x to y. If there were a shorter path from x to y, then there would be a shorter path from u to v.
u x y v
shorter?
Single-Source Shortest PathsGiven a directed graph with weighted edges, what are the shortest paths from some source vertex s to all other vertices?
Note: shortest path to single destination cannot be done asymptotically faster, as far as we know.
3
11
9
5
0
3
6
5
43
6
21 2 7s
Path RecoveryWe would like to find the path itself, not just its length. We’ll construct a shortest-paths tree:
3
11
9
5
0
36
5
43
6
2 72
1s
u v
x y
3
11
9
5
03
6
53
6
2 742
1s
u v
x y
Shortest-Paths Idea(u,v) length of the shortest path from u to v.
All SSSP algorithms maintain a field d[u] for every vertex u. d[u] will be an estimate of(s,u). As the algorithm progresses, we will refine d[u] until, at termination, d[u] = (s,u). Whenever we discover a new shortest path to u, we update d[u].
In fact, d[u] will always be an overestimate of (s,u):
d[u] (s,u)
We’ll use [u] to point to the parent (or predecessorpredecessor) of u on the shortest path from s to u. We update [u] when we update d[u].
SSSP Subroutine
RELAX(u, v, w) (Maybe) improve our estimate of the distance to v by considering a path along the edge (u, v).if d[u] + w(u, v) < d[v] then
d[v] d[u] + w(u, v) actually, DECREASE-KEY
[v] u remember predecessor on path
u vw(u,v)
d[v]d[u]
Shortest Path PropertiesAgain, we have optimal substructure: the shortest path consists of shortest subpaths:
– Proof: suppose some subpath is not a shortest path• There must then exist a shorter subpath • Could substitute the shorter subpath for a shorter
path• But then overall path is not shortest path.
Contradiction
Shortest Path PropertiesDefine (u,v) to be the weight of the shortest path from u to v
Shortest paths satisfy the triangle inequalitytriangle inequality: (u,v) (u,x) + (x,v)
“Proof”:x
u v
This path is no longer than any other path
Shortest Path PropertiesIn graphs with negative weight cycles, some shortest paths will not exist (Why?):
< 0
RelaxationA key technique in shortest path algorithms is relaxation
– Idea: for all v, maintain upper bound d[v] on (s,v)
Relax(u,v,w) {
if (d[v] > d[u]+w) then d[v]=d[u]+w;
}95
2
752
Relax
652
652
Relax
Dijkstra’s Algorithm(pronounced “DIKE-stra”)
Assume that all edge weights are 0.
Idea: say we have a set K containing all vertices whose shortest paths from s are known (i.e. d[u] = (s,u) for all u in K).
Now look at the “frontier” of K—all vertices adjacent to a vertex in K.
the restof the graph
s
K
Dijkstra’s: TheoremAt each frontier vertex u, update d[u] to be the minimum from all edges from K.
Now pick the frontier vertex u with the smallest value of d[u].
Claim: d[u] = (s,u)
s
4
9
6
6
2
1
3
8
min(4+2, 6+1) = 6
min(4+8, 6+3) = 9
Dijkstra’s: ProofBy construction, d[u] is the length of the shortest path to u going through only vertices in K.
Another path to u must leave K and go to v on the frontier.
But the length of this path is at least d[v], (assuming non-negative edge weights), which is d[u].
Proof Explained
Why is the path through v at least d[v] in length?
We know the shortest paths to every vertex in K.
We’ve set d[v] to the shortest distance from s to v via K.
The additional edges from v to u cannot decrease the path length.
s
u
v
d[u] d[v]
K
shortest path to u
another path to u, via v
Dijkstra’s Algorithm, Rough DraftK s
d K
u d
d u s u
K K u
K
{ }
[ ] ( , )
{ }
Update for frontier of
vertex with minimum on frontier
we now know
repeat until all vertices are in .
u
K
K
u
A RefinementNote: we don’t really need to keep track of the frontier.
When we add a new vertex u to K, just update vertices adjacent to u.
Dijkstra Running Time (cont.)1. Priority queue is an array.
EXTRACT-MIN in (n) time, DECREASE-KEY in (1)Total time: (V + VV + E) = (V2)
2. (“Modified Dijkstra”)Priority queue is a binary (standard) heap.EXTRACT-MIN in (lgn) time, also DECREASE-KEYTotal time: (VlgV + ElgV)
3. Priority queue is Fibonacci heap. (Of theoretical interest only.)EXTRACT-MIN in (lgn), DECREASE-KEY in (1) (amortized)Total time: (VlgV+E)
The Bellman-Ford AlgorithmHandles negative edge weights
Detects negative cycles
Is slower than Dijkstra
4
5-10
a negative cycle
Bellman-Ford: IdeaRepeatedly update d for all pairs of vertices connected by an edge.
Theorem: If u and v are two vertices with an edge from u to v, and s u v is a shortest path, and d[u] = (s,u),
then d[u]+w(u,v) is the length of a shortest path to v.
Proof: Since s u v is a shortest path, its length is (s,u) + w(u,v) = d[u] + w(u,v).
Why Bellman-Ford Works• On the first pass, we find (s,u) for all vertices
whose shortest paths have one edge.
• On the second pass, the d[u] values computed for the one-edge-away vertices are correct (= (s,u)), so they are used to compute the correct d values for vertices whose shortest paths have two edges.
• Since no shortest path can have more than |V[G]|-1 edges, after that many passes all d values are correct.
• Note: all vertices not reachable from s will have their original values of infinity. (Same, by the way, for Dijkstra).
Bellman-Ford: AlgorithmBELLMAN-FORD(G, w, s)1 foreach vertex v V[G] do //INIT_SINGLE_SOURCE2 d[v]3 [v]NIL4 d[s]05 for i 1 to |V[G]|-1 do each iteration is a “pass”6 for each edge (u,v) in E[G] do7 RELAX(u, v, w)8 check for negative cycles9 for each edge (u,v) in E[G] do 10 if d[v] > d[u] + w(u,v) then11 return FALSE12 return TRUE Running timeVE)
Negative Cycle DetectionWhat if there is a negative-weightcycle reachable from s?Assume: d[u] d[x]+4
d[v] d[u]+5d[x] d[v]-10
Adding:d[u]+d[v]+d[x] d[x]+d[u]+d[v]-1
Because it’s a cycle, vertices on left are same as those on right. Thus we get 0 -1; a contradiction. So for at least one edge (u,v),
d[v] > d[u] + w(u,v)
This is exactly what Bellman-Ford checks for.
u
x
v
4
5
-10
Example
0
z
u v
x y
6
5
–3
9
7
7
8
–2
–42
Example
0
7
6
z
u v
x y
6
5
–3
9
7
7
8
–2
–42
Example
0
27
46
z
u v
x y
6
5
–3
9
7
7
8
–2
–42
Example
0
27
42
z
u v
x y
6
5
–3
9
7
7
8
–2
–42
Example
0
-27
42
z
u v
x y
6
5
–3
9
7
7
8
–2
–42
BF Dynamic aspect for the same graphNote: This is essentially dynamic programming.
Let d(i, j) = cost of the shortest path from s to i that is at most j hops.
d(i, j) =
0 if i = s j = 0 if i s j = 0min({d(k, j–1) + w(k, i): i Adj(k)} {d(i, j–1)}) if j > 0
z u v x y 1 2 3 4 50 0 1 0 6 7 2 0 6 4 7 23 0 2 4 7 24 0 2 4 7 –2
j
i
A
C
B
ED
2
-1
4-3
2 3
5 3
-1
4
π: nild: 0
π: nild: ∞
π: nild: ∞
π: nild: ∞
π: nild: ∞
Bellman-Ford where G contains no negative weight cycles(iterations from E to A)
A
C
B
ED
2
-1
4-3
2 3
5 3
-1
4
π: nild: 0
π: nil Ad: ∞ -1
π: nil Cd: ∞ 2
π: nil Ad: ∞ 4
π: nild: ∞
A
C
B
ED
2
-1
4-3
2 3
5 3
-1
4
π: nild: 0
π: nil Ad: ∞ -1
π: nil Cd: ∞ 2
π: nil Ad: ∞ 4
π: nil Cd: ∞ 3
A
C
B
ED
2
-1
4-3
2 3
5 3
-1
4
π: nild: 0
π: nil Ad: ∞ -1
π: nil Cd: ∞ 2
π: nil Ad: ∞ 4
π: nil Cd: ∞ 3
A
C
B
ED
2
-1
4-3
2 3
5 3
-1
4
π: nild: 0
π: nil Ad: ∞ -1
π: nil Cd: ∞ 2
π: nil Ad: ∞ 4
π: nil Cd: ∞ 3
A
C
B
ED
2
-1
4-3
2 1
5 1
-1
4
π: nild: 0
π: nild: ∞
π: nild: ∞
π: nild: ∞
π: nild: ∞
Bellman-Ford where G contains no negative weight cycles(iterations from A to E)
A
C
B
ED
2
-1
4-3
2 1
5 1
-1
4
π: nild: 0
π: nil Ad: ∞ -1
π: nil Cd: ∞ 2
π: nil Ad: ∞ 4
π: nild: ∞
A
C
B
ED
2
-1
4-3
2 1
5 1
-1
4
π: nild: 0
π: nil Ad: ∞ -1
π: nil C Bd: ∞ 2 0
π: nil Ad: ∞ 4
π: nil Cd: ∞ 3
A
C
B
ED
2
-1
4-3
2 1
5 1
-1
4
π: nild: 0
π: nil Ad: ∞ -1
π: nil C Bd: ∞ 2 0
π: nil Ad: ∞ 4
π: nil C Cd: ∞ 3 1
A
C
B
ED
2
-1
4-3
2 1
5 1
-1
4
π: nild: 0
π: nil A Ed: ∞ -1 -2
π: nil C B Bd: ∞ 2 0 -1
π: nil Ad: ∞ 4
π: nil C Cd: ∞ 3 1
Comp 122, Fall 2003 Single-source SPs - 40
Lemma 24.2Lemma 24.2: Assuming no negative-weight cycles reachable froms, d[v] = (s, v) holds upon termination for all vertices v reachablefrom s.
Lemma 24.2: Assuming no negative-weight cycles reachable froms, d[v] = (s, v) holds upon termination for all vertices v reachablefrom s.
Proof:
Consider a SP p, where p = ‹v0, v1, …, vk›, where v0 = s and vk = v.
Assume k |V| – 1, otherwise p has a cycle.
Claim: d[vi] = (s, vi) holds after the ith pass over edges.Proof follows by induction on i.
By Lemma 24.11, once d[vi] = (s, vi) holds, it continues to hold.
Comp 122, Fall 2003 Single-source SPs - 41
CorrectnessClaim: Algorithm returns the correct value.
(Part of Theorem 24.4. Other parts of the theorem follow easily from earlier results.)
Case 1: There is no reachable negative-weight cycle.
Upon termination, we have for all (u, v):d[v] = (s, v) , by lemma 24.2 (last slide) if v is reachable; d[v] = (s, v) = otherwise. (s, u) + w(u, v) , by Lemma 24.10. = d[u] + w(u, v)
So, algorithm returns true.
Comp 122, Fall 2003 Single-source SPs - 42
Case 2Case 2: There exists a reachable negative-weight cyclec = ‹v0, v1, …, vk›, where v0 = vk.
We have i = 1, …, k w(vi-1, vi) < 0. (*)
Suppose algorithm returns true. Then, d[vi] d[vi-1] + w(vi-1, vi) fori = 1, …, k. (because Relax didn’t change any d[vi] ). Thus,
i = 1, …, k d[vi] i = 1, …, k d[vi-1] + i = 1, …, k w(vi-1, vi)
But, i = 1, …, k d[vi] = i = 1, …, k d[vi-1].
Can show no d[vi] is infinite. Hence, 0 i = 1, …, k w(vi-1, vi).
Contradicts (*). Thus, algorithm returns false.
Review: Getting Dressed
UnderwearUnderwear SocksSocks
ShoesShoesPantsPants
BeltBelt
ShirtShirt
WatchWatch
TieTie
JacketJacket
SocksSocks UnderwearUnderwear PantsPants ShoesShoes WatchWatch ShirtShirt BeltBelt TieTie JacketJacket
Topological-Sort(){
1- Run DFS2- When a vertex is finished, output it3- Vertices are output in reverse topological order
}Time: O(V+E)
SSSP in a DAGRecall: a dag is a directed acyclic graph.
If we update the edges in topologically sorted order, we correctly compute the shortest paths.
Reason: the only paths to a vertex come from vertices before it in the topological sort.
0 1 4 61 3 2
9s
SSSP in a DAG TheoremTheorem: For any vertex u in a dag, if all the vertices before u in a topological sort of the dag have been updated, then d[u] = (s,u).
Proof: By induction on the position of a vertex in the topological sort.
Base case: d[s] is initialized to 0.
Inductive case: Assume all vertices before u have been updated, and for all such vertices v, d[v]=(s,v). (continued)
Proof, ContinuedSome edge (v,u) where v is before u, must be on the shortest path to u, since there are no other paths to u.
When v was updated, we set d[u] to d[v]+w(v,u)
= (s,v) + w(v,u)
= (s,u)
SSSP-DAG Algorithm
DAG-SHORTEST-PATHS(G,w,s)
1 topologically sort the vertices of G
2 initialize d and as in previous algorithms
3 for each vertex u in topological sort order do
4 for each vertex v in Adj[u] do
5 RELAX(u, v, w)
Running time: (V+E), same as topological sort
Comp 122, Fall 2003 Single-source SPs - 48
Shortest Paths in DAGs
Topologically sort vertices in G;Initialize(G, s);for each u in V[G] (in order) do
for each v in Adj[u] doRelax(u, v, w)
odod
Topologically sort vertices in G;Initialize(G, s);for each u in V[G] (in order) do
for each v in Adj[u] doRelax(u, v, w)
odod
Comp 122, Fall 2003 Single-source SPs - 49
Example
0 r s t u v w
5 2 7 –1 –2
6 1
32
4
Comp 122, Fall 2003 Single-source SPs - 50
Example
0 r s t u v w
5 2 7 –1 –2
6 1
32
4
Comp 122, Fall 2003 Single-source SPs - 51
Example
0 2 6 r s t u v w
5 2 7 –1 –2
6 1
32
4
Comp 122, Fall 2003 Single-source SPs - 52
Example
0 2 6 6 4
r s t u v w5 2 7 –1 –2
6 1
32
4
Comp 122, Fall 2003 Single-source SPs - 53
Example
0 2 6 5 4
r s t u v w5 2 7 –1 –2
6 1
32
4
Comp 122, Fall 2003 Single-source SPs - 54
Example
0 2 6 5 3
r s t u v w5 2 7 –1 –2
6 1
32
4
Comp 122, Fall 2003 Single-source SPs - 55
Example
0 2 6 5 3
r s t u v w5 2 7 –1 –2
6 1
32
4
Bellman-Ford Algorithm DynamicNow consider parallel computations Now consider parallel computations for all destinationsfor all destinations d d
InitializationInitialization– Each node has 1 row for each destination Each node has 1 row for each destination dd– Distance of node Distance of node dd to itself is zero: to itself is zero: DDdd(d)=0(d)=0– Distance of other node Distance of other node jj to to dd is infinite: is infinite: DDjj(d)= (d)= , , for for j j d d– Next node Next node nnjj = -1 since not yet defined = -1 since not yet defined
Send StepSend Step– Send new distance vector to immediate neighbors across local linkSend new distance vector to immediate neighbors across local link
Receive StepReceive Step– For each destination For each destination dd, find the next hop that gives the minimum , find the next hop that gives the minimum
distance to distance to dd, , • MinMinjj { C { Cijij+ D+ Djj(d) }(d) }• Replace old Replace old (n(njj, D, Dii(d))(d)) by new by new (n(njj*, D*, Djj*(d))*(d)) if new next node or if new next node or
distance founddistance found– Go to send stepGo to send step
Iteration Node 1 Node 2 Node 3 Node 4 Node 5
Initial (-1, ) (-1, ) (-1, ) (-1, ) (-1, )
1
2
3
31
5
46
2
2
3
4
2
1
1
2
3
5SanJose
Table entry
@ node 1
for dest SJ
Table entry
@ node 3
for dest SJ
Iteration Node 1 Node 2 Node 3 Node 4 Node 5
Initial (-1, ) (-1, ) (-1, ) (-1, ) (-1, )
1 (-1, ) (-1, ) (6,1) (-1, ) (6,2)
2
3
SanJose
D6=0
D3=D6+1n3=6
31
5
46
2
2
3
4
2
1
1
2
3
5
D6=0D5=D6+2n5=6
0
2
1
Iteration Node 1 Node 2 Node 3 Node 4 Node 5
Initial (-1, ) (-1, ) (-1, ) (-1, ) (-1, )
1 (-1, ) (-1, ) (6, 1) (-1, ) (6,2)
2 (3,3) (5,6) (6, 1) (3,3) (6,2)
3
SanJose
31
5
46
2
2
3
4
2
1
1
2
3
50
1
2
3
3
6
Iteration Node 1 Node 2 Node 3 Node 4 Node 5
Initial (-1, ) (-1, ) (-1, ) (-1, ) (-1, )
1 (-1, ) (-1, ) (6, 1) (-1, ) (6,2)
2 (3,3) (5,6) (6, 1) (3,3) (6,2)
3 (3,3) (4,4) (6, 1) (3,3) (6,2)
SanJose
31
5
46
2
2
3
4
2
1
1
2
3
50
1
26
3
3
4
Iteration Node 1 Node 2 Node 3 Node 4 Node 5
Initial (3,3) (4,4) (6, 1) (3,3) (6,2)
1 (3,3) (4,4) (4, 5) (3,3) (6,2)
2
3
SanJose
31
5
46
2
2
3
4
2
1
1
2
3
50
1
2
3
3
4
Network disconnected; Loop created between nodes 3 and 4
5
Iteration Node 1 Node 2 Node 3 Node 4 Node 5
Initial (3,3) (4,4) (6, 1) (3,3) (6,2)
1 (3,3) (4,4) (4, 5) (3,3) (6,2)
2 (3,7) (4,4) (4, 5) (5,5) (6,2)
3
SanJose
31
5
46
2
2
3
4
2
1
1
2
3
50
2
5
3
3
4
7
5
Node 4 could have chosen 2 as next node because of tie
Iteration Node 1 Node 2 Node 3 Node 4 Node 5
Initial (3,3) (4,4) (6, 1) (3,3) (6,2)
1 (3,3) (4,4) (4, 5) (3,3) (6,2)
2 (3,7) (4,4) (4, 5) (5,5) (6,2)
3 (3,7) (4,6) (4, 7) (5,5) (6,2)
SanJose
31
5
46
2
2
3
4
2
1
1
2
3
50
2
5
57
4
7
6
Node 2 could have chosen 5 as next node because of tie
3
5
46
2
2
3
4
2
1
1
2
3
51
Iteration Node 1 Node 2 Node 3 Node 4 Node 5
1 (3,3) (4,4) (4, 5) (3,3) (6,2)
2 (3,7) (4,4) (4, 5) (2,5) (6,2)
3 (3,7) (4,6) (4, 7) (5,5) (6,2)
4 (2,9) (4,6) (4, 7) (5,5) (6,2)
SanJose
0
77
5
6
9
2
Node 1 could have chose 3 as next node because of tie
31 2 41 1 1
31 2 41 1
X
(a)
(b)
Update Node 1 Node 2 Node 3
Before break (2,3) (3,2) (4, 1)
After break (2,3) (3,2) (2,3)
1 (2,3) (3,4) (2,3)
2 (2,5) (3,4) (2,5)
3 (2,5) (3,6) (2,5)
4 (2,7) (3,6) (2,7)
5 (2,7) (3,8) (2,7)
… … … …
Counting to Infinity Problem
Nodes believe best path is through each other
(Destination is node 4)