Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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

void DFS(int start, vector<int> v[],vector<int> &visited, int &count)
{
  visited[start] = 1;
  count++;
  for(int i= 0; i<v[start].size(); ++i)
  {        
    if(visited[v[start][i]] == 0)
        DFS(v[start][i], v, visited);        
  }    
}
---------------------------------------------------
  for(int i=1;i<=n;++i)
{
  if(visited[i] == 0 )
  {
     connected++;
     int count=0;
     DFS(i,v,visited,count);
     cout<<"This component has "<<count<<" nodes"<<"
";
  }        
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Count #number #nodes #connected #part #undirected #unweighted #graph
ADD COMMENT
Topic
Name
3+7 =