WorkspaceLearning path
Level 4 · Graph Algorithms
BUILD YOUR INTUITION

BFS

Explore vertices in increasing number of edges from the start, using a queue to finish one distance layer before the next.

THE MENTAL MODEL

When should this come to mind?

Explore vertices in increasing number of edges from the start, using a queue to finish one distance layer before the next.

Fewest unweighted movesNearest reachable targetLayer-by-layer traversal

What makes it work

Mark visited and assign distance when enqueuing. Specify whether distance counts edges or vertices and which moves are legal.

When to choose something else

Different nonnegative edge weights require Dijkstra or another weighted-path method; plain BFS cannot compare route costs correctly.

See the pattern in action

Change the input. Follow the state. Make it yours.