lecture 7 all-pairs shortest paths. all-pairs shortest paths

36
Lecture 7 All-Pairs Shortest Paths

Upload: roland-clarke

Post on 04-Jan-2016

236 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Lecture 7 All-Pairs Shortest Paths

Page 2: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

All-Pairs Shortest Paths

nodes. of },{ pairs allfor to frompath

shortest find ),,(digraph aGiven

tsts

EVG

Page 3: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Path Counting Problem

nodes. of },{pair each for to from edges

exactly with paths of #count ,integer

positive a and ),(digraph aGiven

tsts

kk

EVG

Page 4: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Adjacency Matrix

otherwise ,0

,),( if 1,

and },...,2,1{ where

)()(

Ejia

nV

aGA

ij

nnij

Page 5: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

1

2 3

001

100

0111

2

3

1 2 3

Page 6: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Theorem

. to from edges exactly with paths of

number theis element each ,)(In )(

jik

aGA kij

k

Proof. We prove it by induction on k.

Page 7: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

1

2 3

001

100

0111

2

3

1 2 3

k=1

True!

Page 8: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

n

h hjkih

kij aaa

1

)1()()1(

Induction Step

jhi

choices )(kiha choices )1(

hja

Page 9: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

All-Pairs Shortest Pathswith at most k edges

. to from edges most at path withshortest the

of weight thenodes, of },{pair every for compute,

,integer positive a and ),(digraph aGiven

tsk

ts

kEVG

Page 10: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

)(min

Then . to from edges most at

path withshortest theoflength thedenote Let

)1()(

1

)1(

)(

hjkih

nh

kij

kij

lll

jik

l

Recursive formula

jhi

)(kihl )1(

hjl

.for truealso is formula recursive The ji Note:

For i≠j,

Page 11: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

. to from

pathshortest oflength theis Therefore,

edges. 1most at containsit cycle, a

contain not doespath shortest each Since

)1(

ji

l

nnij

Key Observation

Page 12: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Dynamic Program

time.)(in runsit Hemce,

time.)(in computed is Each .11

and ,1 ,1for all compute

torequires This formula. recursive with the

algorithm program dynamic adesign may We

4

)(

)(

nO

nOlnk

njnilkij

kij

Page 13: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Speed Up dynamic Program

Page 14: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Idea

}{min

formula recursive

different a findingby realized becan idea This

.2for some

skippingby purpose reach thismay We

., allfor compute tois purposeOur

)()(

1

)2(

)(

)1(

mhj

mih

nh

mij

kij

nij

lll

nkl

jil

Page 15: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Weighted Adjacency Matrix

otherwise ,

, ),( if ,

and },...,2,1{ where

)()(

Ejicl

nV

lGL

ijij

nnij

Page 16: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

1

2 3

05

60

401

2

3

1 2 3

4

6

5

Page 17: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

n

h hjkih

kij

hjhjhjkih

nh

kij

lll

lllll

1

)1()()1(

)1()1()(

1

)1( )( )( min

min11

n

hnh

Page 18: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

A New Multiplication

pmhjihnh

pnhjnmih baba }){ min()()(1

095

6011

1040

05

60

40

05

60

40

Page 19: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Associative Law

law. eassociativ thefollows operation Matrix

CBACBA )()(

.operation to

respect with )(about can talk weThus,

kGL

Page 20: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Theorem

. to from edges most at path with

shortest theoflength theis element each ,)(In )(

jik

lGL kij

k

Proof. We prove it by induction on k.

Page 21: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

1

2 3

05

60

401

2

3

1 2 3

4

6

5

k=1

True!

Page 22: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

)(min

Then . to from edges most at

path withshortest theoflength thedenote Let

)1()(

1

)1(

)(

hjkih

nh

kij

kij

lll

jik

l

Induction Step

jhi

)(kihl )1(

hjl

Page 23: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

All-Pairs Shortest Paths

nodes. of },{ pairs allfor to frompath

shortest find ),,(digraph aGiven

tsts

EVG

Theorem

. to frompath

shortest theoflength theis element each ,)(In )1(1

ji

lGL nij

n

Proof edges. 1most at containspath Shortest Each n

Page 24: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

How to Compute ?)( )1( nAL

)(

)()()2(

)1(

return

;2

and do

1 while

);(

;1

|;|

m

mmm

L

mm

LLL

mn

GLL

m

Vn

)lg( Time 3 nnO

Page 25: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Lemma

cycle.weight -negative no hasnetwork The

em.between thpath shortest

simple a exists there, and nodes any twoFor vu

Page 26: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Theorem

cycle.weight -negativethout network wiany in

worksalgorithmPath Shortest Pairs-AllFaster

Page 27: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Theorem

.1 somefor )()( ifonly

and if cycleweight -negative a contains network A )()1( nmGLGL

Gmn

Page 28: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Floyd-Warshall Algorithm

Algorithm-)( 3nO

Page 29: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

.1 if ),min(

,0 if

Then }.,...,2,1{in nodes internal with and

to frompath shortest theoflength thedenote Let

)1()1()1()(

)(

kddd

kcd

k

jid

kkj

kik

kij

ijkij

kij

Observation

i

k

j2p1p

}.1,...,2,1{in nodes internal with paths twoare and 21 kpp

Page 30: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Dynamic Program

time.)(in runsit Hemce,

time.)1(in computed is Each .11

and ,1 ,1for all compute

torequires This formula. recursive with the

algorithm program dynamic adesign may We

3

)(

)(

nO

Odnk

njnidkij

kij

Page 31: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

)(return

);,min( do

to1for do

to1for do

to1for

);(

)];([

))((Warshall-Floyd

)()(

)1()1()1()(

)0(

nij

n

kkj

kik

kij

kij

dD

dddd

nj

ni

nk

GLD

GLrowsn

GL

)(nO

)(nO)(nO

)( 3nO

Page 32: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Theorem

cycle.weight -negativewithout

network any in worksalgorithm Warshall-Floyd

Page 33: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

What we learnt in this lecture?

• The relationship between shortest path and matrix multiplication.

• Faster-All-Pairs-Shortest-Paths algorithm

• Floyd-Warshall algorithm.

Page 34: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Puzzle 1

nodes. of },{pair each for to from edges

most at with paths of #count ,integer

positive a and ),(digraph aGiven

tsts

kk

EVG

Page 35: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Puzzle 2

nodes. of },{pair each for to from edges

most at with paths of #count ,integer

positive a and ),(graph aGiven

tsts

kk

EVG

Page 36: Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths

Puzzle 3

nodes. of },{pair each for to from edges

most at with paths of #count ,integer

positive a and loop, without ),(digraph aGiven

tsts

kk

EVG