Subscribe Us

Responsive Advertisement

Advertisement

problem 38 all pair shortest path by warshall algorithm

 

problem 38 all pair shortest path by warshall algorithm


#include<stdio.h>

#define inf 100

#define size 100

int adj[size][size];

void display(int matrix[size][size],int nodes)

{

    for(int i=0;i<nodes;i++)

    {

        for(int j=0;j<nodes;j++)

        {

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

        }

        printf("\n");

    }

}

int main()

{


    int node,edge;

    printf("Enter number of nodes : ");

    scanf("%d",&node);

    printf("Enter number of edge : ");

    scanf("%d",&edge);

    while(edge--)

    {

        int u,v,w;

        printf("enter path and weight : ");

        scanf("%d%d%d",&u,&v,&w);

        adj[u][v]=w;


    }

    display(adj,node);

    printf("\n");

   for(int i=0;i<node;i++)

   {

       for(int j=0;j<node;j++)

       {

           if(adj[i][j]==0)

           {

               adj[i][j]=inf;

           }

       }

   }



    for(int k=0;k<node;k++)

          {

              for(int i=0;i<node;i++)

              {

                  for(int j=0;j<node;j++)

                  {

                    int r;

                    r=adj[i][k]+adj[k][j];

                    if(r<adj[i][j])

                    {

                        adj[i][j]=r;

                    }

                  }

              }

              display(adj,node);

              printf("\n");

          }


}


Post a Comment

0 Comments