Thursday, December 1, 2011

Program for Finding Transitive Closure - Warshall Algorithm

#include
#include
#define MAX 20

display(int matrix[MAX][MAX],int n)
{

int i,j;
for(i=0;i{

for(j=0;j
printf("%3d",matrix[i][j]);

printf("\n");

}

}

int main()
{

int i,j,k,n;
int w_adj[MAX][MAX],adj[MAX][MAX],path[MAX][MAX];

printf("Enter number of vertices : ");
scanf("%d",&n);

printf("Enter weighted adjacency matrix :\n");
for(i=0;i
for(j=0;j
scanf("%d",&w_adj[i][j]);

printf("The weighted adjacency matrix is :\n");
display(w_adj,n);

/* Make weighted adjacency matrix into boolean adjacency matrix*/

for(i=0;i
for(j=0;j
if(w_adj[i][j]==0)

adj[i][j]=0;

else

adj[i][j]=1;

printf("The adjacency matrix is :\n");
display(adj,n);

for(i=0;i
for(j=0;j
path[i][j]=adj[i][j];

for(k=0;k{

printf("P%d is :\n",k);
display(path,n);
for(i=0;i
for(j=0;j
path[i][j]=( path[i][j] || ( path[i][k] && path[k][j] ) );

}

printf("Path matrix P%d of the given graph is :\n",k);
display(path,n);

}/*End of main() */

No comments:

Post a Comment