Algo122 Assignment2 JohnsonsAlgorithm
Algo122 Assignment2 JohnsonsAlgorithm
25.2-6
How can we use the output of the Floyd-Warshall algorithm to detect the presence
of a negative-weight cycle?
25.2-7
Another way to reconstruct shortest paths in the Floyd-Warshall algorithm uses
values ij.k/ for i; j; k D 1; 2; : : : ; n, where ij.k/ is the highest-numbered intermediate vertex of a shortest path from i to j in which all intermediate vertices are
in the set f1; 2; : : : ; kg. Give a recursive formulation for ij.k/ , modify the F LOYD WARSHALL procedure to compute the ij.k/ values, and rewrite the P RINT-A LL
PAIRS -S HORTEST-PATH procedure to take the matrix D ij.n/ as an input.
How is the matrix like the s table in the matrix-chain multiplication problem of
Section 15.2?
25.2-8
Give an O.VE/-time algorithm for computing the transitive closure of a directed
graph G D .V; E/.
25.2-9
Suppose that we can compute the transitive closure of a directed acyclic graph in
f .jV j ; jEj/ time, where f is a monotonically increasing function of jV j and jEj.
Show that the time to compute the transitive closure G D .V; E / of a general
directed graph G D .V; E/ is then f .jV j ; jEj/ C O.V C E /.
701
that allows us to use the same method. The new set of edge weights w
y must satisfy
two important properties:
1. For all pairs of vertices u; 2 V , a path p is a shortest path from u to using
weight function w if and only if p is also a shortest path from u to using
weight function w.
y
2. For all edges .u; /, the new weight w.u;
y / is nonnegative.
As we shall see in a moment, we can preprocess G to determine the new weight
function w
y in O.VE/ time.
Preserving shortest paths by reweighting
The following lemma shows how easily we can reweight the edges to satisfy the
rst property above. We use to denote shortest-path weights derived from weight
function w and y to denote shortest-path weights derived from weight function w.
y
Lemma 25.1 (Reweighting does not change shortest paths)
Given a weighted, directed graph G D .V; E/ with weight function w W E ! R,
let h W V ! R be any function mapping vertices to real numbers. For each edge
.u; / 2 E, dene
w.u;
y / D w.u; / C h.u/ h./ :
(25.9)
w.p/
y
D w.p/ C h.0 / h.k / :
(25.10)
We have
w.p/
y
D
k
X
w.
y i 1 ; i /
i D1
k
X
i D1
k
X
i D1
702
0
3
0
4
0
5 3
1
6
3
2/3
13
10
10
2/2
2/1
13
2
0
2/2
5
3
0/5
2
(f)
3
0/4
0 4
2
(d)
4
1
2/7
13
2
0
10
0/1
4
2/3
5
3
0/0
0
0
2
(e)
0/5
4
4
1
4/8
13
2
0
10
2
2/5
2
0/1
1
2/2
13
2
0
(c)
10
5 3
2
0/4
0
1
2/3
13
2
0/0
0
0/4
(b)
2
2/1
4
0 4
(a)
1
0/0
4
1
0
0
5
2
1
1
4
1
0
2
2
1
703
10
0/0
0/0
3
2/1
0
0
2
(g)
2/6
4
Figure 25.6 Johnsons all-pairs shortest-paths algorithm run on the graph of Figure 25.1. Vertex numbers appear outside the vertices. (a) The graph G 0 with the original weight function w.
The new vertex s is black. Within each vertex is h./ D .s; /. (b) After reweighting each
edge .u; / with weight function w.u;
y / D w.u; / C h.u/ h./. (c)(g) The result of running
Dijkstras algorithm on each vertex of G using weight function w
y. In each part, the source vertex u
is black, and shaded edges are in the shortest-paths tree computed by the algorithm. Within each
y / and .u; /, separated by a slash. The value du D .u; / is equal to
vertex are the values .u;
y u; / C h./ h.u/.
.
704
J OHNSON .G; w/
1 compute G 0 , where G 0 :V D G:V [ fsg,
G 0 :E D G:E [ f.s; / W 2 G:Vg, and
w.s; / D 0 for all 2 G:V
2 if B ELLMAN -F ORD .G 0 ; w; s/ == FALSE
3
print the input graph contains a negative-weight cycle
4 else for each vertex 2 G 0 :V
5
set h./ to the value of .s; /
computed by the Bellman-Ford algorithm
6
for each edge .u; / 2 G 0 :E
7
w.u;
y / D w.u; / C h.u/ h./
8
let D D .du / be a new n n matrix
9
for each vertex u 2 G:V
y / for all 2 G:V
y u/ to compute .u;
10
run D IJKSTRA .G; w;
11
for each vertex 2 G:V
y / C h./ h.u/
12
du D .u;
13
return D
This code simply performs the actions we specied earlier. Line 1 produces G 0 .
Line 2 runs the Bellman-Ford algorithm on G 0 with weight function w and source
vertex s. If G 0 , and hence G, contains a negative-weight cycle, line 3 reports the
problem. Lines 412 assume that G 0 contains no negative-weight cycles. Lines 45
set h./ to the shortest-path weight .s; / computed by the Bellman-Ford algoy For each pair of verrithm for all 2 V 0 . Lines 67 compute the new weights w.
y /
tices u; 2 V , the for loop of lines 912 computes the shortest-path weight .u;
by calling Dijkstras algorithm once from each vertex in V . Line 12 stores in
matrix entry du the correct shortest-path weight .u; /, calculated using equation (25.10). Finally, line 13 returns the completed D matrix. Figure 25.6 depicts
the execution of Johnsons algorithm.
If we implement the min-priority queue in Dijkstras algorithm by a Fibonacci
heap, Johnsons algorithm runs in O.V 2 lg V CVE/ time. The simpler binary minheap implementation yields a running time of O.VE lg V /, which is still asymptotically faster than the Floyd-Warshall algorithm if the graph is sparse.
Exercises
25.3-1
Use Johnsons algorithm to nd the shortest paths between all pairs of vertices in
the graph of Figure 25.2. Show the values of h and w
y computed by the algorithm.