WAP in C++ To Find The Shortest Path Between Source and Destination Using Warshall's Algorithm
WAP in C++ To Find The Shortest Path Between Source and Destination Using Warshall's Algorithm
WAP in C++ To Find The Shortest Path Between Source and Destination Using Warshall's Algorithm
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i,j,k;
int n,e;
int s,d,w;
for(i=0;i<n;i++)
{
distMat[i][i]=0;
}
distMat[s-1][d-1]=w;
}
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(distMat[i][k]!=INT_MAX &&
distMat[k][j]!=INT_MAX &&
distMat[i][k]+distMat[k][j]<distMat[i][j])
{
distMat[i][j]=distMat[i][k]+distMat[k][j];
}
}
}
}
cout<<" ";
for(i=0;i<n;i++)
{
printf("%6d",i+1);
}
cout<<endl;
for(i=0;i<6*n;i++)
{
cout<<"_";
}
cout<<"_______"<<endl;
for(i=0;i<n;i++)
{
printf("%5d |",i+1);
for(j=0;j<n;j++)
{
if(distMat[i][j]==INT_MAX)
printf(" INF");
else
printf("%6d",distMat[i][j]);
cout<<endl;
}
return 0;
}
Ouput: