#include<iostream.h>
#include<conio.h>
int a[20][20],cost[20][20],p[20][20],i,j,n,k;
class path
{
public:
void get();
void allpair(int a[20][20],int n);
void put();
};
void path:: get()
{
cout<<"\n\t\tALL PAIR SHORTEST PATH\n";
cout<<"\nEnter the no of vertices : ";
cin>>n;
cout<<"\nEnter the cost\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==j)
{
cost[i][j]=p[i][j]=0;
a[i][j]=cost[i][j];
}
else
{
cout<<"\ncost["<<i<<"]["<<j<<"]=";
cin>>cost[i][j];
a[i][j]=cost[i][j];
}
}
cout<<"\n";
}
}
void path:: allpair(int a[20][20],int n)
{
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if((a[i][k]+a[k][j])<a[i][j])
{
a[i][j]=a[i][k]+a[k][j];
p[i][j]=k;
}
}
}
}
}
void path:: put()
{
cout<<"\n\n\n\n\n\nALL PAIR SHORTEST PATH\n\n\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
cout<<'\t'<<a[i][j];
cout<<"\n";
}
cout<<"\n\n\nPath Matrix\n";
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
cout<<'\t'<<p[i][j];
cout<<"\n";
}
}
void main()
{
path p;
clrscr();
p.get();
p.allpair(a,n);
p.put();
getch();
}
Floyd–Warshall algorithm
Share This!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment