Saturday, June 14, 2008

Floyd-Warshal algorithm for all pairs of shortest path

Function Floyd-Warshall (W) :
The above Function computes the all pair shortest path for a given
directed weighted graph ‘G’. The distance matrix ‘d’ computes the shortest path. Updation of the shortest path is done by ‘pre’ pointer.

Step 1 Initialization loop
For i  1 to n
{ loop 1
for j  1 to n
{ loop 2
set d[i, j] W [i, j]
set pre [i, j]  NULL
} end of loop 2
} end of loop 1
Step 2 Intermediate vertices, loop.
for k  1 to n
{ loop 1
for i  1 to n
{ loop 2
for j  1 to n
{ loop 3
if (d [i, k] + d[k, j] < d[i, j])
{
set d[i, j]  d[i, k] + d [k, j]
new shortest path
set pre [i, j]  k
new path is through k
} end of loop 3
} end of loop 2
} end of loop 1
Step 3 Return at the point of call
return d.

No comments: