Backtracking
Place four queens on a small chessboard by trying a square, checking the rules, and taking moves back when they lead nowhere. The same recipe solves many puzzles.
In 30 seconds
Backtracking builds a solution one choice at a time and checks the rules after every choice.
If a choice breaks a rule, or leaves no legal next move, it is undone and the next option is tried. When a step runs out of options, the search goes back one step further.
Because a bad partial solution is dropped at once, everything that would have been built on top of it is skipped. This pruning is why backtracking beats trying every combination, though in the worst case it can still be slow.
Backtracking, step by step
Queens go one per row, columns tried left to right; the counts are for reaching the first solution. “Full boards” means every way to put one queen in each row.
- Step 1: The puzzle. Put four queens on a 4 × 4 board so that no queen attacks another. A queen attacks along its row, its column and both diagonals. A single queen near the middle already rules out 11 of the other 15 squares, so filling the board by guesswork rarely works.
- Step 2: Build it one row at a time. Backtracking builds a solution piece by piece and checks the rules after every piece. Each row needs exactly one queen, so go row by row and take the first safe square. Row 0 gets column 0. In row 1, columns 0 and 1 are attacked, so the queen goes to column 2.
- Step 3: Dead end. Row 2 has no safe square: every column is attacked by one of the two queens. The board is only half full, yet it can never be completed. So the search stops here instead of filling in the rest. Dropping a partial solution the moment it cannot work is the heart of backtracking.
- Step 4: Undo and retry. Step back one decision: lift the last queen and move it to its next untried square, column 3. Earlier choices stay as they are. From this new position row 2 does have a safe square, column 1, so the build carries on from there.
- Step 5: Back up further. Row 3 is blocked again. Undoing row 2 does not help, because its remaining squares are attacked, and row 1 has no columns left to try. When a row runs out of options the search steps back another level, here all the way to the first queen, which moves on to column 1.
- Step 6: A solution. With the first queen in column 1 the other rows fall into place: column 3, column 0, column 2. Every row has a queen and no rule is broken, so this is a solution. To list every solution, the search would simply undo and carry on; this board has two.
- Step 7: Pruning the search tree. Every choice is a branch in a tree. A dead end cuts off the whole subtree below it without visiting it: that is pruning. Here 26 squares were tested instead of checking 256 complete boards. Sudoku, mazes, crosswords and timetables are searched the same way: choose, check, undo.
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Learn next
- Sudoku solvingConstraints, propagation and backtracking: how a program fills the grid. Intermediate
- Knapsack problemPacking the most value into a bag that is too small, one table cell at a time. Intermediate
- Travelling salesman problemThe shortest round trip through every city, and why nobody can find it fast. Intermediate
Spotted a mistake on this page?