Floyd's Algorithm for Finding Shortest Paths in a Graph, Slides of Design and Analysis of Algorithms

Floyd's algorithm is a method for finding the shortest path between every pair of vertices in a graph, even if it contains negative edges but no negative cycles. The algorithm uses a weight matrix and iteratively computes the shortest distances between vertices using subsets of vertices as intermediate nodes. A detailed explanation of floyd's algorithm, including its subproblems, the use of d matrices, and examples.

Typology: Slides

2011/2012

Uploaded on 07/11/2012

dharmadaas
dharmadaas 🇮🇳

4.3

(55)

262 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Floyd’s Algorithm
All pairs shortest path
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Floyd's Algorithm for Finding Shortest Paths in a Graph and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Floyd’s Algorithm

All pairs shortest path

Floyd’s Algorithm 2

All pairs shortest path

  • The problem: find the shortest path between every pair of vertices of a graph
  • The graph : may contain negative edges but no negative cycles
  • A representation : a weight matrix where W(i,j)=0 if i=j. W(i,j)= if there is no edge between i and j. W(i,j)=“weight of edge”

Floyd’s Algorithm 4

The subproblems

  • How can we define the shortest distance di,j in terms of “smaller” problems?
  • One way is to restrict the paths to only include vertices from a restricted subset.
  • Initially, the subset is empty.
  • Then, it is incrementally increased until it includes all the vertices.

Floyd’s Algorithm 5

The subproblems

  • Let D ( k )[ i,j ]=weight of a shortest path from vi to vj using only vertices from { v 1 , v 2 ,…, vk } as intermediate vertices in the path

– D (0)= W

  • D ( n )= D which is the goal matrix
  • How do we compute D ( k )^ from D ( k -1)^?

Floyd’s Algorithm 7

Example

W = D^0 =

P =

Floyd’s Algorithm 8

D^1 =

P =

5

(^42)

k = 1

Vertex 1 can

be intermediate

node

D^1 [2,3] = min( D^0 [2,3], D^0 [2,1]+D^0 [1,3] ) = min (, 7) = 7

D^1 [3,2] = min( D^0 [3,2], D^0 [3,1]+D^0 [1,2] ) = min (-3,) = -

D^0 =

Floyd’s Algorithm 10

D^3 =

P =

D^3 [1,2] = min(D^2 [1,2], D^2 [1,3]+D^2 [3,2] ) = min (4, 5+(-3)) = 2

D^3 [2,1] = min(D^2 [2,1], D^2 [2,3]+D^2 [3,1] ) = min (2, 7+ (-1)) = 2

D^2 =

5

(^4 )

k = 3

Vertices 1, 2, 3

can be

intermediate

Floyd’s Algorithm 11

Floyd's Algorithm: Using 2 D matrices

Floyd

  1. DW // initialize D array to W [ ]

  2. P  0 // initialize P array to [0]

  3. for k  1 to n // Computing D’ from D

  4. do for i  1 to n

  5. do for j  1 to n

  6. if ( D [ i , j ] > D [ i , k ] + D [ k , j ] )

  7. then D’ [ i , j ]  D [ i , k ] + D [ k , j ]

  8. P [ i, j ]  k ;

  9. else D’ [ i , j ]  D [ i , j ]

  10. Move D’ to D.

Floyd’s Algorithm 13

The k th column

  • k th column of Dk^ is equal to the k th column of Dk -
  • Intuitively true - a path from i to k will not become shorter by adding k to the allowed subset of intermediate vertices
  • For all i, D(k)[i,k] = = min{ D(k-1)[i,k], D(k-1)[i,k]+ D(k-1)[k,k] } = min { D(k-1)[i,k], D(k-1)[i,k]+0 } = D(k-1)[i,k]

Floyd’s Algorithm 14

The k th row

  • k th row of Dk^ is equal to the k th row of Dk -

For all j , D ( k )[ k,j ] = = min{ D ( k -1)[ k,j ], D( k -1)[ k,k ]+ D ( k -1)[ k,j ] } = min{ D ( k -1)[ k,j ], 0+ D ( k -1)[ k,j ] } = D ( k -1)[ k,j ]

Floyd’s Algorithm 16

Example