← Back to the graphs page

Grid as an Implicit Graph

A 2D matrix where each cell is a node and neighbors are edges — no adjacency list ever built. The most common disguise a graph wears in interviews.

What to know

  • Number of Islands, Rotting Oranges, Word Search are all graph problems on grids.
  • Directions array [(0,1),(1,0),(0,-1),(-1,0)] replaces adjacency lists.
  • Multi-source BFS starts the queue with every source cell at distance 0.
  • Visited state can be stored in the grid itself to save memory.

In the wild: Image editing flood fill, game maps, maze solving, wildfire/epidemic spread simulation.

Algorithms to reach for

Flood fill (DFS/BFS)

O(rows · cols)

Count islands, fill regions

Multi-source BFS

O(rows · cols)

Nearest-exit / rotting-oranges style spreading

0-1 BFS (deque)

O(rows · cols)

Shortest path when edges cost 0 or 1