WORLD 4

BACKTRACKING

Try a choice, explore everything after it, then undo it and try the next: every subset, every order, every sum.

STAGE 0 Backtracking tutorial

0:00 / 0:00

The move

Build an answer one choice at a time and undo the choices that lead nowhere. Three beats: choose (add a candidate), explore (recurse), un-choose (pop it so the next choice starts clean). Prune early when a partial answer can't work.

Spot it
The problem asks for all, every or generate, with combinations, permutations, subsets or placements, and the input is small.
Cost
Exponential: it visits every candidate, such as 2^n subsets or n! orders, so inputs stay small.

15 STAGES Backtracking problems

☆☆☆☆☆ Clear 5 to finish this world.