Professional Documents
Culture Documents
Single-
Jim Anderson Comp 122, Fall 2003 source
General Results (Relaxation)
Lemma
Lemma24.1: 24.1:Let
Letpp==‹v ‹v11,,vv22,,…,
…,vvkk››be
beaaSP
SPfrom
fromvv11to
tovvkk.. Then,
Then,
ppijij==‹v
‹vi,i,vvi+1 , …, v › is a SP from v to v , where 1 i j k.
i+1, …, vj j› is a SP from vi i to vj j, where 1 i j k.
wherepp== ss
uuv. p'
Corollary:
Corollary:LetLetpp==SP
SPfrom
fromssto
tov,v,where v.Then,
Then,
δ(s,
δ(s,v)
v)==δ(s,
δ(s,u)
u) ++w(u,
w(u,v).
v).
Lemma
Lemma24.10:
24.10:LetLetssV.
V. For
Forall
alledges (u,v)E,
edges(u,v) E,we
wehave
have
δ(s, v)δ(s,
δ(s,v) δ(s,u)
u) ++w(u,v).
w(u,v).
Single-
Jim Anderson Comp 122, Fall 2003 source
Relaxation
Algorithms keep track of d[v], [v]. Initialized as follows:
Initialize(G,
Initialize(G,s)s)
for eachvvV[G]
foreach 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
Single-
Jim Anderson Comp 122, Fall 2003 source
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).
Single-
Jim Anderson Comp 122, Fall 2003 source
Properties of Relaxation
Consider any algorithm in which d[v], and [v] are first initialized
by calling Initialize(G, s) [s is the source], and are only changed by
calling Relax. We have:
Lemma
Lemma24.11:
24.11:(
(v:: d[v](s,
v::d[v] (s,v))
v))isisan
aninvariant.
invariant.
Implies d[v] doesn’t change once d[v] = (s, v).
Proof:
Initialize(G, s) establishes invariant. If call to Relax(u, v, w)
changes d[v], then it establishes:
d[v] = d[u] + w(u, v)
(s, u) + w(u, v) , invariant holds before call.
(s, v) , by Lemma 24.10.
Corollary
Corollary24.12:
24.12:IfIfthere
thereisisno
nopath
pathfrom
fromssto
tov,v,then
then
d[v]
d[v]==δ(s, v)==isisan
δ(s,v) aninvariant.
invariant.
Single-
Jim Anderson Comp 122, Fall 2003 source
More Properties
Lemma
Lemma24.13:
24.13:Immediately
Immediatelyafter
afterrelaxing
relaxingedge
edge(u,
(u,v)
v)by
bycalling
calling
Relax(u,
Relax(u,v,v,w),
w),we
wehave d[v]d[u]
haved[v] d[u]++w(u,
w(u,v).
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.
Single-
Note: Cycle must be reachable
Jim Anderson Comp 122, from
Fall 2003s. (Why?) source
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.
Implies d[vi] d[vi-1] + w(vi-1, vi) for i = 1, …, k–1.
Because [v
k k k] is changed by call, d[vk] > d[vk-1] + w(vk-1, vk). Thus,
Show 1 path.
Assume 2 paths.
x
s u z v
y
impossible!
Single-
Jim Anderson Comp 122, Fall 2003 source
Lemma 24.17
Lemma
Lemma24.17:
24.17:Same Sameconditions
conditionsas
asbefore.
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 )
Single-
Jim Anderson Comp 122, Fall 2003 source
Bellman-Ford Algorithm
Can have negative-weight edges. Will “detect” reachable negative-weight
cycles.
Initialize(G,
Initialize(G,s); s);
for
forii:= :=11toto|V[G]|
|V[G]|–1 –1do
do
for
foreach
each(u,(u,v)
v)in
inE[G]
E[G]do
do
Relax(u,
Relax(u,v,v,w)w)
od Time
od
od; Complexity
od;
for is O(VE).
foreach
each(u,(u,v)
v)ininE[G]
E[G]dodo
ififd[v]
d[v]>>d[u]
d[u]++w(u,
w(u,v)
v)then
then
return
returnfalse
false
fifi
od;
od;
return
returntrue true
Single-
Jim Anderson Comp 122, Fall 2003 source
Example
u v
5
–2
6 –3
8
z 0 7
–4
7 2
9
x y
Single-
Jim Anderson Comp 122, Fall 2003 source
Example
u v
5
6
–2
6 –3
8
z 0 7
–4
7 2
7
9
x y
Single-
Jim Anderson Comp 122, Fall 2003 source
Example
u v
5
6 4
–2
6 –3
8
z 0 7
–4
7 2
7 2
9
x y
Single-
Jim Anderson Comp 122, Fall 2003 source
Example
u v
5
2 4
–2
6 –3
8
z 0 7
–4
7 2
7 2
9
x y
Single-
Jim Anderson Comp 122, Fall 2003 source
Example
u v
5
2 4
–2
6 –3
8
z 0 7
–4
7 2
7 -2
9
x y
Single-
Jim Anderson Comp 122, Fall 2003 source
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.
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
Single-
Jim Anderson Comp 122, Fall 2003 source
Lemma 24.2
Lemma
Lemma24.2:
24.2:Assuming
Assumingnononegative-weight
negative-weightcycles
cyclesreachable
reachablefrom
from
d[v]==(s,
s,s,d[v] (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.
Single-
Jim Anderson Comp 122, Fall 2003 source
Case 2
Case 2: There exists a reachable negative-weight cycle
c = ‹v0, v1, …, vk›, where v0 = vk.
Suppose algorithm returns true. Then, d[vi] d[vi-1] + w(vi-1, vi) for
i = 1, …, k. Thus,
Topologically
Topologicallysort
sortvertices
verticesin
inG;
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]dodo
Relax(u,
Relax(u,v,v,w)
w)
od
od
od
od
Single-
Jim Anderson Comp 122, Fall 2003 source
Example
6 1
r s t u v w
5 2 7 –1 –2
0
4
3
2
Single-
Jim Anderson Comp 122, Fall 2003 source
Example
6 1
r s t u v w
5 2 7 –1 –2
0
4
3
2
Single-
Jim Anderson Comp 122, Fall 2003 source
Example
6 1
r s t u v w
5 2 7 –1 –2
0 2 6
4
3
2
Single-
Jim Anderson Comp 122, Fall 2003 source
Example
6 1
r s t u v w
5 2 7 –1 –2
0 2 6 6 4
4
3
2
Single-
Jim Anderson Comp 122, Fall 2003 source
Example
6 1
r s t u v w
5 2 7 –1 –2
0 2 6 5 4
4
3
2
Single-
Jim Anderson Comp 122, Fall 2003 source
Example
6 1
r s t u v w
5 2 7 –1 –2
0 2 6 5 3
4
3
2
Single-
Jim Anderson Comp 122, Fall 2003 source
Example
6 1
r s t u v w
5 2 7 –1 –2
0 2 6 5 3
4
3
2
Single-
Jim Anderson Comp 122, Fall 2003 source
Dijkstra’s Algorithm
Assumes no negative-weight edges.
Maintains a set S of vertices whose SP from s has been determined.
Repeatedly selects u in V–S with minimum SP estimate (greedy choice).
Store V–S in priority queue Q.
Initialize(G,
Initialize(G,s);
s);
:=;
SS:= ;
QQ:=:=V[G];
V[G];
whileQQ
while dodo
uu:=
:=Extract-Min(Q);
Extract-Min(Q);
:=SS
SS:= {u};
{u};
for eachvvAdj[u]
foreach Adj[u]do
do
Relax(u,
Relax(u,v,v,w)
w)
od
od
od
od
Single-
Jim Anderson Comp 122, Fall 2003 source
Example
u v
1
10
9
2 3
s 0 4 6
5 7
2
x y
Single-
Jim Anderson Comp 122, Fall 2003 source
Example
u v
1
10
10
9
2 3
s 0 4 6
5 7
5
2
x y
Single-
Jim Anderson Comp 122, Fall 2003 source
Example
u v
1
8 14
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Single-
Jim Anderson Comp 122, Fall 2003 source
Example
u v
1
8 13
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Single-
Jim Anderson Comp 122, Fall 2003 source
Example
u v
1
8 9
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Single-
Jim Anderson Comp 122, Fall 2003 source
Example
u v
1
8 9
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Single-
Jim Anderson Comp 122, Fall 2003 source
Correctness
Theorem
Theorem24.6:
24.6:Upon
Upontermination,
termination,d[u]
d[u]==δ(s,
δ(s,u)
u)for
forall
alluuin
inVV
(assuming
(assumingnon-negative
non-negativeweights).
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] = (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).
Single-
Jim Anderson Comp 122, Fall 2003 source
Proof (Continued)
Note that there exists a path from s to u, for otherwise
d[u] = (s, u) = by Corollary 24.12.
there exists a SP from s to u. SP looks like this:
p2
u
s
p1
x y
S
Single-
Jim Anderson Comp 122, Fall 2003 source
Proof (Continued)
Claim: d[y] = (s, y) when u is inserted into S.
We had d[x] = (s, x) when x was inserted into S.
Edge (x, y) was relaxed at that time.
By Lemma 24.14, this implies the claim.
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.)
Single-
Jim Anderson Comp 122, Fall 2003 source