Sudoku Maker

Create your own puzzle. Click button --->

Read about this Sudoku puzzle program below


Sunday, February 12, 2006

Preventing Multiple Puzzle Solutions

I've released version 2.0 of my Sudoku Maker program. When I first coded the program, I assumed the hard part was finding all the numbers to make up a Sudoku table. The problem of which squares to blank out seemed easy. After some reading and studying, I realize this is backwards. The hard part is determining which squares to blank out or mask.

The first thing I realized that the hardness level is not determined by only the number of masked squares. Two puzzles can have the same number of masked squares and be drastically different in difficulty. My program determines which numbers to mask in a random method and the easier levels mask fewer numbers. In future versions I'll try to develop better algorithms.

Random masking has a more serious problem in Sudoku puzzles. That problem is the possibility that a puzzle could have multiple solutions. In this second version of my program, I've added two functions that greatly reduce the chance of multiple solutions.

After version 1.0 I noticed two cases that can cause multiple solutions. First is when there are four numbers in two separate 3x3 boxes that are masked. This is shown in example one below. The multiple solutions have the 8 and the 1 in bold. In this example the 8 and the 1 for both columns can be flipped. This flipping maintains the 3x3 box and column requirements of no repeat numbers. By flipping the 8 and 1 in both columns, the row requirement is maintained.

These 2x2 combinations can also appear from top and bottom in addition to left and right. Also, these are quite common. To ensure a unique solution, my version 2.0 program looks for these 2x2 combinations after I generate the 9x9 numbers. I then randomly pick one of the four numbers that cannot be masked. If at least one of the four numbers is not masked, only one solution is possible.

After some more thought I realized that other combinations may cause similar multiple Sudoku solutions. After a lot of experimentation, I came up with a 3x3 combination that can cause multiple solutions that is similar to the 2x2 box. This is shown in example two below. In this case, a column of 3 numbers exist in 3 different 3x3 regions. If all 9 numbers are masked, there exists 6 possible solutions to the Sudoku puzzle. As before this can be prevented my ensuring at least one of the nine numbers are masked. As you would suspect, this 3x3 combination is much less likely than the 2x2 combination.

As more and more squares are masked, the chance of multiple solutions is likely to rise. I'm sure there are likely other combinations that I have not considered that could create multiple solutions. If you know of other combinations that can create multiple solutions, please leave a comment.

Example One - 2x2 Combination

The puzzle to the right has
has 2 solutions due to the
4 specific blank squares.
The two tables below show
the 2 solutions. The top and
bottom 8 and 1 can be
interchanged and still meet
the row, column and
3x3 box requirements
of Sudoku.
  
3 7 8  6 5 9  2 1 4
1 5 4  3 2 8  7 6 9
6 2 9  4 7 1  5 3 8

7 4 3      5  6 9 2
8 6 2  9 4 7  3 5 1
9 1 5  2 3 6  8 4 7

4 8 7  5 9 3  1 2 6
2 3 6      4  9 7 5
5 9 1  7 6 2  4 8 3
3 7 8  6 5 9  2 1 4
1 5 4  3 2 8  7 6 9
6 2 9  4 7 1  5 3 8

7 4 3  1 8 5  6 9 2
8 6 2  9 4 7  3 5 1
9 1 5  2 3 6  8 4 7

4 8 7  5 9 3  1 2 6
2 3 6  8 1 4  9 7 5
5 9 1  7 6 2  4 8 3
  
3 7 8  6 5 9  2 1 4
1 5 4  3 2 8  7 6 9
6 2 9  4 7 1  5 3 8

7 4 3  8 1 5  6 9 2
8 6 2  9 4 7  3 5 1
9 1 5  2 3 6  8 4 7

4 8 7  5 9 3  1 2 6
2 3 6  1 8 4  9 7 5
5 9 1  7 6 2  4 8 3

Example Two - 3x3 Combination

The puzzle to the right has
6 solutions. The numbers
2, 5 and 7 are required
in all three blank columns.
Each column must have a
different order of these
numbers to maintain the
row requirement. Each
column may contain any of
the 3 combinations just so
each column has a
different order.
  
3   8  6   9    1 4
1   4  3   8    6 9
6   9  4   1    3 8

7 4 3  1 8 5  6 9 2
8 6 2  9 4 7  3 5 1
9 1 5  2 3 6  8 4 7

4 8 7  5 9 3  1 2 6
2 3 6  8 1 4  9 7 5
5 9 1  7 6 2  4 8 3
3 7 8  6 5 9  2 1 4
1 5 4  3 2 8  7 6 9
6 2 9  4 7 1  5 3 8

7 4 3  1 8 5  6 9 2
8 6 2  9 4 7  3 5 1
9 1 5  2 3 6  8 4 7

4 8 7  5 9 3  1 2 6
2 3 6  8 1 4  9 7 5
5 9 1  7 6 2  4 8 3
  
3 7 8  6 2 9  5 1 4
1 5 4  3 7 8  2 6 9
6 2 9  4 5 1  7 3 8

7 4 3  1 8 5  6 9 2
8 6 2  9 4 7  3 5 1
9 1 5  2 3 6  8 4 7

4 8 7  5 9 3  1 2 6
2 3 6  8 1 4  9 7 5
5 9 1  7 6 2  4 8 3
3 5 8  6 2 9  7 1 4
1 2 4  3 7 8  5 6 9
6 7 9  4 5 1  2 3 8

7 4 3  1 8 5  6 9 2
8 6 2  9 4 7  3 5 1
9 1 5  2 3 6  8 4 7

4 8 7  5 9 3  1 2 6
2 3 6  8 1 4  9 7 5
5 9 1  7 6 2  4 8 3
  
3 5 8  6 7 9  2 1 4
1 2 4  3 5 8  7 6 9
6 7 9  4 2 1  5 3 8

7 4 3  1 8 5  6 9 2
8 6 2  9 4 7  3 5 1
9 1 5  2 3 6  8 4 7

4 8 7  5 9 3  1 2 6
2 3 6  8 1 4  9 7 5
5 9 1  7 6 2  4 8 3
3 2 8  6 7 9  5 1 4
1 7 4  3 5 8  2 6 9
6 5 9  4 2 1  7 3 8

7 4 3  1 8 5  6 9 2
8 6 2  9 4 7  3 5 1
9 1 5  2 3 6  8 4 7

4 8 7  5 9 3  1 2 6
2 3 6  8 1 4  9 7 5
5 9 1  7 6 2  4 8 3
  
3 2 8  6 5 9  7 1 4
1 7 4  3 2 8  5 6 9
6 5 9  4 7 1  2 3 8

7 4 3  1 8 5  6 9 2
8 6 2  9 4 7  3 5 1
9 1 5  2 3 6  8 4 7

4 8 7  5 9 3  1 2 6
2 3 6  8 1 4  9 7 5
5 9 1  7 6 2  4 8 3

5 Comments:

At 12:28 PM, June 16, 2006, Anonymous Anonymous said...

I am running into this exact problem with my sudoku puzzle generator. What I did was took the algorithm for generating valid sudoku puzzles and stored about 100,000 records in a database. Then when the user clicks on "create a new puzzle," I randomly pull from one of the 100,000 pre-made puzzles.

Then I thought, "hey, I'll just randomly mask the cells and I've got myself a workable puzzle generator!" Wrongo. People who tested it kept coming up with the "wrong" solution (i.e. not the one I had stored in the database). Their combinations would be valid puzzles, just not the one I had stored.

So now I'm trying to rethink how to approach the masking. Your post has been enlightening. Good(?) to know there's someone else struggling with this!

 
At 9:34 AM, September 05, 2006, Blogger Riz said...

Happy I found your blog and application! Thanks.

 
At 7:04 AM, November 29, 2006, Anonymous Anonymous said...

I agree with Alberto, having exercised my solver with this example and found 12 solutions that included the given 6. My extra 6 also permute 2 5 7 and look OK. Thanks for providing the example.
Regards, Pete

 
At 2:08 AM, January 20, 2007, Anonymous Anonymous said...

Hi,

Contrary to mainstream I am specifically desiring sudokus with multiple solutions.

Therefore I am grateful for whatever advice or piece of software that would provide for this, i.e. filtering out and thrashing every single solution sudoku.

Mats Eric Olausson
mats@synergy.se

 
At 1:42 PM, February 23, 2007, Anonymous Anonymous said...

3 7 8 6 5 9 2 1 4
1 2 4 3 7 8 5 6 9
6 5 9 4 2 1 7 3 8
7 4 3 1 8 5 6 9 2
8 6 2 9 4 7 3 5 1
9 1 5 2 3 6 8 4 7
4 8 7 5 9 3 1 2 6
2 3 6 8 1 4 9 7 5
5 9 1 7 6 2 4 8 3

take:

3 7 8 6 5 9 2 1 4
1 2 4 3 7 8 5 6 9
6 5 9 4 2 1 7 3 8

Order the numbers by col like:

1 2 4 3 2 1 2 1 4
3 5 8 4 5 8 5 3 8
6 7 9 6 7 9 7 6 9

compare: 3x3

2 2 2
5 5 5
7 7 7

Set one visible if 3x3 is the same.

Greetings,

Christian
XiViX

 

Post a Comment

<< Home