




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Floyd Warshall Algorithm, Dynamic programming, Formulation, Current k iteration, Mid vertex pointers, Subset of these vertices, Algorithm, Two basic cases are the key points in this study notes file.
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





8.6.5 Floyd-Warshall Algorithm
We consider the generalization of the shortest path problem: to compute the shortest paths between all
pairs of vertices. This is called the all-pairs shortest paths problem.
Let G=(V, E) be a directed graph with edge weights. If (u, v) ∈ E is an edge then w(u, v) denotes its
weight. δ(u, v) is the distance of the minimum cost path between u and v. We will allow G to have
negative edges weights but will not allow G to have negative cost cycles. We will present an Θ(n
3 )
algorithm for the all pairs shortest path. The algorithm is called the Floyd-Warshall algorithm and is
based on dynamic programming.
We will use an adjacency matrix to represent the digraph. Because the algorithm is matrix based, we
will employ the common matrix notation, using i, j and k to denote vertices rather than u, v and w.
The input is an n × n matrix of edge weights:
if i j
w w i j if i j and i j E ij
if i j and i j E
The output will be an n × n distance matrix D= dij, where dij = δ(i, j), the shortest path cost from
vertex i to j.
The algorithm dates back to the early 60’s. As with other dynamic programming algorithms, the genius of
the algorithm is in the clever recursive formulation of the shortest path problem. For a path p=〈v 1 , v 2 ,
..., vl , we say that the vertices v 2 , v 3 , ..., vl - 1 are the intermediate vertices of this path. Formulation:
Define
( ) k d ij
to be the shortest path from i to j such that any intermediate vertices on the path are chosen
from the set {1, 2, ..., k}. The path is free to visit any subset of these vertices and in any order. How do
we compute
( ) k d ij
assuming we already have the previous matrix d
( k- 1 )? There are two basic cases:
Figure 8.71: Two cases for all-pairs shortest path
Don’t go through k at all
Then the shortest path from i to j uses only intermediate vertices {1, 2, ..., k- 1}. Hence the length of
the shortest is
( k 1) d ij
Do go through k
First observe that a shortest path does not go through the same vertex twice, so we can assume that we
pass through k exactly once. That is, we go from i to k and then from k to j. In order for the overall path
to be as short as possible, we should take the shortest path from i to k and the shortest path from k to j.
Since each of these paths uses intermediate vertices {1, 2, ..., k- 1}, the length of the path is
( k 1) d ik
( k 1) d kj
.
The following illustrate the process in which the value of 3, 2
k d is updated as k goes from 0 to 4.
This suggests the following recursive (DP) formulation:
d(^0 )
~ d(k^ )
i j , (^) d( k - 1 ) i k + (^) d( k - 1 ) k j
The final answer is (^) d(n )
i j because this allows all possible vertices as intermediate vertices.
As is the case with DP algorithms, we will avoid recursive evaluation by generating a table for (^) d(k )
i j. The
3
2
Figure 8.77 through 8.81 demonstrate the algorithm when applied to a graph. The matrix to left of the
Figure 8.77: Floyd-Warshall Algorithm example: d
(
Figure 8.78: Floyd-Warshall Algorithm example: d
(
(
Extracting Shortest Path: