title: BFS on Adjacency Matrix
body: <p>I'm trying to implement a BFS on adjacency matrix of undirected unweighted graph which returns the number of nodes visited. I've come up with this till now but I think this is not right as when I print out the top/ visited node, I'm getting multiple occurrences of some nodes as well as it's not sorted. I've read somewhere that BFS is a topological sort and the order I get is not sorted.</p>
  
  <pre><code>int BFS(std::vector&lt;std::vector&lt;int&gt; &gt; &amp;matrix, int start)
  {
      std::vector&lt;bool&gt; visited(matrix.size(), false);
      std::queue&lt;int&gt; Q;
      Q.push(start);
      int count = 0;
  
      while( ! Q.empty())
      {
          int top = Q.front(); Q.pop();
          visited[top] = true; count++;
          for (int i = 0; i &lt; matrix.size(); ++i)
          {
              if(matrix[top][i] != 0 &amp;&amp; (! visited[i]) )
              {
                  Q.push(i);
              }
          }
      }
      return count;
  }
  </code></pre>