Appendix A Parhi-Ch4
Appendix A Parhi-Ch4
Shortest Path
Algorithms
A.l INTRODUCTION
717
718 SHORTEST PATH ALGORITHMS
where the length of the edge is simply a number associated with the edge.
Let n be the number of nodes in the graph, and assume that the n nodes are
numbered 1, 2, ... , n. The shortest path from the node U to the node V is
denoted as Suv.
1, r< 1 l(U)=O
2. For k = 1 to n
3. Jfk #u
4. r< 1 >(k) = w(U 4 k)
5. For k = 1 to n - 2
6. For V = 1 to n
7. r(k+l) (V) = r(k) (V)
8. For W = 1 to n
9. If r<k+l) (V) > r<k> (W) + w(W 4 V)
10. r<k+l) (V) = r<k> (W) + w(W 4 V)
11. For V = 1 to n
12. For W = 1 to n
13. Ifr(n-l)(V) > r(n-l)(W) + w(W 4 V)
14. return FALSE and exit
15. return TRUE and exit
are each of size n x 1, as described in Figure A.2. Note that r(k) (U) represents
the U-th entry in the column vector r(k).
If TRUE is returned, then r(n-l) (V) is Suv, which is the shortest path from
the node U to the node V. If FALSE is returned, then the graph contains at
least one negative cycle.
Table A.2 Values of r(k) (V) for k = 1, 2, 3 and V = 1, 2, 3, 4 from Example A.2.2
Example A.3.1 In this example, the Floyd- Warshall algorithm in Figure A.4
is used to solve the all-pairs shortest path problem for the graph in Figure A.3.
The values of r(k) (U, V) for U, V E {1, 2, 3, 4} and k = 1, 2, 3, 4, 5 are given
in Table A.3, where r(k) (U, V) is the element U, V in the matrix R(k). The
COMPUTATIONAL COMPLEXITIES 721
1. For V = 1 to n
2. For U = 1 to n
3. r(ll(U, V) = w(U 4 V)
4. Fork= 1 ton
5. For V = 1 to n
6. For U = 1 to n
7. r(k+l)(U, V) = r(k)(U, V)
8. If r<k+t)(U, V) > r<kl(U, k) + r(k)(k, V)
9. r<k+t)(U, V) = r<kl(U,k) +r(k)(k, V)
10. For k = 1 to n
11. For U = 1 to n
12. If r<kl(U, U) < 0
13. return FALSE and exit
14. return TRUE and exit
Floyd- Warshall algorithm returns TRUE because all of the diagonal elements
in the matrices R(k), k = 1, 2, 3, 4, are nonnegative, so r( 5 ) (U, V) is the short-
est path from the node U to the node V. •
The Bellman-Ford algorithm detects negative cycles and solves the single-
source shortest path problem if no negative cycles exist in O(n 3 ) time, while
the Floyd-Warshall algorithm detects negative cycles and solves the all-pairs
shortest path problem if no negative cycles exist in 0( n 3 ) time. The Bellman-
Ford algorithm requires O(n 4 ) time to solve the all-pairs shortest path prob-
lem since it needs to be run once for each of the n nodes. The Floyd-Warshall
algorithm is preferred for applications that require all-pairs shortest path so-
lution, and the Bellman-Ford algorithm and Floyd-Warshall algorithm can
be used interchangeably for applications that require a single-source shortest
path solution.
Modifications to the Bellman-Ford algorithm and early exit conditions for
both algorithms can be utilized to improve computational efficiency. The
722 SHORTEST PATH ALGORITHMS
[~ ~ ~ ~J [: ~ ~ ~J [:
-3 -2
00 1
0000 2 00 0000 2 00 00 00
00 00 00 1 -2 00 00 1 -2 -1
R(4) R(s
[~ ~ ~ -~] uJJ
Table A.4 Values of r(k)(U, V) for Example A.3.2
[~ ]] [~
-3
]] J~
-3
-~]
00 00 -3 -2
00 1 00 1 00 1
00 00 00 00 00 00
00 00 -2 00 -2 -1 -1
R(4)
[~
-3 -2
00
00
1
-2 -1 -1
00
-~] [-~ -4 -3
-1
0
0
1
-3 -2 -2
-~]
interested reader can find these details in [1].
REFERENCES