Algorithms · Visualized

Matrix: Game of Life

Matrix & 2D

Rotations, traversals, and grid searches on 2D arrays.

A zero-player cellular automaton: every cell is alive (1) or dead (0), and one generation is replaced by the next using only each cell’s eight neighbours. Time: O(rows × cols) average, O(rows × cols) — eight neighbour reads per cell worst; space: O(rows × cols) for the next-generation copy; O(1) extra if you encode both states in each cell.

Big-O: Classic O(m·n) sweeps and in-place tricks.

Matrix: Game of Life

Matrix / 2D Array

A zero-player cellular automaton: every cell is alive (1) or dead (0), and one generation is replaced by the next using only each cell’s eight neighbours. Live cells with two or three live neighbours survive, live cells with fewer than two die of loneliness, live cells with more than three die of overcrowding, and a dead cell with exactly three live neighbours is born. The subtlety is that all cells must be judged against the same previous generation, so the algorithm reads the old grid and writes a separate next grid — updating in place would let already-modified cells pollute later neighbour counts.

How Game of Life Works

  1. Copy the grid so the generation you read from and the one you write to never mix.
  2. For every cell (i, j), count the live cells among its up-to-eight neighbours, skipping positions outside the 4×3 grid.
  3. Alive with 2 or 3 live neighbours → stays alive; alive with 0, 1 or 4+ → becomes dead.
  4. Dead with exactly 3 live neighbours → becomes alive; any other dead cell stays dead.
  5. Write the verdict into the copy, never into the grid being read.
  6. Advance the generation counter and show the copy — that is one complete step of the animation.

Game of Life Worked Example

Input: {matrix: [[0, 1, 0], [0, 0, 1], [1, 1, 1], [0, 0, 0]]}

The visualizer’s default grid is four rows of three: row 0 = 0 1 0, row 1 = 0 0 1, row 2 = 1 1 1, row 3 = 0 0 0 — five live cells. Cell (0,1) has one live neighbour and dies; (0,2) has two and survives; (1,0) has three and is born; (1,1) sees five and stays dead; (1,2) has three and survives; (2,0) has one and dies; (2,1) has three and survives; (2,2) has two and survives; (3,1) has three and is born. The next generation reads 0 0 1 / 1 0 1 / 0 1 1 / 0 1 0 — six live cells, the shape having crept one row downward and one column right, exactly like the glider it is.

Matrix: Game of LifeO(m·n)
Compute the next generation from neighbour counts.
0
1
0
0
0
1
1
1
1
0
0
0
generation0
Step 1 / 14
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Game of Life: each cell survives/dies/born based on its 8 live neighbours.
Python · Matrix Algorithms in Code
def game_of_life(board):
    rows, cols = len(board), len(board[0])
    for r in range(rows):
        for c in range(cols):
            live = 0
            for dr in (-1, 0, 1):
                for dc in (-1, 0, 1):
                    if dr == 0 and dc == 0:
                        continue
                    nr, nc = r + dr, c + dc
                    if 0 <= nr < rows and 0 <= nc < cols and abs(board[nr][nc]) == 1:
                        live += 1
            if board[r][c] == 1 and (live < 2 or live > 3):
                board[r][c] = -1    # was alive, dies
            if board[r][c] == 0 and live == 3:
                board[r][c] = 2     # was dead, lives
    for r in range(rows):
        for c in range(cols):
            board[r][c] = 1 if board[r][c] > 0 else 0
    return board

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Game of Life Time & Space Complexity

BestO(rows × cols)
AverageO(rows × cols)
WorstO(rows × cols) — eight neighbour reads per cell
SpaceO(rows × cols) for the next-generation copy; O(1) extra if you encode both states in each cell

When to Use Game of Life

  • Teaching simultaneous-update semantics — the bug of updating in place is a classic interview trap.
  • Neighbour-counting grids in general: minesweeper, image smoothing, and convolution kernels use the same skeleton.
  • Simulation exercises where a cheap local rule produces surprisingly complex global behaviour.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.