Chapter 24: Single-Source Shortest Paths: Given
Chapter 24: Single-Source Shortest Paths: Given
=
=
So, equality holds and p is a shortest path.
Jim Anderson
And note that this shortest path tree will be
found after V(G) - 1 iterations of Relax.
Comp 122, Fall 2003 Single-source SPs - 18
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 19
Bellman-Ford Algorithm
Can have negative-weight edges. Will detect reachable negative-weight
cycles.
Initialize(G, s);
for i := 1 to |V[G]| 1 do
for each (u, v) in E[G] do
Relax(u, v, w)
od
od;
for each (u, v) in E[G] do
if d[v] > d[u] + w(u, v) then
return false
fi
od;
return true
Time
Complexity
is O(VE).
Jim Anderson
So if Bellman-Ford has not converged after
V(G) - 1 iterations, then there cannot be a
shortest path tree, so there must be a negative
weight cycle.
Comp 122, Fall 2003 Single-source SPs - 20
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 21
Example
0
z
u v
x y
6
5
3
9
7
7
8
2
4
2
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 22
Example
0
7
6
z
u v
x y
6
5
3
9
7
7
8
2
4
2
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 23
Example
0
2 7
4 6
z
u v
x y
6
5
3
9
7
7
8
2
4
2
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 24
Example
0
2 7
4 2
z
u v
x y
6
5
3
9
7
7
8
2
4
2
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 25
Example
0
-2 7
4 2
z
u v
x y
6
5
3
9
7
7
8
2
4
2
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 26
Another Look
Note: 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 = 0
min({d(k, j1) + w(k, i): i e Adj(k)}
{d(i, j1)}) if j > 0
z u v x y
1 2 3 4 5
0 0
1 0 6 7
2 0 6 4 7 2
3 0 2 4 7 2
4 0 2 4 7 2
j
i
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 27
Lemma 24.2
Lemma 24.2: Assuming no negative-weight cycles reachable from
s, d[v] = o(s, v) holds upon termination for all vertices v reachable
from s.
Proof:
Consider a SP p, where p = v
0
, v
1
, , v
k
, where v
0
= s and v
k
= v.
Assume k s |V| 1, otherwise p has a cycle.
Claim: d[v
i
] = o(s, v
i
) holds after the i
th
pass over edges.
Proof follows by induction on i.
By Lemma 24.11, once d[v
i
] = o(s, v
i
) holds, it continues to hold.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 28
Correctness
Claim: 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] = o(s, v) , by lemma 24.2 (last slide) if v is reachable;
d[v] = o(s, v) = otherwise.
s o(s, u) + w(u, v) , by Lemma 24.10.
= d[u] + w(u, v)
So, algorithm returns true.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 29
Case 2
Case 2: There exists a reachable negative-weight cycle
c = v
0
, v
1
, , v
k
, where v
0
= v
k
.
We have E
i = 1, , k
w(v
i-1
, v
i
) < 0. (*)
Suppose algorithm returns true. Then, d[v
i
] s d[v
i-1
] + w(v
i-1
, v
i
) for
i = 1, , k. (because Relax didnt change any d[v
i
] ). Thus,
E
i = 1, , k
d[v
i
] s E
i = 1, , k
d[v
i-1
] + E
i = 1, , k
w(v
i-1
, v
i
)
But, E
i = 1, , k
d[v
i
] = E
i = 1, , k
d[v
i-1
].
Can show no d[v
i
] is infinite. Hence, 0 s E
i = 1, , k
w(v
i-1
, v
i
).
Contradicts (*). Thus, algorithm returns false.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 30
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] do
Relax(u, v, w)
od
od
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 31
Example
0
r s t
u
v w
5 2 7 1 2
6 1
3
2
4
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 32
Example
0
r s t
u
v w
5 2 7 1 2
6 1
3
2
4
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 33
Example
0 2 6
r s t
u
v w
5 2 7 1 2
6 1
3
2
4
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 34
Example
0 2 6 6 4
r s t
u
v w
5 2 7 1 2
6 1
3
2
4
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 35
Example
0 2 6 5 4
r s t
u
v w
5 2 7 1 2
6 1
3
2
4
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 36
Example
0 2 6 5 3
r s t
u
v w
5 2 7 1 2
6 1
3
2
4
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 37
Example
0 2 6 5 3
r s t
u
v w
5 2 7 1 2
6 1
3
2
4
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 38
Dijkstras Algorithm
Assumes no negative-weight edges.
Maintains a set S of vertices whose SP from s has been determined.
Repeatedly selects u in VS with minimum SP estimate (greedy choice).
Store VS in priority queue Q.
Initialize(G, s);
S := C;
Q := V[G];
while Q = C do
u := Extract-Min(Q);
S := S {u};
for each v e Adj[u] do
Relax(u, v, w)
od
od
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 39
Example
0
s
u v
x y
10
1
9
2
4 6
5
2 3
7
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 40
Example
0
5
10
s
u v
x y
10
1
9
2
4 6
5
2 3
7
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 41
Example
0
7 5
14 8
s
u v
x y
10
1
9
2
4 6
5
2 3
7
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 42
Example
0
7 5
13 8
s
u v
x y
10
1
9
2
4 6
5
2 3
7
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 43
Example
0
7 5
9 8
s
u v
x y
10
1
9
2
4 6
5
2 3
7
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 44
Example
0
7 5
9 8
s
u v
x y
10
1
9
2
4 6
5
2 3
7
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 45
Correctness
Theorem 24.6: Upon termination, d[u] = (s, u) for all u in V
(assuming non-negative weights).
Proof:
By Lemma 24.11, once d[u] = (s, u) holds, it continues to hold.
We prove: For each u in V, d[u] = o(s, u) when u is inserted in S.
Suppose not. Let u be the first vertex such that d[u] = o(s, u) when
inserted in S.
Note that d[s] = o(s, s) = 0 when s is inserted, so u = s.
S = C just before u is inserted (in fact, s e S).
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 46
Proof (Continued)
Note that there exists a path from s to u, for otherwise
d[u] = o(s, u) = by Corollary 24.12.
there exists a SP from s to u. SP looks like this:
x
s
y
u
S
p
1
p
2
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 47
Proof (Continued)
Claim: d[y] = o(s, y) when u is inserted into S.
We had d[x] = o(s, x) when x was inserted into S.
Edge (x, y) was relaxed at that time.
By Lemma 24.14, this implies the claim.
Now, we have: d[y] = o(s, y) , by Claim.
s o(s, u) , nonnegative edge weights.
s d[u] , by Lemma 24.11.
Because u was added to S before y, d[u] s d[y].
Thus, d[y] = o(s, y) = o(s, u) = d[u].
Contradiction.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 48
Complexity
Running time is
O(V
2
) using linear array for priority queue.
O((V + E) lg V) using binary heap.
O(V lg V + E) using Fibonacci heap.
(See book.)