Subscribe Us

Responsive Advertisement

Advertisement

Counting Connected Component in Graph | Graph Theory | DFS | Algorithm | using C++

 


Counting Connected Component in Graph | Graph Theory  | DFS | Algorithm | using  C++


#include<bits/stdc++.h>

using namespace std;

#define ll long long int

 vector<ll> arr[100];

 ll vis[100];

 void dfs(ll v)

 {

     vis[v]=1;


     for(ll i=0;i<arr[v].size();i++)

     {

         ll child;

         child=arr[v][i];

         if(vis[child]==0)dfs(child);

     }


 }

int main()

{


    ll edge,node,n,ans=0;


    cin>>node>>edge;

    while(edge--)

    {

        ll x,y;

        cin>>x>>y;


        arr[x].push_back(y);

        arr[y].push_back(x);

    }

   for(ll i=1;i<=node;i++)

   {

       if(vis[i]==0)

       {

           ans++;

           dfs(i);

       }


   }


  cout<<ans<<endl;

    return 0;

}


Post a Comment

0 Comments