Monday, May 13, 2024

Floyd's Algorithm

 #include<iostream>

using namespace std;


int main()

{

    int n;

    cout<<"Enter total number of vertices"<<endl;

    cin>>n;

    //matrix

    int a[n][n];

    int i,j,k;

    cout<<"Input the value of adjacency matrix"<<endl;

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

    {

        cout<<"Enter the value of "<<i+1<<"row";

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

        {

            cin>>a[i][j];

        }

    }

    

    //display adjacency matrix

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

    {

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

        {

            cout<<a[i][j]<<" ";

        }

        cout<<endl;

    }

    //floyd algo

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

    {

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

    {

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

        {

            if(a[i][j]>a[i][k]+a[k][j])

            {

                a[i][j]=a[i][k]+a[k][j];

            }

        }

    }

    }

    //diaplay 

    cout<<"output matrix"<<endl;

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

    {

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

        {

            cout<<a[i][j]<<" ";

        }

        cout<<endl;

    }

    

}

No comments:

Post a Comment

Complete Works of Swami Vivekananda [Volume 8,Page - 2069]

  Complete Works of Swami Vivekananda [ Volume 8, Page - 2069] Jesus Christ was God — the Personal God become man. He has manifested Himsel...