SaravananKS
SaravananKS

Reputation: 587

Graph-Finding unreachable nodes in undirected graph using Depth First Search

I have a undirected graph (may be disconnected graph). I need to find Number of unreachable nodes for a given node.

#include<bits/stdc++.h>
using namespace std;
vector<int> graph[1000];
bool visited[1000];
int count=0;

void dfs(int s)
{
  count=count+1;
  visited[s]=true;
  for(int i=0;i<graph[s].size();i++)
    if(visited[graph[s][i]]==false)
      dfs(i);
}

int main()
{
  int n,m;
  cin>>n>>m;
  int i;
  for(i=0;i<m;i++)
  {
    int x,y; 
    cin>>x>>y;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  int k;
  cin>>k;
  dfs(k);

  cout<<n-count;
}

Initially the graph has n nodes. After DFS process, for the particular node k ,dfs(k) finds number of nodes connected with k. So the unreachable nodes can be calculated by n-count.

But this codes shows an error that says reference to 'count' is ambiguous. What is the problem? Did I make any mistake in DFS recursive method?

Upvotes: 1

Views: 930

Answers (1)

rafix07
rafix07

Reputation: 20938

In C++ library there is count function template - in algorithm header (which was included by #include <bits/stdc++.h> directive), you can resolve your problem by adding :: before count in dfs and main function.

  ::count= ::count+1;

and

  cout<<n-::count;

Upvotes: 2

Related Questions