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.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification 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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.