0% found this document useful (0 votes)
149 views13 pages

What Is BFS?: Let's Consider The Below Graph For The Breadth First Search Traversal

BFS is an algorithm that uses a queue to traverse nodes in a graph in levels, starting from a root node. It works by inserting the root node into an empty queue, removing nodes from the queue in First In First Out order, exploring their unvisited neighbors, and inserting unvisited neighbors into the queue. This process continues until the queue is empty, resulting in a level order traversal of the graph.

Uploaded by

Ayush Pathak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views13 pages

What Is BFS?: Let's Consider The Below Graph For The Breadth First Search Traversal

BFS is an algorithm that uses a queue to traverse nodes in a graph in levels, starting from a root node. It works by inserting the root node into an empty queue, removing nodes from the queue in First In First Out order, exploring their unvisited neighbors, and inserting unvisited neighbors into the queue. This process continues until the queue is empty, resulting in a level order traversal of the graph.

Uploaded by

Ayush Pathak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

What is BFS?

BFS

stands for Breadth First Search. It is also known as level order traversal. The Queue data
structure is used for the Breadth First Search traversal. When we use the BFS algorithm for
the traversal in a graph, we can consider any node as a root node.
Let's consider the below graph for the breadth first search traversal.
Suppose we consider node 0 as a root node. Therefore, the traversing
would be started from node 0

Once node 0 is removed from the Queue, it gets printed and marked as a visited
node.
Once node 0 gets removed from the Queue, then the adjacent nodes of node 0
would be inserted in a Queue as shown below:
Once node 1 gets removed from the Queue, then all the adjacent nodes of a
node 1 will be added in a Queue. The adjacent nodes of node 1 are 0, 3, 2, 6,
and 5. But we have to insert only unvisited nodes in a Queue. Since nodes 3,
2, 6, and 5 are unvisited; therefore, these nodes will be added in a Queue as
shown below:
The next node is 3 in a Queue. So, node 3 will be removed from the Queue, it gets
printed and marked as visited as shown below:

Once node 3 gets removed from the Queue, then all the adjacent nodes of
node 3 except the visited nodes will be added in a Queue. The adjacent
nodes of node 3 are 0, 1, 2, and 4. Since nodes 0, 1 are already visited, and
node 2 is present in a Queue; therefore, we need to insert only node 4 in a
Queue.
Now, the next node in the Queue is 2. So, 2 would be deleted from the Queue. It gets
printed and marked as visited as shown below:

Once node 2 gets removed from the Queue, then all the adjacent nodes of node 2 except
the visited nodes will be added in a Queue. The adjacent nodes of node 2 are 1, 3, 5, 6,
and 4. Since the nodes 1 and 3 have already been visited, and 4, 5, 6 are already added in
the Queue; therefore, we do not need to insert any node in the Queue.
The next element is 5. So, 5 would be deleted from the Queue. It gets printed and
marked as visited as shown below:

Once node 5 gets removed from the Queue, then all the adjacent nodes of node 5 except the visited
nodes will be added in the Queue. The adjacent nodes of node 5 are 1 and 2. Since both the nodes have
already been visited; therefore, there is no vertex to be inserted in a Queue.
The next node is 6. So, 6 would be deleted from the Queue. It gets printed and marked as visited as
shown below:
Once the node 6 gets removed from the Queue, then all the adjacent nodes of node
6 except the visited nodes will be added in the Queue. The adjacent nodes of node
6 are 1 and 4. Since the node 1 has already been visited and node 4 is already
added in the Queue; therefore, there is not vertex to be inserted in the Queue.
The next element in the Queue is 4. So, 4 would be deleted from the Queue. It gets
printed and marked as visited.

Once the node 4 gets removed from the Queue, then all the adjacent
nodes of node 4 except the visited nodes will be added in the Queue. The
adjacent nodes of node 4 are 3, 2, and 6. Since all the adjacent nodes
have already been visited; so, there is no vertex to be inserted in the
Queue
Consider the following graph:
Step 1:
Start from source s = 3.
Set Q = {3}.
Q is the queue of nodes to visit.
3 being the only node in the queue, we
visit it.
We mark 3 as visited and enqueue its
unvisited neighbors 1, 2, 4, 7.
Step 2:

Q is now {1, 2, 4, 7} while V is {3}.


We dequeue 1 and visit it. 0 is its only unvisited neighbor, so we mark 1 as visited and
enqueue 0.
We then dequeue 2 and visit it. 10 and 6 are its unvisited neighbors, so we mark 2 as visited
and enqueue 6 and 10.
Similarly, we mark 4 as visited and enqueue 5, 12, and 8 and mark 7 as visited and enqueue
9.
Step 3:

Q is now {0, 10, 6, 5, 12, 8, 9} while V is {3, 1, 2, 4, 7}.


We dequeue 0 and visit it. There are no unvisited neighbors, so we mark 0 as visited.
Similarly, we mark 10 as visited and enqueue 11 and mark 6 as visited. We then
dequeue 5 and visit it. There are no unvisited neighbors, so we mark 5 as visited.
Same goes for 12 and 8. For 12 we enqueue 13. We then dequeue 9 and visit it.
Step 4:

Q is now {11, 13} while V is {3, 1, 2, 4, 7, 0, 10, 6, 5, 12, 8, 9}.


We dequeue 11 and visit it. There are no unvisited neighbors, so we
mark 11 as visited. We then dequeue 13 and visit it.
Step 5:

Awesome! We have visited all the nodes in the graph. The queue is now
empty, so we stop the search. The BFS traversal of the graph is 3, 1, 2, 4,
7, 0, 10, 6, 5, 12, 8, 9, 11, 13.

You might also like