Upload
roland-clarke
View
257
Download
0
Embed Size (px)
Citation preview
Lecture 7 All-Pairs Shortest Paths
All-Pairs Shortest Paths
nodes. of },{ pairs allfor to frompath
shortest find ),,(digraph aGiven
tsts
EVG
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
Adjacency Matrix
otherwise ,0
,),( if 1,
and },...,2,1{ where
)()(
Ejia
nV
aGA
ij
nnij
1
2 3
001
100
0111
2
3
1 2 3
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.
1
2 3
001
100
0111
2
3
1 2 3
k=1
True!
n
h hjkih
kij aaa
1
)1()()1(
Induction Step
jhi
choices )(kiha choices )1(
hja
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
)(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,
. to from
pathshortest oflength theis Therefore,
edges. 1most at containsit cycle, a
contain not doespath shortest each Since
)1(
ji
l
nnij
Key Observation
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
Speed Up dynamic Program
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
Weighted Adjacency Matrix
otherwise ,
, ),( if ,
and },...,2,1{ where
)()(
Ejicl
nV
lGL
ijij
nnij
1
2 3
05
60
401
2
3
1 2 3
4
6
5
n
h hjkih
kij
hjhjhjkih
nh
kij
lll
lllll
1
)1()()1(
)1()1()(
1
)1( )( )( min
min11
n
hnh
A New Multiplication
pmhjihnh
pnhjnmih baba }){ min()()(1
095
6011
1040
05
60
40
05
60
40
Associative Law
law. eassociativ thefollows operation Matrix
CBACBA )()(
.operation to
respect with )(about can talk weThus,
kGL
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.
1
2 3
05
60
401
2
3
1 2 3
4
6
5
k=1
True!
)(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
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
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
Lemma
cycle.weight -negative no hasnetwork The
em.between thpath shortest
simple a exists there, and nodes any twoFor vu
Theorem
cycle.weight -negativethout network wiany in
worksalgorithmPath Shortest Pairs-AllFaster
Theorem
.1 somefor )()( ifonly
and if cycleweight -negative a contains network A )()1( nmGLGL
Gmn
Floyd-Warshall Algorithm
Algorithm-)( 3nO
.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
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
)(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
Theorem
cycle.weight -negativewithout
network any in worksalgorithm Warshall-Floyd
What we learnt in this lecture?
• The relationship between shortest path and matrix multiplication.
• Faster-All-Pairs-Shortest-Paths algorithm
• Floyd-Warshall algorithm.
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
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
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