0% found this document useful (0 votes)
23 views

Bfs

Uploaded by

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

Bfs

Uploaded by

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

Breadth First Search (BFS)

• Breadth-first search is the most common search strategy for


traversing a tree or graph. This algorithm searches breadthwise
in a tree or graph, so it is called breadth-first search.
• BFS algorithm starts searching from the root node of the tree
and expands all successor node at the current level before
moving to nodes of next level.
• The breadth-first search algorithm is an example of a general-
graph search algorithm.
• Breadth-first search implemented using FIFO queue data
structure.
Breadth First Search (BFS)
 QUEUE1 holds all the nodes that are to be processed, while QUEUE2 holds all the nodes that are
processed and deleted from QUEUE1.
 Step 1 - First, add A to queue1 and NULL to queue2.
 Step 2 - Now, delete node A from queue1 and add it into queue2. Insert all neighbors of
node A to queue1.
 Step 3 - Now, delete node B from queue1 and add it into queue2. Insert all neighbors of
node B to queue1.
 Step 4 - Now, delete node D from queue1 and add it into queue2. Insert all neighbors of
node D to queue1. The only neighbor of Node D is F since it is already inserted, so it will
not be inserted again.
 Step 5 - Delete node C from queue1 and add it into queue2. Insert all neighbors of node
C to queue1.
 Step 6 - Delete node F from queue1 and add it into queue2. Insert all neighbors of node
F to queue1. Since all the neighbors of node F are already present, we will not insert
them again.
 Step 7 - Delete node E from queue1. Since all of its neighbors have already been added,
so we will not insert them again. Now, all the nodes are visited, and the target node E is
encountered into queue2.

You might also like