0% found this document useful (0 votes)
261 views5 pages

Algo122 Assignment2 JohnsonsAlgorithm

The document describes Johnson's algorithm for finding shortest paths between all pairs of vertices in a sparse graph with negative edge weights. It works by first reweighting the edges such that all weights are nonnegative, allowing the use of Dijkstra's algorithm. It runs Bellman-Ford to compute the reweighting values and then runs Dijkstra's algorithm from each vertex to compute the shortest path distances, taking the reweighting into account. The overall runtime is O(V^2 lg V + VE) for Fibonacci heap implementation of the priority queue.

Uploaded by

kaushik81190
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
261 views5 pages

Algo122 Assignment2 JohnsonsAlgorithm

The document describes Johnson's algorithm for finding shortest paths between all pairs of vertices in a sparse graph with negative edge weights. It works by first reweighting the edges such that all weights are nonnegative, allowing the use of Dijkstra's algorithm. It runs Bellman-Ford to compute the reweighting values and then runs Dijkstra's algorithm from each vertex to compute the shortest path distances, taking the reweighting into account. The overall runtime is O(V^2 lg V + VE) for Fibonacci heap implementation of the priority queue.

Uploaded by

kaushik81190
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

700

Chapter 25 All-Pairs Shortest Paths

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  /.

25.3 Johnsons algorithm for sparse graphs


Johnsons algorithm nds shortest paths between all pairs in O.V 2 lg V C VE/
time. For sparse graphs, it is asymptotically faster than either repeated squaring of
matrices or the Floyd-Warshall algorithm. The algorithm either returns a matrix of
shortest-path weights for all pairs of vertices or reports that the input graph contains
a negative-weight cycle. Johnsons algorithm uses as subroutines both Dijkstras
algorithm and the Bellman-Ford algorithm, which Chapter 24 describes.
Johnsons algorithm uses the technique of reweighting, which works as follows.
If all edge weights w in a graph G D .V; E/ are nonnegative, we can nd shortest paths between all pairs of vertices by running Dijkstras algorithm once from
each vertex; with the Fibonacci-heap min-priority queue, the running time of this
all-pairs algorithm is O.V 2 lg V C VE/. If G has negative-weight edges but no
negative-weight cycles, we simply compute a new set of nonnegative edge weights

25.3 Johnsons algorithm for sparse graphs

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)

Let p D h0 ; 1 ; : : : ; k i be any path from vertex 0 to vertex k . Then p is a


shortest path from 0 to k with weight function w if and only if it is a shortest path
y 0 ; k /.
y
D .
with weight function w.
y That is, w.p/ D .0 ; k / if and only if w.p/
Furthermore, G has a negative-weight cycle using weight function w if and only
if G has a negative-weight cycle using weight function w.
y
Proof

We start by showing that

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

.w.i 1 ; i / C h.i 1 /  h.i //

i D1

k
X

w.i 1 ; i / C h.0 /  h.k /

i D1

D w.p/ C h.0 /  h.k / :

(because the sum telescopes)

702

Chapter 25 All-Pairs Shortest Paths

Therefore, any path p from 0 to k has w.p/


y
D w.p/ C h.0 /  h.k /. Because h.0 / and h.k / do not depend on the path, if one path from 0 to k is
shorter than another using weight function w, then it is also shorter using w.
y Thus,
y
y
D .0 ; k /.
w.p/ D .0 ; k / if and only if w.p/
Finally, we show that G has a negative-weight cycle using weight function w if
and only if G has a negative-weight cycle using weight function w.
y Consider any
cycle c D h0 ; 1 ; : : : ; k i, where 0 D k . By equation (25.10),
w.c/
y
D w.c/ C h.0 /  h.k /
D w.c/ ;
and thus c has negative weight using w if and only if it has negative weight using w.
y
Producing nonnegative weights by reweighting
Our next goal is to ensure that the second property holds: we want w.u;
y / to be
nonnegative for all edges .u; / 2 E. Given a weighted, directed graph G D
.V; E/ with weight function w W E ! R, we make a new graph G 0 D .V 0 ; E 0 /,
where V 0 D V [ fsg for some new vertex s 62 V and E 0 D E [ f.s; / W  2 V g.
We extend the weight function w so that w.s; / D 0 for all  2 V . Note that
because s has no edges that enter it, no shortest paths in G 0 , other than those with
source s, contain s. Moreover, G 0 has no negative-weight cycles if and only if G
has no negative-weight cycles. Figure 25.6(a) shows the graph G 0 corresponding
to the graph G of Figure 25.1.
Now suppose that G and G 0 have no negative-weight cycles. Let us dene
h./ D .s; / for all  2 V 0 . By the triangle inequality (Lemma 24.10),
we have h./  h.u/ C w.u; / for all edges .u; / 2 E 0 . Thus, if we dene the new weights w
y by reweighting according to equation (25.9), we have
w.u;
y / D w.u; / C h.u/  h./  0, and we have satised the second property.
Figure 25.6(b) shows the graph G 0 from Figure 25.6(a) with reweighted edges.
Computing all-pairs shortest paths
Johnsons algorithm to compute all-pairs shortest paths uses the Bellman-Ford algorithm (Section 24.1) and Dijkstras algorithm (Section 24.3) as subroutines. It
assumes implicitly that the edges are stored in adjacency lists. The algorithm returns the usual jV j jV j matrix D D dij , where dij D .i; j /, or it reports that
the input graph contains a negative-weight cycle. As is typical for an all-pairs
shortest-paths algorithm, we assume that the vertices are numbered from 1 to jV j.

25.3 Johnsons algorithm for sparse graphs

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

Chapter 25 All-Pairs Shortest Paths

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.

You might also like