Docsity

Floyd-Warshall Algorithm: Deriving Shortest Paths Between Vertices, Study notes of Computer Science

Toggle sidebar
bg1
Design & Analysis of Algorithm
Class notes taken on : 10/28/2003
By
Balaji Chandrasekaran & Finny
Floyds Algorithm:
Consider the Matrix given below
V1 V2 V3 V4 V5
V1 0 3 8 -4
V2 01 7
V3 40
V4 2 -5 0
V5 6 0
Initially we consider k=1 which denotes vertex V 1. Now we determine the path length
between every vertex passing through just V1. The initial matrix is updated after this and
the new matrix we get is given below
V1 V2 V3 V4 V5
V1 0 3 8 -4
V2 01 7
V3 40
V4 2 5 -5 0 -2
V5 6 0
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Floyd-Warshall Algorithm: Deriving Shortest Paths Between Vertices and more Study notes Computer Science in PDF only on Docsity!

Design & Analysis of Algorithm

Class notes taken on : 10/28/

By

Balaji Chandrasekaran & Finny

Floyds Algorithm: Consider the Matrix given below V1 V2 V3 V4 V V1 0 3 8  - V2  0  1 7 V3  4 0   V4 2  -5 0  V5    6 0 Initially we consider k=1 which denotes vertex V 1. Now we determine the path length between every vertex passing through just V1. The initial matrix is updated after this and the new matrix we get is given below V1 V2 V3 V4 V V1 0 3 8  - V2  0  1 7 V3  4 0   V4 2 5 -5 0 - V5    6 0

We now pick a few values in the matrix and explain how we obtained them. V4  V V4  V1  V2 = V4 V1 + V1  V = 2 + 3 = 5 <  (which is initially present in the matrix)which is initially present in the matrix) Hence we update (4,2) position with 5 V4  V V4  V1  V3 = V4 V1 + V1  V = 2 + 8 = 10 > -5 (which is initially present in the matrix)which is initially present in the matrix) Hence we leave (4,3) position unchanged with - V3  V V3  V1  V4 = V3 V1 + V1  V =  +  =  and  is present in the initial matrix too) Hence we leave (3,4) position with  V4  V V4  V1  V5 = V4 V1 + V1  V = 2 +(- 4) = -2 <  (which is initially present in the matrix)which is initially present in the matrix) Hence we update (4,5) position with -

Now we consider k=3 which denotes vertex V3. We determine the path length between every vertex passing through just V3. The previous matrix is updated after this and the new matrix we get is given below V1 V2 V3 V4 V V1 0 3 8 4 - V2  0  1 7 V3  4 0 5 11 V4 2 -1 -5 0 - V5    6 0 The only change from the previous matrix occurs in postion (which is initially present in the matrix)4,2) The reason is given below. V4  V V4  V3  V2 = V4 V3 + V3  V = -5 + 4 = -1 < 5 (which is initially present in the matrix)which is initially present in the matrix) Hence we update (4,2) position with - Now we consider k=4 which denotes vertex V4. We determine the path length between every vertex passing through just V4. The previous matrix is updated after this and the new matrix we get is given below V1 V2 V3 V4 V V1 0 3 -1 4 - V2 3 0 -4 1 - V3 7 4 0 5 3 V4 2 -1 -5 0 - V5 8 5 1 6 0 We now pick a few values in the matrix and explain how we obtained them.

V5  V

V5  V4  V1 = V5 V4 + V4  V

= 8 <  (which is initially present in the matrix)which is initially present in the matrix) Hence we update (5,1) position with 8 V3  V V3  V4  V5 = V3 V4 + V4  V = 5 + (-2) = 3 < 11 (which is initially present in the matrix)which is initially present in the matrix) Hence we update (3,5) position with 3 Now we consider k=5 which denotes vertex V5. We determine the path length between every vertex passing through just V4. The previous matrix is updated after this and the new matrix we get is given below. This is the final matrix that contains the shortest path length between each vertex. V1 V2 V3 V4 V V1 0 1 -3 2 - V2 3 0 -4 1 - V3 7 4 0 5 3 V4 2 -1 -5 0 - V5 8 5 1 6 0 The Pseudo Code that shows this algorithm:

Path (^) k [i] [j] = Path (^) k-1 [i] [j]  we don’t update the path matrix = Path (^) k-1 [k] [j]  Stores last vertex on path k to j before you hit j rationale for this is, when we add k as the intermediate vertex what we really do is i k j eg. V3  V = V3  V2  V = 4 + 7 = 11 Hence path[3][5] = path [2][5] in our matrix So in the path matrix we update the (3,5) position with 2 V1 V2 V3 V4 V V1 1 1 NIL 1 V2 NIL NIL 1 - V3 NIL 3 NIL 2 V4 4 NIL 4 NIL V5 NIL NIL NIL 5

Path [i][j] stores the last vertex in the shortest path from i j

before we hit j

The final path matrix for the Floyds Algortihm example is as given below V1 V2 V3 V4 V V1 NIL 3 4 5 1 V2 4 NIL 4 2 1 V3 4 3 NIL 2 1 V4 4 3 4 NIL 1 V5 4 3 4 5 NIL signifies no path between V5 to V Now as an example we trace the path between V1  V The path matrix for V1  V2 has 3 as its entry hence the shortest path between vertex1 and vertex2 goes through vertex 3 V1  V3  V Now we see the entry for (which is initially present in the matrix)1,3) and we get 4. Hence the shortest path between 1 and 3 passes through 4. V1  V4  V3  V Now we see the entry for (which is initially present in the matrix)1,4) and we get 5. Hence the shortest path between 1 and 4 passes through 5. V1  V5  V4  V3  V The above path gives the shortest path from V1  V