Breadth-first search (BFS)

Time complexity: O(V + E).

Result

Examples

How it works

BFS explores every cell one step away, then two steps, and so on, using a queue. It finds the path with the fewest steps but ignores the cost of each step.

When to use it

Fewest moves on an unweighted grid or graph, like a maze where every step costs the same.

Other algorithms