You are on page 1of 22

1

8a-ShortestPathsMore
Shortest Paths
in a Graph (contd)
Fundamental Algorithms

2
8a-ShortestPathsMore
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 O(V
3
), which seems to be as
good as you can do on dense graphs.

3
8a-ShortestPathsMore
Doing APSP with SSSP
Dijkstra would take time
O(V V
2
) = O(V
3
) (standard version)
O(V (VlgV + E)) = O(V
2
lgV + VE))
(modified, Fibonacci heaps),
but doesnt work with negative-weight edges.
Bellman-Ford would take O(V VE) = O(V
2
E).
4
8a-ShortestPathsMore
The Floyd-Warshall Algorithm
Represent the directed, edge-weighted graph in
adjacency-matrix form.

W= matrix of weights =
w w w
w w w
w w w
11 12 13
21 22 23
31 32 33


(
(
(
w
ij
is the weight of edge (i, j), or if there is no
such edge.
Return a matrix D, where each entry d
ij
is o(i,j).
Could also return a predecessor matrix, P, where
each entry t
ij
is the predecessor of j on the
shortest path from i.
5
8a-ShortestPathsMore
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
Now how can we extend this from k-1 to k? In
other words, we want to compute . Can we
use , and if so how?
d
ij
k ( ) 1
d
ij
k ( )
d
ij
k ( ) 1
6
8a-ShortestPathsMore
Floyd-Warshall Idea, 2
Two possibilities:
1. Going through the vertex k doesnt 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.
j
k
i
7
8a-ShortestPathsMore
Floyd-Warshall Idea,3
Thus,


(since there are no intermediate vertices.)
When k = |V|, were done.


ij
k
ij
k
ik
k
kj
k
ij ij
d d d d
d
w
( ) ( ) ( ) ( )
( )
min( , ) = +
=
1 1 1
0
Also,
8
8a-ShortestPathsMore
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.
9
8a-ShortestPathsMore
Code for Floyd-Warshall
Floyd-Warshall(W)

simple.) are operations because ality, proportion
of constant (Small (V zippy a : time Running
return

to for do 5
to 1 i for do
to for 3

vertices of number //
3
) (
) (
) (
).
7
) , min( 6
1
4
1
2
] [ 1
) 1 ( ) 1 ( ) 1 (
0
u
n
k
kj
k
ik
k
ij
k
ij
D
n j
n
n k
W D
W rows n
d d d d

+

10
8a-ShortestPathsMore
Example of Floyd-Warshall
11
8a-ShortestPathsMore
Johnsons 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 O(V
2
lgV +VElgV)
(Modified Dijkstra), or O(V
2
lgV +VE) (Fibonacci
Dijkstra). This beats O(V
3
) (Floyd-Warshall)
when the graph is sparse.
Problem: negative edge weights.
12
8a-ShortestPathsMore
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.
13
8a-ShortestPathsMore
Counterexample
Subtracting the minimum weight from every
weight doesnt work.
Consider:
Paths with more edges are unfairly penalized.
-2 -1
-2
0 1
0
14
8a-ShortestPathsMore
Johnsons Insight
Add a vertex s to the original graph G, with
edges of weight 0 to each vertex in G:
Assign new weights to each edge as follows:
(u, v) = w(u, v) + o(s, u) - o(s, v)
s
0
0
0
15
8a-ShortestPathsMore
Question 1
Are all the s non-negative? Yes:

Otherwise, s u v would be
shorter than the shortest path
from s to v.





s
u
v
w(u, v)
0 ) , ( ) , ( ) , (
) , ( ) , ( ) , (
> +
> +
v s u s v u w
v s v u w u s
o o
o o
: Rewriting
) , (

v u w
) , ( ) , ( ) , ( v s v u w u s o o > + be must
16
8a-ShortestPathsMore
Question 2
Does the reweighting preserve shortest paths? Yes: Consider any
path

k
v v v p , , ,
2 1
=
) , ( ) , ( ) (
) , ( ) , ( ) , (
) , ( ) , ( ) , (
) , ( ) , ( ) , (
) , ( ) (
1
1 1
3 2 3 2
2 1 2 1
1
1
1
k
k k k k
k
i
i i
v s v s p w
v s v s v v w
v s v s v v w
v s v s v v w
v v w p w
o o
o o
o o
o o
+ =
+ +
+ +
+ =
=

=
+

the sum
telescopes
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.
17
8a-ShortestPathsMore
Question 3
How do we compute the o(s, v)s?
Use Bellman-Ford.
This also tells us if we have a negative-weight
cycle.
18
8a-ShortestPathsMore
Johnsons: 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
o(s,v)s
3. Reweight by computing for each edge
4. Run Dijkstra on each vertex to compute
5. Undo reweighting factors to compute o
o

19
8a-ShortestPathsMore
Johnsons: CLRS
20
8a-ShortestPathsMore
Johnson: reweighting
(u, v) = w(u, v) + d(s, u) - d(s, v)
21
8a-ShortestPathsMore
Johnson using Dijkstra
22
8a-ShortestPathsMore
Johnsons: Running Time
1. Computing G: O(V)
2. Bellman-Ford: O(VE)
3. Reweighting: O(E)
4. Running (Modified) Dijkstra: O(V
2
lgV +VElgV)
5. Adjusting distances: O(V
2
)

Total is dominated by Dijkstra: O(V
2
lgV +VElgV)

You might also like