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.
/*
* C++ Program to Implement Floyd-Warshall Algorithm
*/
#include <iostream>
#include <conio.h>
using namespace std;
void floyds(int b[][7])
{
int i, j, k;
for (k = 0; k < 7; k++)
{
for (i = 0; i < 7; i++)
{
for (j = 0; j < 7; j++)
{
if ((b[i][k] * b[k][j] != 0) && (i != j))
{
if ((b[i][k] + b[k][j] < b[i][j]) || (b[i][j] == 0))
{
b[i][j] = b[i][k] + b[k][j];
}
}
}
}
}
for (i = 0; i < 7; i++)
{
cout<<"\nMinimum Cost With Respect to Node:"<<i<<endl;
for (j = 0; j < 7; j++)
{
cout<<b[i][j]<<"\t";
}
}
}
int main()
{
int b[7][7];
cout<<"ENTER VALUES OF ADJACENCY MATRIX\n\n";
for (int i = 0; i < 7; i++)
{
cout<<"enter values for "<<(i+1)<<" row"<<endl;
for (int j = 0; j < 7; j++)
{
cin>>b[i][j];
}
}
floyds(b);
getch();
}
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.
Next Steps:
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for Computer Science Internship
- Practice Programming MCQs
- Buy Programming Books
- Buy Computer Science Books
- Apply for Information Technology Internship