Algorithms / Sudoku solving

Sudoku solving

How a program solves Sudoku: rule out what each cell cannot be, fill what is forced, and guess and back up only when it must. Shown on a 4×4 grid.

Intermediate7 steps~7 min
Given — a digit printed in the puzzle
Guess — a digit tried, which may be undone
Forced cell — only one candidate left, so it is certain
Dead end — an empty cell that no digit fits
// THE SHORT VERSION

In 30 seconds

A Sudoku is a set of constraints: every row, column and box must hold each digit once. For any empty cell, the digits not yet ruled out are its candidates.

The simple way to solve it is backtracking: guess a candidate, move on, and when some cell has no candidate left, undo the last guess and try the next.

Good solvers guess far less. They fill every cell that has a single candidate, let each fill rule out digits nearby, and when they must guess, they pick the cell with the fewest candidates.

Sudoku solving, step by step

STEPS01 / 07

This small puzzle falls to propagation alone, so the guessing is shown with a plain solver. Hard 9×9 puzzles need both: propagate, guess, propagate again. r1c3 means row 1, column 3.

STEP 01 / 07 The puzzle and its rules
THE MATH
each row: 1 2 3 4 once each column: 1 2 3 4 once each box: 1 2 3 4 once
  1. Step 1: The puzzle and its rules. A small Sudoku: a 4×4 grid, six digits given, ten cells to fill. Three rules, called constraints, limit every cell: its row, its column and its 2×2 box must each hold 1, 2, 3 and 4 exactly once. Solving means finding the one filling that breaks no rule.
  2. Step 2: Candidates for one cell. Before guessing, ask what a cell can still be. Take the top-left cell, r1c1 (row 1, column 1). Its row already holds a 4 and its column a 1, so those are out. What remains, 2 or 3, are its candidates. The rules have halved the options without a single guess.
  3. Step 3: Guess, and hit a dead end. The simplest solver walks the cells in reading order and writes the smallest candidate in each. It puts 2 in r1c1, then 1 in r1c2. No rule is broken yet, but the third cell is now impossible: 1, 2 and 4 are in its row and 3 is in its column. A dead end.
  4. Step 4: Back up, try the next. Backtracking means undoing the most recent guess and trying its next candidate. The solver erases the 1 in r1c2 and writes 3 instead. Now 1 is free for r1c3 and the walk continues. Tried in order like this, every combination gets its turn, so the answer is always found, though it can take very long.
  5. Step 5: Constraint propagation. A smarter solver starts again and writes the candidates of every open cell first. Four cells have only one, so they are no guesses: fill them. Each filled digit is then removed from the candidates in its row, column and box, which can leave new cells with one candidate. That spreading of consequences is constraint propagation.
  6. Step 6: Fewest candidates first. Which cell should a solver work on next? The one with the fewest candidates. Here each cell shows how many it has. A count of 1 is certain. A count of 2 is a guess that can be wrong once. The plain solver began at r1c1, a 2, while four 1s were waiting, and paid for it with a dead end.
  7. Step 7: Solved without a guess. Fill the four forced cells and five more become forced; fill those and the last cell follows. The small numbers show the pass in which each cell fell. This puzzle needed no guessing. Harder ones stall, and then the solver guesses in the cell with the fewest candidates, propagates again, and backs up if it reaches a dead end.
VALUES SO FAR
grid4×4, 6 givensinput
r1c1····options
dead end····backtrack
retry····undo
forced····certain
pick····rule
guesses····result
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

// AFTER

Learn next

Nothing builds on this one yet. See every algorithms concept.

Spotted a mistake on this page?