6. Constraints & Backtracking

What happens when WFC paints itself into a corner -- failure recovery, constraint propagation strategies, and how to make collapse more robust.

When collapse fails

Every WFC run carries a risk: the algorithm picks a tile that locally satisfies all adjacency rules but globally leaves some cell with zero remaining options. That cell's superposition is empty -- the wave function has contradicted itself.

A naive implementation simply restarts from scratch. That works for small grids but becomes painful as the output grows. This article explores smarter ways to handle -- and prevent -- contradictions.


Constraint propagation

After collapsing a cell, WFC propagates the consequences: neighbouring cells lose any option that would violate an adjacency rule. The simplest strategy is arc consistency (AC-3) -- iterate over every neighbour, remove invalid options, and if anything changed, re-check their neighbours.

Propagation is the inner loop of WFC. Two things control how well it works:

  1. Propagation depth -- how far from the collapsed cell do we push updates? Full propagation (until no more removals) is thorough but slow. Bounded propagation trades completeness for speed.

  2. Constraint ordering -- checking the most-constrained neighbours first detects contradictions earlier, avoiding wasted work.

Interactive

Watch propagation spread

Click a cell to collapse it, then step through propagation one neighbour at a time. Highlighted cells show the current wavefront; dimmed numbers show how many options remain.

Click any cell to collapse it, then step through propagation.

Backtracking

Instead of restarting on contradiction, we can undo the most recent collapse and try a different option.

Simple chronological backtracking

Roll back the last collapsed cell, remove the option that led to the contradiction, and re-propagate. If that cell now has zero options too, continue unwinding.

This is easy to implement: keep a stack of (cell, chosen option, grid snapshot) frames. On contradiction, pop and retry.

Backjumping

Chronological backtracking can waste time undoing cells that had nothing to do with the failure. Backjumping analyses the conflict to identify which earlier decision actually caused the empty cell, and jumps straight back to it.

Interactive

Backtracking in action

Watch WFC hit a contradiction, then recover by unwinding decisions. The decision stack on the right shows which cells were collapsed and in what order. Red frames are rolled back on contradiction.

Ready. Press Step or Run.

Lookahead

Rather than waiting for a contradiction and reacting, we can try to prevent it. Before committing to a collapse, tentatively propagate each candidate option and check whether any cell ends up empty. If so, eliminate that candidate before it causes damage.

Lookahead is expensive -- it multiplies propagation work by the number of candidates -- but for tricky tilesets it dramatically reduces restarts.


Weighted constraints

Not all adjacency rules are equally important. WFC can assign weights to tile options so that the algorithm prefers certain patterns while still respecting hard constraints. Weights influence which option is chosen during collapse (the random sampling step) without changing what is allowed.

Combined with backtracking, weighted constraints let you bias output toward desirable patterns while maintaining correctness.


Putting it together

The best WFC implementations layer these techniques:

Technique Cost Benefit
Full AC-3 propagation Medium Fewer contradictions
Chronological backtracking Low No full restarts
Backjumping Medium Faster recovery
1-step lookahead High Far fewer contradictions

The interactive demo below lets you toggle each strategy and watch how it affects collapse success rate and speed on a tricky tileset.

Interactive

Strategy comparison

Run 50 WFC solves with each strategy and compare success rates, average steps, and backtracks. The tricky tileset is deliberately constrained to force frequent contradictions.

Press "Run comparison" to start.

Next up

With robust failure recovery in hand, the next article looks at performance -- entropy heuristics, spatial indexing, and other tricks that make WFC fast enough for real-time use.