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

30
Single-Source Shortest-paths

Upload: kristin-skipworth

Post on 16-Dec-2015

249 views

Category:

Documents


0 download

TRANSCRIPT

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

Single-Source Shortest-paths

Page 2: Single-Source 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 :

11

(P) ( , )k

i ii

w w v v

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

p

otherwise. ,

Page 3: Single-Source 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

{ : . } { }V v V v NIL s

{( . , ) : { }}E v v E v V s

台北

新竹

台中

嘉義

高雄

台東

花蓮

宜蘭

台南

埔里

30100

80

100

70

30 50

100

90140

60

90

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

Optimal substructure of a shortest path:

Lemma 1 (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: Single-Source Shortest- paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

Cor :

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

.' vusp p

Proof :

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

).,(),(

),()'(

)(),(

vuwus

vuwpw

pwvs

).,(),(),( vuwusvs

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

p6.

Relaxation : v.d : 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.

.vInitialize-Single-Source(G,s)

{ for each vertex ][GVvdo{ v.d=

. }v NIL . 0}s d

Relax(u,v,w)

{ if . . ( , )v d u d w u v { . . ( , )v d u d w u v

}.v u }

Eg.

5 9

S

u v2

Relax(u,v)

5 7u v2

5 6u v2

5 6u v2

v.d

w(u,v)u.d

Sv

u

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

p7.

Properties of shortest paths and relaxation : Triangle inequality : For all edge (u, v) in E, (s, v) (s, u) + w(u, v) Upper-bound property: v.d ≥ (s, v) for all v and once v.d = (s, v) it never

changes. No-path property: If there is no path from s to v, then v.d = (s, v) = . Convergence property: If s --- u v is a shortest path in G, and if u.d = (s,

u) prior to relaxing (u, v), then v.d = (s, v) afterward.

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

p8.

Properties of shortest paths and relaxation : Path-relaxation property: p=<v0, v1, …, vk> is a shortest path from s=v0 to vk,

and the edges of p are relaxed in the order (v0, v1), (v1, v2),…,(v k-1, vk), then vk.d= (s, vk).

Predecessor-subgraph property: Once v.d = (s, v) for all v, the predecessor subgraph

is a shortest-paths tree rooted at s.

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

p9.

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

}

for each edge ( , )u v E elax(u,v,w)R

Evu ),( edgeeach for if v.d u.d w(u,v)

return False;

True;return

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

p10.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

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

Lemma 2 :

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

from s.

Corollary 3 :For each vertex , there is a path from s to v iff Bellman-Ford terminates

with when it is run on G.

Proof :

. ( , )v d s v

Let v be a vertex reachable from s.

0 1, ,..., | | 1.kp v s v v v k V

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

. ( , )i iv d s v

Note that there are |V|-1 passes.

Basis : 0 0. ( , ) 0v d s v

Inductive step : Assume after the (i-1)st pass.1 1. ( , )i iv d s v

(v i-1,vi) is relaxed in the i-th pass, by the path-relaxation property

we have after the i-th pass. . ( , )i iv d s v

Vv.v d

Proof :Similar to lemma 2. Exercise!

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

Thm 4 : (Correctness of the Bellman-Ford algorithm)

The algorithm returns TRUE and we have and the predecessor

subgraph is a shortest-paths tree rooted at s.---- when no negative cycle.If G does contain a negative-weight cycle reachable from s, then the

algorithm returnsFalse.

Proof :

. ( , ) for all ,v d s v v V (1)

G

G has no negative cycle reachable from s :

(2)

(1)a. v : reachable from s.

By lemma 2, . ( , ).v d s vb. v : not reachable from s.

. ( , ) .v d s v With lemma 17 is a shortest-paths tree (Predecessor-subgraph property).

At termination, for all

G

,),( Evu . ( , ) ( , ) ( , ) . ( , )v d s v s u w u v u d w u v

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

k

iii vvw

11 0.),(

Assume the algorithm returned TRUE, i.e.

1 1. . ( , ) i 1,...,ki ii iv d v d w v v

1 11 1 1

. . ( , )k k k

i ii ii i i

v d v d w v v

11

0 ( , ) Thus it returns False~k

i iiw v v

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

p13.

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. for each vertex

5. Relax(u,v,w).

][uAdjv

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

p14.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

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

p15.

Thm 5 :

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

Proof :

. ( , )v d s v

v : not reachable from s : then(1)

By a simple induction using lemma 15 (Path-relaxation property), shows at termination for i=0,1,…,k.

By lemma 17 (Predecessor-subgraph property), is a shortest-paths tree.

GVv and ,

. ( , )v d s v (2)v : reachable from s :

S=v0

v1

vk-1

vk=v

sortedally topologic:),( ii vv 1

. ( , )i iv d s vG

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

p16.

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.Q GV

do while Q{

}{uSS ][ex each vertfor uadjv

Re ( )lax u,v,w}

}

Page 17: Single-Source 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

u10

x5

v

y

10

5

2 3

1

9

2

74 6

0s

u8

x5

v14

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 18: Single-Source Shortest- paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=

Theorem 6 : (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 :

. ( , )u d s u

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

. ( , )u d s uBy 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.

, . ( , ) 0. u s s d s s S

. ( , )y d s y

, . ( , )u V u d s u

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 . ( , )s u u d s u 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

. ( , )x d s x when x was inserted into S.

When u is inserted into S, (x,y) had been relaxed, thus . ( , )y d s y. ( , ) ( , ) . .y d s y s u u d y d

.. .u d y d. ( , ) ( ., ) .y d s y s u u d

Contradict the assumption that . ( , ).u d s u

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

p19.

Analysis : Priority queue :

Time :

Linear array

Binary heap

Fibonacci heap

2( )O V

(( ) lg ) ( lg )O V E V O E V

( lg )O V V E

Corollary 7:

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, the predecessor subgraph is a shortest-paths tree rooted at s.

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

p20.

Difference constraints and shortest paths :

Linear programming problem:

Given an m x n matrix A, an m-vector b and an n-vector c, we wish to find a vector x of n elements that maximizes the object function

∑ ci xi subject to the m constraints given by Ax b.

Systems of difference constraints:

Each row of A has one 1 and one –1, and all other entries of A are 0.

1

2

3

4

5

1 1 0 0 0 0

1 0 0 0 1 1 .

0 1 0 0 1 1

x

x

x

x

x

1 2

51

52

0,

1,

1.

x x

x x

x x

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

p21.

Lemma 8: Let x = (x1, x2 ,… ,xn ) be a solution to a

system Ax ≤ b of difference constraints, and let d be any

constant. Then x+ d = (x1+d, x2 +d,…. ,xn +d) is a solution to

Ax ≤ b as well. Why?

Constraint graphs: Given an m x n matrix A, and a system Ax b of difference

constraints, the corresponding constraint graph is a weighted, directed graph G=(V, E), where V={v0, v1 ,… ,vn } and

E={(vi, vj ) : xj - xi ≤ bk is a constraint}

{(v0, vi ): i= 1 … n}.

If xj - xi ≤ bk is a constraint, then the weight of (vi, vj ) is bk.

The weight of each edge leaving v0 is 0.

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

p22.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

1

2

3

4

5

1 1 0 0 0 01 0 0 1

1 0 0 151 1 0 041 1 0

10 0 1 030 0 030 0 0 1 1

xxxxx

0 - 10 - 1

0

0 0 1 - 1 1

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

Theorem 9 Given a system Ax ≤ b of difference constraints, let G=(V,E) be the

corresponding constraint graph. (1) If G contains no negative-weight cycle, then x = ((v0, v1),

(v0, v2), … , (v0, vn)) is a feasible solution for the system.

(2) If G contains a negative-weight cycle, then there is no feasible solution for the system.

Pf: (1) For any (vi, vj) in E, (v0, vj) ≤ (v0, vi) + w(vi, vj), which is

(v0, vj) - (v0, vi) ≤ w(vi, vj). Let xi= (v0, vi) and xj= (v0, vj).

(2) Suppose there is a negative cycle c=< v1, …, vk= v1>.

x2- x1 ≤ w(v1, v2), x3- x2 ≤ w(v2, v3), …, xk- x k-1 ≤ w(v k-1, vk).

Sum them up, we have 0 ≤ w(c) < 0. A contradiction!

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

Proofs of shortest-paths properties:

Lemma 10 (Triangle inequality)G(V,E) : weighted, directed graph, s: source vertex.Then, for all edges ,we have Evu ),( ).,(),(),( vuwusvs

Proof :

S u

v

Shortest. ),( vs

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

Lemma 13 :

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

Lemma 11 (Upper-bound property)Given 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 v.d achieves its lower

bound Proof :

. . ( , ).v d u d w u v

. ( , )v d s v ,Vv

changes.never it ),,( vs

(1)

(2)

Clearly,

. ( , ) .u d w u v v d

after, initialization.

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

Therefore, we have

),( vs),(),( vuwus

is not the f. ( irst ver, tex )u d u vs .with .(s,v)v d

. ( , .)v d s v

(1) . ( , )v d s v

(2) Suppose . ( , )v d s vThen v.d cannot decrease, since it reaches the lower bound.And it cannot be increased, because relaxation steps do not increase d values.

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

Cor 12 (No-path property)

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.

Lemma 14 (Convergence property)Let 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 :

.v V

.( , ) . ; So . ( , )s v v d v d s v

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

),(),( vuwus

.. ( , )v d s v

. . ( , ) v d u d w u v

. ( , )v d s v

vus p ., Vvu

. ( , )u d s u . ( , )v d s vProof :

. ( , )u d s uAfter relaxing edge(u,v), we have

),( vs

Thus this equality is maintained thereafter.

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

Lemma 15 (Path-relaxation property)

Let be any shortest path in G from s = v0 to vk.

Suppose that G is initialized by Initialize-Single-Source(G,s) and then a sequence of

Relaxation steps that includes, in order, the edges (v0, v1),…., (v k-1, vk), then

vk.d = (s, vk) after these relaxations and at all times afterward.

Pf: By induction, we show that after the i-th edge of path p is relaxed, we have vi.d = (s, vi).

For the basis i=0: s.d=0= (s, s), which never changes after initialization.

For the induction step: Assume vi-1.d = (s, vi-1) and exam Relax(vi-1 , vi).

By the convergence property, after this relaxation, we have vi.d = (s, vi).

This equality is maintained at all times thereafter.

0 1, ,..., kp v v v

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

Relaxation and shortest-paths trees:

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

predecessor subgraph forms 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. for i 1...k.i iv v

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 vi-1.d was by Relax(vi, vi-1,w) and if changed1.i iv v

since then, it decreased. Therefore, just before the call Relax (vk-1,vk,w), we have

1 1. . ( , ), for 1,2,..., 1.i ii iv d v d w v v i k (*)

Vk=v0

Vk-1

S

Page 29: Single-Source 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

1 1. . ( , ).k k k kv d v d w v v (**)(*)+(**)

1 11 1

. ( . ( , ))k k

i ii ii iv d v d w v v

1 11 1

. ( , )k k

ii ii iv d w v v

1 11 1 1

0 ( , ), ( . . )k k k

i ii ii i iw v v v d v d

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 .GBy 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

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

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

Lemma 17 (Predecessor-subgraph property)

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, v.d is finite iff v is reachable from s.

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

. ( , ) for all .v d s v v V

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

(1)

.Vs

G

.G: from reachable : sV

),( vs

But, for , v.d is finite iff}{sVv . .v NIL Thus, are exactly those vertices reachable from s.V(2) Gforms a rooted tree with root s :

By lemma 16.S

vi-1

vivi-1.d

vi.d

(3)For all , the unique simple path is a shortest path from s to v in G. Vv Gvs p in Let 10 1 1, ,..., . For 1... , , and . ( , ) . . ( , ) i i i ii ik v d s v v dp v s vv v v i k d w v v

).,(),(),( 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.