Professional Documents
Culture Documents
Initialize(G,
Initialize(G,s)s)
for
foreach
eachvvV[G]
V[G]do
do
d[v]
d[v]:=
:=;;
[v]:=
[v] :=NIL
NIL
od;
od;
d[s]
d[s]:=
:=00
These values are changed when an edge (u, v) is relaxed:
Relax(u,
Relax(u,v,v,w)w)
ififd[v]
d[v]>>d[u]
d[u]++w(u,
w(u,v)
v)then
then
d[v]
d[v]:=
:=d[u]
d[u]++w(u,
w(u,v);
v);
[v]:=
[v] :=uu
fifi
Jim Anderson Comp 122, Fall 2003 Single-source SPs -
Properties of Relaxation
d[v], if not , is the length of some path from s
to v.
d[v] either stays the same or decreases with time
Therefore, if d[v] = (s, v) at any time, this
holds thereafter
Note that d[v] (s, v) always
After i iterations of relaxing on all (u,v), if the
shortest path to v has i edges, then d[v] = (s, v).
p'
Lemma 24.14: Let p = SP from s to v, where
Lemma 24.14: Let p = SP from s to v, where p = s p = s uuv.
v.
IfIfd[u]
d[u]==δ(s,
δ(s,u)
u)holds
holdsatatany
anytime
timeprior
priorto
tocalling
callingRelax(u,
Relax(u,v,v,w),
w),
then
thend[v]
d[v]==δ(s,
δ(s,v)
v) holds
holdsatatall
alltimes
timesafter
afterthe
thecall.
call.
Proof:
After the call we have:
d[v] d[u] + w(u, v) , by Lemma 24.13.
= (s, u) + w(u, v) , d[u] = (s, u) holds.
= (s, v) , by corollary to Lemma 24.1.
By Lemma 24.11, d[v] δ(s, v), so d[v] = δ(s, v).
Jim Anderson Comp 122, Fall 2003 Single-source SPs -
Lemma 24.13 follows simply from the structure
of Relax.
Lemma 24.14 shows that the shortest path will
be found one vertex at a time, if not faster.
Thus after a number of iterations of Relax equal
to V(G) - 1, all shortest paths will be found.
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
Jim Anderson Compfrom s.2003(Why?)
122, Fall Single-source SPs - 1
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
k
[v
kk
] is changed by call, d[vk] > d[vk-1] + w(vk-1, vk). Thus,
d[vi ] (d[vi 1 ] w(vi 1 , vi ))
i 1 i 1
k k
d[vi 1 ] w(v i 1 , v i )
i 1 i 1
k k k
Because d[vi ] d[v i 1 ], w(v i 1 , v i ) 0, i.e., neg. - weight cycle!
i 1 i 1 i 1
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1
Comment on Proof
Show 1 path.
Assume 2 paths.
x
s u z v
y
impossible!
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1
Lemma 24.17
Lemma
Lemma24.17:
24.17:Same Sameconditions
conditionsasasbefore.
before. Call
CallInitialize
Initialize& &repeatedly
repeatedly
call
callRelax
Relaxuntil
untild[v]d[v]==δ(s,
δ(s,v)
v)for
forall
allvvin
inV.
V. Then,
Then,GGisisaashortest-path
shortest-path
tree
treerooted
rootedatats.s.
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.
δ(s , v k ) δ(s , v 0 )
δ(s , v k )
9
x y
7
9
x y
7 2
9
x y
7 2
9
x y
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
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 2
Lemma 24.2
Lemma
Lemma24.2:24.2:Assuming
Assumingno nonegative-weight
negative-weightcycles
cyclesreachable
reachablefrom
from
s,s,d[v]
d[v]==(s,
(s,v)
v)holds
holdsupon
upontermination
terminationfor
forall
allvertices
verticesvvreachable
reachable
from
froms.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.
Suppose algorithm returns true. Then, d[vi] d[vi-1] + w(vi-1, vi) for
i = 1, …, k. (because Relax didn’t change any d[vi] ). Thus,
Topologically
Topologicallysort
sortvertices
verticesininG;
G;
Initialize(G,
Initialize(G,s);
s);
for
foreach
eachuuin
inV[G]
V[G](in(inorder)
order)do
do
for
foreach
eachvvininAdj[u]
Adj[u]do do
Relax(u,
Relax(u,v,v,w)
w)
od
od
od
od
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
10
9
2 3
s 0 4 6
5 7
2
x y
10
9
2 3
s 0 4 6
5 7
5
2
x y
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
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).
p2
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.)