+ All Categories
Home > Documents > 25.All-Pairs Shortest Paths Hsu, Lih-Hsing. Computer Theory Lab. Chapter 25P.2.

25.All-Pairs Shortest Paths Hsu, Lih-Hsing. Computer Theory Lab. Chapter 25P.2.

Date post: 20-Dec-2015
Category:
View: 280 times
Download: 0 times
Share this document with a friend
Popular Tags:
41
25.All-Pairs Shortest Paths Hsu, Lih-Hsing
Transcript

25.All-Pairs Shortest Paths

Hsu, Lih-Hsing

Chapter 25 P.2

Computer Theory Lab.

G V E( , ) with w E R:

Suppose that w e( )0 for all e E .

Using Dijkstra’s algorithm

O v O v

O v v eO v v Ev

O v

( ) ( )

( log )( log )

( )

2 3

2

3

Suppose negative weight are allowed

Using Bellman-Ford algorithm

O VE O v E O v( ) ( ) ( ) 2 4

Can be improved.

Chapter 25 P.3

Computer Theory Lab.

Input form: matrix W

w

i=j

(i,j)i j (i,j) Eij

0 if the weight of the

directed edge if and

otherwise

Output:

D d d i jij ij ( ), ( , ) .

Predecessor matrix ( ) ij

ij

ij

NILi=j

i jj

i

if either or there is

no path from to is some predecessor of

on the shortest path from

.

.

Chapter 25 P.4

Computer Theory Lab.

F o r e a c h v e r t e x i V , w e d e f i n e d t h e p r e d e c e s s o r s u b g r a p h o f G f o r i

a s ),(,,, iii

EVG w h e r e }}{|),{(

}{}|{

,,

,

iVjjE

iNILjV

iiji

iji

P R I N T - A L L - S H O R T E S T - P A T H ( , ,i j )

1 i f i j

2 t h e n p r i n t i

3 e l s e i f i j N I L

4 t h e n p r i n t “ n o p a t h f r o m ” i t o j “ e x i s t s ”

5 e l s e P R I N T - A L L - S H O R T E S T - P A T H ( , , )i i j

6 P r i n t j

Chapter 25 P.5

Computer Theory Lab.25.1 Shortest paths and matrix

multiplication

A dynamic programming approach:

1. characterize the structure of the optimal solution,

2. recursively define the value of optimal solution,

3. compute the value of an optimal solution in a bottom-up fashion.

The structure of a optimal solution:

Consider a shortest path p from vertex i to vertex j, and suppose that p

contains at most m edges. Assume that there are no negative-weight

cycles. Hence m is finite.

Chapter 25 P.6

Computer Theory Lab.

If i = j, then p has weight 0 and no edge.

If i j , we decompose p into ip

k j'

~ where p' contains at most

m-1 edges.

Moreover, p' is a shortest path from i to k and ( , ) ( , )i j i k wkj .

Chapter 25 P.7

Computer Theory Lab.

A recursive solution to the all-pairs shortest-path problem.

Define: )(m

ijl = minimum weight of any path from i to j that contains at

most m edges.

jiif

jiiflij

0)0(

Then

}{min

}}{min,min{

)1(1

)1(1

)1()(

kjm

iknk

kjm

iknkm

ijm

ij

wl

wlll

Chapter 25 P.8

Computer Theory Lab.

Since shortest path from i to j contains at most m-1 edges,

)1()()1(),( nij

nij

nij lllji

Computing the shortest-path weight bottom up:

Compute )1()2()1( ,,, nLLL where )( )()( m

ijm lL .

Note that WL )1(.

Chapter 25 P.9

Computer Theory Lab.

EXTENDED-SHORTEST-PATHS(L, w)

1 ][Lrown

2 Let )( ijlL be an n n matrix

3 for i1 to n

4 do for j1 to n

5 do ijl

6 for k1 to n

7 do ),min(

kjikijijwlll

8 return L

Chapter 25 P.10

Computer Theory Lab.

MATRIX-MULTIPLY(A,B)

1 n row A [ ]

2 Let C be an n n matrix

3 for i 1 to n

4 do for j 1 to n

5 do cij 0

6 for k 1 to n

7 do c c a bij ij ik kj

8 return C

Chapter 25 P.11

Computer Theory Lab.

min

)(

)1(

cl

bw

al

m

m

(Time complexity: O n( )3 .)

Chapter 25 P.12

Computer Theory Lab.

1)2()1(

3)2()3(

2)1()2(

)0()1(

nnn WWLL

WWLL

WWLL

WWLL

Chapter 25 P.13

Computer Theory Lab.

SLOW-ALL-PAIRS-SHORTEST-PATHS(W)

1 n row W [ ]

2 WL )1(

3 for m 2 to n-1

4 do )(mL EXTENDED-SHORTEST-PATHS( ),)1( WL m

5 return )1( nL

Time complexity: O n( )4 .

Chapter 25 P.14

Computer Theory Lab.

Improving the running time:

)1log()1log( 2)2(

224)4(

2)2(

)1(

nn

WL

WWWL

WWWL

WL

i.e., using repeating squaring!

Time complexity: O n n( log )3 .

Chapter 25 P.15

Computer Theory Lab.

2

1

5 4

3

3

6

-4

7

2

8

1

4

-5

06

052

04

710

4830

)1(L

0618

20512

11504

71403

42830

)2(L

Chapter 25 P.16

Computer Theory Lab.

06158

20512

115047

11403

42330

)3(L

06158

20512

35047

11403

42310

)4(L

Chapter 25 P.17

Computer Theory Lab.

FASTER-ALL-PAIRS-SHORTEST-PATHS(W)

1 n row W [ ]

2 WL )1(

3 m 1

4 while n m 1

5 do )2( mL EXTENDED-SHORTEST-PATHS( ), )()( mm LL

6 m m 2

7 return )(mL

Chapter 25 P.18

Computer Theory Lab.

25.2 The Floyd-Warshall algorithmA different dynamic programming formulation

The structure of a shortest path:

Let V(G)={1,2,…,n} and consider a subset {1,2,…,k} of vertices for some

k. For any pair of vertices i j V, , consider all paths from i to j whose

intermediate vertices are drawn from {1,2,…,k} and p be a minimum

weight path among them.

Chapter 25 P.19

Computer Theory Lab.

If k is not an intermediate vertex of p, then all intermediate vertices are in

{1,2,…,k-1}.

If k is an intermediate vertices of p, then p can be decomposed into

ip

kp

j1 2~ ~

where p1 is a shortest path from i to k with all the

intermediate vertex in {1,2,…,k-1} and p2 is a shortest path from k to j

with all the intermediate vertex in {1,2,…,k-1}.

Chapter 25 P.20

Computer Theory Lab.

A recursive solution to the all-pairs shortest path problem

Definition: dijk the weight of a shortest path from vertex i to vertex

j with all intermediate vertices in the set {1,2,…,k}.

1),min(

0)1()1()1(

)(

kifddd

kifwd k

kjk

ikk

ij

ijkij

D dnij

n( ) ( )( ) is the final solution!

Chapter 25 P.21

Computer Theory Lab.

Computing the Shortest-path weight bottom-up.

FLOYD-WARSHALL(W)

1 n row W [ ]

2 D W( )0

3 for k 1 to n

4 do for i 1 to n

5 do for j 1 to n

6 d d d dijk

ijk

ikk

kjk( ) ( ) ( ) ( )min( , ) 1 1 1

7 return D n( )

Complexity: O n( )3 .

Chapter 25 P.22

Computer Theory Lab.

C o n s t r u c t i n g a s h o r t e s t p a t h

( ) ( ) ( ), , . . . ,0 1 n

i jk( ) : i s t h e p r e d e c e s s o r o f t h e v e r t e x j o n a s h o r t e s t p a t h f r o m v e r t e x i

w i t h a l l i n t e r m e d i a t e v e r t i c e s i n t h e s e t { 1 , 2 , … , k } .

ij

ij

ij wjii

=wi=jNIL

and if

or if)0(

i j

k i jk

i jk

i kk

k jk

k jk

i jk

i kk

k jk

d d d

d d d

( )( ) ( ) ( ) ( )

( ) ( ) ( ) ( )

1 1 1 1

1 1 1 1

i f

i f

Chapter 25 P.23

Computer Theory Lab.

D( )0

0 3 8 4

0 1 7

4 0

2 5 0

6 0

( )0

1 1 1

2 2

3

4 4

5

N N

N N N

N N N N

N N N

N N N N

D( )1

0 3 8 4

0 1 7

4 0

2 5 5 0 2

6 0

( )1

1 1 1

2 2

3

4 1 4 1

5

N N

N N N

N N N N

N

N N N N

Chapter 25 P.24

Computer Theory Lab.

D( )2

0 3 8 4 4

0 1 7

4 0 5 11

2 5 5 0 2

6 0

( )2

1 1 2 1

2 2

3 2 2

4 1 4 1

5

N

N N N

N N

N

N N N N

D( )3

0 3 8 4 4

0 1 7

4 0 5 11

2 1 5 0 2

6 0

( )3

1 1 2 1

2 2

3 2 2

4 3 4 1

5

N

N N N

N N

N

N N N N

Chapter 25 P.25

Computer Theory Lab.

D( )4

0 3 1 4 4

3 0 4 1 1

7 4 0 5 3

2 1 5 0 2

8 5 1 6 0

( )4

1 4 2 1

4 4 2 1

4 3 2 1

4 3 4 1

4 3 4 5

N

N

N

N

N

D( )5

0 1 3 2 4

3 0 4 1 1

7 4 0 5 3

2 1 5 0 2

8 5 1 6 0

( )5

1 4 5 1

4 4 2 1

4 3 2 1

4 3 4 1

4 3 4 5

N

N

N

N

N

Chapter 25 P.26

Computer Theory Lab.

Transitive closure of a directed graph: Given a directed graph G = (V, E)

with V = {1, 2,…, n}. The transitive closure of G is G V E* ( . *)

where E i j* {( , )| there is a path from i to j in G}.

Modify FLOYD-WARSHALL algorithm:

ti j (i,j) E

i j (i,j) Eij( )0 0

1

if and

if or

for k 1,

t t t tijk

ijk

ikk

kjk( ) ( ) ( ) ( )( ) 1 1 1

Chapter 25 P.27

Computer Theory Lab.

T R A N S I T I V E - C L O S U R E ( G )

1 n G | |

2 f o r i 1 t o n

3 d o f o r 1j t o n

4 d o i f i j o r ( , ) ( )i j E G

5 t h e n t i j( )0 1

6 e l s e t i j( )0 0

7 f o r k 1 t o n

8 d o f o r i 1 t o n

9 d o f o r j 1 t o n

1 0 d o t t t ti jk

i jk

i kk

k jk( ) ( ) ( ) ( )( ) 1 1 1

1 1 r e t u r n T n( )

T i m e c o m p l e x i t y : O n( )3 .

Chapter 25 P.28

Computer Theory Lab.

1

4 3

2T( )0

1 0 0 0

0 1 1 1

0 1 1 0

1 0 1 1

T ( )1

1 0 0 0

0 1 1 1

0 1 1 0

1 0 1 1

T ( )2

1 0 0 0

0 1 1 1

0 1 1 1

1 0 1 1

T ( )3

1 0 0 0

0 1 1 1

0 1 1 1

1 1 1 1

T ( )4

1 0 0 0

1 1 1 1

1 1 1 1

1 1 1 1

Chapter 25 P.29

Computer Theory Lab.

25.3 Johnson’s algorithm for sparse graphs

t i m e c o m p l e x i t y O V V V E( l g )2 b y a s s u m i n g D i j k s t r a a l g o r i t h m t a k e s

O V V V E( l g )2 u s i n g F i b o n a c c i h e a p .

C o m b i n e w i t h B e l l m a n - F o r d a l g o r i t h m t a k e s O V E( ) .

u s i n g r e w e i g h t i n g t e c h n i q u e

I f a l l e d g e w e i g h t s i n G a r e n o n n e g a t i v e , w e c a n f i n d a l l s h o r t e s t p a t h s i n

O V V V E( l g )2 u s i n g D i j k s t r a a l g o r i t h m .

Chapter 25 P.30

Computer Theory Lab.

If G has negative-weighted edge, we compute a new set of nonnegative

weight that allows us to use the same method. The new set of edge weight

w satisfies:

1. For all pairs of vertices u v V, , a shortest path from u to v using

weight function w is also a shortest path from u to v using the weight

function w .

2. ( , ) ( ), ( , )u v E G w u v is nonnegative.

Chapter 25 P.31

Computer Theory Lab.

Preserving shortest paths by reweighting

Lemma 25.1 (Reweighting doesn’t change shortest paths)

Given a weighted directed graph G = (V, E) with weight function

w E R: , let h V R: be any function mapping vertices to real

numbers. For each edge ( , )u v E , define

( , ) ( , ) ( ) ( )w u v w u v h u h v .

Let P v v vk 0 1, ,..., be a path from vertex v0 to vk . Then

w P v vk( ) ( , ) 0 if and only if ( ) ( , )w P v vk 0 . Also, G has a

negative-weight cycle using weight function w iff G has a negative

weight cycle using weight function w .

Chapter 25 P.32

Computer Theory Lab.

Proof.

( ) ( ) ( ) ( )w P w P h v h vk 0

( ) ( , )

( ( , ) ( ) ( ))

( , ) ( ) ( )

( ) ( ) ( )

w P w v v

w v v h v h v

w v v h v h v

w P h v h v

i ii

k

i ii

ki i

i ii

kk

k

11

11

1

11

0

0

Chapter 25 P.33

Computer Theory Lab.

w P v v k( ) ( , ) 0 i m p l i e s ( ) ( , )w P v v k 0 .

S u p p o s e t h e r e i s a s h o r t e r p a t h P ' f r o m v 0 t o v k u s i n g t h e

w e i g h t f u n c t i o n w . T h e n )(ˆ)'(ˆ PwPw .

T h e n

).()'(

)()()()(ˆ

)'(ˆ)()()'(

0

0

PwPw

vhvhPwPw

PwvhvhPw

k

k

W e g e t a c o n t r a d i c t i o n !

Chapter 25 P.34

Computer Theory Lab.

G has a negative-weight cycle using w iff G has a negative-weight

cycle using w .

Consider any cycle C v v vk 0 1, ,..., with v vk0 . Then

( ) ( ) ( ) ( ) ( )w C w C h v h v w Ck 0 .

Producing nonnegative weight by reweight

Chapter 25 P.35

Computer Theory Lab.

2

3

45

1

-4

3

7

6

21

4

8

-5

Chapter 25 P.36

Computer Theory Lab.

0

-4 0

-5

2

3

45

1

-4

3

7

6

21

4-1

8

-5

0

0

0

0

0

0s

Chapter 25 P.37

Computer Theory Lab.

0

-4 0

-5

2

3

45

1

0

4

10

2

20

0-1

13

0

5

1

0

4

0

0s

( , ) ( , ) ( ) ( )w u v w u v h u h v

h v s v( ) ( , )

Chapter 25 P.38

Computer Theory Lab.

D e f i n e h v s v( ) ( , ) f o r a l l v V '

'),(,0)()(),(),(ˆ

'),(),,()()(

Evuvhuhvuwvuw

Evuvuwuhvh

Chapter 25 P.39

Computer Theory Lab.

J O H N S O N ( G )

1 C o m p u t i n g G ' , w h e r e }{}[]'[ sGVGV

a n d E G E G s v v V G[ ' ] [ ] { ( , ) | [ ] } .

2 i f B E L L M A N - F O R D ( G w s' , , ) = F A L S E

3 t h e n p r i n t “ t h e i n p u t g r a p h c o n t a i n s n e g a t i v e w e i g h t c y c l e ”

4 e l s e f o r e a c h v e r t e x v V G [ ' ]

5 d o s e t h ( v ) t o b e t h e v a l u e o f ( , )s v

6 f o r e a c h e d g e [ , ] [ ' ]u v E G

Chapter 25 P.40

Computer Theory Lab.

7 do ( , ) ( , ) ( ) ( )w u v w u v h u h v

8 for each vertex u V G [ ]

9 do run DIJKSTRA(G w u, , ) to compute ( , ) u v for all v V G [ ] .

10 for each vertex v V G [ ]

11 do d u v h v h uuv ( , ) ( ) ( )

12 return D

Complexity: O V V VE( lg )2 .

Chapter 25 P.41

Computer Theory Lab.

2/7

2/3 0/5

0/0

2

3

45

1

0

4

10

2

20

00/4

13

0

0/0

0/-4 2/2

2/-3

2

3

45

1

0

4

10

2

20

02/1

13

0

2/3

2/-1 0/1

0/-4

2

3

45

1

0

4

10

2

20

00/0

13

0

4/8

0/0 2/6

2/1

2

3

45

1

0

4

10

2

20

02/5

13

0

2/2

2/-2 0/0

0/-5

2

3

45

1

0

4

10

2

20

00/-1

13

0

( , ) / ( , ) u v u v


Recommended