C++ program to implement Floyd-Warshal's algorithm for finding all pair shortest path.

Aim:- C++ program to implement Floyd-Warshal's algorithm for finding all pair shortest path.

Input:-1.No. of vertices or node of graph.
           2.Adjacency cost matrix.

Output:-Shortest path for all possible two nodes. 

Source Code:-

#include
#include
#define max 10

void allpairshort(int a[max][max][max],int n)
{
int i,j,k;
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(a[k-1][i][j]<(a[k-1][i][k]+a[k-1][k][j]))
{
a[k][i][j]=a[k-1][i][j];
}
else
{
a[k][i][j]=a[k-1][i][k]+a[k-1][k][j];
}
}
}
}


cout<<"\nAll pair Shortest path is=\n";

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<}
cout<<"\n";
}

}
void main()
{
int a[max][max][max],i,j,n;
clrscr();
cout<<"\nEnter no. of nodes:";
cin>>n;
cout<<"\n(Enter 999 for infinity";
cout<<"\nEnter adjacency cost matrix:";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cin>>a[0][i][j];
}
}

allpairshort(a,n);

getch();
}

Comments

Post a Comment

Popular Posts