All Pairs Shortest Paths

AllAll-Pairs Shortest Paths
Problem.
Problem. Find the shortest path between all pairs of
vertices of a weighted graph (G=(V,E),w
(G=(V,E),w)).
All Paths
Shortest Path
Finder
Wn+1
Wn
Input
W = ( wij )


wij =  w ( i , j ) if
 ∞
if

0
Output
D = ( d ij )
Π = ( π ij )
i= j
if
i ≠ j and ( i , j ) ∈ E
i ≠ j and ( i , j ) ∉ E
AllAll-Pairs Shortest Paths
We could solve all pairs shortest path problem by
running singlesingle-source shortestshortest-paths algorithm |V|
times.
We can use Dijtra’
Dijtra’s alg if all wts nonnon-neg.using a
linear implementation
• With a priority queue O
Weight Matrix
i= j
i ≠ j and ( i , j ) ∈ E
i ≠ j and ( i , j ) ∉ E
Predecessor Matrix
 NIL if or ∃ i → j
π ij =  Predecessor of j on some

shortest path I Æj
3
+ VE ) = O (V 3 )
• With binarybinary-heap implementation of priority queue
O (VE lgV )
• With Fibonaci heaps O
(V
2
lgV + VE )
On the other hand, if neg edges, must use slower
2
4
BellmanBellman-Ford algorithm O V E = O V
In this case we can do better
2
(
AllAll-Pairs Shortest Paths
if
 0

wij =  w ( i , j ) if
 ∞
if

(V
)
(
)
O (V lgV + VE )
AllAll-Pairs Shortest Paths
Recall:
p = ( v0 , v1 ,… , vk )
k
w ( p ) = ∑ w ( v i −1 , v i )
i =1
min { w ( p )} if ∃p : u → v
δ ( u, v ) = 
 ∞ otherwise
Objective:
Objective: Find shortest path p from u to v s.t.
s.t.
w(p)=
δ(u,v)
w(p)=δ
(u,v)
Note: i-th row of Π gives shortest path tree rooted
at i (i-th predecessor graph)
AllAll-Pairs Shortest Paths
Compute an by noting that
Compute an
Dumb(a,n)
Dumb(a,n)
Prod Å1
loop k from 1 to n do
ProdÅ
ProdÅa*Prod
end loop
RETURN(Prod)
RETURN(Prod)
end
AllAll-Pairs Shortest Paths
 lg n 
O ( n)
∑ ni 2i  lg n 2i
=∏ a
a n = a i =0
i =0
( )
ni
Smart(a,n)
Smart(a,n)
Method of Repeated Squaring
ProdÅ
ProdÅ1
SqÅ
SqÅa
loop i from 0 to  lg n  do
if ni=1 then ProdÅ
ProdÅProd*Sq
Prod*Sq end if
SqÅ
SqÅSq*Sq
Sq*Sq
end loop
RETURN(Prod)
RETURN(Prod)
end
O ( lg n )
1
AllAll-Pairs Shortest Paths
(m)
ij
=
AllAll-Pairs Shortest Paths
Min wt of any path from vertex i to vertex j that
contains at most m edges.
Given W = (w
(wij), we compute L(1),L(2), … ,
(m) )
(n-1) , where L(m)
(m) = (l
L(n(lij(m)
AllAll-Pairs Shortest Paths
AllAll-Pairs Shortest Paths
AllAll-Pairs Shortest Paths
2
AllAll-Pairs Shortest Paths
AllAll-Pairs Shortest Paths
3