Graph BFS / DFS — definition.
Definition
Breadth-first for shortest unweighted paths; depth-first for exhaustive traversal.
Complexity
Time O(V + E); space O(V).
When to use it
- Grid or graph traversal with connectivity.
- Shortest path on unweighted edges (BFS).
- Multi-source frontier expansion (BFS from multiple starts).
Representative scenarios
- Oil Patch Count — number of islands in a grid.
- Firefront Spread — BFS from all fires simultaneously.
- Exit-Sign Distances — shortest distance from each cell to an exit.
Related patterns
Read the full pattern page: Graph BFS / DFS — template, code, and drills.
Run the free diagnostic.
Ten-minute patterns quiz. No card. Personalized loop starts on the other side.