C++ Program to Implement Floyd-Warshall Algorithm

This C++ program displays the shortest path traversal from a particular node to every other node present inside the graph relative to the former node.

Here is the source code of the C++ program of the Floyd Warshall Algoritm of finding shortest paths from any node in graph to every other node with the shortest path length displayed beside each pair of vertices. This C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is given below.

  1. /*
  2.  * C++ Program to Implement Floyd-Warshall Algorithm
  3.  */
  4. #include <iostream>
  5. #include <conio.h>
  6. using namespace std;
  7. void floyds(int b[][7])
  8. {
  9.     int i, j, k;
  10.     for (k = 0; k < 7; k++)
  11.     {
  12.         for (i = 0; i < 7; i++)
  13.         {
  14.             for (j = 0; j < 7; j++)
  15.             {
  16.                 if ((b[i][k] * b[k][j] != 0) && (i != j))
  17.                 {
  18.                     if ((b[i][k] + b[k][j] < b[i][j]) || (b[i][j] == 0))
  19.                     {
  20.                         b[i][j] = b[i][k] + b[k][j];
  21.                     }
  22.                 }
  23.             }
  24.         }
  25.     }
  26.     for (i = 0; i < 7; i++)
  27.     {
  28.         cout<<"\nMinimum Cost With Respect to Node:"<<i<<endl;
  29.         for (j = 0; j < 7; j++)
  30.         {
  31.             cout<<b[i][j]<<"\t";
  32.         }
  33.  
  34.     }
  35. }
  36. int main()
  37. {
  38.     int b[7][7];
  39.     cout<<"ENTER VALUES OF ADJACENCY MATRIX\n\n";
  40.     for (int i = 0; i < 7; i++)
  41.     {
  42.         cout<<"enter values for "<<(i+1)<<" row"<<endl;
  43.         for (int j = 0; j < 7; j++)
  44.         {
  45.             cin>>b[i][j];
  46.         }
  47.     }
  48.     floyds(b);
  49.     getch();
  50. }

Output
ENTER VALUES OF ADJACENCY MATRIX

enter values for 1 row
0
3
6
0
0
0
0
enter values for 2 row
3
0
2
4
0
0
0
enter values for 3 row
6
2
0
1
4
2
0
enter values for 4 row
0
4
1
0
2
0
4
enter values for 5 row
0
0
4
2
0
2
1
enter values for 6 row
0
0
2
0
2
0
1
enter values for 7 row
0
0
0
4
1
1
0

Minimum Cost With Respect to Node:0
0       3       5       6       8       7       8
Minimum Cost With Respect to Node:1
3       0       2       3       5       4       5
Minimum Cost With Respect to Node:2
5       2       0       1       3       2       3
Minimum Cost With Respect to Node:3
6       3       1       0       2       3       3
Minimum Cost With Respect to Node:4
8       5       3       2       0       2       1
Minimum Cost With Respect to Node:5
7       4       2       3       2       0       1
Minimum Cost With Respect to Node:6
8       5       3       3       1       1       0

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.