BFS

The Breadth First Search (BFS) algorithm traverses a graph in a breadth-toward motion and uses a queue to remember when a dead end occurs in any iteration to get the next vertex to start a search. The Breadth First Search (BFS) algorithm traverses a graph in a breadth-toward motion and uses a queue to remember when a dead end occurs in any iteration to get the next vertex to start a search.The Breadth First Search (BFS) algorithm traverses a graph in a breadth-toward motion and uses a queue to remember when a dead end occurs in any iteration to get the next vertex to start a search.

As in the above example, the BFS algorithm first traverses from A to B to E to F, then to C and G to D. The following laws are hired.

Rule 1 − Visit the unvisited vertex next to it. Mark it as having been visited. Only show it. In a queue, insert it.

Rule 2 − Select the first vertex from the queue if no adjacent vertex is located.

Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.

We are left with no unmarked (unvisited) nodes at this point. But in order to get all unvisited nodes, we keep on dequeuing as per the algorithm. The programme is finished when the queue gets empty.