Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Solutions · 10. Problem reduction: exact cover with Algorithm X

School of Engineering, Nazarbayev University

Worked solutions to the three exercises of Lecture 10. Each solution is computed here and checked with assert statements against an exact identity or an independent calculation. The first code cell sets up the notebook and runs Demonstration 1 of the lecture without showing its output, because some of the solutions use its variables.

Exercise 1

Problem. Compile 1×21\times 2 domino and 3-cell L-tromino tilings of 4×44\times 4 boards into exact_cover_algorithm_x. Verify (a) Kasteleyn’s exact count of 36 domino tilings on 4×44\times 4, (b) 0 domino tilings when opposite same-color corners (0,0) and (3,3) are deleted vs. >0 when (0,0) and (0,3) are deleted, and (c) exact L-tromino tilability of all 16 single-defect 4×44\times 4 boards.


Exercise 1: Domino and L-tromino exact cover + mutilated checkerboard parity invariant
======================================================================================
Intact 4x4 domino tilings          : 36 (Kasteleyn analytical formula = 36.0)
Mutilated same-color (0,0)&(3,3)   : 0 solutions (W=6, B=8 parity obstruction)
Mutilated opposite-color (0,0)&(0,3): 12 solutions
Golomb L-tromino tilability verified across all 16 single-cell defect positions on 4x4

Explanation. Coloring cell (r,c)(r, c) by (r+c) mod 2(r + c) \bmod 2 forces every 1×21 \times 2 domino to cover one white and one black cell (Δ(W−B)=0\Delta(W - B) = 0). Deleting opposite corners (0,0) and (3,3) removes two white cells (0+00 + 0 and 3+33 + 3 are both even), leaving 6 white and 8 black cells. Seven dominoes would have to cover 7 white cells, so no tiling exists.


Exercise 2

Problem. Verify all ValueError input guards on exact_cover_algorithm_x, reduce_n_queens_to_exact_cover, and reduce_latin_square_completion, then demonstrate (a) why making NN-Queens diagonals primary constraints returns 0 solutions for all N≥2N \geq 2, and (b) how omitting cell(r,c) constraints in Latin-square reduction produces multi-symbol cell collisions.


Exercise 2: Failure-mode audit — guards, primary-diagonal bug and missing cell constraints
==========================================================================================
Guard checks passed : 6/6 invalid Exact Cover / N-Queens / Latin inputs raised ValueError.
Bug (a) Primary diagonals on 8-Queens : 0 solutions (2*8 - 1 = 15 diagonals cannot all hold 1 of 8 queens)
Bug (b) Omitting cell(r,c) on 3x3     : Produced 188 corrupted grids stacking multiple symbols in one cell (e.g. ['(0,0)=1', '(1,1)=1', '(2,2)=1', '(0,0)=2'])

Explanation. (a) An N×NN \times N board has 2N−12N - 1 main diagonals, yet row/column uniqueness allows at most NN queens on the entire board. Marking diagonals primary demands 2N−12N - 1 queens simultaneously with NN queens—an instant pigeonhole contradiction for every N≥2N \geq 2. (b) Without cell(r,c) primary columns, placing 1, 2, and 3 all inside (0,0), (1,1), and (2,2) while leaving the other 6 cells blank satisfies every row(r,v) and col(c,v) sum.


Exercise 3

Problem. Implement reduce_mini_sudoku_4x4(clue_grid, max_solutions=300) compiling 4×44\times 4 Mini-Sudoku into 64 primary columns (16 cell + 16 row + 16 col + 16 box) and solving via exact_cover_algorithm_x. Verify: (i) an empty 4×44\times 4 grid has exactly 288 valid Sudoku completions, (ii) a 6-clue puzzle has two completions and becomes unique (solution_count == 1) when a seventh clue is added, and (iii) a box-conflict grid yields 0 solutions.


Exercise 3: Implement it yourself — compiling 4x4 mini-Sudoku into 64-column exact cover
========================================================================================
Total valid 4x4 Mini-Sudoku boards : 288 (exact combinatorics = 288)
7-clue unique puzzle solved in     : 17 search nodes ->
[[1 2 3 4]
 [3 4 1 2]
 [2 1 4 3]
 [4 3 2 1]]
2x2 box-conflict detection         : 0 solutions returned in 1 root check

Summary. Upgrading a Latin-Square solver into a full Sudoku solver needs no change to exact_cover_algorithm_x, only the addition of 'box(b, v)' to each candidate tuple. The value of solution_count also classifies the puzzle (0 = inconsistent clues, 1 = well-posed unique puzzle, >1 = under-constrained).