TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Breadth First Search Algorithm (Breadth First Search) More...
#include <algorithm>
#include <cassert>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <string>
#include <vector>
Go to the source code of this file.
Classes | |
class | graph::Graph< T > |
Namespaces | |
namespace | graph |
Graph Algorithms. | |
Functions | |
static void | tests () |
int | main () |
Breadth First Search Algorithm (Breadth First Search)
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
And there are many more...
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.
Definition in file breadth_first_search.cpp.
int main | ( | void | ) |
Main function
Definition at line 186 of file breadth_first_search.cpp.
|
static |
Test function
Test 1 Begin
Test 2 Begin
Test 3 Begins
Definition at line 139 of file breadth_first_search.cpp.