You are on page 1of 2

Enter number of vertices : 5

Enter graph in matrix form


0 5 0 2 0
0 0 2 0 0
3 0 0 0 7
0 0 4 0 1
1 3 0 0 0

Graph is ...
0 5 0 2 0
0 0 2 0 0
3 0 0 0 7
0 0 4 0 1
1 3 0 0 0

After initialization...D and P matrix

D Matrix...
0 5 999 2 999
999 0 2 999 999
3 999 0 999 7
999 999 4 0 1
1 3 999 999 0

P Matrix...
-1 0 -1 0 -1
-1 -1 1 -1 -1
2 -1 -1 -1 2
-1 -1 3 -1 3
4 4 -1 -1 -1

After Floyd Warshall...D and P matrix

D Matrix...
0 5 6 2 3
5 0 2 7 8
3 8 0 5 6
2 4 4 0 1
1 3 5 3 0

P Matrix...
-1 0 3 0 3
2 -1 1 0 3
2 0 -1 0 3
4 4 3 -1 3
4 4 1 0 -1

Printing Path
path from 0 to 1 is 0 --> 1
path from 0 to 2 is 0 --> 3 --> 2
path from 0 to 3 is 0 --> 3
path from 0 to 4 is 0 --> 3 --> 4
path from 1 to 0 is 1 --> 2 --> 0
path from 1 to 2 is 1 --> 2
path from 1 to 3 is 1 --> 2 --> 0 --> 3
path from 1 to 4 is 1 --> 2 --> 0 --> 3 --> 4
path from 2 to 0 is 2 --> 0
path from 2 to 1 is 2 --> 0 --> 1
path from 2 to 3 is 2 --> 0 --> 3
path from 2 to 4 is 2 --> 0 --> 3 --> 4
path from 3 to 0 is 3 --> 4 --> 0
path from 3 to 1 is 3 --> 4 --> 1
path from 3 to 2 is 3 --> 2
path from 3 to 4 is 3 --> 4
path from 4 to 0 is 4 --> 0
path from 4 to 1 is 4 --> 1
path from 4 to 2 is 4 --> 1 --> 2
path from 4 to 3 is 4 --> 0 --> 3

You might also like