Subscribe:

Labels

Monday, February 15, 2010

warshalls

#include<iostream.h>
class warshal
{
 int n,i,j,k,d[10][10];
public:
 void getgraph();
 void war();
 void disp();
};
void warshal::getgraph()
{
 cout<<"Enter the no of vertices";
 cin>>n;
 cout<<"Enter the adjacency matrix";
 for(i=1;i<=n;i++)
  for(j=1;j<=n;j++)
  {
   cout<<"["<<i<<";"<<j<<"]:";
   cin>>d[i][j];
  }
}
void warshal::war()
{
 for(k=1;k<=n;k++)
  for(i=1;i<=n;i++)
   for(j=1;j<=n;j++)
    if(i!=j)
     d[i][j]=(d[i][j]||(d[i][k]&&d[k][j]));
}
void warshal::disp()
{
 cout<<"The transitive closure";
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
  {
   cout<<"\t"<<d[i][j];
  }
  cout<<"\n";
 }
}
void main()
{
 warshal f;

 f.getgraph();
 f.war();
 f.disp();

}

No comments:

Post a Comment