FloydWarshal Algorithm code
#include<limits.h>
#include<bits/stdc++.h>
#define v 4
#define V 4
using namespace std;
void printSolution(int dist[][V])
{
cout<<"The following matrix shows the shortest distances"
" between every pair of vertices \n";
for (int i = 0; i < V; i++)
{#include<bits/stdc++.h>
#define v 4
#define V 4
using namespace std;
void printSolution(int dist[][V])
{
cout<<"The following matrix shows the shortest distances"
" between every pair of vertices \n";
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
{
cout<<dist[i][j]<<" ";
}
cout<<endl;
}
}
void floyd (int graph[][V])
{
int dist[V][V], i, j, k;
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
dist[i][j] = graph[i][j];
for (k = 0; k < V; k++)
{
for (i = 0; i < V; i++)
{
for (j = 0; j < V; j++)
{
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
printSolution(dist);
}
int main()
{
int graph[v][v] = { {0, 5, 70, 10},
{90, 0, 3, 100},
{98, 100, 0, 1},
{76, 95, 200, 0}
};
floyd(graph);
}
Comments
Post a Comment