Shortest Paths in A Graph (Cont'd) : Fundamental Algorithms

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 22

Shortest Paths

in a Graph (cont’d)
Fundamental Algorithms

8a-ShortestPathsMore 1
All-Pairs Shortest Paths
We now want to compute a table giving the
length of the shortest path between any two
vertices. (We also would like to get the shortest
paths themselves.)
We could just call Dijkstra or Bellman-Ford |V|
times, passing a different source vertex each
time.
It can be done in (V3), which seems to be as
good as you can do on dense graphs.

8a-ShortestPathsMore 2
Doing APSP with SSSP
Dijkstra would take time
(V  V2) = (V3) (standard version)
(V  (VlgV + E)) = (V2lgV + VE))
(modified, Fibonacci heaps),
but doesn’t work with negative-weight edges.
Bellman-Ford would take (V  VE) = (V2E).

8a-ShortestPathsMore 3
The Floyd-Warshall Algorithm
Represent the directed, edge-weighted graph in
adjacency-matrix form.
 w11 w12 w13 
w w w 
W= matrix of weights =  21 22 23 
 w31 w32 w33 
• wij is the weight of edge (i, j), or  if there is no
such edge.
• Return a matrix D, where each entry dij is (i,j).
• Could also return a predecessor matrix, P, where
each entry ij is the predecessor of j on the
shortest path from8a-ShortestPathsMore
i. 4
Floyd-Warshall: Idea
Consider intermediate vertices of a path:

i j

Say we know the length of the shortest path


from i to j whose intermediate vertices are only
those with numbers 1, 2, ..., k-1. Call this
length d ij( k 1)
Now how can we extend this from k-1 to k? In
(k )
other words, we want to compute d ij . Can we
( k 1)
use d ij , and if so how?
8a-ShortestPathsMore 5
Floyd-Warshall Idea, 2
k

i j

Two possibilities:
1. Going through the vertex k doesn’t help— the
path through vertices 1...k-1 is still the shortest.
2. There is a shorter path consisting of two
subpaths, one from i to k and one from k to j.
Each subpath passes only through vertices
numbered 1 to k-1.8a-ShortestPathsMore 6
Floyd-Warshall Idea,3
Thus,

d (ijk )  min( d (ijk 1) , d (ikk 1)  d (kjk 1))


Also, d (ij0 )  wij
(since there are no intermediate vertices.)
When k = |V|, we’re done.

8a-ShortestPathsMore 7
Dynamic Programming
Floyd-Warshall is a dynamic programming
algorithm:
Compute and store solutions to sub-
problems. Combine those solutions to
solve larger sub-problems.
Here, the sub-problems involve finding the
shortest paths through a subset of the vertices.

8a-ShortestPathsMore 8
Code for Floyd-Warshall
Floyd-Warshall(W)
1 n  rows[W ] // number of vertices
2 D(0)  W
3 for k  1 to n
4 do for i  1 to n
5 do for j  1 to n
(k ) ( k 1) ( k 1) ( k 1)
6 d ij d
 min( ij
, d ik
 d kj
)

7 return D ( n )

Running time : a zippy  (V 3 ). (Small constant of


proportionality, because operations are simple.)
8a-ShortestPathsMore 9
Example of Floyd-Warshall

8a-ShortestPathsMore 10
Johnson’s Algorithm
Makes clever use of Bellman-Ford and Dijkstra
to do All-Pairs-Shortest-Paths efficiently on
sparse graphs.
Motivation: By running Dijkstra |V| times, we
could do APSP in time (V2lgV +VElgV)
(Modified Dijkstra), or (V2lgV +VE) (Fibonacci
Dijkstra). This beats (V3) (Floyd-Warshall)
when the graph is sparse.
Problem: negative edge weights.

8a-ShortestPathsMore 11
The Basic Idea
Reweight the edges so that:
1. No edge weight is negative.
2. Shortest paths are preserved. (A
shortest path in the original graph is still
one in the new, reweighted graph.)
An obvious attempt: subtract the minimum
weight from all the edge weights. E.g. if the
minimum weight is -2:
-2 - -2 = 0
3 - -2 = 5
etc.
8a-ShortestPathsMore 12
Counterexample
Subtracting the minimum weight from every
weight doesn’t work.
Consider:

-2 0

-2 -1 0 1

Paths with more edges are unfairly penalized.

8a-ShortestPathsMore 13
Johnson’s Insight
Add a vertex s to the original graph G, with
edges of weight 0 to each vertex in G:
0
0
s
0

Assign new weights ŵ to each edge as follows:


ŵ(u, v) = w(u, v) + (s, u) - (s, v)

8a-ShortestPathsMore 14
Question 1
Are all the ŵ’s non-negative? Yes:

Otherwise, s  u  v would be
shorter than the shortest path
u )
 ( s, u )  w(u , v) must be   ( s, v) (s, u
from s to v.

s w(u, v)
(s, v
v)

 ( s, u )  w(u , v)   ( s, v)
Rewriting :
w(u , v)   ( s, u )   ( s, v)  0

wˆ (u , v)

8a-ShortestPathsMore 15
Question 2
Does the reweighting preserve shortest paths? Yes: Consider any
path p  v1 , v2 , , vk
k 1
wˆ ( p )   wˆ (v , v
i 1
i i 1 )

 w(v1 , v2 )   ( s, v1 )   ( s, v2 )
 w( v2 , v3)   ( s , v2 )   ( s , v 3 )
the sum 
telescopes
 w(vk 1 , vk )   ( s, vk 1 )   ( s, vk )
 w( p )   ( s, v1 )   ( s, vk )
A value that depends only on
the endpoints, not on the path.
In other words, we have adjusted the lengths of all paths by the same
amount. So this will not affect the relative ordering of the paths—
shortest paths will be preserved.
8a-ShortestPathsMore 16
Question 3
How do we compute the (s, v)’s?
Use Bellman-Ford.
This also tells us if we have a negative-weight
cycle.

8a-ShortestPathsMore 17
Johnson’s: Algorithm
1. Compute G’, which consists of G augmented
with s and a zero-weight edge from s to
every vertex in G.
2. Run Bellman-Ford(G’, w, s) to obtain the
(s,v)’s
3. Reweight by computing ŵ for each edge
4. Run Dijkstra on each vertex to compute ̂
5. Undo reweighting factors to compute 

8a-ShortestPathsMore 18
Johnson’s: CLRS

8a-ShortestPathsMore 19
Johnson: reweighting

ŵ(u, v) = w(u, v) + d(s, u) - d(s, v)

8a-ShortestPathsMore 20
Johnson using Dijkstra

8a-ShortestPathsMore 21
Johnson’s: Running Time
1. Computing G’: (V)
2. Bellman-Ford: (VE)
3. Reweighting: (E)
4. Running (Modified) Dijkstra: (V2lgV +VElgV)
5. Adjusting distances: (V2)
—————————————————————
Total is dominated by Dijkstra: (V2lgV +VElgV)

8a-ShortestPathsMore 22

You might also like