shortest-paths. p2. shortest-paths problems : g=(v,e) : weighted, directed graph w : e r :...

21
Shortest-paths

Upload: carrie-lemacks

Post on 16-Dec-2015

238 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

Shortest-paths

Page 2: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

p2.

Shortest-paths problems :

G=(V,E) : weighted, directed graph w : E R : weight function P=<v0,v1,…,vk) : path form v0 to vk.

Shortest-path weight from u to v :

k

iii vvwpw

11 ),()(

v.u to frompath a if },:)(min{),( vupwvu

p

otherwise. ,

Page 3: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

p3.

Shortest paths tree rooted at s: 1. V’ is the set of vertices reachable from s in G. 2. G’ forms a rooted tree with root s. 3. the unique simple path from s to v in G’ is a

shortest path from s to v in G. Predecessor subgraph

s.t. .','),','(' EEVVEVG

,'Vv

).,( EVG

}{}][:{ sNILvVvV

}}{:)],[{ sVvEvvE

台北

新竹

台中

嘉義

高雄

台東

花蓮

宜蘭

台南

埔里

30100

80

100

70

30 50

100

90140

60

90

Page 4: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

Optimal substructure of a shortest path:

Lemma1(subpaths of shortest paths are shortest paths)Given a weighted, dircted graph G=(V,E) with weight function w:ER, let p=<v1,v2,…,vk> be a shortest path from v1 to vk and , for any i and j such

that let Pij=<vi,vi+1,…,vj> be the subpath of p from vi to vj. Then,

pij is a

shortest path from vi to vj.

,kji 1

v1 vk

vi

vj

Proof :

Decompose p into .kp

jp

ip vvvv jkiji 1

1

).()()()( jkiji pwpwpwpw 1

Suppose there is a path p’ij from vi to vj with w(p’ij)<w(pij).

Then W(p)>w(p1j)+w(p’ij)+w(pjk).

P is not shortest !

Page 5: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

Cor2 :

Let p be a shortest path from s to a vertex v. P’ is a subpath of p.

Then the shortest path from s to v has weight

Lemma3G(V,E) : weighted, directed graph,S:source vertex.Then, for all edges ,we have

.' vusp p

Proof :

By the above lemma, p’ is a shortest path froms to u.Thus,

).,(),(

),()'(

)(),(

vuwus

vuwpw

pwvs

).,(),(),( vuwusvs

Evu ),( ).,(),(),( vuwusvs

Proof :

S u

v

Shortest. ),( vs

Page 6: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

p6.

Relaxation

d[v] : upper bound on the weight of a shortest path from

source s to v. : the predecessor of v along a shortest path

from s to v.

][v

Initialize-Single-source(G,s)

{ for each vertex ][GVv

do{ d[v]=}][ NILv

}][ 0sd

Relax(u,v,w)

{ if ),(][][ vuwudvd

then { ),(][][ vuwudvd }][ uv

}

Eg.

5 9

S

u v2

Relax(u,v)

5 7u v2

5 6u v2

5 6u v2

d[v]

w(u,v)d[u]

Sv

u

Page 7: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

Lemma4 :

Immediately after relaxing edge(u,v) by executing Relax(u,v,w), we have

Lemma5Given G=(V,E),w,s, let the graph be initialized by Initialize-Single-

Source(G,s). Then, for all and this invariant is maintained over any sequence

of relaxation steps on the edges of G. Moreover, once d[v] achieves its lower

bound Proof :

).,(][][ vuwudvd

),(][ vsvd ,Vv

changes.never it ),,( vs

(1)

(2)

Clearly,

][),(][ vdvuwvd

after, initialization.

By contradiction, let v be the first vertex for which a relaxation step of an edge (u,v) cause ).,(][ vsvd

Therefore, we have

),( vs),(),( vuwus

ex first vert not the is ),(][ vusud v).(s,d[v]with

).,(][ vsvd

(1) ),(][ vsvd

(2) Suppose ),(][ vsvd Then d[v] cannot decrease, since it reaches the lower bound.And it cannot increased, because relaxation steps do not increase d values.

Page 8: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

Cor6 :

Suppose no path connects a source vertex s to a given vertex . Then after

Initialize-Single-Source(G,s), we have , and this equality is maintained as

an invariant over any sequence of relaxation steps on the edges of G.

Lemma7Let be a shortest path in G for some verticesSuppose that G is initialized by Initialize-Single-Source(G,s) and then a

sequence of Relaxation steps that includes the call Relax(u,v,w) is executed on the

edges of G.If at any time prior to the call, then at all times after

the call.

Proof :

,Vv

).,(][ So ];[),( vsvdvdvs

If at some point prior to relaxing edge (u,v), then this equality holds thereafter.

),(),( vuwus

).,(][ vsvd

),(][][ vuwudvd

),(][ vsvd

vus p ., Vvu

),(][ usvd ),(][ vsvd

Proof :

),(][ usvd

After relaxing edge(u,v), we have

),( vs

Thus this equality is maintained thereafter.

Page 9: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

Shortest-paths trees:

Lemma8G=(E,V) : weighted, directed graph S : source vertex.W : weight function.Assume G no negative-weight cycles that are reachable from s.Then, after the graph is initialized by Initialize_Single-Source, the

predecessor subgraph from s a rooted tree with root s, and any sequence of

relaxation steps on edges of G maintains this property as an invariant.

G

Proof :

Initially, s is the only vertex in .

Consider a predecessor subgraph that arises after a sequence of relaxation steps.

Initially, s is the only vertex in . G

G

Claim : is acyclic.G

By contradiction, suppose that some relaxation step creates a cycle in . Let theGcycle be c=<vo,v1,….,vk>, where vk=v0. Then 1...k.ifor ][ 1ii vv

WLOG, assume it was Relax(vk-1,vk,w) that created the cycle in.GEach vertex on c has a non-NIL predecessor and so was assigned a finite shortest-pathestimate, which implies each vertex on c is reachable from s.

Just before the call Relax(vk-1,vk,w), we have for i=1,2,…,k-1. Thus, for

i=1,2,…,k-1, the last update to d[vi-1] changed since then, it decreased.

1 ii vv ][

Therefore, just before the call Relax (vk-1,vk,w), we have

.,...,,for ],,[][][ 12111 kivvwvdvd iiii(*)

Vk=v0

Vk-1

S

Page 10: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

is changed by the call, immediately beforehand we also have the strict inequality. ][ kv

).,(][][ kkkk vvwvdvd 11 (**)(*)+(**)

k

iiii

k

ii vvwvdvd

111

1

)),(][(][

k

iii

k

ii vvwvd

11

11 ),(][

)]d[]d[ ( ),,(

k

ii

k

i

k

iiii vvvvw

11

1 110 Why?

a negative-weight cycle reachable form s.

Thus, in , there is no cycle.G

By induction, show that a path exists from s to all vertices in .V (exercise!)

Claim : for any vertex , there is at most one path from s to v in Vv .G

By contradiction,Suppose that there are 2 simple paths from s to some vertex v :

s u

x

y

z v

P1 : S u x z vpa

pb

pc

pd

pa pb pd

P2 : S u y z vpa pc pd

. ][,][ yzxz

Thus, exists a unique simple path in from s to v, and thus froms a rooted tree with root s..G.G

Page 11: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

Lemma9

G=(E,V) : weighted, directed graph with weight function w and source vertex

G has no negative cycle reachable from s.

Let us call Initialize-Single-Source(G,s) and then execute any sequence of relaxation

steps on edges of G that produces Then, the predecessor

Subgraph is a shortest-paths tree rooted at s.

Proof :

Thus, d[v] is finite iff v is reachable from s.

By definition, is finite iff v is reachable from s.

. allfor ),(][ Vvvsvd

Need to show the 3 properties of shortest-paths trees hold for

(1)

.Vs

G

.G

: from reachable : sV

),( vs

But, for , d[v] is finite iff}{sVv .][ NILv

Thus, are exactly those vertices reachable from s.V

(2) Gforms a rooted tree with root s :By lemma 8.

S

vi-1

vid[vi-1]

d[vi]

(3)For al , the unique simple path is a shortest path from s to v in G. Vv Gvs p in Let ),(][][ and ),,(][ ,...For .,...,, iiiiiiki vwwvdvdvsvdkivvvsvp 110 1

).,(),(),( 11 iiii vsvsvvw

k

iii vvwpw

11 ),()( ),(),()),(),(( 0

11 vsvsvsvs k

k

iii

0=

).,()(),()( kk vspwvspw

Thus, p is a shortest path from s to v.

Page 12: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

p12.

Dijkstra’s algorithm :

Algorithm : G=(V,E), Q : priority queue with d as the key.

.),(each for ),( Evuvuw 0

Dijsktra(G,w,s)

{ Initialize-Single-Source(G,s)

u=Extract-Min(Q)

S][GVQ

do while Q{

}{uSS ][ex each vertfor uadjv

)(Re do u,v,wlax}

}

Page 13: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

p13.

Analysis :

Priority queue : Time :

linear array

binary heap

Fibonacci heap

)( 2vQ

)lg()lg)(( VEOVEVQ

)lg( EVVQ

Page 14: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

0s

u

x

v

y

10

5

2 3

1

9

2

74 6 0s

u

x5

v

y

10

5

2 3

1

9

2

74 6

0s

u

x5

v

y7

10

5

2 3

1

9

2

74 6 0s

u8

x5

v13

y7

10

5

2 3

1

9

2

74 6

0s

u8

x5

v9

y7

10

5

2 3

1

9

2

74 6 0s

u8

x5

v9

y7

10

5

2 3

1

9

2

74 6

Page 15: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

Lemma10 : (correctness of Dijkstra’s algorithm)

If we run Dijkstra’s algorithm on a weighted, directed graph G=(V,E) with nonnegative weight function w and source s, then at termination,

for all vertices Proof :

),(][ usud

Want to show : for each at the time when u is inserted into S, and maintainsthereafter.

),(][ usud By contradiction, let u be the first vertex for which when it is inserted into S.

, when u is inserted into S.

Claim : , when u is inserted into S.

Ssssdsu .),(]{ , 0

),(][ ysyd

),(][, usudVu

Since both u and y were in V-S. When u was chosen in the algorithm, we have

Thus,

.Vu

There must be a path ),(][ otherwise , usudus Thus there is a shortest path p from s to u.

S

pu SV

S

y

x u

p1 p2

S

: the first one in V-S along p

),(][ xsyd when x was inserted into S.

When u is inserted into S, (x,y) had been relaxed, thus ),(][ ysyd ][),(),(][ udusysyd ][yd

].[][ ydud ].[),(),(][ udusysyd

Contradict the assumption that ).,(][ usud

Page 16: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

p16.

Bellman-Ford algorithm :

Allow edge weights to be negative Return TRUE iff the graph contains no negative cycle

that are reachable from s.

Bellman-Ford(G,w,s)

{ Initialize-Single-Source(G,s)

1 V to1ifor

}

Evu ),( edgeeach for dow)v,elax(u, do REvu ),( edgeeach for

v)w(u,d[u]d[v] if do False;return then

True;return

Page 17: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

0s

u

x

v

y

6

7

85

-3

9

27

relax order : (u,v),(u,x),(u,y),(v,u),(x,v),(x,y),(y,v),(y,z),(z,u),(z,x)

),(][][ uzwzdud

-4

-2

0s

u6

x7

v

y

6

7

85

-3

9

27

-4

-2

0s

u6

x7

v4

y2

6

7

85

-3

9

27

-4

-2

0s

u6

x7

v4

y2

6

7

85

-3

9

27

-4

-2

0s

u6

x7

v4

y2

6

7

85

-3

9

27

-4

-2

Page 18: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

Lemma 12 :

At the termination of Bellman-Ford, we have for all v that are reachable

from s.

Corollary 13 :For each vertex , there is a path from s to v iff Bellman-Ford terminates with when it is run on G.

Proof :

),(][ vsvd

Let v be a vertex reachable from s.

. ,...,, 110 Vkvvvsvp k

By induction, for I=0,…,k, we have after the I-th pass and this equality is maintained thereafter.

),(][ ii vsvd

Note that there are V-1 passes.

Basis : 000 ),(][ vsvd Inductive step : Assume after the (i-1)st pass.),(][ 11 ii vsvd

(Vi-1,vi) is relaxed in the I-th pass, by lemma7

we have after the I-th pass. ),(][ ii vsvd

Vv][vd

Proof :Similar to lemma12.

Page 19: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

Thm14 : (correctness of the Bellman-Ford algorithm)

The algorithm returns TRUE, we have and the predecessor

subgraph is a shortest=paths tree rotted at s.If G does maintain a negative-weight cycle reachable from s, then the

algorithm returnsFalse.

Proof :

, allfor ),(][ Vvvsvd (1)

G

G has no negative cycle reachable from s :

(2)

(1)a. V : reachable from s.

By lemma12, ).,(][ vsvd b. V : not reachable from s.

By corllary6, ).,(][ vsvd

With lemma9 is a shortest-paths tree.

At termination, for all

G

,),( Evu ),(][),(),(),(][ vuwudvuwusvsvd

Thus, the algorithm return TRUE.(2)G has a negative cycle reachable form s. 010 vvvvp k,...,,

k

iii vvw

11 0.),(

Assume the algorithm returned TRUE, i.e.

k1,...,i ),(][][ iiii vvwvdvd 11

k

i

k

iii

k

iii vvwvdvd

1 11

11 ),(][][

~False returnsit Thus ),(

k

iii vvw

110

Page 20: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

p20.

Single-source shortest paths in DAGs :

DAG-Shortest-Paths(G,w,s)

1. Topologically sort the vertices of G.

2. Initialize-Single-Source(G,s).

3. For each vertex u taken in topologically sorted order

4. Do for each vertex

5. Do Relax(u,v,w).

][uAdjv

Page 21: Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

p21.

Thm15 :

G : a dag with source s.At termination of the above procedure , for all is a shortest-paths tree.

Proof :

),(][ vsvd

v : not reachable from s : then(1)

By a simple induction using lemma7, shows at termination for I=0,1,…,k.

By lemma9, is a shortest-paths tree.

GVv and ,

),(][ vsvd (2)v : reachable from s :

S=v0

v1

Vk-1

Vk=v

sortedally topologic:),( ii vv 1

),(][ ii vsvd

G