how many nodes in graph are reachable?
<p>I want to print the number of nodes which are reachable from a particular node. I read graph and stored in adjacency list and performed bfs.i had tried the following code.it works with certain graph.can you traced out what's wrong with this?</p>

<pre><code> #include &lt;vector&gt;
    #include &lt;iostream&gt;
    #include &lt;list&gt;
    #include&lt;queue&gt;
    using namespace std;
    int BFS(int s)
    {
        const int V=100;
        int r=0;
        vector&lt;list&lt;int&gt; &gt; a(V);    
        int visited[V]={0};
        queue&lt;int&gt; Q;
        visited[s]=1;
        Q.push(s);
        ++r;
        while(!Q.empty())
        {
            int x=Q.front();
            Q.pop(); // pop here. we have x now
            ++r;
            vector&lt;list&lt;int&gt; &gt;::iterator it1=a.begin()+x;
            list&lt;int&gt; it2=*it1;
            list&lt;int&gt;::iterator iter=it2.begin();
            while(iter!=it2.end())
            {
                if(visited[*iter]==0)
                {
                    visited[*iter]=1;
                    Q.push(*iter);

                }
                ++iter;
            }

            visited[x]=2; // set visited here.

        }
        return r;
    }
    void printAsGrid(int V) 
{
  // Create a local 2D projection grid
  int size = V.size();
  double *grid = new double[size*size];
  memset(grid, 0.0, size*size*sizeof(double));

  // Get the edge connection and weight
  int index;
  for (index = 0; index &lt; size; index++) {
    list&lt;Edge&gt;::const_iterator eit;
    for (eit = V[index].edges.begin();
         eit != V[index].edges.end(); eit++) {
      int to = (*eit).to;
      double w = (*eit).weight;
      // record weight in the projection grid
      grid[(index*size)+to] = w;
    }
  }

  // print header
  out &lt;&lt; "   |";
  for (index = 0; index &lt; size; index++)
    out &lt;&lt; "    " &lt;&lt; index;
  out &lt;&lt; endl;
  out &lt;&lt; "---+";
  for (index = 0; index &lt; size; index++)
    out &lt;&lt; "-----";
  out &lt;&lt; endl;

  // print content
  out.setf(ios::fixed);
  out.setf(ios::showpoint);
  for (index = 0; index &lt; size; index++) {
    out &lt;&lt; " " &lt;&lt; index &lt;&lt; " |";
    for (int j = 0; j &lt; size; ++j)
      out &lt;&lt; setw(5) &lt;&lt; setprecision(1) &lt;&lt; grid[(index*size)+j];
    out &lt;&lt; endl;
  }
  // delete grid before exit
  delete [] grid;
}

    int main()
    {
        int s;

        int V,total_neighbors, id, weight;
        //number of vertices
        cout&lt;&lt;"enter the no.of vertices:";
        cin&gt;&gt;V;

         vector&lt; list&lt; int &gt; &gt; graph(V + 1);
        for(int i= 0; i&lt;V;i++) {
            cout&lt;&lt;"Enter no.of neighbours of"&lt;&lt;i&lt;&lt;":";
            cin&gt;&gt;total_neighbors;
            cout&lt;&lt;"Enter the neighbours of"&lt;&lt;i&lt;&lt;":";
            for(int j = 0; j &lt;total_neighbors; j++) {
                cin&gt;&gt;id;
                graph[i].push_back(id);
            }
        }
        vector&lt;list&lt;int&gt; &gt;::iterator i;
        int c=0;
        for (vector&lt;std::list&lt;int&gt; &gt;::iterator i=graph.begin(); i !=graph.end(); ++i){


            cout&lt;&lt;"vertices connected to node "&lt;&lt;c &lt;&lt;" are ";
            //cout&lt;&lt;*i;

            std::list&lt;int&gt; li = *i;
            for(std::list&lt;int&gt;::iterator iter = li.begin(); iter!= li.end(); ++iter){

                cout&lt;&lt;*iter&lt;&lt;" ";
            }
            cout&lt;&lt;endl;
            c++;
    }
            int f;
            cin&gt;&gt;f;
            s=BFS(f);
            cout&lt;&lt;s&lt;&lt;" ";
            return 0;
        }



adjacencyList 0 -&gt; 1 -&gt; 2
adjacencyList 1 -&gt; 2 -&gt; 4
adjacencyList 2 -&gt; 4
adjacencyList 3 -&gt; 5
adjacencyList 4
adjacencyList 5 -&gt; 3
</code></pre>

<p>returns 2 but actual answer is 3</p>