Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
breadth_first_search.cpp File Reference

Breadth First Search Algorithm (Breadth First Search) More...

#include <algorithm>
#include <cassert>
#include <iostream>
#include <queue>
#include <vector>
#include <map>
#include <list>
#include <string>
Include dependency graph for breadth_first_search.cpp:

Classes

class  graph::Graph< T >
 

Namespaces

 graph
 Graph Algorithms.
 

Functions

static void tests ()
 
int main ()
 

Detailed Description

Breadth First Search Algorithm (Breadth First Search)

Author
Ayaan Khan
Aman Kumar Pandey

Breadth First Search also quoted as BFS is a Graph Traversal Algorithm. Time Complexity O(|V| + |E|) where V are the number of vertices and E are the number of edges in the graph.

Applications of Breadth First Search are

  1. Finding shortest path between two vertices say u and v, with path length measured by number of edges (an advantage over depth first search algorithm)
  2. Ford-Fulkerson Method for computing the maximum flow in a flow network.
  3. Testing bipartiteness of a graph.
  4. Cheney's Algorithm, Copying garbage collection.

And there are many more...

working

In the implementation below we first created a graph using the adjacency list representation of graph. Breadth First Search Works as follows it requires a vertex as a start vertex, Start vertex is that vertex from where you want to start traversing the graph. We maintain a bool array or a vector to keep track of the vertices which we have visited so that we do not traverse the visited vertices again and again and eventually fall into an infinite loop. Along with this boolen array we use a Queue.

  1. First we mark the start vertex as visited.
  2. Push this visited vertex in the Queue.
  3. while the queue is not empty we repeat the following steps
    1. Take out an element from the front of queue
    2. Explore the adjacency list of this vertex if element in the adjacency list is not visited then we push that element into the queue and mark this as visited

Function Documentation

◆ main()

int main ( void  )

Main function

184  {
185  tests();
186  size_t edges = 0;
187  std::cout << "Enter the number of edges: ";
188  std::cin >> edges;
189 
191 
192  std::cout << "Enter space-separated pairs of vertices that form edges: "
193  << std::endl;
194  while (edges--) {
195  int u = 0, v = 0;
196  std::cin >> u >> v;
197  g.add_edge(u,v);
198  }
199 
201  return 0;
202 }
Here is the call graph for this function:

◆ tests()

static void tests ( )
static

Test function

Test 1 Begin

Test 2 Begin

Test 3 Begins

137  {
138  /// Test 1 Begin
140  std::map<int,bool> correct_result;
141  g.add_edge(0,1);
142  g.add_edge(1,2);
143  g.add_edge(2,3);
144  correct_result[0]=true;
145  correct_result[1]=true;
146  correct_result[2]=true;
147  correct_result[3]=true;
148 
149  std::map<int,bool> returned_result = g.breadth_first_search(2);
150 
151  assert(returned_result==correct_result);
152  std::cout << "Test 1 Passed..." << std::endl;
153 
154  /// Test 2 Begin
155  returned_result = g.breadth_first_search(0);
156 
157  assert(returned_result==correct_result);
158  std::cout << "Test 2 Passed..." << std::endl;
159 
160  /// Test 3 Begins
162 
163  g2.add_edge("Gorakhpur","Lucknow",false);
164  g2.add_edge("Gorakhpur","Kanpur",false);
165  g2.add_edge("Lucknow","Agra",false);
166  g2.add_edge("Kanpur","Agra",false);
167  g2.add_edge("Lucknow","Prayagraj",false);
168  g2.add_edge("Agra","Noida",false);
169 
170  std::map<std::string,bool> correct_res;
171  std::map<std::string,bool> returned_res=g2.breadth_first_search("Kanpur");
172  correct_res["Gorakhpur"]=false;
173  correct_res["Lucknow"]=false;
174  correct_res["Kanpur"]=true;
175  correct_res["Agra"]=true;
176  correct_res["Prayagraj"]=false;
177  correct_res["Noida"]=true;
178  assert(correct_res==returned_res);
179  std::cout << "Test 3 Passed..." << std::endl;
180 
181 }
Here is the call graph for this function:
tests
static void tests()
Definition: breadth_first_search.cpp:137
std::cout
std::map
STL class.
graph::Graph::add_edge
void add_edge(T u, T v, bool bidir=true)
Definition: breadth_first_search.cpp:72
std::endl
T endl(T... args)
graph::Graph
Definition: breadth_first_search.cpp:64
std::cin
graph::Graph::breadth_first_search
std::map< T, bool > breadth_first_search(T src)
Definition: breadth_first_search.cpp:94