title: BFS implementation C++
body: <p>I was trying to implement BFS using Vertex classes and BFS class, since I wanted to learn implementation of classes along with algorithms.</p>
  <p><strong>vertex.h</strong></p>
  <pre><code>#ifndef vertex_H_
  #define vertex_H_
  #include&lt;vector&gt;
  
  
  class Vertex{
  
      private:
           int _data;
           bool _visited;
           std::vector&lt;Vertex&gt; _neighbours;
  
      public:
           Vertex(int data);
           Vertex();
           void setData(int data);
           int getData();
           bool isVisited();
           void setVisited(bool visited);
           std::vector&lt;Vertex&gt; getNeighbours();
           void setNeighbours(std::vector&lt;Vertex&gt;&amp; neighbours);
           void addNeighbourVertex(Vertex&amp; vertex);
           ~Vertex();
  };
  
  #endif  
  </code></pre>
  <p><strong>vertex.cpp</strong></p>
  <pre><code>#include &quot;vertex.h&quot;
  
  
  Vertex::Vertex(int data):_data(data){}
  Vertex::Vertex(){}
  Vertex::~Vertex(){}
  
  void Vertex::setData(int data){
      _data = data;
  }
  
  
  int Vertex::getData(){
      return _data;
  }
  
  void Vertex::setVisited(bool visited){
      _visited=visited;
  }
  
  bool Vertex::isVisited(){
      return _visited;
  }
  
  std::vector&lt;Vertex&gt; Vertex::getNeighbours(){
      return _neighbours;
  }
  
  void Vertex::setNeighbours(std::vector&lt;Vertex&gt;&amp; neighbours){
      _neighbours = neighbours;
  }
  
  void Vertex::addNeighbourVertex(Vertex&amp; vertex){
      _neighbours.push_back(vertex); 
  }
  </code></pre>
  <p><strong>BFS.h</strong></p>
  <pre><code>#ifndef BFS_H_
  #define BFS_H_
  #include &quot;vertex.h&quot;
  
  class BFS{
      public:
          void bfs(Vertex&amp; root);
          
  };
  
  #endif
  </code></pre>
  <p><strong>BFS.cpp</strong></p>
  <pre><code>#include&lt;iostream&gt;
  #include&lt;vector&gt;
  #include&quot;vertex.h&quot;
  #include&lt;deque&gt;
  #include&quot;BFS.h&quot;
  
  void BFS::bfs(Vertex&amp; root)
  {  
      std::deque&lt;Vertex&gt; Q;
      root.setVisited(true);
      Q.push_back(root);
  
      while(!Q.empty())
      {
          Vertex select_node = Q.front();
          Q.pop_front();
          std::cout&lt;&lt;select_node.getData()&lt;&lt;&quot; &quot;&lt;&lt;std::endl;
          for(Vertex node : select_node.getNeighbours())
          {
              if (!node.isVisited())
              {
                  node.setVisited(true);
                  Q.push_back(node);
              }
          }
          
      }
  }
  </code></pre>
  <p><strong>application.cpp</strong></p>
  <pre><code>#include&lt;iostream&gt;
  #include &quot;BFS.h&quot;
  #include &quot;vertex.h&quot;
  
  
  int main(){
  
      BFS bfs;
  
      Vertex v1(1);
      Vertex v2(2);
      Vertex v3(3);
      Vertex v4(4);
      Vertex v5(5);
  
      v1.addNeighbourVertex(v2);
      v1.addNeighbourVertex(v4);
      v4.addNeighbourVertex(v5);
      v2.addNeighbourVertex(v3);
  
      bfs.bfs(v1);
  
  
  
  
  
      return 0;
  }
  </code></pre>
  <ul>
  <li>getNeighbours function from vertex class gets the vector of neighbouring vertices of particular vertex.</li>
  </ul>
  <p><strong>Problem I am facing is that for graph in application.cpp that I have defined</strong>.</p>
  <p>vertex 1-&gt; vertices{2,4}</p>
  <p>vertex 2-&gt; vertex{3}</p>
  <p>vertex 3-&gt; vertex{2}</p>
  <p>vertex 4-&gt; vertex{5}</p>
  <p>vertex 5-&gt; vertex{4}</p>
  <p><strong>expected output is:</strong>
  1
  2
  4
  3
  5</p>
  <p><strong>My output</strong> :
  1
  2
  4</p>
  <p>I would really like some assistance.. there is some thing i am unable to see here, and hence not getting the proper output.</p>