count number of nodes in each connected part of an undirected unweighted graph

Solutions on MaxInterview for count number of nodes in each connected part of an undirected unweighted graph by the best coders in the world

showing results for - "count number of nodes in each connected part of an undirected unweighted graph"
Sara
27 Jan 2019
1void DFS(int start, vector<int> v[],vector<int> &visited, int &count)
2{
3  visited[start] = 1;
4  count++;
5  for(int i= 0; i<v[start].size(); ++i)
6  {        
7    if(visited[v[start][i]] == 0)
8        DFS(v[start][i], v, visited);        
9  }    
10}
11---------------------------------------------------
12  for(int i=1;i<=n;++i)
13{
14  if(visited[i] == 0 )
15  {
16     connected++;
17     int count=0;
18     DFS(i,v,visited,count);
19     cout<<"This component has "<<count<<" nodes"<<"\n";
20  }        
21}