Google News
logo
Algorithm - Interview Questions
Explain the BFS algorithm?
BFS (Breadth First Search) is a graph traversal algorithm. It starts traversing the graph from the root node and explores all the neighboring nodes. It selects the nearest node and visits all the unexplored nodes. The algorithm follows the same procedure for each of the closest nodes until it reaches the goal state.
 
Algorithm
 
Step1 : Set status=1 (ready state)
 
Step2 : Queue the starting node A and set its status=2, i.e. (waiting state)
 
Step3 : Repeat steps 4 and 5 until the queue is empty.
 
Step4 : Dequeue a node N and process it and set its status=3, i.e. (processed state)
 
Step5 : Queue all the neighbors of N that are in the ready state (status=1) and set their status =2 (waiting state)
[Stop Loop]
 
Step6 : Exit
Advertisement