Skip to main content

Section 9.2 Sudoku

In this section, we explore a curious application of linear programming. Solving sudokus!

Exploration 9.2.1.

Consider the following sudoku puzzle:
A sudoku puzzle.
Note that the rules for sudoku are that we fill in each entry with an integer 1,…,9 so that:
  • Each row contains exactly one of each number.
  • Each column contains exactly one of each number.
  • Each 3Γ—3 β€œblock” contains exactly one of each number.

Remark 9.2.2.

We consider the standard sudokus like the one above an order sudoku: we have 3Γ—3 β€œblocks”, each with 3Γ—3 entries. Potential values range from 1 through 32. Other orders are also possible.
More sudokus may be found at https://www.websudoku.com/
 1 
www.websudoku.com/

Activity 9.2.3.

Consider a general order 3 sudoku puzzle. We want to define a linear maximization problem that solves a given puzzle. Let xijk=1 is a solution has value k in entry i,j (measured from the bottom left), and xijk=0 otherwise.

(a)

For the i,jth entry, write a linear equality constraint which ensures a value is chosen.

(b)

For row i, write 9 equality conditions so that this row contains one of each entry.

(c)

For column j, write 9 equality conditions so that this column contains one of each entry.

(d)

For an arbitrary block, write 9 equality conditions so that this block contains one of each entry.
Obviously, this would be an absolutely absurdly large problem to even fully write out, much less solve. We consider something simpler.

Activity 9.2.4.

Consider the following order 2 sudoku puzzle:
An order 2 sudoku puzzle.

(b)

If you really want to, solve this problem (there are 64 decision variables so...):
This provides an oppourtunity for an enteprising student to engage in some further exploration.

Project 9.2.5.

Write effecient code in Sage where one inputs a 9Γ—9 matrix representing a sudoku puzzle (with maybe 0’s for blank entries), and the code produces the appropriate linear optimization problem and solves it.
For more advanced or experienced coders, generalize this to allow the order of the sudoku puzzle to be a parameter.