Algorithm


Breadth-First Search (BFS) Algorithm:

  1. Initialize a queue and mark the starting node as visited.
  2. Enqueue the starting node into the queue.
  3. While the queue is not empty:
    1. Dequeue a node from the front of the queue.
    2. Process the dequeued node (e.g., print or store it).
    3. For each unvisited adjacent node of the dequeued node:
      1. Mark the adjacent node as visited.
      2. Enqueue the adjacent node into the queue.

Example:

Breadth-First Search Example

Fig. 1: Step-by-Step Process of Breadth-First Search

Note: BFS is particularly useful for finding the shortest path in an unweighted graph and for traversing all nodes level by level.