Module A-9·14 min read

Grids as implicit graphs — flood fill, multi-source BFS, and the 4/8-directional patterns behind island-counting and rot-spreading problems.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

Module A-9 — Grid Traversal Patterns: Flood Fill and Multi-Source BFS

What this module covers: Modules A-4 and A-5 already used a grid as an implicit graph informally, in passing. This module makes that framing explicit and puts it to work on two named patterns: flood fill (the algorithm behind an image editor's paint bucket tool) and multi-source BFS — starting a single breadth-first search from many points at once, which turns out to be meaningfully different from running several separate single-source searches.


A Grid Is a Graph With Implicit Edges

Every cell in a 2D grid is a node; its up/down/left/right neighbors (4-directional) — or additionally its four diagonal neighbors (8-directional) — are its edges. Nothing about DFS (Module A-4) or BFS (Module A-5) changes when applied to a grid; only how neighbors are generated changes, exactly as Module A-5's shortestPathInGrid already demonstrated.

typescript

Which set to use depends entirely on the problem's own rules — a rook moves 4-directionally, a king moves 8-directionally, and getting this wrong silently changes what counts as "connected."


Flood Fill: Paint Everything Connected

Flood fill starts at one cell and marks every cell reachable from it (through matching, connected cells) — precisely Module A-4's numIslands logic, generalized from "count islands" to "fill this specific region":

typescript

The if (originalColor === newColor) return grid guard is worth noticing specifically: without it, repainting a cell to the same color it already is would never trigger the grid[r][c] !== originalColor early return, since the color never actually changes — turning what should be a no-op into unbounded recursion. This is exactly the kind of edge case Module A-4's visited set handled explicitly for general graphs; here, the "already painted" check is the visited tracking, folded directly into the color comparison.


Multi-Source BFS: Seed the Queue With Every Source at Once

Rotting Oranges — a grid contains fresh oranges, rotten oranges, and empty cells; every minute, a rotten orange rots every fresh orange adjacent to it; find the minimum time for every fresh orange to rot (or determine it's impossible). The key move: seed the BFS queue with every initially-rotten orange simultaneously, not one at a time.

typescript

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.