When to Use

Pattern triggers

Look for these signals in the problem:

  • "Shortest path" in unweighted graph: BFS finds minimum edge count path.

First time you reach a node is via shortest path.

  • "Level by level" or "minimum steps": When counting steps or levels matters, BFS processes all nodes at distance k before distance k+1.
  • "Nearest" or "closest" in grid: BFS from source finds nearest target by expanding outward in all directions simultaneously.
  • Multi-source shortest path: Start BFS with multiple sources in queue to find distance to nearest source for all cells.