Design and Analysis of Algorithms: CSE 5311 Lecture 21 Single-Source Shortest Paths
Design and Analysis of Algorithms: CSE 5311 Lecture 21 Single-Source Shortest Paths
CSE 5311
Lecture 21 Single-Source Shortest Paths
Initialize(G, s)
for each v ∈ V[G] do
d[v] := ∞;
π[v] := NIL
end;
d[s] := 0
These values are changed when an edge (u, v) is relaxed:
Relax(u, v, w)
if d[v] > d[u] + w(u, v) then
d[v] := d[u] + w(u, v);
π[v] := u
end
Dept. CSE, UT Arlington CSE5311 Design and Analysis of Algorithms 6
Properties of Relaxation
Proof:
Two proof obligations:
(1) Gπ is acyclic.
(2) There exists a unique path from source s to each vertex in Vπ.
Proof of (1):
Suppose there exists a cycle c = ‹v0, v1, …, vk›, where v0 = vk.
We have π[vi] = vi-1 for i = 1, 2, …, k.
Assume relaxation of (vk-1, vk) created the cycle.
We show cycle has a negative weight.
Note: Cycle must be reachable from s.
Dept. CSE, UT Arlington CSE5311 Design and Analysis of Algorithms 13
Proof of (1) (Continued)
Before call to Relax(vk-1, vk, w):
π[vi] = vi-1 for i = 1, …, k–1.
Implies d[vi] was last updated by “d[vi] := d[vi-1] + w(vi-1, vi)”
for i = 1, …, k–1. [Because Relax updates π.]
Implies d[vi] ≥ d[vi-1] + w(vi-1, vi) for i = 1, …, k–1. [Lemma 24.13]
Because π[vk] is changed by call, d[vk] > d[vk-1] + w(vk-1, vk). Thus,
k k
Proof:
Key Proof Obligation: For all v in Vπ, the unique simple path p from
s to v in Gπ (path exists by Lemma 24.16) is a shortest path from s to v
in G.
Let p = ‹v0, v1, …, vk›, where v0 = s and vk = v.
We have d[vi] = δ(s, vi)
d[vi] ≥ d[vi-1] + w(vi-1, vi) (reasoning as before)
Implies w(vi-1, vi) ≤ δ(s, vi) – δ(s, vi-1).
Dept. CSE, UT Arlington CSE5311 Design and Analysis of Algorithms 16
Proof (Continued)
w(p)
k
= ∑ w(vi −1 , v i )
i =1
k
≤ ∑ (δ(s , v i ) − δ(s , v i-1 ))
i =1
= δ(s , v k ) − δ(s , v 0 )
= δ(s , v k )
u v
5
∞ ∞
–2
6 –3
8
z 0 7
–4
7 2
∞ ∞
9
x y
u v
5
6 ∞
–2
6 –3
8
z 0 7
–4
7 2
7 ∞
9
x y
u v
5
6 4
–2
6 –3
8
z 0 7
–4
7 2
7 2
9
x y
u v
5
2 4
–2
6 –3
8
z 0 7
–4
7 2
7 2
9
x y
u v
5
2 4
–2
6 –3
8
z 0 7
–4
7 2
7 -2
9
x y
Let d(i, j) = cost of the shortest path from s to i that is at most j hops.
0 if i = s ∧ j = 0
∞ if i ≠ s ∧ j = 0
d(i, j) =
min({d(k, j–1) + w(k, i): i ∈ Adj(k)}
∪ {d(i, j–1)}) if j > 0
i
z u v x y
1 2 3 4 5
j 0 0 ∞ ∞ ∞ ∞
1 0 6 ∞ 7 ∞
2 0 6 4 7 2
3 0 2 4 7 2
4 0 2 4 7 –2
Dept. CSE, UT Arlington CSE5311 Design and Analysis of Algorithms 26
Lemma 24.2
Lemma 24.2: Assuming no negative-weight cycles reachable from
s, d[v] = δ(s, v) holds upon termination for all vertices v reachable
from s.
Proof:
Consider a SP p, where p = ‹v0, v1, …, vk›, where v0 = s and vk = v.
Claim: d[vi] = δ(s, vi) holds after the ith pass over edges.
Proof follows by induction on i.
6 1
r s t u v w
5 2 7 –1 –2
∞ 0 ∞ ∞ ∞ ∞
4
3
2
6 1
r s t u v w
5 2 7 –1 –2
∞ 0 ∞ ∞ ∞ ∞
4
3
2
6 1
r s t u v w
5 2 7 –1 –2
∞ 0 2 6 ∞ ∞
4
3
2
6 1
r s t u v w
5 2 7 –1 –2
∞ 0 2 6 6 4
4
3
2
6 1
r s t u v w
5 2 7 –1 –2
∞ 0 2 6 5 4
4
3
2
6 1
r s t u v w
5 2 7 –1 –2
∞ 0 2 6 5 3
4
3
2
6 1
r s t u v w
5 2 7 –1 –2
∞ 0 2 6 5 3
4
3
2
u v
1
∞ ∞
10
9
2 3
s 0 4 6
5 7
∞ ∞
2
x y
u v
1
10 ∞
10
9
2 3
s 0 4 6
5 7
5 ∞
2
x y
u v
1
8 14
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
u v
1
8 13
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
u v
1
8 9
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
u v
1
8 9
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Proof:
By Lemma 24.11, once d[u] = δ(s, u) holds, it continues to hold.
We prove: For each u in V, d[u] = δ(s, u) when u is inserted in S.
Suppose not. Let u be the first vertex such that d[u] ≠ δ(s, u) when
inserted in S.
Note that d[s] = δ(s, s) = 0 when s is inserted, so u ≠ s.
⇒ S ≠ ∅ just before u is inserted (in fact, s ∈ S).
u
s
p1
x y
S
Running time is
O(V2) using linear array for priority queue.
O((V + E) lg V) using binary heap.
O(V lg V + E) using Fibonacci heap.
(See book.)