Breadth-first search
AlgorithmsExplore level by level with a queue — finds the shortest path in an unweighted graph.
Definition
BFS explores a graph level by level using a queue, visiting all of a node's neighbours before going deeper. Because it reaches nodes in order of distance from the source, on an unweighted graph the first time it sees a node is via a shortest (fewest-edge) path.
When to use
Reach for BFS for shortest-path-in-unweighted, level-order tree traversal, and 'minimum number of steps' problems (including grids). Mark nodes visited when you enqueue them, not when you dequeue, to avoid adding the same node twice.