Breadth First Traversal
Breadth First Traversal
Breadth First Search algorithmBFS traverses a graph in a breadthwards motion and uses a queue
to remember to get the next vertex to start a search when a dead end occurs in any iteration.
As in example given above, BFS algorithm traverses from A to B to E to F first then to C and G lastly
to D. It employs following rules.
Rule 1 Visit adjacent unvisited vertex. Mark it visited. Display it. Insert it in a queue.
Rule 2 If no adjacent vertex found, remove the first vertex from queue.
Rule 3 Repeat Rule 1 and Rule 2 until queue is empty.
Step
Traversal
Description
1.
2.
3.
4.
5.
6.
7.
At this stage we are left with no unmarked unvisited nodes. But as per algorithm we keep on
dequeuing in order to get all unvisited nodes. When the queue gets emptied the program is over.