Pattern #14
Grid & Graph BFS
EssentialShortest paths in unweighted graphs and multi-source floods on grids.
When to use
Every edge costs the same (or each grid step is one move). You need distance, reachability, or connected components via flood fill.
Recognition cues
- Shortest path in a grid / word ladder
- Number of islands / rotting oranges
- Multi-source BFS from all gates or rotting cells
Common pitfalls
- Not marking visited when enqueueing (duplicates explode)
- 4-dir vs 8-dir inconsistency with the problem
- Using DFS when shortest path is required
90-second recognition drill
Which pattern fits best?
- Shortest path in a grid / word ladder
- Number of islands / rotting oranges
- Multi-source BFS from all gates or rotting cells
Interactive
Mental model
A full worked walkthrough of the invariant. Pause, scrub the dots, or use ← →. Aim to narrate each step yourself.
queue = [S] · dist[S] = 0
How to think about it
BFS explores level by level. The mental model draws grids and free graphs (adjacency) so you see nodes, edges, and distances. The first time you reach a node is the shortest number of edges. On grids, neighbors are up/down/left/right. Multi-source BFS seeds the queue with all starts at distance 0.
Template shapes
| Shape | Core move | Example |
|---|---|---|
| Word ladder | An edge changes one letter. Mark a word when it is enqueued. The distance when endWord is dequeued is shortest. Bidirectional BFS grows the smaller frontier and returns that same length |
LC 127 |
| Islands | Each unvisited '1' starts a traversal that marks the whole 4-connected component. DFS or BFS can do the marking. The answer counts those starts |
LC 200 |
| Multi-source distance | Every gate or every 0 enters at distance 0. Walls stay out of the queue. On an unweighted grid the first visit is the distance | LC 286, LC 542 |
| Lock | States start at "0000". One wheel turns by 1, wrapping around. Deadends are never enqueued. The first visit of the target is the fewest turns |
LC 752 |
| Rotting oranges | Every rotten cell starts at minute 0. One full queue layer is one minute. Leftover fresh oranges answer -1. No fresh orange at the start answers 0 | LC 994 |
Complexity baseline
O(V+E) (or O(R·C) on a grid). Space O(V) for queue and visited.
From template to problem
- Mark a node when it is enqueued, not when it is dequeued. A shortest-path BFS that marks late visits the same node twice (LC 127, LC 752).
- Seed every source before the loop when the starts are already known (LC 286, LC 542, LC 994).
- Islands count components, not distances. Either DFS or BFS may mark the land (LC 200). Do not use DFS when the answer is a shortest path.
- Process one queue layer at a time when each layer is one minute (LC 994). A fresh cell with no path stays fresh and the answer is -1.
Template
Same skeleton in TypeScript, Python, and C#. Adapt the invariant; keep the structure.
/** Grid BFS template: multi-source flood from all zeros (dist fill). */
export function wallsAndGatesTemplate(rooms: number[][]): void {
const m = rooms.length, n = rooms[0]?.length ?? 0;
const q: [number, number][] = [];
for (let r = 0; r < m; r++)
for (let c = 0; c < n; c++)
if (rooms[r]![c] === 0) q.push([r, c]);
const dirs = [[1, 0], [-1, 0], [0, 1], [0, -1]] as const;
while (q.length) {
const [r, c] = q.shift()!;
for (const [dr, dc] of dirs) {
const nr = r + dr, nc = c + dc;
if (nr < 0 || nc < 0 || nr >= m || nc >= n) continue;
if (rooms[nr]![nc] !== 2147483647) continue;
rooms[nr]![nc] = rooms[r]![c]! + 1;
q.push([nr, nc]);
}
}
}
/** Grid BFS template: multi-source flood from all zeros (dist fill). */
export function wallsAndGatesTemplate(rooms: number[][]): void {
const m = rooms.length, n = rooms[0]?.length ?? 0;
const q: [number, number][] = [];
for (let r = 0; r < m; r++)
for (let c = 0; c < n; c++)
if (rooms[r]![c] === 0) q.push([r, c]);
const dirs = [[1, 0], [-1, 0], [0, 1], [0, -1]] as const;
while (q.length) {
const [r, c] = q.shift()!;
for (const [dr, dc] of dirs) {
const nr = r + dr, nc = c + dc;
if (nr < 0 || nc < 0 || nr >= m || nc >= n) continue;
if (rooms[nr]![nc] !== 2147483647) continue;
rooms[nr]![nc] = rooms[r]![c]! + 1;
q.push([nr, nc]);
}
}
}
- 1#127 Word LadderGuidehard
- 2#200 Number of IslandsGuidemedium
- 3#286 Walls and GatesGuidemedium
- 4#542 01 MatrixGuidemedium
- 5#752 Open the LockGuidemedium
- 6#994 Rotting OrangesGuidemedium