AfterAcademy Tech
•
10 Feb 2020

In the previous blog i.e. The Introduction to Graph in Programming, we saw what a graph is and we also saw some of the properties and types of graph. We also saw how to represent a graph i.e. using the Adjacency Matrix and Adjacency List. In this blog, we will learn about the Breadth-First Search i.e. BFS that is used to search some node in a graph by traversing it.
Graph traversal is a process of visiting all the nodes from a source node only once in some defined order. The order of traversal of nodes of a graph is very important while solving some graph problems. Also, you must track the nodes that are already visited because, in traversal, you need to traverse a node only once. So, a proper list of the traversed nodes of the graph must be maintained. There are two ways of Graph traversal:
In this blog, we will cover the BFS part. So, let's get started.
Breadth-First Search or BFS is a graph traversal algorithm that is used to traverse the graph level wise i.e. it is similar to the level-order traversal of a tree.
Here, you will start traversing the graph from a source node and from that node you will first traverse the nodes that are the neighbours of the source node. After traversing all the neighbour nodes of the source node, you need to traverse the neighbours of the neighbour of the source node and so on.
Based on the source node, the whole graph can be divided into various levels i.e. the nodes that are at distance 1 from the source node are said to be at level 1. Similarly, the nodes that are at distance 2 from the source node are said to be at level 2 and so on. Based on the layers of the graph, the BFS can be performed by the following steps:
NOTE: There can be more than one path from node i to node j because a graph can contain a cycle. But you should visit a node once. So, you have to keep a record of all the visited nodes so that one node can be visited only once.
The following is an example of Breadth-First Search:

In order to implement BFS, we need to take care of the following things:
So, to apply the above conditions, we make the use of Queue data structure that follow First In First Out(FIFO) order. We will insert the nodes in the queue and mark it as visited and after that, all the neighbour nodes of that node will also be inserted in the queue. Since it follows FIFO order, the node entered first will be visited first and their neighbours will be added in the queue first. During insertion of nodes in the queue, we will check if the nodes are visited or not. If it is visited then we will not add those nodes in the queue. Otherwise, we will add the node in the queue.
The following is the pseudocode of Breadth-First Search:
BFS (G, s): //here, G is the graph and s is the source or initial node
Q.enqueue( s ) //here, Q is queue for storing nodes. So, we are inserting node "s" in the queue Q
also mark "s" as visited node
while ( Q is not empty)
//removing that vertex from queue whose neighbour will be visited now
v = Q.dequeue( ) //this will return the node inserted first in the queue
//processing all the neighbours of v
//if the node "w" is already visited, then ignore it
//if the node "w" is not visited, then insert it into the queue
for all neighbours w of v in Graph G
if w is not visited
Q.enqueue( w ) //Stores w in Q to further visit its neighbour
mark w as visited.
Let's understand the above pseudocode with the help of one example.



The following process will be followed in different iteration:
First Iteration
Second Iteration
Third Iteration
Fourth Iteration
Fifth Iteration
Sixth Iteration
These are some of the applications of Breadth-First Search.
Happy Learning, Enjoy Algorithms!
Team AfterAcademy!
AfterAcademy Tech
In this blog, we will learn Depth First Search that is used for Graph Traversal i.e. the Depth First Search is used to traverse each and every node of a graph. So, let's learn together.

AfterAcademy Tech
Given a sorted integer array arr[] of n elements and a target value k, write a program to search k in arr[]. The famous binary search algorithm is easy to implement and the best optimization technique for performing searching operations

AfterAcademy Tech
You are given a matrix arr of m x n size. Write a program to searches for a value k in arr. This arr has the following properties: - Integers in each row are sorted from left to right. - The first value of each row is greater than the last value of previous row. This is a basic optimization problem that will clear the concept of searching.

AfterAcademy Tech
It is important to know traversal techniques in a tree before solving tree problems. We have discussed the 3 primary traversals in a tree at length in this blog with both recursive and iterative implementations.
