Introduction to Breadth First Search
Breadth First Search is an algorithm which is a part of an uninformed search strategy. This is used for searching for the desired node in a tree. The algorithm works in a way where breadth wise traversal is done under the nodes. It starts operating by searching starting from the root nodes, thereby expanding the successor nodes at that level. Then it moves to the other node by expanding along with its neighboring breadth. The algorithm requires a considerable amount of memory space and time for its execution in the case where the required node lies at the bottom of the tree or graph. The BFS algorithm requires the usage of First in First Out queue for its implementation. The BFS strategy works without any domain knowledge.
Now we shall put our glance at the steps involved in the BFS strategy.
Step 1: We take an empty queue.
Step 2: Then we select a starting point or root node to start with and insert the data from the node into the queue.
Step 3: If the queue is not empty then we extract the node from the neighboring nodes in breadth wise fashion and insert its child nodes into the queue.
Step 4: Then we can print the extracted nodes.
Example of Breadth First Search
We will now see the steps of the above BFS strategy into this example. We take a look at the graph below. We will use the BFS strategy to traverse the graph.
We can allocate ‘a’ as the root node. Then we start searching for the goal node in the downward direction following the steps mentioned above.
The figure illustrates the steps involved in BFS which binds in the end to end process. The steps are executed in the following manner.
- We allocate node ‘a’ as a starting root node and insert it into the queue.
- Then ‘a’ node is extracted and the child nodes of ‘a’ which are ‘b’ and ‘c’ put into the queue.
- Then we Print ‘a’.
- Then the first node in the queue is extracted again similarly.
- Next, the child of ‘b’ which are ‘d’ and ‘e’ are inserted into the queue and extracted for printing.
- Then we recursively repeat the steps until the queue is empty.
- We cannot repeat the steps for already visited nodes.
Now let us see below the pseudo-code of the algorithm.
- Input: s as the source node
- BFS (G, s)
- let Q be the queue.
- enqueue( s )
- mark s as visited
- while ( Q is not empty)
- v = Q.dequeue( )
- for all neighbors w of v in Graph G
- if w is not visited
- enqueue( w )
- mark was visited
Applications of Breadth First Search
We can say that there are numerous applications of BFS in data sciences, one of them being in finding the shortest path using a minimum spanning tree. There are various applications in networking, broadcasting, GPS navigation, etc. The range of applications surprises us though the execution time might not allow us to choose it sometimes. The BFS is one of the main algorithms used in indexing web pages.
The Program in C language for BFS implementation is as follows:
#define MAX 200
#define initial 1
#define waiting 2
#define visited 3
void BFS(int x);
int queue[MAX], front = -1,rear = -1;
void insert_queue(int vertex);
for(x=0; x<m; x++)
state[x] = initial;
printf("Enter the starting vertex for doing BFS: \n");
void BFS(int x)
state[x] = waiting;
x = delete_queue( );
state[x] = visited;
for(i=0; i<m; i++)
if(adj[x][i] == 1 && state[i] == initial)
state[i] = waiting;
void insert_queue(int vert)
if(rear == MAX-1)
printf("Queue is overflowing\n");
if(front == -1)
front = 0;
rear = rear+1;
queue[rear] = vert ;
if(front == -1 || front > rear)
if(front == -1 || front > rear)
printf("Queue is underflowing\n");
delete_item = queue[front];
front = front+1;
printf("Enter number of vertices : ");
m_edge = m*(m-1);
for(count=1; count<=m_edge; count++)
printf("Enter edge %d( -1 -1 to quit ) : ",count);
if((ori == -1) && (dest == -1))
if(ori>=m || dest>=m || ori<0 || dest<0)
adj[ori][dest] = 1;
The run time of the code is given by O (V+E) where V =Number of vertices and E=Number of edges.
Advantages and Disadvantages of Breadth First Search
Below are the advantages and disadvantages of BFS
- Breadth first search algorithm is complete.
- The optimal solution is possible to obtain from BFS.
- There is vast number of applications of BFS algorithm in data science.
- Optimality means admissibility if all the operators used in the code are having the same cost and the goal node can be reached in time. The other possibility is that the solution is found with shortest length without optimality.
- The execution time is exponential and the memory space complexity can be expressed as: O(b^d), where d=depth of the search space and b=branching factor(child nodes)
- The run time may exceed when the goal node is not known.
- The execution time might get into infinite loop.
- The main demerit looks to exist in the time taken to find the solution with large data and thus it gives priority to look at shorter lengths first.
The Breadth first search is a simple algorithm for implementing the search operation breadth wise and using FIFO queues. The memory space requirements might rise high but yet it proves to be high in demand. It is used in determining the levels of a tree using their node information. From this basic application to higher range usages in networking, peer to peer networking, we may find the applications of BFS. Thus, we may say that BFS is an important uninformed search algorithm that opens the research gateway as well.
This is a guide to Breadth First Search. Here we discuss the examples of Breadth First Search along with the steps involved in the BFS strategy and advantages and disadvantages. You may also have a look at the following articles to learn more –