Title: Chapter 26 All-Pairs Shortest Paths
1Chapter 26 All-Pairs Shortest Paths
- Problem definition
- Shortest paths and matrix multiplication
- The Floyd-Warshall algorithm
2Problem definition
- Real problemWhen to make a table of distances
between all pairs of cities for a road atlas,how
can we compute the distances? - Model the problem
- Given a weighted,directed graph G(V,E) with a
weight function wE R that maps edges to
real-valued weights. - For every pair of vertices u,v V, find a
shortest(least weight) path from u to v, where
the weight of a path is the sum of the weights of
its constituent edges.
3Solve the problem by Single-Source shortest paths
algorithm
- If all edge weights are nonnegative, we can use
Dijkstras algorithm. - If negative-weight edges are allowed, then we use
Bellman-Ford algorithm instead. - Can you analysis the complexity of the two
solutions ? Is it possible for us to find a more
efficient algorithm?
4Preliminary knowledge
- In this chapter, we use an adjacency-matrix
representation - wij
if ij - the weight of directed edge (i,j) if
i j and (i,j) E -
if i j and (i,j) E - The output of the algorithm presented in this
chapter is an nn matrix D(dij),where entry dij
contains the weight of a shortest path from
vertex i to vertex j. That is , if we let denotes
the shortest-path weight from vertex i to vertex
j, then dij at termination.
5Continue
- To solve the all-pairs shortest-paths problem on
an input adjacency matrix, we need to compute not
only the shortest path weights but also a
predecessor matrix ( ij), where ij is
NIL if either ij or there is no path from i to
j, and otherwise ij is some predecessor of j
on a shortest path from i. - The subgraph induced by the ith row of the
matrix should be a shortest-paths tree with root
i. For each vertex i V,we define the predecessor
subgraph of G for i as G ,i (V ,i,E ,i) - V ,ij V i,j NIL i
- E ,i( ij , j) j V ,i and ij NIL
6Shortest paths and matrix multiplication
- This section presents a dynamic-programming
algorithm for the all-pairs shortest-paths
problem on a directed graph G(V,E). Each major
loop of the dynamic program will invoke an
operation that is very similar to matrix
multiplication, so that the algorithm will look
like repeated matrix multiplication. We shall
start by developing a (V4)-time algorithm for
the all-pairs shortest-paths problem and the
improve its running time to (V3lgV).
7Review the steps to develop a dynamic-programming
algorithm
- Characterize the structure of an optimal
solution. - Recursively define the value of an optimal
solution. - Computing the value of an optimal solution in a
bottom-up fashion. - Constructing an optimal solution from computed
information.
8The structure of a shortest path
- Consider a shortest path p from vertex i to
vertex j, and suppose that p contains at most m
edges.Assuming that there are no negative-weight
cycles,m is finite.If ij,then p has weight 0 and
no edges.If vertices i and j are distinct, then
we decompose path p into i k
j,where path p now contains at most m-1
edges.Moreover, p is a shortest path from i to
k. Thus, we have wkj.
9A recursive solution to the all-pairs
shortest-paths problem
- Now,let dij(m) be the minimum weight of any path
from vertex i to vertex j that contains at most m
edges.When m0,there is a shortest path from i to
j with no edges if and only if ij.For mgt1,we
compute dij(m) as the minimum of dij(m-1) (the
weight of the shortest path from i to j
consisting of at most m-1 edges) and the minimum
weight of any path from i to j consisting of at
most m edges,obtained by looking at all possible
predecessors k of j.Thus,we recursively define - dij(m)min ( dij(m-1), dik(m-1)wkj)
dik(m-1)wkj.
10Continue
- If the graph contains no negative-weight
cycles,then all shortest paths are simple and
thus contain at most n-1 edges.A path from vertex
i to vertex j with more than n-1 edges cannot
have less than a shortest path from i to j.The
actual shortest-path weights are therefore given
by - dij(n-1)dij(n)dij(n1)...
11Computing the shortest-path weights bottom up
- We compute the shortest-path weights by extending
shortest paths edge by edge.Letting AB denote
the matrix product returned by
EXTEND-SHORTEST-PATH(A,B) .We compute the
sequence of n-1 matrices - D(1)D(0)WW,
- D(2)D(1)WW2,
-
- D(n-1)D(n-2)WWn-1.
- As we argued above, the matrix D(n-1)Wn-1
contains the shortest-path weights.So we get the
algorithm.
12Continue
- SLOW-ALL-PAIRS-SHORTEST-PATH(W)
- n rowsW
- D(1) W
- for m 2 to n-1
- do D(m) EXTEND-SHORTEST-PATHS(D
(m-1),W) - return D(n-1)
13Continue
- EXTEND-SHORTEST-PATH(D,W)
- n rowsD
- let D(dij) be a nn matrix
- for i 1 to n
- do for j 1 to n
- do dij
- for k 1 to n
- do dij
min(dij,dikwkj) - return D
14Example
2
4
3
1
3
8
1
-5
-4
2
7
5
4
6
15(No Transcript)
16Improving the running time
- Our goal,however, is not to compute all the D(m)
matrices we are interested in matrix
D(n-1).Recall that in the absence of
negative-weight cycles, D(m)D(n-1),for all
integers mgtn-1.So, we can compute D(n-1) with
only matrix products by
computing the sequence - D(1)W,
- D(2)W2WW
- D(4) W2 W2
-
- D(2 )W2 W2
-1 W2 -1
17Continue
- FASTER-ALL-PAIRS-SHORTEST-PATHS(W)
- n rowsW
- D(1) W
- while n-1gtm
- do D(2m) EXTEND-SHORTEST-PATHS(D(m
),D(m)) - m 2m
- return D(m)
18Floyd-Warshall algorithm
- In this section, we shall use a different
dynamic-programming formulation to solve the
all-pairs shortest-paths problem on a directed
graph G(V,E).The resulting algorithm, known as
the Floyd-Warshall algorithm, runs in (V3)
time. As before, negative-weight edges may be
present, but we shall assume that there are no
negative-weight cycles.
19The structure of a shortest path
- In the Floyd-Warshall algorithm, we use a
different characterization of the structure of a
shortest path than we used in the
matrix-multiplication-based all-pairs
algorithms.The algorithm considers the
intermediate vertices of a shortest path, where
an intermediate vertex of a simple path
pltv1,v2,,vlgt is any vertex of p other than v1
or vl, that is, any vertex in the set
v2,v3,,vl-1
20Continue
- Let the vertices of G be V1,2,,n, and
consider a subset 1,2,,k of vertices for some
k.For any pair of vertices i,j V,consider
all paths from i to j whose intermediate vertices
are all drawn from 1,2,,k,and let p be a
minimum-weight path from among them.The
Floyd-Warshall algorithm exploits a relationship
between path p and shortest paths from i to j
with all intermediate vertices in the set
1,2,,k-1.
21Continue
- The relationship depends on whether or not k is
an intermediate vertex of path p. - If k is not an intermediate vertex of path p,
then all intermediate vertices of path p are in
the set 1,2,,k-1. Thus, a shortest path from
vertex i to vertex j with all intermediate
vertices in the set 1,2,,k-1 also a shortest
path from i to j with all intermediate vertices
in the set 1,2,,k. - If k is an intermediate vertex of path p,then we
break p down into i k
j as shown Figure 2.p1 is a shortest path from i
to k with all intermediate vertices in the set
1,2,,k-1, so as p2.
22All intermediate vertices in 1,2,,k-1
p2
k
p1
j
i
Pall intermediate vertices in 1,2,,k
Figure2 Path p is a shortest path from vertex i
to vertex j,and k is the highest-numbered
intermediate vertex of p. Path p1, the portion
of path p from vertex i to vertex k,has all
intermediate vertices in the set 1,2,,k-1.The
same holds for path p2 from vertex k to vertex j.
23A recursive solution to the all-pairs shortest
paths problem
- Let dij(k) be the weight of a shortest path from
vertex i to vertex j with all intermediate
vertices in the set 1,2,,k. A recursive
definition is given by - dij(k) wij
if k0, - min(dij(k-1),dik(k-1)dkj(k-1))
if k 1. - The matrix D(n)(dij(n)) gives the final
answer-dij(n) for all i,j
V-because all intermediate vertices are in the
set 1,2,,n.
24 Computing the shortest-path weights bottom up
- FLOYD-WARSHALL(W)
- n rowsW
- D(0) W
- for k 1 to n
- do for i 1 to n
- do for j 1 to n
- dij(k)
min(dij(k-1),dik(k-1)dkj(k-1)) - return D(n)
25Example
2
4
3
1
3
8
1
-5
-4
2
7
5
4
6
26(No Transcript)
27D(2)
(2)
(3)
D(3)
28D(4)
(4)
(5)
D(5)
29The End