Friday, 31 July 2020

3. WARSHALL’S ALGORITHM


SOURCE CODE:

 #include<stdio.h>

#include<conio.h>

 

int n,i,j,k;

int mat[25][25];

void input();

void plus();

void main()

{

      clrscr();

      input();

      plus();

      getch();

}

 

void input()

{

      printf("Enter the order of matrix:");

      scanf("%d",&n);

      printf("\n\nEnter the matrix of given order:\n\n");

      for(i=0;i<n;i++)

      {

            for(j=0;j<n;j++)

            {

                  scanf("%d",&mat[i][j]);

            }

      }

      printf("\nEntered matrix A is:\n\n");    

for(i=0;i<n;i++)

      {

            for(j=0;j<n;j++)

            {

                  printf("%d  ",mat[i][j]);

            }

            printf("\n\n");

      }

}

 

void plus()

{

      printf("\nWarshall of a given matrix is:\n\n");

      for(i=0;i<n;i++)

      {

            for(j=0;j<n;j++)

            {

                  if(mat[i][j]==1)

                  {

                        for(k=0;k<n;k++)

                        {

                              if( mat[j][k]==1)

                              {

                                    mat[i][k]=1;

                              }

                         }

                  }

 

                  printf("%d  ",mat[i][j]);

            }

 

            printf("\n\n");

      }

}

 

 

 

 

 

 

 

 

Output:

 Enter the order of matrix:5

Enter the matrix of given order:

1 1 0 0 0

0 1 1 0 0

0 0 0 1 1

0 0 0 0 0

0 0 0 0 0

Entered matrix A is:

1  1  0  0  0

0  1  1  0  0

0  0  0  1  1

0  0  0  0  0

0  0  0  0  0

Warshall of a given matrix is:

1  1  1  1  1

0  1  1  1  1

0  0  0  1  1

0  0  0  0  0

0  0  0  0  0


 


No comments:

Post a Comment

If you have any doubts please let me know