Abstract

Given a partially-filled Sudoku puzzle, find a completion satisfying the usual Sudoku constraints.

  • Category: Backtracking / Constraint Satisfaction
  • Input: A partially filled grid
  • Output: A grid with all squares filled, or an indication no solution exists
  • Paradigm: Backtracking (fill in order, prune on constraint violation)
  • Typical use cases: the other canonical constraint-satisfaction example alongside 8 Queens; generalizes to Latin squares and graph-coloring-style problems

Problem Specification

  • Instance: A partially filled puzzle.
  • Solution: A grid with all squares filled with the numbers 1 through 9.
  • Constraint: No repeats of any number within a given sub-square, row, or column.
  • Decision: Find a solution satisfying the constraint (or determine none exists).

Candidate Strategies / Approaches

Exhaustive Search ✘

Try every possible digit in every blank cell, independently, then check the whole grid against all constraints at the end. For blank cells, this is — the constraints are only used to validate a finished guess, never to cut the search short.

Backtracking ✔

  • Fill in the first available cell with the least possible number, and recurse.
  • If a cell is reached that can’t be legally filled with any number, backtrack to the last decision point and try the next-largest possible number there instead (if one is available).

Key Idea

Unlike Exhaustive Search, backtracking checks the row/column/sub-square constraints as each digit is placed, not just at the end — an illegal digit is rejected immediately, so the search never wastes time filling in the other 80 cells behind a guess that was already doomed.


Pseudocode (Chosen Approach)

Algorithm 5 Sudoku Backtracking

Input: Grid GG (partially filled), cell index ii (in some fixed cell ordering)

Output: A boolean value: whether the remaining cells from ii onward can be completed

procedure SolveSudoku(G,iG, i)

if ii is past the last cell then

return true

if cell ii is already filled then

return SolveSudoku(G,i+1G, i+1)

for d=19d = 1 \dots 9 do

if placing dd at cell ii violates no row/column/sub-square constraint then

Place dd at cell ii

if SolveSudoku(G,i+1)SolveSudoku(G, i+1) is True then

return true

Remove dd from cell ii//backtrack

return false

Variables & Data Structures

NameTypePurpose
G gridThe puzzle state, partially filled
iCell indexWhich cell is currently being considered, in a fixed traversal order
dDigit, The candidate value being tried for cell i

Helper Functions / Operations Used

  • Constraint check — verify digit d doesn’t already appear in cell ‘s row, column, or sub-square; (bounded by grid size).
  • Backtrack (remove d) — undo a placement when it leads to a dead end further down the recursion, restoring the grid to try the next candidate digit.

Proof of Correctness / Optimality

SolveSudoku only ever places a digit that satisfies all three constraints at the moment of placement, so no branch it explores can violate the row/column/sub-square rules. It tries every digit at each cell in order, backtracking to try the next digit whenever a placement leads to failure further down the recursion — so every legally reachable completion is eventually tried. Since there are finitely many cells and finitely many digits per cell, and the recursion always advances to on success, the search terminates.


Time & Space Complexity Analysis

Worst-case, naive backtracking Sudoku solving remains exponential — generalized Sudoku (on an grid) is known to be NP-complete, so no polynomial-time algorithm is expected for the general case. In practice, the constraint checks prune the search so aggressively that even the hardest standard puzzles solve near-instantly; this is a case where empirical performance is far better than the worst-case bound suggests.


Drawbacks / Constraints

  • Plain backtracking can still be slow on adversarially hard puzzles without additional heuristics — e.g. picking the cell with the fewest remaining legal candidates first (minimum-remaining-values), rather than a fixed left-to-right cell order, typically prunes much faster in practice.
  • Doesn’t scale to generalized Sudoku — the general problem is NP-complete, so worst-case exponential blowup is expected as grows, regardless of how well-tuned the backtracking is.

References / Links