Graph Traversal (BFS & DFS)

Breadth-first and depth-first search reach everything connected to a start, and mark each node the moment they find it, so nothing is visited twice.

Number of islands: one flood fill per island

StopGrid
grid
1100100100110000
now at—
queue
empty
islands0

1/34islands = 0, and no cell has been seen. The scan reads the 4 × 4 grid row by row.

  • CH4 queue
Typical cost O(V + E) time, O(V) spaceFull lesson: Number of Islands →

When to reach for it

  • The input is a grid, a tree, or a list of what connects to what.
  • You're asked what's reachable, how many groups there are, or how far apart two things are.
  • Without marking, you'd reach the same cell or node from several sides.
  • A copy or a count has to follow every edge once.

What stays true

A cell is marked as it's queued, and the check that queues a neighbor skips seen cells. So a cell reachable from two sides is queued by whichever gets there first, and never again.

Nothing is queued twice

StopGrid
InvariantEvery cell in the queue is already marked seen.✓ Holds
11000110000010000011
now at—
queue
empty
islands0

1/42islands = 0, and no cell has been seen. The scan reads the 4 × 5 grid row by row.

  • CH4 queue

Break it

Each input here has tripped up a version of this code. The stage runs the one that gets it right.

Code that also checks the four diagonals joins these five cells into one island. Up, down, left and right keep them apart.

Number of islands on the grids that trip it up

StopGrid
101010101
now at—
queue
empty
islands0

1/31islands = 0, and no cell has been seen. The scan reads the 3 × 3 grid row by row.

  • CH4 queue
Answer5Steps31

Variants

Level order

22/22
StopBinary tree
3, visited39, visited920, visited2015, visited157, visited7
  • this step
  • in the queue
  • done, in order
  • not reached
queue
empty
result
  1. [3]
  2. [9, 20]
  3. [15, 7]
  • CH4 queue

Breadth-first on a tree: drain one level's worth of the queue at a time.

Step through it →

Maximum depth

26/26
StopBinary tree
3, active, node, returned 33N9, visited, returned 1920, visited, returned 22015, visited, returned 1157, visited, returned 17
now at3
left9
right20
returns3
  • this step
  • on the call stack
  • done, in order
  • not reached
call stack
bottom
  1. max_depth(3) · left 1 · right 2 → 3
top
  • CH1 node
  • CH4 call stack

Depth-first on a tree: each node returns one more than its deeper subtree.

Step through it →

Invert a binary tree

29/29
StopBinary tree
4, active, node4N2, visited27, visited71, visited13, visited36, visited69, visited9
now at4
left7
right2
  • this step
  • on the call stack
  • done, in order
  • not reached
call stack
bottom
  1. invert_tree(4) → 4
top
  • CH1 node
  • CH4 call stack

Visit every node once and swap its two children.

Step through it →

Clone graph

20/20
StopGraph
1, visited12, visited23, visited34, visited4
graph
1′, the clone of 11′2′, the clone of 22′4′, the clone of 44′3′, the clone of 33′
clones
now at—
  • this step
  • in queue
  • done
  • not reached
  • found by
  • looked along
queue
empty
visit order
  1. 1
  2. 2
  3. 4
  4. 3
clones4
  • CH4 queue

Breadth-first with a map: make each node's clone the moment it's first seen.

Step through it →

The code

The same algorithm in plain Python. The lit line is the one the current frame came from.

Number of islands: one flood fill per island

StopGrid
1100100100110000
now at—
queue
empty
islands0

1/34islands = 0, and no cell has been seen. The scan reads the 4 × 4 grid row by row.

  • CH4 queue
number_of_islands.py
from collections import dequedef num_islands(grid):    rows, cols = len(grid), len(grid[0])    seen = set()    islands = 0    for r in range(rows):        for c in range(cols):            if grid[r][c] != "1" or (r, c) in seen:                continue            islands += 1            seen.add((r, c))            queue = deque([(r, c)])            while queue:                row, col = queue.popleft()                for nr, nc in ((row + 1, col), (row - 1, col), (row, col + 1), (row, col - 1)):                    if 0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == "1" and (nr, nc) not in seen:                        seen.add((nr, nc))                        queue.append((nr, nc))    return islands

Drill it