Powered By Blogger

Friday, April 29, 2011

Warshall's Algorithm

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[10][10],i,j,k,n;
    printf("Enter the no. of nodes : ");
    scanf("%d",&n);
    //Enter only 1 or 0
    printf("Now enter the matrix row wise : \n");
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            scanf("%d",&a[i][j]);
    printf("\nThe matrix u entered is : \n");
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
            printf("%2d",a[i][j]);
        printf("\n");
    }

    //To find out the transitive relation matrix
    for(k=0;k<n;k++)
    {
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
                a[i][j]=a[i][j]||(a[i][k]&&a[k][j]);
    }

    //To print the required transitive relation matrix
    printf("\n\nThe required transitive matrix is : \n");
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
            printf("%2d",a[i][j]);
        printf("\n");
    }
}

N.B. : Check this program for 5x5 matrix and lemme know if there is any fault...

No comments:

Post a Comment