Skip to Content
PGF
Session 4

4.1What is WFC?

Superposition, collapse, propagation and contradiction.

Markov Junior example Github: Markov Junior Website: Markov Junior

Wave Function Collapse (WFC) solves a grid the way people solve Sudoku: write down every possibility, fill in the most constrained cell, cross out what that rules out, repeat. Maxim Gumin published in 2016 and borrowed the name from quantum physics, but nothing in it is quantum. It is a constraint solver with a good story.

Four words carry the whole lesson:

WordMeaning in WFCMeaning in Sudoku
SuperpositionEvery cell starts holding the full list of tiles it could becomePencil all nine digits into every empty square
CollapsePick one undecided cell and fix it to a single tileWrite a digit in ink
PropagationRemove every option in the neighbours that no longer fits, and keep spreading while anything changesCross the digit out of its row, column and box, then follow the knock-on effects
ContradictionSome cell is left with no options at allA square where no digit fits: rub it all out and start again

Solve a Sudoku Wikihow: Solve a Sudoku

Which cell to collapse next matters. WFC always picks the cell with the fewest options left, its lowest entropy. Deciding the most constrained cell first, while it still has a choice, keeps contradictions rare. Ties are broken at random, and that randomness is where the variety comes from.

Solve a Sudoku

The tiles never know about the whole design. Each tile only states what sits on each of its edges, called sockets: a pipe or nothing. Two tiles may touch when the sockets on their shared edge are equal.

A first exercise without a computer

Draw a 4 × 4 grid, pencil the six tile shapes into every cell, and run the algorithm by hand for five steps.

The algorithm step by step

After one set-up pass, the solver runs a three-step loop until every cell is decided or one runs out of options.

Two loops sit around that work. The inner loop runs once per collapse, about one per cell: find the lowest-entropy cell, collapse it, then propagate. The outer loop restarts the whole grid with a new seed, and is rare with a forgiving tile set. With Border on, the edge cells lose their outward-facing tiles straight after Superposition, and that change is propagated before the first Observe.

Propagate in plain words

Keep a stack of cells that just changed. Take one off. For each of its neighbours, collect every tile allowed next to any option still left in this cell, and keep only those in the neighbour. If the neighbour lost anything, push it on the stack, because its own neighbours may now be affected. Stop when the stack is empty, or report a contradiction the moment a cell runs out of options.

for each direction d:
    allowed = union of rules[t][d] for every t still in Wave[i]
    Wave[j] = Wave[j] ∩ allowed
    if Wave[j] shrank: push j
    if Wave[j] is empty: contradiction

In 2D there are four directions. In 3D the same loop runs over six. The rule does not change.

Check yourself

Your answers stay in this browser.

  1. What is a collapse?