Introduction
The Sudoku puzzle has become a distraction, entertainment, and addiction for
many people. In many ways it resembles previous fads like Rubrik's
Cube and "Instant Insanity". However, in respect it is different: it is a
paper and pencil puzzle rather than being based on a piece of equipment.
It provides a nice example that teachers can use in class.
This document develops a precise mathematical definition of Sudoku.
The
[ Wikipedia sudoku entry ]
which provides a very good but informal description of the puzzle.
Here I try to define it as accurately as possible. I do this partially because I believe that the first step to solving many problem with many interconnected parts is to understand them. One way to test one's understanding is to create a mathematical model.
Writing these models also lets me test and demonstrate a language I developed (MATHS) for writing such descriptions. I found a small bug (a missing "/") in the tool I use to generate these "Sample" pages while writing this plus some motivation to improve my description of bijections for the MATHS language.
In practice, I tend to do these descriptions very quickly and roughly
with lots of crossing out and scribbling.
Sudoku Defined
Here is a picture of a simple 4 by 4 Sudoku:
Here is a link to a larger 9><9 puzzle
[ http://www.websudoku.com/ ]
on a popular "Web Sudoku" site.
Here is one from the flyer for this seminar:
In all rectangular Sudoku the large rectangle is divided into small regions each of which is a smaller rectangle of square boxes. In the 4><4 case there are 4 regions each containing 4 squares. In the 9 case there are 9 regions (in a kind of 3><3 tic-tac-toe grid) each containing a smaller 3><3 grid of square. A 6><6 puzzle has a 3><2 grid of regions and each region is a 2><3 grid of squares. There is horribly big 16><16 form of the puzzle with 16 regions and each region containing 16 (4><4) squares.
Typically most of the squares are blank and some of them contain a single digit. The puzzle is to fill in the blank squares with digits:
| Size | Region | |Regions| | Digits |
|---|---|---|---|
| 4><4 | 2><2 | 4 | 1,2,3,4 |
| 6><6 | 3><2 | 6 (2><3 layout) | 1,2,3,4,5,6 |
| 9><9 | 3><3 | 9 | 1,2,3,4,5,6,7,8,9 |
| 16><16 | 4><4 | 16 | 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F |
The elegance and power of the puzzle comes from the fact that the number of rows in the big grid is precisely equal to the number of columns and equal to the number of squares in each region. With out this restriction the Rules_0 can not be applied.
In general we start with the number of rows and columns in each region:
Each region has r rows and c columns, so the size of a region is
These regions are also organized in a grid with c rows and r cols. So the total number of squares is N*N.
This is also the number of digits:
And we have precisely N of them:
Now each r><c region is one element in a c >< r grid of regions(note2). So the whole grid has N^2 square in it and we want to find a digit to put in each one.
A grid is defined mathematically as a Cartesian (as in Rene Descartes) grid like many American cities. Symbolically,
So
| (i,j) | j=1 | j=2 | j=3 | j=4 |
|---|---|---|---|---|
| i=1 | (1,1) | (1,2) | (1,3) | (1,4) |
| i=2 | (2,1) | (2,2) | (2,3) | (2,4) |
| i=3 | (3,1) | (3,2) | (3,3) | (3,4) |
| i=4 | (4,1) | (4,2) | (4,3) | (4,4) |
Indeed
The placing of digits is modeled as a mapping (or function) from the Grid into the Digits:
Or using a table:
| 1 2 3 4 |
| 1 2 3 4 |
| 1 2 3 4 |
| 1 2 3 4 |
However this is not valid sudoku board. To define what makes a board valid (Rules_0), we first need to defines the rows, cols, and regions of a given board.
So, for example:
| 1 | 2 |
| 2 | 1 |
Rows and columns are not difficult to define:
(note for formalists: b.row(i) is one of many ways of writing (row(b))(i). row: Board->(Row->Digits) )
Defining the regions is a little trickier,
(I hope I got the above formula right... otherwise
[click here
if you can fill this hole]
)
Here, box : Board->((1..c)><(1..r)->((1..r)><(1..c)->Digits)).
We can now express the idea that a list has each digit once and once only. Since this must define a one-to-one map from Row to Digit (symbolized "Row---Digit" in MATHS, see [ A---B in logic_5_Maps ] ).
A note on terminology above: When one to one maps from a set into itself is called a permutation. With most sudoku the Digit = Row = Col (this is not true when N=16). And this is where the names perm_row and perm_col come from.
Sudoku Simply but Slowly Solved
Prolog is a useful prototyping tool. You can express the
problem in the language and it often gives a way to solve the
problem. As I expected this work well with small (4><4) examples
[ ../cs320/prolog/sudoku4.plg ]
but for normal size problems the program
[ ../cs320/prolog/sudoku9.plg ]
takes hours to run.
The reason is simple. They both work by generating
every possible permutation of the rows in turn. There are
N! ( 1*2*3*4* ... *N) permutations of one row and N rows. So the
number of cases tried is
(N!)^N
| N | N! | (N!)^N | Approximately | Times |
|---|---|---|---|---|
| 1 | 1 | 1 | 1 | |
| 2 | 2 | 4 | 4*10^0 | |
| 3 | 6 | 216 | 2*10^2 | |
| 4 | 24 | 331,776 | 3*10^5 | <0.1 second |
| 5 | 120 | 24,883,200,000 | 2*10^11 | |
| 6 | 720 | ... | 10^17 | |
| 7 | 5,040 | ... | 10^26 | |
| 8 | 40,320 | ... | 10^37 | |
| 9 | 362,880 | ... | 10^50 | 3..4 hours |
| ... | ... |
Standard computer science predicts that the time will be dominated by the above numbers. The actual times on a 9><9 even with many speed ups is too slow to be useful.
Solving by Hand
I use some strategies that are quite well known.
One is to list the possible digits for
each square that does not have a known digit
(Marking up in the
[ Wikipedia sudoku entry ]
). The second
is looking at each digit and its pattern of occurrence
(Cross-hatching in the
[ Wikipedia sudoku entry ]
).
Tracking the possible digits in MATHS would be described as a grid of sets of digits:
As each digit is determined then it is removed from all sets in the same row, the same column, and the same region.
Demo TBD.
I've found a spreadsheet is quite helpful to keep track of these. I've also found a spread sheet a handy way to handle the cases where I have to guess what is in a square and have 2 or 3 options: you can copy and paste the partly solved grid and do each possibility in a separate grid.
I have another strategy that works better than I'd expect. I pick one digit to focus on -- say 7. I can look at the grid and see all the 7's. Often a row of three regions has two regions that have a 7 and one without. For example
| _ | _ | 1 | _ | _ | 7 | _ | _ | 2 |
| _ | _ | _ | _ | _ | _ | 7 | _ | _ |
| 2 | _ | 3 | _ | _ | _ | _ | _ | _ |
A similar pattern can be found with a column of regions.
By hand I solve tough 9><9 is 2 or 3 days of spare time. A gentle 9><9, on a Palmtop takes an hour or so. But I'm still learning.
Links
(Wikipedia sudoku entry): An entry in the Wikipedia describing Sudoku
[ Sudoku ]
(Wikipedia): A social encyclopedia
[ http://www.wikipedia.org/ ]
(MATHS): My language for writing mathematical and logical descriptions
[ ../maths/ ]
Notations and Notes
(note2): I use "><" for Cartesian grids and products(MATHS).
(map): "map" is a MATHS key word used to describe functions and mappings.
As an example, map[x](x*x) is the function that squares a number. The
set of maps(functions) from set A to set B is indicated A->B. One-one
maps: A---B.
[ ../maths/intro_function.html ]
(Seminar): Oct 28th 10-12 Jack Brown Hall CSUSB
. . . . . . . . . ( end of section Sudoku -- an Example of Applied Logic and Mathematics) <<Contents | End>>