Docsity

Analog and digital C - Floyd - Warshall Shortest path Algorithm, Study notes of Digital & Analog Electronics

Partial preview of the text

Download Analog and digital C - Floyd - Warshall Shortest path Algorithm and more Study notes Digital & Analog Electronics in PDF only on Docsity!

The Floyd-Warshall single

source shortest path

Algorithm

The Floyd-Warshall

Algorithm

• Graph analysis algorithm for finding

shortest paths in a weighted, directed

graph

• Graph can have negative weights, but not

negative weight cycles

• Example of dynamic programming, a

method of solving problems where one

needs to find the best decision one after

another

Pseudo code

• M = matrix

• n = number of nodes

• k = n loops

• i = vertical axis (start point)

• j = horizontal axis (end point)

for ( k=0; k < n; k++ ) {

for ( i=0; i < n; i++ ) {

for ( j=0; j < n; j++ ) {

M[i][j]= min( M[i][j], M[i][k] + M[k][j])

First Loop

A B C D E F

A 0 inf inf 3 inf 2 B 5 0 3 inf inf inf C inf 3 0 inf inf inf D inf inf 2 0 inf 4 E inf inf inf inf 0 1 F inf inf inf inf inf 0

 n = 6

 k = 0

 i = 0

 j = 0

 M[i][j]= min( M[i][j], M[i][k] + M[k][j])

 M[0][0] = min( M[0][0], M[0][0] + M[0][0] )

 M[0][0] = min (0, 0) = 0

 So M[A][A] = 0

First Loop, final

A B C D E F

A 0 inf inf 3 inf 2 B 5 0 3 8 inf inf -> 7 C inf 3 0 inf inf inf D inf inf 2 0 inf 4 E inf inf inf inf 0 1 F inf inf inf inf inf 0

 n = 6

 k = 0

 i = 1

 j = 5

 M[i][j]= min( M[i][j], M[i][k] + M[k][j])

 M[1][5] = min( M[1][5], M[1][0] + M[0][5] )

 M[1][5] = min (999, 5 + 2) = 7

 So M[B][F] = 7

Matrix Final State

(all shortest paths) crunch

A B C D E F

A 0 8 5 3 inf 2 B 5 0 3 8 inf 7 C 8 3 0 11 inf 10 D 10 5 2 0 inf 4 E inf inf inf inf 0 1 F inf inf inf inf inf 0

Applications

• Shortest path in directed graphs

• Transitive closure of directed graphs

• Regular expressions

• Matrix inversion

• Optimal routing