Monday, May 13, 2024

Warshall 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 cost matrix

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

    {

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

        {

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

        }

        cout<<endl;

    }

    // algo

    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]);

        }

    }

    }

    //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

A physicist confesses that the electron is round and that Jesus rose from the dead. (Bengali)

একজন পদার্থবিজ্ঞানী স্বীকার করেন যে ইলেকট্রন গোলাকার এবং যিশু মৃতদের মধ্য থেকে পুনরুত্থিত হয়েছিলেন। আমার নিজের বিশ্বাস প্রমাণ দ্বারা প্রভাব...