Floyd Warshall Algorithm - Design and Analysis - Study Notes, Study notes of Digital Systems Design

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

2011/2012

Uploaded on 11/03/2012

ankitay
ankitay 🇮🇳

4.4

(50)

106 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture No. 40
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:
0
(, ) (, )
(, )
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=
d
ij
,
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=v1, v2,
..., vl, we say that the vertices v2, v3, ..., vl-1 are the intermediate vertices of this path. Formulation:
Define
()
k
dij
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
dij
assuming we already have the previous matrix
d
(
k
-
1
)? There are two basic cases:
1. Don’t go through vertex k at all.
2. Do go through vertex k.
Docsity.com
pf3
pf4
pf5
pf8

Partial preview of the text

Download Floyd Warshall Algorithm - Design and Analysis - Study Notes and more Study notes Digital Systems Design in PDF only on Docsity!

Lecture No. 40

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:

  1. Don’t go through vertex k at all.
  2. Do go through vertex k.

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.

Figure 8.76: k= 4, d( 4 )

This suggests the following recursive (DP) formulation:

d(^0 )

i j = w i j

~ d(k^ )

i j = min ~d( k - 1 )

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

algorithm also includes mid-vertex pointers stored in mid[i , j] for extracting the final path.

FLOYD-WARSHALL(n,w[1..n, 1..n])

1 for (i = 1, n)

2 dofor( j=1,n )

3 do d[i, j] w[i, j]; mid[i , j] n u l l

4 for (k = 1, n)

5 do for (i = 1, n)

6 dofor( j=1,n )

7 do if ( d[i, k] + d[k, j] < d[i, j] )

8 then d[i, j] = d[i, k] + d[k, j]

9 mid[i , j] = k

Clearly, the running time is Θ(n

3

). The space used by the algorithm is Θ(n

2

Figure 8.77 through 8.81 demonstrate the algorithm when applied to a graph. The matrix to left of the

graph contains the matrix d entries. A circle around an entry di,k indicates that it was updated in the

current k iteration.

2 CHAPTER 8. GRAPHS

Figure 8.77: Floyd-Warshall Algorithm example: d

(

Figure 8.78: Floyd-Warshall Algorithm example: d

(

Figure 8.81: Floyd-Warshall Algorithm example: d

(

Extracting Shortest Path:

The matrix d holds the final shortest distance between pairs of vertices. In order to compute the shortest

path, the mid-vertex pointers mid[i , j] can be used to extract the final path. Whenever we discovered that

the shortest path from i to j passed through vertex k, we set mid[i , j] = k. If the shortest path did not

pass through k then mid[i, j] = n u l l.

To find the shortest path from i to j, we consult mid[i , j]. If it is null, then the shortest path is just the

edge (i , j). Otherwise we recursively compute the shortest path from i to mid[i , j] and the shortest path

from mid[i , j] to j.

PATH (i , j )

1 if (mid[i , j] = = n u l l )

2 then output(i , j )

3 else PATH(i , mid[i , j])

4 PATH(mid[i , j]