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 dependency graph for breadth_first_search.cpp:

Namespaces

 graph
 Graph algorithms.
 

Typedefs

using graph::adjacency_list = std::vector< std::vector< int > >
 Representation of the graph as an adjacency list. More...
 

Functions

void graph::add_directed_edge (adjacency_list *graph, int u, int v)
 Adds a directed edge from vertex u to vertex v. More...
 
void graph::add_undirected_edge (adjacency_list *graph, int u, int v)
 Adds an undirected edge from vertex u to vertex v. Essentially adds too directed edges to the adjacency list reprsentation of the graph. More...
 
std::vector< bool > graph::breadth_first_search (const adjacency_list &graph, int start)
 Function performs the breadth first search algorithm over the graph. More...
 
static void tests ()
 
int main ()
 

Detailed Description

Breadth First Search Algorithm (Breadth First Search)

Author
Ayaan Khan

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

174  {
175  tests();
176 
177  size_t vertices = 0, edges = 0;
178  std::cout << "Enter the number of vertices: ";
179  std::cin >> vertices;
180  std::cout << "Enter the number of edges: ";
181  std::cin >> edges;
182 
183  graph::adjacency_list graph(vertices);
184 
185  std::cout << "Enter space-separated pairs of vertices that form edges: "
186  << std::endl;
187  while (edges--) {
188  int u = 0, v = 0;
189  std::cin >> u >> v;
190  // Decrement the vertex index so that we can read more convenint
191  // 1-based indexing from the user input.
192  graph::add_directed_edge(&graph, u - 1, v - 1);
193  }
194 
196  return 0;
197 }
Here is the call graph for this function:

◆ tests()

static void tests ( )
static

Test function

Test 1 Begin

Test 2 Begin

Test 3 Begins

134  {
135  /// Test 1 Begin
140 
142  std::vector<bool> correct_result = {true, true, true, true};
143 
144  assert(std::equal(correct_result.begin(), correct_result.end(),
145  returned_result.begin()));
146  std::cout << "Test 1 Passed..." << std::endl;
147 
148  /// Test 2 Begin
149  returned_result = graph::breadth_first_search(graph, 0);
150 
151  assert(std::equal(correct_result.begin(), correct_result.end(),
152  returned_result.begin()));
153  std::cout << "Test 2 Passed..." << std::endl;
154 
155  /// Test 3 Begins
156  graph.clear();
157  graph.resize(6);
164 
165  returned_result = graph::breadth_first_search(graph, 2);
166  correct_result = {false, false, true, true, false, true};
167 
168  assert(std::equal(correct_result.begin(), correct_result.end(),
169  returned_result.begin()));
170  std::cout << "Test 3 Passed..." << std::endl;
171 }
Here is the call graph for this function:
std::equal
T equal(T... args)
std::vector< std::vector< int > >
tests
static void tests()
Definition: breadth_first_search.cpp:134
graph::add_undirected_edge
void add_undirected_edge(adjacency_list *graph, int u, int v)
Adds an undirected edge from vertex u to vertex v. Essentially adds too directed edges to the adjacen...
Definition: breadth_first_search.cpp:92
std::cout
graph::breadth_first_search
std::vector< bool > breadth_first_search(const adjacency_list &graph, int start)
Function performs the breadth first search algorithm over the graph.
Definition: breadth_first_search.cpp:107
std::endl
T endl(T... args)
std::vector::begin
T begin(T... args)
graph
Graph algorithms.
std::vector::end
T end(T... args)
std::cin
graph::add_directed_edge
void add_directed_edge(adjacency_list *graph, int u, int v)
Adds a directed edge from vertex u to vertex v.
Definition: breadth_first_search.cpp:77