Practice Discrete Math

Algorithms / Bfs Shortest Path

Least You Need to Know: BFS Layers, Unweighted Shortest Paths, and Multi-Source Search

Breadth-first search works on **unweighted** graphs because it explores states in layers of equal edge distance. That layer structure is the interview reason BFS gives shortest-path lengths when every move costs the same.

The least you need to know

Key notation

dist(v) shortest number of edges from the source set to node v
queue FIFO structure that preserves layer order
multi-source BFS BFS started from many distance-0 sources at once

Tiny worked example

  • Suppose each word can change by one letter and each valid word is a node.
  • Put the start word into a queue at distance 0.
  • Every pop exposes all words one move farther away.
  • Because BFS finishes distance 0 before distance 1, then distance 2, the first time you reach the target is the fewest moves.

Common mistakes

How to recognize this kind of problem

Start practice