Published by Dimitris Kougioumtzis · Updated
Choose the empty cell with the fewest candidates. Propagate naked singles before branching, and restore failed guesses. Distinguish invalid givens, no solution, and search-budget exhaustion.
For each empty cell, remove digits already used in its row, column and box. Choose the cell with the fewest candidates, propagating naked singles before branching. Try candidate digits in ascending order and undo failed guesses.
When to use Sudoku with candidate propagation and backtracking
Teaching constraint propagation, minimum-remaining-values search and bounded backtracking on standard 9 by 9 Sudoku.
Choosing an alternative
Row-first brute force ignores candidate counts. MRV selects the most constrained cell, while naked singles are forced assignments. This is not an exact-cover or dancing-links implementation.
Worked example: input and state changes
Example input
530070000;600195000;098000060;800060003;400803001;700020006;060000280;000419005;000080079 | 2000
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Preserve nonzero givens. Choose smallest candidate sets, then lowest cell index.
State at this checkpoint
{ "values": [ 5, 3, 0, 0, 7, 0, 0, 0, 0, 6, 0, 0, 1, 9, 5, 0, 0, 0, 0, 9, 8, 0, 0, 0, 0, 6, 0, 8, 0, 0, 0, 6, 0, 0, 0, 3, 4, 0, 0, 8, 0, 3, 0, 0, 1, 7, 0, 0, 0, 2, 0, 0, 0, 6, 0, 6, 0, 0, 0, 0, 2, 8, 0, 0, 0, 0, 4, 1, 9, 0, 0, 5, 0, 0, 0, 0, 8, 0, 0, 7, 9 ], "active": [], "kind": "matrix", "columns": 9, "rowLabels": [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ], "columnLabels": [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ], "result": 0, "output": [ 5, 3, 0, 0, 7, 0, 0, 0, 0, 6, 0, 0, 1, 9, 5, 0, 0, 0, 0, 9, 8, 0, 0, 0, 0, 6, 0, 8, 0, 0, 0, 6, 0, 0, 0, 3, 4, 0, 0, 8, 0, 3, 0, 0, 1, 7, 0, 0, 0, 2, 0, 0, 0, 6, 0, 6, 0, 0, 0, 0, 2, 8, 0, 0, 0, 0, 4, 1, 9, 0, 0, 5, 0, 0, 0, 0, 8, 0, 0, 7, 9 ], "assignments": 0 }Step 13: Propagate naked single 1.
State at this checkpoint
{ "values": [ 5, 3, 0, 6, 7, 8, 0, 0, 0, 6, 0, 0, 1, 9, 5, 0, 0, 0, 1, 9, 8, 0, 4, 2, 0, 6, 0, 8, 0, 0, 7, 6, 0, 0, 0, 3, 4, 2, 6, 8, 5, 3, 7, 9, 1, 7, 0, 0, 9, 2, 0, 0, 0, 6, 0, 6, 0, 0, 3, 0, 2, 8, 0, 0, 0, 0, 4, 1, 9, 0, 0, 5, 0, 0, 0, 0, 8, 0, 0, 7, 9 ], "active": [ 18 ], "kind": "matrix", "columns": 9, "rowLabels": [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ], "columnLabels": [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ], "result": 0, "output": [ 5, 3, 0, 6, 7, 8, 0, 0, 0, 6, 0, 0, 1, 9, 5, 0, 0, 0, 1, 9, 8, 0, 4, 2, 0, 6, 0, 8, 0, 0, 7, 6, 0, 0, 0, 3, 4, 2, 6, 8, 5, 3, 7, 9, 1, 7, 0, 0, 9, 2, 0, 0, 0, 6, 0, 6, 0, 0, 3, 0, 2, 8, 0, 0, 0, 0, 4, 1, 9, 0, 0, 5, 0, 0, 0, 0, 8, 0, 0, 7, 9 ], "assignments": 13 }Step 26: Propagate naked single 3.
State at this checkpoint
{ "values": [ 5, 3, 0, 6, 7, 8, 0, 0, 0, 6, 0, 0, 1, 9, 5, 3, 0, 0, 1, 9, 8, 3, 4, 2, 5, 6, 7, 8, 5, 9, 7, 6, 1, 4, 2, 3, 4, 2, 6, 8, 5, 3, 7, 9, 1, 7, 1, 3, 9, 2, 4, 8, 0, 6, 0, 6, 0, 0, 3, 0, 2, 8, 0, 0, 0, 0, 4, 1, 9, 0, 0, 5, 0, 0, 0, 0, 8, 0, 0, 7, 9 ], "active": [ 15 ], "kind": "matrix", "columns": 9, "rowLabels": [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ], "columnLabels": [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ], "result": 0, "output": [ 5, 3, 0, 6, 7, 8, 0, 0, 0, 6, 0, 0, 1, 9, 5, 3, 0, 0, 1, 9, 8, 3, 4, 2, 5, 6, 7, 8, 5, 9, 7, 6, 1, 4, 2, 3, 4, 2, 6, 8, 5, 3, 7, 9, 1, 7, 1, 3, 9, 2, 4, 8, 0, 6, 0, 6, 0, 0, 3, 0, 2, 8, 0, 0, 0, 0, 4, 1, 9, 0, 0, 5, 0, 0, 0, 0, 8, 0, 0, 7, 9 ], "assignments": 26 }Step 39: Propagate naked single 1.
State at this checkpoint
{ "values": [ 5, 3, 4, 6, 7, 8, 9, 1, 2, 6, 7, 2, 1, 9, 5, 3, 4, 8, 1, 9, 8, 3, 4, 2, 5, 6, 7, 8, 5, 9, 7, 6, 1, 4, 2, 3, 4, 2, 6, 8, 5, 3, 7, 9, 1, 7, 1, 3, 9, 2, 4, 8, 5, 6, 9, 6, 1, 5, 3, 7, 2, 8, 0, 0, 0, 0, 4, 1, 9, 0, 0, 5, 0, 0, 0, 0, 8, 0, 0, 7, 9 ], "active": [ 56 ], "kind": "matrix", "columns": 9, "rowLabels": [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ], "columnLabels": [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ], "result": 0, "output": [ 5, 3, 4, 6, 7, 8, 9, 1, 2, 6, 7, 2, 1, 9, 5, 3, 4, 8, 1, 9, 8, 3, 4, 2, 5, 6, 7, 8, 5, 9, 7, 6, 1, 4, 2, 3, 4, 2, 6, 8, 5, 3, 7, 9, 1, 7, 1, 3, 9, 2, 4, 8, 5, 6, 9, 6, 1, 5, 3, 7, 2, 8, 0, 0, 0, 0, 4, 1, 9, 0, 0, 5, 0, 0, 0, 0, 8, 0, 0, 7, 9 ], "assignments": 39 }Step 52: Solved grid. Uniqueness was not tested.
State at this checkpoint
{ "values": [ 5, 3, 4, 6, 7, 8, 9, 1, 2, 6, 7, 2, 1, 9, 5, 3, 4, 8, 1, 9, 8, 3, 4, 2, 5, 6, 7, 8, 5, 9, 7, 6, 1, 4, 2, 3, 4, 2, 6, 8, 5, 3, 7, 9, 1, 7, 1, 3, 9, 2, 4, 8, 5, 6, 9, 6, 1, 5, 3, 7, 2, 8, 4, 2, 8, 7, 4, 1, 9, 6, 3, 5, 3, 4, 5, 2, 8, 6, 1, 7, 9 ], "active": [], "kind": "matrix", "columns": 9, "rowLabels": [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ], "columnLabels": [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ], "result": 1, "output": [ 5, 3, 4, 6, 7, 8, 9, 1, 2, 6, 7, 2, 1, 9, 5, 3, 4, 8, 1, 9, 8, 3, 4, 2, 5, 6, 7, 8, 5, 9, 7, 6, 1, 4, 2, 3, 4, 2, 6, 8, 5, 3, 7, 9, 1, 7, 1, 3, 9, 2, 4, 8, 5, 6, 9, 6, 1, 5, 3, 7, 2, 8, 4, 2, 8, 7, 4, 1, 9, 6, 3, 5, 3, 4, 5, 2, 8, 6, 1, 7, 9 ], "assignments": 51 }
Expected final state
Solved grid. Uniqueness was not tested.
Complexity and input bounds
Family: Backtracking. Time complexity: Exponential search, bounded to 2,000 assignments.
Implementation detail: Nine rows of digits use zero for an empty cell. At most 2,000 attempted assignments bound each run. Results distinguish solved (1), proven unsatisfiable (0), invalid givens (-1) and budget exhausted (-2). Downloads leave the input unchanged and restore the original board to output on every nonsuccess. One solution is returned; uniqueness is not tested.
Space complexity: O(81) board, constraint masks and traversal stack, plus saved visualization frames.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
Nine 9-digit rows separated by ;, 0 empty | optional assignment budget 0..2000. Returns first solution, not uniqueness.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
Conflicting givens differ from a valid puzzle with no solution. Exhausting a search budget does not prove unsatisfiability. Preserve givens and restore every guessed digit and constraint mask when backtracking.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // Standard 9x9 Sudoku, board has 81 row-major digits 0..9 (0 empty), budget 0..2000 attempted assignments. Returns 1 solved, 0 proven unsatisfiable, -1 invalid givens/budget, -2 budget exhausted (unknown solvability). Output capacity at least 81; on every nonsuccess output equals original board; input unchanged. MRV chooses smallest candidate count then lowest cell; digits ascending. Naked singles propagate before guesses. Returns one solution, not uniqueness. Portable versions use bit masks and an explicit search stack; Python recurses. Exponential worst-case search capped by budget, O(81) state, excluding frames. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(board, budget, output) { for (let initial = 0; initial < 81; initial++) { output[initial] = board[initial]; } if (budget < 0 || budget > 2000) { return -1; } let rowOf = Array(256).fill(0); let colOf = Array(256).fill(0); let boxOf = Array(256).fill(0); let rows = Array(256).fill(0); let cols = Array(256).fill(0); let boxes = Array(256).fill(0); let positions = Array(256).fill(0); let remaining = Array(256).fill(0); let box = 0; let p = 0; let digit = 0; let bit = 0; for (let r = 0; r < 9; r++) { for (let c = 0; c < 9; c++) { p = r*9+c; box = 0; if (r >= 3) { box = 3; } if (r >= 6) { box = 6; } if (c >= 6) { box += 2; } else { if (c >= 3) { box++; } } rowOf[p] = r; colOf[p] = c; boxOf[p] = box; digit = board[p]; if (digit < 0 || digit > 9) { return -1; } if (digit != 0) { bit = 1 << digit; if (((rows[r] | cols[c] | boxes[box]) & bit) != 0) { return -1; } rows[r] = rows[r] | bit; cols[c] = cols[c] | bit; boxes[box] = boxes[box] | bit; } } } let depth = 0; let choose = 1; let steps = 0; let best = -1; let bestCount = 10; let bestMask = 0; let mask = 0; let count = 0; while (depth >= 0) { if (choose != 0) { best = -1; bestCount = 10; bestMask = 0; for (let cell = 0; cell < 81; cell++) { if (output[cell] == 0) { mask = (~(rows[rowOf[cell]] | cols[colOf[cell]] | boxes[boxOf[cell]])) & 1022; count = 0; for (let d = 1; d <= 9; d++) { if ((mask & (1 << d)) != 0) { count++; } } if (count < bestCount) { best = cell; bestCount = count; bestMask = mask; } if (count <= 1) { break; } } } if (best == -1) { return 1; } positions[depth] = best; remaining[depth] = bestMask; choose = 0; } if (remaining[depth] == 0) { if (depth == 0) { for (let cell = 0; cell < 81; cell++) { output[cell] = board[cell]; } return 0; } depth--; p = positions[depth]; bit = 1 << output[p]; rows[rowOf[p]] = rows[rowOf[p]] & (~bit); cols[colOf[p]] = cols[colOf[p]] & (~bit); boxes[boxOf[p]] = boxes[boxOf[p]] & (~bit); output[p] = 0; } else { if (steps >= budget) { for (let cell = 0; cell < 81; cell++) { output[cell] = board[cell]; } return -2; } digit = 1; while ((remaining[depth] & (1 << digit)) == 0) { digit++; } bit = 1 << digit; remaining[depth] = remaining[depth] & (~bit); p = positions[depth]; output[p] = digit; rows[rowOf[p]] = rows[rowOf[p]] | bit; cols[colOf[p]] = cols[colOf[p]] | bit; boxes[boxOf[p]] = boxes[boxOf[p]] | bit; steps++; depth++; choose = 1; } } return 0; } |
Source
Related primary research on backtrack programs. The playground uses naked-single propagation and MRV for Sudoku; this does not attribute the original puzzle or these heuristics to the cited paper.
- Estimating the efficiency of backtrack programs
Donald E. Knuth (1975). Related research paper.