Sudoku backtracking algorithm
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Sudoku solving is a classic constraint-satisfaction problem, and backtracking is the most practical exact algorithm for standard puzzles. The algorithm tries candidate values, recurses, and undoes decisions when constraints fail. Good implementation details, such as candidate ordering and fast validity checks, make a huge difference in runtime.
Core Sections
Model Sudoku constraints clearly
A valid Sudoku board must satisfy three constraints for every filled cell:
- value appears once per row,
- value appears once per column,
- value appears once per
3x3box.
Backtracking works by filling one empty cell at a time with candidates that satisfy all three constraints.
Baseline recursive solver
A simple implementation is easy to verify and is a good base for optimization.
This version is correct but can be slow for harder puzzles.
Improve performance with candidate sets
Repeated row and column scans are expensive. Keep row, column, and box sets for used values so validity checks are constant-time average.
With sets, adding and removing candidates during recursion is cheaper than scanning full board each time.
Use Minimum Remaining Values heuristic
Select the empty cell with fewest valid candidates first. This heuristic reduces branching early and often speeds up hard puzzles significantly.
Instead of first-empty selection, compute candidate counts for all empty cells and choose the smallest domain cell. If any cell has zero candidates, backtrack immediately.
Forward checking during assignment
After placing a number, update candidate options of related cells. If any related cell loses all candidates, undo immediately. This catches dead ends earlier than pure backtracking.
Forward checking plus MRV usually gives good practical performance without making solver too complex.
Validate board before solving
Do not run solver on invalid initial board. Pre-check duplicates in rows, columns, and boxes. Failing fast on invalid input avoids misleading recursion behavior and helps surface bad puzzle sources quickly.
Input validation should be explicit in production pipelines where puzzle data may come from external files.
Keep solver deterministic for testing
To make results reproducible, use fixed candidate order and deterministic tie-breakers in MRV selection. Deterministic behavior simplifies profiling, regression testing, and comparison across optimizations.
If you add randomization for puzzle generation workflows, keep it optional and seed-controlled.
Complexity discussion
Backtracking worst-case search is exponential, but Sudoku constraints prune aggressively in typical puzzles. Efficiency mostly depends on heuristics and validity-check implementation.
For standard 9x9 Sudoku, optimized backtracking is usually fast enough for interactive tools and APIs.
Practical testing strategy
Include test sets for:
- easy puzzles,
- hard puzzles,
- already solved boards,
- invalid boards,
- unsolvable boards.
A solver should return success with valid solution or explicit failure without mutating board irreversibly.
Common Pitfalls
- Forgetting to reset cell value during backtrack step.
- Not validating initial board and chasing impossible states.
- Using first-empty strategy only and suffering heavy branching on hard puzzles.
- Mixing solver state in globals and introducing hidden mutation bugs.
- Assuming every valid-looking puzzle has at least one solution.
Summary
- Sudoku backtracking solves puzzles by incremental assignment and undo.
- Correctness depends on row, column, and box constraint enforcement.
- MRV and forward checking dramatically reduce search effort.
- Fast validity data structures improve runtime on difficult boards.
- Input validation and deterministic tests keep solver reliable in production use.
Related reading
- Sudoku generator algorithm
- Suffix Array Algorithm
- Suffix array nlogn creation
- Suffix tree and Tries. What is the difference?
- Suggest an algorithm graph - possibly NP-Complete
- Suggest websites to practice C/C algorithms/puzzles
- Suggested algorithms/methods for laying out labels on an image
- Suggestions to learn distributed algorithms involving multi-processes for a beginner

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.