#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int main()
{
int n,i,j,k,m,p;
cout<<"\nEnter the number of nodes:";
cin>>n;
fflush(stdin);
char name[n][3],ch;
int recon[n][n];
int adj[n][n],path[n][n];
for(i=0;i<n;i++)
{
cout<<"\n\a\aEnter the name on node:";
cin>>name[i];
for(j=0;j<n;j++)
{
fflush(stdin);
cout<<"\n\nEnter element at position ("<<i+1<<","<<j+1<<") : ";
cin>>adj[i][j];
if(i!=j)
{cout<<"\n\nIs this path direct?(Y/N):";
cin>>ch;}
else
ch='n';
if(ch=='y' || ch=='Y')
recon[i][j]=i+1;
else
recon[i][j]=0;
path[i][j]=adj[i][j];
}
}
system("cls");
cout<<"\nThe adjacency matrix::\n\n";
cout<<" ";
for(i=0;i<n;i++)
{
cout<<" ";
cout<<name[i];
}
cout<<endl;
for(i=0;i<n;i++)
{
cout<<name[i]<<" ";
for(j=0;j<n;j++)
{
cout<<adj[i][j];
cout<<" ";
}
cout<<endl;
}
cout<<"\nEnter:\n1:Warshall's Law\n2:Warshall Floyd law\nYour choice:";
cin>>i;
system("cls");
if(i==1)
{
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(path[i][k]*path[k][j]==1)
path[i][j]=1;
}
}
if(k!=n-1)
cout<<"\n\nAfter pass "<<k+1<<" for k="<<k<<" the intermediate path matrix::\n\n";
else
cout<<"\n\nFinal Path matrix::\n\n";
cout<<" ";
for(m=0;m<n;m++)
{
cout<<" ";
cout<<name[m];
}
cout<<endl;
for(m=0;m<n;m++)
{
cout<<name[m]<<" ";
for(p=0;p<n;p++)
{
cout<<path[m][p];
cout<<" ";
}
cout<<endl;
}
}
}
else if(i==2)
{
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((path[i][k]+path[k][j])<path[i][j])
{
path[i][j]=path[i][k]+path[k][j];
recon[i][j]=recon[k][j];
}
}
}
if(k!=n-1)
cout<<"\n\nAfter pass "<<k+1<<" for k="<<k<<" the intermediate path matrix::\n\n";
else
cout<<"\n\nFinal Path matrix::\n\n";
cout<<" ";
for(m=0;m<n;m++)
{
cout<<" ";
cout<<name[m];
}
cout<<endl;
for(m=0;m<n;m++)
{
cout<<name[m]<<" ";
for(p=0;p<n;p++)
{
cout<<path[m][p];
cout<<" ";
}
cout<<endl;
}
}
//*******************************************************
cout<<"\a\a\n\n\nReconstructed Path matrix::\n\n";
cout<<" ";
for(m=0;m<n;m++)
{
cout<<" ";
cout<<name[m];
}
cout<<endl;
for(m=0;m<n;m++)
{
cout<<name[m]<<" ";
for(p=0;p<n;p++)
{
cout<<recon[m][p];
cout<<" ";
}
cout<<endl;
}
}
else
cout<<"\a\a\nWrong call!!";
return 0;
}
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int main()
{
int n,i,j,k,m,p;
cout<<"\nEnter the number of nodes:";
cin>>n;
fflush(stdin);
char name[n][3],ch;
int recon[n][n];
int adj[n][n],path[n][n];
for(i=0;i<n;i++)
{
cout<<"\n\a\aEnter the name on node:";
cin>>name[i];
for(j=0;j<n;j++)
{
fflush(stdin);
cout<<"\n\nEnter element at position ("<<i+1<<","<<j+1<<") : ";
cin>>adj[i][j];
if(i!=j)
{cout<<"\n\nIs this path direct?(Y/N):";
cin>>ch;}
else
ch='n';
if(ch=='y' || ch=='Y')
recon[i][j]=i+1;
else
recon[i][j]=0;
path[i][j]=adj[i][j];
}
}
system("cls");
cout<<"\nThe adjacency matrix::\n\n";
cout<<" ";
for(i=0;i<n;i++)
{
cout<<" ";
cout<<name[i];
}
cout<<endl;
for(i=0;i<n;i++)
{
cout<<name[i]<<" ";
for(j=0;j<n;j++)
{
cout<<adj[i][j];
cout<<" ";
}
cout<<endl;
}
cout<<"\nEnter:\n1:Warshall's Law\n2:Warshall Floyd law\nYour choice:";
cin>>i;
system("cls");
if(i==1)
{
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(path[i][k]*path[k][j]==1)
path[i][j]=1;
}
}
if(k!=n-1)
cout<<"\n\nAfter pass "<<k+1<<" for k="<<k<<" the intermediate path matrix::\n\n";
else
cout<<"\n\nFinal Path matrix::\n\n";
cout<<" ";
for(m=0;m<n;m++)
{
cout<<" ";
cout<<name[m];
}
cout<<endl;
for(m=0;m<n;m++)
{
cout<<name[m]<<" ";
for(p=0;p<n;p++)
{
cout<<path[m][p];
cout<<" ";
}
cout<<endl;
}
}
}
else if(i==2)
{
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((path[i][k]+path[k][j])<path[i][j])
{
path[i][j]=path[i][k]+path[k][j];
recon[i][j]=recon[k][j];
}
}
}
if(k!=n-1)
cout<<"\n\nAfter pass "<<k+1<<" for k="<<k<<" the intermediate path matrix::\n\n";
else
cout<<"\n\nFinal Path matrix::\n\n";
cout<<" ";
for(m=0;m<n;m++)
{
cout<<" ";
cout<<name[m];
}
cout<<endl;
for(m=0;m<n;m++)
{
cout<<name[m]<<" ";
for(p=0;p<n;p++)
{
cout<<path[m][p];
cout<<" ";
}
cout<<endl;
}
}
//*******************************************************
cout<<"\a\a\n\n\nReconstructed Path matrix::\n\n";
cout<<" ";
for(m=0;m<n;m++)
{
cout<<" ";
cout<<name[m];
}
cout<<endl;
for(m=0;m<n;m++)
{
cout<<name[m]<<" ";
for(p=0;p<n;p++)
{
cout<<recon[m][p];
cout<<" ";
}
cout<<endl;
}
}
else
cout<<"\a\a\nWrong call!!";
return 0;
}
No comments:
Post a Comment