I wrote a program that demonstrates warshall's algorithm. Can someone please verify that I have done this correctly?
Code:#include<iostream.h> #include<process.h> #include<conio.h> #define MAX 20 void display(int matrix[MAX][MAX],int n) { int i,j; for(i=0;i<n;i++) { for(j=0;j<n;j++) cout<<"\t"<<matrix[i][j]; cout<<"\n"; } } int main() { int i,j,k,n; int w_adj[MAX][MAX],adj[MAX][MAX],path[MAX][MAX]; cout<<"Enter number of verticies:"; cin>>n; cout<<"Enter weighted adjacency matrix:\n"; for(i=0;i<n;i++) for(j=0;j<n;j++) cin>>w_adj[i][j]; cout<<"The weighted adjacency matrix is:\n"; display(w_adj,n); /* Make weighted adjacency matrtix into a boolean adjacency matrix */ for(i=0;i<n;i++) for(j=0;j<n;j++) if(w_adj[i][j]==0) adj[i][j]=0; else adj[i][j]=1; cout<<"The adjacency matrix is:\n"; display(adj,n); for(i=0;i<n;i++) for(j=0;j<n;j++) path[i][j]=adj[i][j]; for(k=0;k<n;k++) { cout<<"P "<<k<<"is :\n"; display(path,n); for(i=0;i<n;i++) for(j=0;j<n;j++) path[i][j]=(path[i][j]||(path[i][k] && path[k][j])); } cout<<"Path Matrix P"<<k<<" of the given graph is:\n"; display(path,n); getch(); } /* End of Function Main */
Here is the output
Enter number of verticies:2
Enter weighted adjacency matrix:
23
34
32
12
The weighted adjacency matrix is:
23 34
32 12
The adjacency matrix is:
1 1
1 1
P 0is :
1 1
1 1
P 1is :
1 1
1 1
Path Matrix P2 of the given graph is:
1 1
1 1


Reply With Quote
Bookmarks