a jigsaw sudoku generator and...

47
A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2 nd May 2007. Supervisor: Dr. Andrea Schalk This report discusses the game of Jigsaw Sudoku. The main aim of the project is to create a program which can generate and solve Jigsaw Sudoku. Algorithms are discussed dealing with the creation of Jigsaw Sudoku puzzles. Strategies for solution of Sudoku and Jigsaw Sudoku are defined and a method of linking these strategies is dealt with. A discourse of the mathematics associated with Jigsaw Sudoku is provided, including sections on the Four-Colour Theorem and setting of hints. An analysis of the effects of the number of hints on solvability is carried out. Evaluations of the success of the project are made and extensions are indicated. A conclusion and summary of the project as a whole is provided. 1

Upload: nguyenkhanh

Post on 28-Jun-2018

407 views

Category:

Documents


17 download

TRANSCRIPT

Page 1: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

A JIGSAW SUDOKU GENERATOR AND SOLVER

Christopher Stephens.2nd May 2007.

Supervisor: Dr. Andrea Schalk

This report discusses the game of Jigsaw Sudoku. The main aim of the project is to create a program which can generate and solve Jigsaw Sudoku. Algorithms are discussed dealing with the creation of Jigsaw Sudoku puzzles. Strategies for solution of Sudoku and Jigsaw Sudoku are defined and a method of linking these strategies is dealt with. A discourse of the mathematics associated with Jigsaw Sudoku is provided, including sections on the Four-Colour Theorem and setting of hints. An analysis of the effects of the number of hints on solvability is carried out. Evaluations of the success of the project are made and extensions are indicated. A conclusion and summary of the project as a whole is provided.

1

Page 2: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

ACKNOWLEDGEMENTS

I would like to thank my supervisor and tutor Andrea Schalk for the support she has given me during my three years at university and her valuable input into this project.

I would like to thank my girlfriend, proof reader and thesaurus Lesley Campbell for managing to resist strangling me, regardless of requests for assistance at 3am.

2

Page 3: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

In memory of my dad, Trevor.

Regrettably he will not be able to attend my graduation.

3

Page 4: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

CONTENTS

Figures............................................................................................................................................ 6

Excursuses......................................................................................................................................7

Précis...............................................................................................................................................8

§1: Background............................................................................................................................101.1: Sudoku..............................................................................................................................101.2: Jigsaw Sudoku................................................................................................................. 111.3: Mathematics of Sudoku..................................................................................................121.4: The Strategies of Sudoku............................................................................................... 12

§2: Aims and Challenges............................................................................................................132.1: The Project........................................................................................................................132.2: Why?................................................................................................................................. 132.3: The Challenges................................................................................................................ 142.4: Extras.................................................................................................................................142.5: Thoughts...........................................................................................................................14

§3: The Creation.......................................................................................................................... 163.1: Shuffling........................................................................................................................... 163.2: Setting the Hints.............................................................................................................. 193.3: Counting Solutions......................................................................................................... 20

§4: The Solution...........................................................................................................................224.1: Levels of Difficulty..........................................................................................................224.2: Narrowing the Possibilities........................................................................................... 224.3: In the Game...................................................................................................................... 234.4: Novice Sudoku Strategies.............................................................................................. 23

4.4.1: Only-Possible........................................................................................................... 244.4.2: Singles....................................................................................................................... 244.4.3: Conjugate Pairs........................................................................................................ 24

4.5: Selected Harder Strategies............................................................................................. 254.5.1: Pointing Couplets.................................................................................................... 254.5.2: X-Wings.....................................................................................................................26

4.6: Jigsaw Sudoku Specific Strategies................................................................................ 274.6.1: Little Law of Leftovers............................................................................................274.6.2: Big Law of Leftovers............................................................................................... 28

4

Page 5: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

§5: Usability................................................................................................................................. 305.1: The Game......................................................................................................................... 30

5.1.1: Saving and Loading............................................................................................... 305.1.2: Clues.......................................................................................................................... 305.1.3: User-Inputted Grids................................................................................................ 31

5.2: Four-Colouring................................................................................................................31

§6: Analysis.................................................................................................................................. 336.1: Hints vs. Solvability........................................................................................................ 336.2: The 3x3 Experiment........................................................................................................ 346.3: The 4x4 Experiment........................................................................................................ 356.4: N – 1 Hints........................................................................................................................366.5: N – 2 Hints........................................................................................................................386.6: Permutations of Non-Unique Grids............................................................................. 38

§7: Evaluation and Extensions.................................................................................................. 407.1: Generating Large Grids..................................................................................................407.2: Further Strategies............................................................................................................ 407.3: Choice of Difficulty......................................................................................................... 417.4: Superfluity........................................................................................................................41

§8: The Conclusion......................................................................................................................438.1: Achievements.................................................................................................................. 438.2: Issues and Revisions....................................................................................................... 448.3: Summary.......................................................................................................................... 44

Reading List................................................................................................................................. 45

Appendix A: Results of Experiments.......................................................................................47

5

Page 6: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

FIGURES

Figure 1.1: A Sudoku Puzzle..................................................................................................... 11

Figure 1.2: A Jigsaw Sudoku Puzzle........................................................................................ 11

Figure 3.1: Pieces Sharing a Single Edge................................................................................. 17

Figure 3.2: Shuffling a 4x4 Grid................................................................................................ 18

Figure 4.1: The possibilities....................................................................................................... 23

Figure 4.2: A Sketched X-Wing................................................................................................. 26

Figure 5.1: A Comparison of a Graduated vs. a Four-Coloured Grid.................................31

Figure 6.1: Number of Hints Placed vs. Unique Solutions Generated (3x3)...................... 35

Figure 6.2: Number of Hints Placed vs. Unique Solutions Generated (4x4)...................... 36

Figure 6.3: Permutations of a and b in a Grid of Size 3......................................................... 39

Figure 7.1: Superfluous Hints................................................................................................... 42

6

Page 7: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

EXCURSUSES

Excursus 3.1: The Shuffling Algorithm....................................................................................17

Excursus 3.2: Checking Completeness of a Given Piece....................................................... 18

Excursus 3.3: Arbitrary Numbers.............................................................................................19

Excursus 3.4: To Place the Hints............................................................................................... 20

Excursus 3.4: Counting Solutions – An Example................................................................... 21

Excursus 4.1: Conjugate Pairs – An Example......................................................................... 24

Excursus 4.2: Pointing Couplets............................................................................................... 25

Excursus 4.3: X-Wings – An Example...................................................................................... 27

Excursus 4.4: The Little Law of Leftovers............................................................................... 28

Excursus 4.5: The Big Law of Leftovers...................................................................................29

Excursus 5.1: A Proof of the Four-Colour Theorem.............................................................. 32

Excursus 6.3: Proof of N – 1 Hints Leading to a Unique Solution....................................... 37

Excursus 6.4: Proof that N – 2 Hints will not Lead to a Unique Solution...........................38

7

Page 8: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

PRÉCIS

§1: BACKGROUND

This section introduces the reader to the games of Sudoku and Jigsaw Sudoku. The rules of the games are explained and uniqueness is discussed. A range of mathematical facts about Sudoku are imparted and the idea of using strategies to solve a grid is presented.

§ 2: AIMS

Here, a more specific discussion of the project shall be carried out. The main aim of the project is announced, as well as a consideration of the merits of project. Challenges identified within the project are also described.

§3 : THE CREATION

This chapter addresses the main challenges associated with the creation of a playable Jigsaw Sudoku puzzle. The problem of creating a grid of randomly shaped pieces is considered, followed by the question of setting the hints.

§4 : THE SOLUTION

Here I shall explain a selection of the strategies used in the report.To begin, there shall be an explanation of the difficulty of a strategy and the term possibility. A selection of strategies will then be outlined.

§5: USABILITY

Usability covers the extra bits of implementation which makes the game usable and playable. Firstly, the functionality of the game itself is discussed. Following this is a discourse of the Four-Colour Threorem, which I have used in the game.

§6: ANALYSIS

Here, an investigation is performed into the dependence of the uniqueness on the number of given hints. Following this a discussion of the mathematics associated with setting the

8

Page 9: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

hints is provided. Proofs and further original thoughts are outlined.

§7: EVALUATION AND EXTENSIONS

This, the penultimate chapter, provides an insight into problems faced within the project. It also describes a selection of extensions which have been thought about but not implemented.

§8: CONCLUSION

This final chapter sums-up the project as a whole. An account of the achievements is given, along with a selection of notable issues. Finally, the project as a whole is summarised and concluding thoughts given.

9

Page 10: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

§1: BACKGROUND This section introduces the reader to the games of Sudoku and Jigsaw Sudoku. The rules of the games are explained and uniqueness is discussed.

Following this, there is a look into some mathematical facts about Sudoku. These facts include the number of Sudoku grids and the number of hints needed to create a 9x9 game.

Finally the question of solving a puzzle is addressed and the idea of the strategies used in the game is approached.

1.1: SUDOKU

The Oxford English Dictionary defines Sudoku to be:

A type of logic puzzle, the object of which is to fill a grid of nine squares by nine squares (subdivided into nine regions of three by three squares) with the numbers one to nine, in such a way that every number appears only once in each horizontal line, vertical line, and three-by-three subdivision.1

To expand upon the OED:

Sudoku is a popular single player pen and paper puzzle. The aim is to complete a partially filled 9x9 grid with numbers whilst obeying the law of the game: namely that no numeral should be duplicated in any row, column or 3x3 sub-grid.

Initially the player is given a partially complete grid. The numbers within this grid are referred to as the hints. With these as a starting point the player must set out to fill in the remainder of the puzzle.

A Sudoku puzzle is considered well formed if there is a unique solution. That is to say, given an initial set of hints, there exists just one possible legal way of completing the puzzle. A unique solution for a Sudoku puzzle is obviously desirable. It removes ambiguity and, in the simplest example, it makes it possible for a newspaper to print an answer.

1 'Sudoku, n.' OED Online. December 2006. Oxford University Press. <http://dictionary.oed.com/cgi/entry/50291646> (28th March 2007).

10

Page 11: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

Figure 1.1: A Sudoku Puzzle.

1.2: JIGSAW SUDOKU

Jigsaw Sudoku is a variation of Sudoku in which the 3x3 sub-grids are replaced by irregular jigsaw pieces. Jigsaw Sudoku is know by many pseudonyms: Squiggly Sudoku, Du-sum-oh, Nononimo, Geometric Sudoku to name a few.

Figure 1.2: A Jigsaw Sudoku Puzzle.

Often published Jigsaw Sudoku puzzles have some degree of symmetry. Note that Figure 1.2 is symmetrical about 180°.

11

Page 12: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

1.3: MATHEMATICS OF SUDOKU

The set of all completed 9x9 Sudoku puzzles is a subset of a much larger set of 9x9 Latin Squares,2 in which there are no repeated digits in each row and column. Latin Squares were introduced by Leonhard Euler in 1783.3

A count of all complete 9x9 Sudoku grids was found to be 6,670,903,752,021,072,936,960, approximately 0.00012% of the total number of Latin Squares, in 2005.4 Of these 5472730538 are said to be essentially unique when symmetries are taken into account.5

The smallest number of hints found to generate a uniquely solvable 9x9 Sudoku is 17;6 this has not been proven. The smallest number of hints needed to generate a uniquely solvable 9x9 Jigsaw Sudoku has been proven to be as low as 8.7 This is discussed in 6.4.

1.4: THE STRATEGIES OF SUDOKU

Almost all Sudoku puzzles are solvable using logic alone. In most published Sudoku puzzles there will be a set of methods that enable the player to solve the game without requiring a guess.

A strategy is a method that reduces the problem faced by the player when completing a given Sudoku puzzle. An enormous number of strategies, based on logical deductions, have been created.8

There are many well documented strategies available to aid a player whilst solving a standard Sudoku puzzle. The same cannot be said for Jigsaw Sudoku; in researching the game I have only managed to find two documented Jigsaw Sudoku specific strategies.9

The strategies of Sudoku will be discussed further in Section 4.

2 Richard Turner. Sudoku Tea Talk. <http://www.gatsby.ucl.ac.uk/~turner/sudoku/Sudoku.html> (7th April 2007).3 Cut the Knot. Latin Squares. <http://www.cut-the-knot.org/arithmetic/latin.shtml> (7th April 2007).4 B Felgenhauer and F Jarvis. Sudoku Enumeration Problems. <http://www.afjarvis.staff.shef.ac.uk/sudoku/> (29th

March 2007).5 E Russell and F Jarvis. Sudoku Enumeration Problems. <http://www.afjarvis.staff.shef.ac.uk/sudoku/> (29th March

2007).6 Gordon Royle. Minimum Sudoku. <http://people.csse.uwa.edu.au/gordon/sudokumin.php> (3rd April 2007).7 Bob Harris. Du-Sum-Oh Puzzles. <http://www.bumblebeagle.org/dusumoh/proof/index.html> (2nd April 2007).8 Andrew Stuart. Sudoku Solver by Andrew Stuart. <http://www.scanraid.com/sudoku.htm> (2nd April 2007).9 Bob Harris. Du-Sum-Oh Puzzles. <http://www.bumblebeagle.org/dusumoh/8x8/tutorial/index.html> (2nd April

2007).

12

Page 13: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

§2: AIMS AND CHALLENGES The previous chapter gave an overview of the game of Sudoku. Here, a more specific discussion of the project shall be carried out.

Firstly, the principal all-encompassing aim is announced.

Following this is a brief discourse about the merits of choosing the project. This compares Sudoku and Jigsaw Sudoku puzzles. In addition, it contrasts the volume of prior research into each type of puzzle.

Next, the primary challenges identified in the project are noted and described. The secondary objectives are also covered.

Finally, a round up of the original expectations of the project is included.

2.1: THE PROJECT

It is my aim to create a computer program capable of generating and solving Jigsaw Sudoku puzzles.

2.2: WHY?

The setting and solving of traditional Sudoku has been substantially documented, but Jigsaw Sudoku is less well known.

In recent months Jigsaw Sudoku has become more prominent. Whilst researching this project I was unable to find a Jigsaw Sudoku solver on-line. Today there are a handful of Jigsaw Sudoku solvers,10 most of which are extensions to traditional Sudoku programs.

Of the solvers I have found on-line there are none that can set randomly generated games. They shun generation in favour of user-inputted grids or a selection of ready made puzzles. In this aspect, I believe that my work in generating Jigsaw Sudoku games is wholly original.

10 These include:Funahashi Study Private School, Japan. Zigzag Sudoku Solver / Irregular Number Place / Geometric <http://homepage3.nifty.com/funahashi/game/game655eng.html> (10th April 2007).Andrew Stuart. Jigsaw Solver by Andrew Stuart. <http://www.scanraid.com/jigsaw.htm> (10th April 2007).

13

Page 14: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

2.3: THE CHALLENGES

Initial research identified three major challenges:

1. Creating a grid of jigsaw pieces.

2. Placing the hints to create a uniquely solvable grid.

3. Solving the game using a bank of strategies.

Firstly a grid of jigsaw pieces is created. This is then filled with randomly shaped pieces of the correct size.

Following the setting of the shape of the puzzle, the hints are placed. It is important that the game is solvable by logic alone.

The third challenge is to create an artificial intelligence capable of solving a Sudoku puzzle. This would be very easy to implement using brute force computing, but this is not desirable. The final program must be able to explain to the user what is happening at each step in the solution process.

2.4: EXTRAS

In addition to the above challenges it is necessary to design a program that is usable. For example: it would be undesirable to have a complex program that is text-based. To this end, there are many further features which need to be created.

These features include, but are not limited to:

1. Designing an intuitive user interface.

2. Implementing useful functions (e.g. saving and loading).

3. Providing assistance when a player is stuck.

2.5: THOUGHTS

Due to the extensive scope of Sudoku, there are other possible considerations to the challenges I have presented here. Because of the constraints on the project I have to be realistically selective about what I will achieve. I feel the aims outlined above are a good and varied selection suitable for a third year project. Each of these aims demonstrates a

14

Page 15: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

different set of skills.

During the implementation it will be important to make careful decisions and to prioritise certain aspects above others. These decisions are crucial to the success of the project.

As a joint honours computer science and mathematics student I would like to draw upon my mathematical background. To this end, I shall research proofs and aim to give an analysis based around the game of Jigsaw Sudoku.

15

Page 16: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

§3: THE CREATION Two of the three significant challenges identified in the previous section are concerned with the creation of a playable Jigsaw Sudoku puzzle. This chapter addresses these two aims in detail.

The problem of creating a grid of jigsaw pieces is investigated. After the initial thoughts are addressed, an elegant solution is provided to solve the problem.

3.2 looks at setting the hints to create a uniquely solvable game. Here, the problem of counting solutions is uncovered. Lastly, thoughts are turned to resolving this problem.

3.1: SHUFFLING

The initial step in the process of creating a Jigsaw Sudoku puzzle is producing the jigsaw. My first thought was to generate random shaped pieces of the required size and then try to fit them into the board. This turned out to be much harder than I imagined.

Anyone who has played the game of Tetris will tell you how difficult space-filling problems can be. Another example of a space filling problem is the game of Pentominoes. In Pentominoes, shapes made up of five squares are attempted to be inserted into a grid. Entire computer science projects have been made to play Pentominoes.11

To create a standard Jigsaw Sudoku puzzle, pieces of size nine must be considered. Hence the idea of space-filling was discounted. An elegant and simple solution was needed.

I formulated the idea of shuffling the board. Given a grid of regularly shaped pieces (i.e. a standard Sudoku board), I found it possible to swap squares around to create a shuffled grid.

11 For Example: Robert Simms. Pentominoes. <http://www.math.clemson.edu/~simms/java/pentominoes/> (23rd April 2007).

16

Page 17: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

THE SHUFFLING ALGORITHM

for(required number of shuffles){ Select two random squares; Swap these squares; if(both pieces are still “whole”) { Re-colour grid; } else { Swap squares back; }}

Excursus 3.1: The Shuffling Algorithm.

The original attempt at creating a shuffling algorithm intended to swap squares from adjacent pieces. Finding a pair of adjacent pieces proved to be a difficult task in itself. In addition, there is no guarantee that swapping squares from neighbouring pieces will leave the two pieces complete (See Figure 3.1). Obviously pieces which are incomplete are undesirable and lead to a very confusing situation for the player.

Consider two pieces sharing a single edge, as shown in the diagram below. Evidently, swapping any two squares within these pieces will leave neither piece whole.

Figure 3.1: Pieces Sharing a Single Edge.

After a good deal of thought, the need to find adjacent pieces was deemed unnecessary. It would be sufficient to choose random pieces and random squares, then to check whether the chosen pieces would remain whole upon swapping.

17

Page 18: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

Figure 3.2: Shuffling a 4x4 Grid.

To implement the shuffling algorithm it was necessary to create a method of checking that a piece is whole. To achieve this, start with a square within the jigsaw piece, then recursively check the surrounding squares that lie within the piece. If every square in the jigsaw piece can be counted then it is whole.

CHECKING COMPLETENESS OF A GIVEN PIECE

Starting with any square within the piece.if (the given square lies within the selected piece and this square has not yet been visited){ Note that this square has now been visited; Recursively call this method with the 4 surrounding squares;}if (all squares within the selected piece have been visited){ The piece is whole and this can be returned;}

Excursus 3.2: Checking Completeness of a Given Piece.

18

Page 19: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

3.2: SETTING THE HINTS

The next step in creating a puzzle is to generate a set of hints which lead to a uniquely solvable game. The hints, as described in 1.1, are the initial numbers placed on the grid. To achieve numbers are added to the grid at random (obeying the laws of the game) and the number of possible solutions is counted.

In the first instance, the program counted solutions after placing each hint. If the count was zero then the program removed the last hint placed. Likewise, if the count was greater than one then further hints were added. Where neither of these cases were true, the count was equal to one, hence the game must have been uniquely solvable and ready to play.

This was found to be slow and impractical. Since it is the case that the number of hints has no measurable effect on the difficulty12 (further explained in 4.5), I decided that placing an arbitrary number of hints and then counting solutions would be a better method.

ARBITRARY NUMBERS

In deciding on an arbitrary number I considered formulas which give a reasonable outcome for every possible size of grid.

In my experience most published 9x9 Sudoku grids have roughly 25 hints, I based my decision on this and settled on the formula:

N 2

3 − 2 (Where N is the size of the grid)

This gives the results below:

N 3 4 5 6 7 8 9Hints 2 4 7 11 15 20 26

Excursus 3.3: Arbitrary Numbers.

Once an arbitrary number of hints have been placed, the program counts the number of solutions. If the solution is not unique, the grid will be cleared and the hints will be replaced. Given that counting solutions is considerably more complex than placing the hints, I decided that this would be preferable to the alternative of counting solutions after

12 Wikipedia. Sudoku – Wikipedia, the free encyclopedia. <http://en.wikipedia.org/wiki/Sudoku#Difficulty_ratings> (14th April 2007).

19

Page 20: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

placing each hint.

TO PLACE THE HINTS

For(Required number of hints){ Choose random number and random square; If(number and square chosen obey laws) { Place number in square; }}If(Board not solvable){ Start this process again.}

Excursus 3.4: To Place the Hints.

3.3: COUNTING SOLUTIONS

Counting solutions is a variation on the solving method. A set of possibilities for each square is generated. A possibility is a number that could be placed in its square such that the grid remains solvable.

The program must count all possible legal permutations of these possibilities. To begin, it looks at the first square with a single possibility, and inserts this into the grid. At this stage the set of possibilities for every square is re-generated.

It is not always the case that there exists a square with a single possibility; all squares may contain multiple possibilities. In this instance, a choice would be made from the first square with the smallest number of possibilities. If this choice was found to lead to a dead end then it would be removed, and another possibility from the square put in its place.

The aim of this process is to be able to fill in every square on the grid from their respective possibilities. For each combination where every square is filled the count is incremented.

The following example demonstrates this in more detail.

20

Page 21: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

COUNTING SOLUTIONS – AN EXAMPLE

In the case shown, in the top-left square (1,1) the possibles are 1 and 2.

If 1 is chosen then a 4 must be placed below it (1,2). This leads to (1,3) and (1,4) both having a 3 as a single possibility. Since (1,3) and (1,4) are in the same column this would be contradictory. Hence placing a 1 in (1,1) leads to the grid being unsolvable.

On the other hand, if a 2 is placed at (1,1), then at every subsequent step there exists a square with only one possible and the game is uniquely solvable (the solution count equals one).

This could be represented as a game tree:13

(1,1) = 1 (1,1) = 2| |

(1,2) = 2 (1,3) = 1| ⋮

(1,3) = 3 | ⋮

STUCK! (4,4) = 2 |

SOLUTION!This is a small simple case, larger cases lead to much more complex trees. Since there is just one branch that leads to a solution this grid is uniquely solvable.

Excursus 3.5: Counting Solutions – An Example.

13 Mikhael Shor. Game Tree – Game Theory .net <http://www.gametheory.net/dictionary/GameTree.html> (29th April 2007).

21

Page 22: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

§4: THE SOLUTION There are many different strategies of varying difficulty used to solve Sudoku. Here I shall explain a selection of the strategies used in the program.

To begin, there shall be an explanation of what is meant by the difficulty of a strategy. Following this, the idea of possibilities will be explored further. An understanding of possibilities is required to link the strategies together.

It is then considered how the strategies are applied in the game itself.

The selection is limited by constraints on the report. There are a huge number of known strategies for solving Sudoku; it is not feasible to explore or implement all of them in the available time.

4.1: LEVELS OF DIFFICULTY

Many people assume that the level of difficulty is dependent on the given number of hints. This is not necessarily the case; two Sudoku puzzles containing the same number of hints could be hugely different in their complexity.

The level of difficulty is dependant on the strategies needed to solve the given Sudoku puzzle. A puzzle that is solvable using just elementary strategies would be regarded as straightforward.

Due to the qualitative nature of the problem, it is not possible to grade a strategy's difficulty exactly. Saying that, I have broadly categorised a selection of strategies as easy, intermediate or hard.

4.2: NARROWING THE POSSIBILITIES

Returning to the Oxford English Dictionary the object of the game is

to fill a grid ... with the numbers one to nine, in such a way that every number appears only once in each horizontal line, vertical line, and three-by-three subdivision.14

Given this and the original set of hints, the possibilities can be generated for every square.

14 "Sudoku, n." OED Online. December 2006. Oxford University Press. <http://dictionary.oed.com/cgi/entry/50291646>. (28th March 2007).

22

Page 23: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

The possibilities are the set of numbers such that when one of the set is placed in its square the grid remains valid and legal. These are generated by searching through the row, column and piece occupied by the square, and discarding any numbers which appear.

Figure 4.1: The possibilities.

4.3: IN THE GAME

The Jigsaw Sudoku solver reduces the possibilities by finding numbers within the grid that it can apply a strategy to. Like a human player it will first consider the easiest strategy known to it. If this provides no assistance it shall attempt to use further strategies.

Once a strategy is identified that can help in solving the grid, it is implemented and the process starts again on the simplified grid with the easiest strategy.

The grand aim of the bank of strategies is to reduce the possibilities in every square to a single number. Whence they are vacuously solved.

4.4: NOVICE SUDOKU STRATEGIES

The novice strategies provided here should be sufficient to solve any published Sudoku puzzle classed as easy or straightforward. Many very easy puzzles are solvable using just the only-possible strategy.

23

Page 24: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

4.4.1: ONLY-POSSIBLE

The only-possible is the most straightforward strategy. If there exists only one possibility in a square then obviously the square is solved, and the possibility can be placed. Identifying and placing these possibilities shall be known as the only-possible strategy.

In Figure 5.1 there is an only-possible at square (1,2), hence a 4 must be placed here.

4.4.2: SINGLES

A single is almost as straightforward as an only-possible. If a number occurs just once within the possibilities of a row, column or piece then it is a single. All other numbers can be removed from the possibilities in the square where this single occurs.

In Figure 5.1 there is a single 2 in the first column at (1,1). Hence the 4 can be removed and 2 is left as an only-possible.

4.4.3: CONJUGATE PAIRS

Whilst conjugate pairs are more complex than the two strategies above, I believe that they should be here as the basis for a variety of other strategies.

A conjugate pair is a set of two possibilities that exist at just two squares in a given row, column or piece. The two squares containing the conjugate pair are the only potential locations for the numbers within it. Thus the possibilities of these two squares can be reduced to the numbers within the conjugate pair.

CONJUGATE PAIRS – AN EXAMPLE

To the left is an example of a conjugate pair. Note that the numbers 1 and 2 appear in just the two highlighted squares. This fact forces both of the highlighted squares to be a 1 or 2. Hence the 4 and 8 can be deleted from these two squares.

Note: This piece could easily be solved using other strategies.

Excursus 4.1: Conjugate Pairs – An Example.

24

Page 25: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

This strategy will reduce the possibilities in two squares to two, simplifying the puzzle and enabling other strategies to be used. The same principle can be extended to triples and quads (or further if required).

4.5: SELECTED HARDER STRATEGIES

The novice strategies discussed in 4.4 might be enough to solve a Sudoku puzzle described as easy. The following strategies are of assistance when solving a harder puzzle. Again, the strategies explained here are just a small selection of all the known strategies.

4.5.1: POINTING COUPLETS

Consider a partially filled piece within a Sudoku puzzle. If there exists a couplet (or triplet or quad) of possibilities within the intersection of the piece and a single row or column then the pointing couplets strategy can be used.

Consider the couplet again. Since the possibilities say that they cannot lie elsewhere in the given piece, they can be removed from elsewhere in the given row or column.

This strategy is far easier to explain with an example:

POINTING COUPLETS

Consider the couplet of 4s which are highlighted in the partial grid above. This couplet contains the only occurrences of 4 in the centre piece.

Now, it must be the case that the 4s in the centre piece must lie on the row highlighted. Hence all other occurrences of 4 in the row can be discarded.

Excursus 4.2: Pointing Couplets

25

Page 26: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

4.5.2: X-WINGS

The X-wing strategy is more complicated again. The strategy looks at couplets of occurrences in rows and columns. To improve the clarity of this explanation I shall refer to just the row variant. The column variant is exactly the same, simply substitute the word row for column, and vice versa.

The X-wing strategy is as follows: If a couplet of numbers exists in the same column of two different rows, and the numbers exist at no other points in these rows, then the occurrences of the numbers in the columns can be eliminated.

Figure 4.2: A Sketched X-Wing.

Consider the sketched grid above. Imagine there are two couplets of numbers in the blue squares, and that these numbers occur nowhere else in the two selected rows. Then all other occurrences of the numbers in the selected columns can be deleted.

The term X-wing is used due to the linking between the four squares. In fact the squares joined by a single line in the diagram must contain the same number.

Again, the X-wing strategy is a great deal easier to explain given an example:

26

Page 27: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

X-WINGS – AN EXAMPLE

Regard the two grids below. Note that a matching couplet of 6s occurs in the first and fourth rows, at (1,1), (1,6), (4,1) and (4,6). There are no other 6s in these rows.

These couplets form an X-Wing, and thus the 6s at (1,5), (1,6) and (6,5) can be removed.

Excursus 4.3: X-Wings – An Example.

4.6: JIGSAW SUDOKU SPECIFIC STRATEGIES

The earlier strategies in this chapter can be applied to all Sudoku and Jigsaw Sudoku puzzles. This section describes two Jigsaw Sudoku specific strategies.

Most Jigsaw Sudoku puzzles could be solved with just adaptations of standard strategies. The Jigsaw Sudoku specific strategies are useful because once they have been learnt, they are easy to spot.

4.6.1: LITTLE LAW OF LEFTOVERS

The Little Law of Leftovers strategy can be applied to a Jigsaw Sudoku puzzle when rows, columns or pieces have squares in common.

The strategy states that given two intersecting rows, columns or pieces then the subsets of

27

Page 28: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

these two sets lying outside the intersection must be equal. The sets lying outside the intersection are known as the leftovers. More mathematically, given two sets A and B:

A − A∩B = B − A∩B

This is better explained below:

THE LITTLE LAW OF LEFTOVERS

Consider the grid below, specifically the intersection (marked in grey) between the lower-right piece and the final column.

In this case, five out of six numbers in the column and the piece respectively lie in this intersection. It follows that the two highlighted squares outside the intersection must contain the same number.

The idea can be better visualised in the example Venn diagram. Both sets are of size six and the intersection is of size five. Hence the pink and blue subsets must be the same single number.

Excursus 4.4: The Little Law of Leftovers.

4.6.2: BIG LAW OF LEFTOVERS

The Big Law of Leftovers considers the intersection between rows, columns or pieces with regions of the grid. A region could be any selection of squares on the grid, which has at least one of each number within it. Usually it would be a number of adjoining rows or columns,

28

2 4

1 5 6

3 3

Page 29: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

since they clearly include all numbers.

When a group of rows, columns or pieces intersect with a region they leave two sets of leftovers. These leftovers must contain the same numbers, as is the case in the Little Law.

THE BIG LAW OF LEFTOVERS

The Big Law of Leftovers is an extension of the little law.

The case here demonstrates an example of the intersection, shaded in grey, of three pieces and the region of the first three rows.

The two pink squares lie outside of this intersection, but inside of the three pieces. Likewise the blue squares lie within the rows but not in the intersection.

Thus, the numbers contained in the highlighted squares must be the same.

The Big Law of Leftovers can obviously be extended to various sizes of sets.

Excursus 4.5: The Big Law of Leftovers.

29

Page 30: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

§5: USABILITY For a program to be regarded as useful it first must be usable. This section details issues which enhance the game's solvability.

Firstly, the functionality of the game itself is discussed.

Following this, there is a discourse of the Four-Colour Theorem. This is a mathematical result, of particular interest to computer scientists, which I have used in the game.

5.1: THE GAME

The program is implemented in Java and Swing using the conventional packages and GUI components. The GUI is a series of labels, buttons and menus. The puzzle itself is created from an array of coloured buttons.

Below are some examples of specific functionality within the game. This is just a small selection of the components implemented. Further functions are omitted from discussion due to their tedious nature and the limited size of this report.

5.1.1: SAVING AND LOADING

A saving and loading system has been implemented so that a user can put aside a puzzle and return to it at a later time. This is accessible, in the usual way, through a menu. The user can choose a directory and a file-name for the puzzle as required.

The data is stored as a list of squares in a file. Each square contains data about its co-ordinate, the piece it belongs to, the number it contains (if any) and whether it was originally a hint.

5.1.2: CLUES

Many Sudoku players keep track of their game by marking their assessed possibilities for each square on the grid.

There is an analogue of this within the program. Should the Show Clues button be enabled, the user can hover over a square with the cursor and be presented with a pop-up of the legal possibilities.

30

Page 31: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

5.1.3: USER-INPUTTED GRIDS

Another thing that a user might want to do is input his own Sudoku or Jigsaw Sudoku puzzles. E.g. They may wish to copy a Sudoku puzzle from a newspaper. It is possible to input grids into the game. Once a puzzle has been inputted, it is treated exactly like a computer generated game.

5.2: FOUR-COLOURING

Originally, the shuffled Jigsaw Sudoku grid was coloured with a graduated scheme. I found this to be undesirable due to adjacent pieces being difficult to distinguish and so set about reducing the number of colours used. To this end, I made use of a mathematical result known as the Four-Colour Theorem.

The Four-Colour Theorem dates back to 1852 and a South African mathematician Francis Guthrie.15 Whilst attempting to colour a county map of England, Guthrie noticed that at least four different colours were required so that no two adjacent counties shared a colour. He went on to speculate that four colours were sufficient to shade any map without neighbouring areas sharing a colour. This became known as The Four-Colour Problem and remained unproven until 1976.16

Figure 5.1: A Comparison of a Graduated vs. a Four-Coloured Grid.

15 Kenneth O'May, 'The Origin of the Four-Color Conjecture', Isis, Vol. 56, No. 3. (Autumn, 1965), pp. 346-348. 16 Robin Wilson. Four Colours Suffice? <http://www.gresham.ac.uk/event.asp?PageId=39&EventId=187> (21st April

2007).

31

Page 32: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

By four colouring the Jigsaw Sudoku grid it could be guaranteed that there was sufficient contrast between adjacent jigsaw pieces, as shown in figure 5.1.

A PROOF OF THE FOUR-COLOUR THEOREM

The proof of The Four-Colour Theorem is of special interest to Computer Scientists; it was the first mathematical proof dependent on the calculative power of a computer.

In the early 1970s, two mathematicians at the University of Illinois, Kenneth Appel and Wolfgang Haken decided that a computer would be the perfect tool for providing a proof.17

The proof is by contradiction. As is the case with mathematical contradiction, one must assume that there is a case which works and then show it does not. Appel and Haken commenced by assuming that there existed some map that required at least five colours to shade satisfactorily.

Building on the work of other mathematicians regarding the Four-Colour Theorem, Appel and Haken created formulas to describe the set of all possible maps. This set reduced the infinity of possible maps to just 1,936 standard maps (later 1,476),18 all of which were four-colourable.

It is a mathematical fact that if there exists maps which require five colours to shade satisfactorily, then there must exist a smallest map requiring five colours. The proof proceeded by showing that such a smallest map cannot exist; any map contained a region within their set or a region reducible to to a member of their set. The calculations in this reduction were achieved using a computer.

Since every member of their set is four-colourable it is the case that every map is four-colourable.

Many mathematicians at the time questioned their methods, and disapproved of the use of a computer. Appel and Haken's convoluted proof was not able to be feasibly checked by hand. It was not until a new proof was provided in 1996 that Appel and Haken's proof was accepted in all mathematical circles.19

Excursus 5.1: A Proof of the Four-Colour Theorem.

17 David Brooks. UNH Magazine Online. Math Pioneers. <http://unhmagazine.unh.edu/sp02/mathpioneers.html> (7th

April 2007).18 Wikipedia. Four Color Theorem – Wikipedia the Free Encyclopedia. <http://en.wikipedia.org/wiki/Four-

color_theorem> (7th April 2007).19 Robin Thomas. The Four Color Theorem <http://www.math.gatech.edu/~thomas/FC/fourcolor.html> (20th April

2007).

32

Page 33: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

§6: ANALYSIS This chapter covers a statistical experiment to analyse the effect of the number of hints upon the solvability and uniqueness of the grid.

In 6.1 the investigation is introduced. Then the experiments used to perform the investigation are detailed.

Following this there is a discussion and analysis of the results. Patterns are identified, and thoughts to explain these patterns are considered.

6.4 onwards discusses the mathematics associated with setting the hints. Proofs of theorems are provided and original thoughts into further investigations are outlined.

6.1: HINTS VS. SOLVABILITY

As an interesting evaluation, I decided to test the number of random hints placed versus the solvability of the grid. By incrementing the number of hints placed onto the grid, the impact on the solvability could be assessed.

The bulk experiment ran as follows: A grid was generated and shuffled, then hints were placed at random onto the grid in a similar manner to the setting algorithm (as described in 4.3). This grid was tested for solvability; if it was found to be unsolvable it was discarded and a count was incremented. Once a solvable grid was found, the count was noted and this process started again.

In each test I repeated the experiment 100 times, and repeated these tests six times each (the results are shown in the appendix). The average count was found and then converted into a percentage chance of creating a solvable grid. E.g. If the average count was 5 then the chance of creating a uniquely solvable puzzle would be 20%.

Because of the volume of experiments that needed to be completed I only tested 3x3 and 4x4 grids. Larger grids would have taken much longer and, as will be shown, an interesting pattern clearly emerges in just these basic cases.

Before running the test, suitable numbers of hints had to be chosen. For a 3x3 grid the number of hints placed could clearly vary from zero to nine. Similarly, for a 4x4 grid the number of hints could vary from zero to 16.

Using the 3x3 grid as an example: If nine values were able to be placed on the grid obeying

33

Page 34: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

the laws then the puzzle is already solved. Hence N number of hints was discarded.

Vacuously, a game with zero hints given has no chance of generating a unique game. In addition it has been proved that for a grid of size N, a set of N – 1 hints can lead to a unique solution20 (this shall be explained later). Hence I chose to test the range N – 1 to N2 – 1 (i.e. 2 to 8 for the 3x3 example).

6.2: THE 3X3 EXPERIMENT

As the graph shows there is an S-shaped curve dependent on the number of hints.

From the graph it seems that three is the number of hints required to give the highest chance of generating a uniquely solvable grid. Placing three random hints on to a 3x3 board gives a 45% chance of producing a uniquely solvable game.

Six is the number of hints that gives the least chance of generating a uniquely solvable grid. Six hints give a 23% chance of producing a uniquely solvable game.

Interesting results occur at the extreme values. I can offer a good explanation for these.

At the lower end of the scale, two hints provide less of a chance of generating unique puzzles than three hints. I believe, given two hints placed, there are just too many free variables. This leads to a larger number of grids with many solutions. In this 3x3 case, the effect is limited. The trends are much stronger in the 4x4 case, which is discussed in the next section.

Towards the upper limit, the chances of generating a uniquely solvable grid start to rise. In these cases there are less squares left to fill and hence there is a lower chance of creating a contradictory game.

Aside from these extremes, the decreasing nature of the graph could indicate an rising number of contradictions. I believe this is a feasible conclusion to draw from the data.

20 Bob Harris, Du-Sum-Oh Puzzles <http://www.bumblebeagle.org/dusumoh/proof/index.html> (27th April 2007).

34

Page 35: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

X-Axis – Number of hints placed.Y-Axis – Percentage of placements that lead to a unique solution.

Hints 2 3 4 5 6 7 8Mean 2.85 2.24 2.92 3.17 4.30 3.79 3.01

Take the number of hints to be 2. In this case, the likelihood of creating a solvable grid is 1 in 2.85 or 35%.

Figure 6.1: Number of Hints Placed vs. Unique Solutions Generated (3x3).

6.3: THE 4X4 EXPERIMENT

The 4x4 experiment follows a similar pattern to the 3x3 experiment.

The most interesting results of the 4x4 experiment are, again, at the extreme values. In this case the results are amplified. I believe this to be because of the further number of variables available.

Overall, in every case there is a lower chance of generating a uniquely solvable puzzle than in the 3x3 experiment. This is due, again, to the extra variables to take into account.

35

2 3 4 5 6 7 80

5

10

15

20

25

30

35

40

45

50

Page 36: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

I estimate that for larger sized experiments the results will follow the same pattern as the two carried out and that the extremities will give even stronger results.

X-Axis – Number of hints placed.Y-Axis – Percentage of placements that lead to a unique solution.

Hints 3 4 5 6 7 8 9 10 11 12 13 14 15

Mean 21.46 9.31 6.17 6.89 7.45 8.28 12.21 15.44 16.94 18.58 22.17 21.47 7.29

In the case where there are three hints, there is a 1 in 21.46 chance (or 4.7%) of generating a solvable grid.

Figure 6.2: Number of Hints Placed vs. Unique Solutions Generated (4x4).

6.4: N – 1 HINTS

As mentioned earlier, there exist Jigsaw Sudoku puzzles of size N for which N – 1 hints lead to a unique solution. This is true for grids of certain shapes, one such shape is detailed below.

36

3 4 5 6 7 8 9 10 11 12 13 14 150

2

4

6

8

10

12

14

16

18

20

Page 37: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

PROOF OF N – 1 HINTS LEADING TO A UNIQUE SOLUTION

In January 2006, Bob Harris published the proof An N-1 hint NxN Du-Sum-Oh exists for any N on his website.21 I shall give a sketch of the proof here.

Consider the grid to the left.

That is the grid where piece 1 lies in the first N – 2 squares of the first row and the first 3 squares of the first column. Piece 2 completes the first row and all but the last square of the second. Piece 3 completes the final column plus the second-last square in the bottom row.

This can be extended to any grid of size N in the obvious manner.

Firstly place a hint in the second last square of the final row. It follows that all the other squares on this diagonal contain the same number. In the lower example the number placed here would be a 5.

From this it is shown that it is sufficient to place hints onto the grid such that there lies N – 1 distinct hints on N – 1 diagonals, as shown.

This is the only solution (excluding the N! Permutations, see 6.6) to the original grid.

Excursus 6.1: Proof of N – 1 Hints Leading to a Unique Solution

Unfortunately no similar lower bound has been found for standard Sudoku puzzles. It is believed that the lower bound is 17 hints.22 This has not been proven, though no uniquely solvable grids with fewer hints have been found.

21 Bob Harris, Du-Sum-Oh Puzzles <http://www.bumblebeagle.org/dusumoh/proof/index.html> (27th April 2007).22 David Bau. Sudoku Generator. <http://davidbau.com/archives/2006/09/04/sudoku_generator.html> (27th April

2007).

37

Page 38: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

6.5: N – 2 HINTS

In addition to the above proof, I believe that no puzzle of size N can be uniquely solvable given N – 2 hints. I provide a proof of this below:

PROOF THAT N – 2 HINTS WILL NOT LEAD TO A UNIQUE SOLUTION

Suppose there exists a Jigsaw Sudoku puzzle of size N which is uniquely solvable given N – 2 hints.

Given there are N – 2 hints placed, there must be two numbers to be placed which are not in the original hints. Since these two numbers are not in the hints they are interchangeable. Hence a unique solution does not exist.

Therefore it is not possible for there to exist a grid of size N which is uniquely solvable given N – 2 hints.

Excursus 6.2: Proof that N – 2 Hints will not Lead to a Unique Solution.

This proof can be extended to any number less than N – 1. Thus N – 1 is the minimum number of hints required to generate a solvable grid.

6.6: PERMUTATIONS OF NON-UNIQUE GRIDS

For a grid of size N with any n < N distinct hints placed on the board, there are at least (N – n)! solutions.

Given N – 2 hints, it is the case that there are at least 2! permutations (solutions). It is the case (as shown in 6.5) that the two numbers not contained in the hints are interchangeable.

The number of permutations of any set are given by n!. For example given a set of three undeclared hints, there are at least 6 possible permutations obtained from interchanging these hints.

It is not the case that n! is the maximum number of permutations. Consider a grid with two undeclared hints, it may not be necessary to swap every undeclared number when finding solutions.

Take a grid of size three, which has three occurrences of a single hint and two undeclared hints a and b. The following figure represents all of the permutations of the two

38

Page 39: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

undeclared hints in the six remaining squares.

a a ab b b

a a bb b a

a b ab a b

b a aa b b

b a ba b a

b b aa a b

a b bb a a

b b ba a a

Figure 6.3: Permutations of a and b in a Grid of Size 3.

It follows that for any grid size N with n undeclared hints and N – n numbers with all there occurrences, there are at most nN permutations.

39

Page 40: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

§7: EVALUATION AND EXTENSIONS This, the penultimate chapter, provides an insight into problems faced within the project. It also describes a selection of extensions which have been entertained but not implemented.

Firstly the problem of generating large grids is discussed.

Two sections follow on the topics of further strategies and choice of difficulty. These issues could be looked at from different angles as either a problem or an extension.

The idea of superfluity is then addressed and insight is given into a potential implementation.

7.1: GENERATING LARGE GRIDS

The one major problem with the project is the generation of large grids. Grids smaller than 6x6 are created almost instantly, whilst 7x7 grids usually take less than 20 seconds to make. Larger grids than this do not seem feasible given my current algorithms.

Much of the problem lies in the way the grids are created. Given that 4x4 grids take on average roughly twelve attempts to generate a solvable puzzle (as shown in 6.3), the problem with larger grids become obvious.

The algorithms used to generate the grids are clearly not optimal. Saying this, they do serve their purpose, and given a longer period of time could be much improved.

I feel that a good deal of the trouble generating large grids could be put down to the order in which I designed the project. Had I implemented the solution first I could have used this to narrow down the possibilities.

7.2: FURTHER STRATEGIES

There are literally dozens of well documented strategies available for Sudoku. Many programmers and hobbyists spend years researching, collecting and implementing new strategies in their spare time.

It would not be possible to create a complete Sudoku solver within the time prescribed for this project. In addition to this, I believe it would not add value. Implementing the extra

40

Page 41: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

strategies would simply be a case of researching descriptions and designing a version in code.

7.3: CHOICE OF DIFFICULTY

A possible extension is to implement a system that allows a choice of difficulty. Below is a suggestion of how this could be completed.

Firstly the known strategies of the game must be grouped based on their perceived difficulty. For this example, say that the strategies are placed into three groups: novice, intermediate and advanced.

To assess the difficulty of the generated game, it is sufficient to check which strategies are needed to solve it. Thus, once a game has been created, the computer shall attempt to solve the game from its bank of strategies. For example: if the computer is able to solve the game using only novice strategies then the game would be classed as easy.

Whilst this sounds straightforward it does have flaws. The computer would have to know all possible solving strategies; if it was missing a novice strategy then it might regard the game to be more difficult than it actually is. Also, it is very hard to categorise the level of different strategies; this is a highly subjective matter, a strategy that one player may find easy, another player may not be able to grasp.

A much harder thing to do would be to let the player select the difficulty of the game before generation. This is why I believe the vast majority of on-line Sudoku puzzles rely on ready made puzzles, rather than generating a game based on the user's required level.

The only feasible method I could see of achieving this would be to generate a series of games and checking their solvability until finding one at the required level. This could be very time consuming; since the boards are randomly generated, the worst case could be never being able to find a game of the required difficulty.

7.4: SUPERFLUITY

A placed hint is called superfluous if it can be deduced from the other hints in the Sudoku puzzle. Consider figure 7.1, in the left hand grid there is a 3 placed at (4,2). In the right hand grid this 3 has been removed, yet the game can still be solved. Hence this 3 is superfluous.

Many people consider Sudoku puzzles which contain superfluous hints to be undesirable. One member of sudoku.org.uk succinctly states “In my opinion any Sudoku which contains

41

Page 42: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

any superfluous clue is thereby aesthetically flawed”.23

Figure 7.1: Superfluous Hints.

I would have liked to implement a method of removing superfluous hints within the program. Again there simply was not enough time.

I imagine it being a straightforward, yet tedious, algorithm to implement. It may suffice to remove one hint from the grid in turn, and to check the possibles given at the now empty square.

A variation of this algorithm could be used to generate a grid with the minimal number of hints. Consider a completed Sudoku puzzle and the set of all the numbers placed in this puzzle. There must be a smallest subset of these numbers that generate the remainder of the set. This subset could be called minimal. There exists a minimal number of hints for any given Jigsaw Sudoku.

A potential algorithm to generate a minimal set of hints could involve placing and removing hints dependent on their superfluity.

To achieve these algorithms with good effect, the program would need to have a large bank of strategies at its disposal. Since the program does not have a large bank of strategies, any attempt at identifying superfluity would have been flawed from the outset.

23 Gurth Bruins. Sudoku Discussions. <http://www.sudoku.org.uk/discus/messages/29/2313.html?1155827002> (29th

April 2007).

42

Page 43: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

§8: THE CONCLUSION This final chapter sums-up the project as a whole. The highs and lows of the project are discussed.

Firstly, an account of the achievements is given. This details the successes of the project.

8.2 covers a selection of notable issues. Poor choices are outlined and an assessment of the scope of the project is provided.

Finally, I summarise the project as a whole and give some concluding thoughts.

8.1: ACHIEVEMENTS

The second chapter of this report identified three main challenges:

1. Creating a grid of jigsaw pieces.

2. Placing the hints to create a uniquely solvable grid.

3. Solving the game using a bank of strategies.

To a large extent, all of these challenges have been achieved.

Creating a grid of jigsaw pieces works fantastically well. The solution of swapping squares then checking for completeness of the piece is an elegant alternative to the space-filling problem.

The program is able to place hints and make a uniquely solvable game. This works well for smaller sized grids; problems occur when the algorithm is faced with larger cases, as identified in 7.1. As such, placing the hints can be regarded as a limited success.

All of the strategies implemented work well and help solve games within their reach. Further strategies could have been designed and produced. Given the short space of time it was decided that there were more pressing issues to consider. The strategies that are included in the program are capable of solving most generated games. Overall I feel that, in this sense, the original brief has been met successfully.

In addition to the three main challenges there is a stylish and usable GUI. Methods such as saving and loading have been implemented. The clues mechanism is a great help to a player that is stuck. All of the smaller aspects identified have been achieved.

43

Page 44: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

8.2: ISSUES AND REVISIONS

There are issues which need to be resolved in the creation of every piece of software. New thoughts and methodologies develop as the design process is carried out.

Issues have been identified in the previous chapter regarding setting hints and the bank of strategies. Many of these problems could be put down to time constraints upon the project. These constraints meant that the potential scope of the project is much larger than could be realistically entertained.

Decisions had to be made and certain aims had to be prioritised. In order to ensure the completion of the project, difficult choices had to be made. These choices meant that selected elements did not receive the attention which they potentially deserved.

8.3: SUMMARY

As stated in 2.1, the encompassing aim of the project was to create a computer program capable of generating and solving Jigsaw Sudoku puzzles. With this in mind, I am delighted with the achievements made.

I feel that any variation of Sudoku could have made a good project. The choice of Jigsaw Sudoku contained many avenues to explore and elements to consider. The recent increase in popularity of Jigsaw Sudoku demonstrates the interesting nature of the game.

As a joint honours student, I believe I have done justice to my knowledge in both computer science and mathematics.

It is impossible reach perfection, however I am more than happy with what has been achieved.

44

Page 45: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

READING LIST S.F.Bammel, J.Rothstein. 'The number of 9 × 9 Latin squares', Discrete Mathematics 11 (1975) 93–95 .

David Bau. Sudoku Generator. <http://davidbau.com/archives/2006/09/04/sudoku_generator.html> (27th

April 2007).

David Brooks. UNH Magazine Online. Math Pioneers. <http://unhmagazine.unh.edu/sp02/mathpioneers.html> (7th April 2007).

Gurth Bruins. Sudoku Discussions <http://www.sudoku.org.uk/discus/messages/29/2313.html?1155827002> (29th April 2007).

Centre for Innovation in Mathematics Teaching. Pentominoes – Introduction <http://www.cimt.plymouth.ac.uk/resources/puzzles/pentoes/pentoint.htm> (28th April 2007).

Cut the Knot. Latin Squares. <http://www.cut-the-knot.org/arithmetic/latin.shtml> (7th April 2007).

Georges Gonthier,. A Computer-Checked Proof of the Four Colour Theorem. <http://research.microsoft.com/ ~gonthier/4colproof.pdf > (3rd April 2007).

B Felgenhauer and F Jarvis. Sudoku Enumeration Problems. <http://www.afjarvis.staff.shef.ac.uk/sudoku/> (29th March 2007).

Bertram Felgenhauer and Frazer Jarvis, 'Mathematics of Sudoku I and II' (2006). <http://www.afjarvis.staff.shef.ac.uk/sudoku/felgenhauer_jarvis_spec1.pdf> and <http://www.afjarvis.staff.shef.ac.uk/sudoku/russell_jarvis_spec2.pdf> (24th April 2007).

Bertam Felgenhauer and Frazer Jarvis. 'Enumerating Possible Sudoku Grids' (2005) . <http://web.inf.tu-dresden.de/~bf3/sudoku/sudoku.pdf> (24th April 2007).

Funahashi Study Private School, Japan. Zigzag Sudoku Solver / Irregular Number Place / Geometric <http://homepage3.nifty.com/funahashi/game/game655eng.html> (10th April 2007).

Bob Harris. Du-Sum-Oh Puzzles. <http://www.bumblebeagle.org/dusumoh> (2nd April 2007).

Francis Heaney. Snakes on a Sudoku (Pennsylvania, 2006).

Simona Mancini, 'Sudoku Game Theory, Model and Algorithms', (2006). <http://www2.polito.it/didattica/polymath/htmlS/Studenti/Tesi/SudokuGame/tesi_Mancini_Simona%20SUDOKU.pdf> (1st May 2007)

Kenneth O'May, 'The Origin of the Four-Color Conjecture', Isis, Vol. 56, No. 3. (Autumn, 1965), pp. 346-348.

'Sudoku, n.' OED Online. December 2006. Oxford University Press. <http://dictionary.oed.com/cgi/entry/50291646> (28th March 2007).

45

Page 46: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

Gordon Royle. Minimum Sudoku. <http://people.csse.uwa.edu.au/gordon/sudokumin.php> (3rd April 2007).

E Russell and F Jarvis. Sudoku enumeration problems. <http://www.afjarvis.staff.shef.ac.uk/sudoku/> (29th

March 2007).

Marcus Du Sautoy, The Music of the Primes (London, 2004).

Mikhael Shor. Game Tree – Game Theory .net <http://www.gametheory.net/dictionary/GameTree.html> (29th

April 2007).

Robert Simms. Pentominoes. <http://www.math.clemson.edu/~simms/java/pentominoes/> (23rd April 2007).

Helmut Simonis. 'Sudoku as a Constraint Problem', (2006). <http://www.icparc.ic.ac.uk/~hs/report.pdf> (27th

April 2007).

Andrew Stuart. Sudoku Solver by Andrew Stuart. <http://www.scanraid.com/sudoku.htm> (2nd April 2007).

Andrew Stuart. Jigsaw Solver by Andrew Stuart. <http://www.scanraid.com/jigsaw.htm> (10th April 2007).

Robin Thomas. The Four Color Theorem <http://www.math.gatech.edu/~thomas/FC/fourcolor.html> (20th

April 2007).

Richard Turner. Sudoku Tea Talk. <http://www.gatsby.ucl.ac.uk/~turner/sudoku/Sudoku.html> (7th April 2007).

Robin Wilson. Four Colours Suffice? <http://www.gresham.ac.uk/event.asp?PageId=39&EventId=187> (21st

April 2007).

Wikipedia. Four Color Theorem – Wikipedia the Free Encyclopedia. <http://en.wikipedia.org/wiki/Four-color_theorem> (7th April 2007).

Wikipedia. Sudoku – Wikipedia, the Free Encyclopedia. <http://en.wikipedia.org/wiki/Sudoku#Difficulty_ratings> (14th April 2007).

Wikipedia. Francis Guthrie. <http://en.wikipedia.org/wiki/Francis_Guthrie> (7th April 2007).

Robin Wilson. 'The Sudoku Epidemic' (2003) <http://www.mualphatheta.org/sudoku.pdf> (23rd April 2007).

46

Page 47: A JIGSAW SUDOKU GENERATOR AND SOLVERstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · A JIGSAW SUDOKU GENERATOR AND SOLVER Christopher Stephens. 2nd May 2007

APPENDIX A: RESULTS OF EXPERIMENTS 3X3 RESULTS

Number of Hints 2 3 4 5 6 7 8

1st Iteration 2.73 1.85 2.90 2.78 3.59 5.49 2.852nd 2.89 2.96 2.87 3.64 3.33 3.54 3.163rd 2.98 2.23 2.67 2.91 3.30 3.90 3.254th 2.23 1.96 2.98 4.28 5.07 2.71 2.755th 2.46 2.51 3.24 3.08 4.48 3.94 3.966th 4.96 2.20 3.31 4.07 5.14 3.19 3.117th 2.61 1.90 3.29 2.48 4.09 4.69 2.688th 3.25 2.46 3.36 2.52 3.58 3.67 2.669th 2.41 1.63 3.17 3.00 5.31 3.07 2.9710th 1.94 2.68 3.39 2.92 5.13 3.76 2.68Mean 2.85 2.24 2.92 3.17 4.30 3.79 3.01Percentage 35.09 44.64 34.25 31.57 23.26 26.39 33.22

4X4 RESULTS

Number of Hints 3 4 5 6 7 8 9 10 11 12 13 14 15

1st 21.38 10.38 6.99 8.49 7.98 6.36 8.68 16.12 18.88 18.99 18.33 25.13 4.842nd 18.64 7.27 6.55 8.21 6.74 10.49 11.18 12.06 15.76 18.10 24.42 18.76 9.353rd 22.18 4.43 5.90 4.70 7.97 8.44 12.93 16.00 18.51 17.02 20.78 19.60 5.304th 24.01 11.23 8.23 6.53 7.67 7.10 8.90 16.63 17.12 18.75 24.71 25.34 5.085th 20.22 6.46 5.39 7.50 6.57 10.00 16.85 14.12 13.08 17.20 25.47 24.20 9.156th 22.32 16.06 3.95 5.89 7.75 7.29 14.69 17.71 18.26 20.42 19.33 15.78 10.00Mean 21.46 9.31 6.17 6.89 7.45 8.28 12.21 15.44 16.94 18.58 22.17 21.47 7.29Percentage 4.66 10.75 16.21 14.51 13.42 12.08 8.19 6.48 5.90 5.38 4.51 4.66 13.72

47