The remarkably popular puzzle demonstrates man versus machine, backtraking and recursion, and the mathematics of symmetry.

Similar documents
The Mathematics Behind Sudoku Laura Olliverrie Based off research by Bertram Felgenhauer, Ed Russel and Frazer Jarvis. Abstract

You ve seen them played in coffee shops, on planes, and

Taking Sudoku Seriously

Sudoku. How to become a Sudoku Ninja: Tips, Tricks and Strategies

1 Introduction. 2 An Easy Start. KenKen. Charlotte Teachers Institute, 2015

Sudoku an alternative history

An Exploration of the Minimum Clue Sudoku Problem

Applications of Advanced Mathematics (C4) Paper B: Comprehension INSERT WEDNESDAY 21 MAY 2008 Time:Upto1hour

Applications of Advanced Mathematics (C4) Paper B: Comprehension WEDNESDAY 21 MAY 2008 Time:Upto1hour

Take Control of Sudoku

ISudoku. Jonathon Makepeace Matthew Harris Jamie Sparrow Julian Hillebrand

An improved strategy for solving Sudoku by sparse optimization methods

T H E M A T H O F S U D O K U

of Nebraska - Lincoln

Kenken For Teachers. Tom Davis January 8, Abstract

Yet Another Organized Move towards Solving Sudoku Puzzle

Latin Squares for Elementary and Middle Grades

arxiv: v2 [math.ho] 23 Aug 2018

Using KenKen to Build Reasoning Skills 1

Some results on Su Doku

Sudoku: Is it Mathematics?

UN DOS TREZ Sudoku Competition. Puzzle Booklet for Preliminary Round. 19-Feb :45PM 75 minutes

The mathematics of Septoku

Mathematics of Magic Squares and Sudoku

WPF SUDOKU/PUZZLE GRAND PRIX 2014 WPF SUDOKU GP 2014 COMPETITION BOOKLET ROUND 4. Puzzle authors: Russia Andrey Bogdanov, Olga Leontieva.

CSE373: Data Structure & Algorithms Lecture 23: More Sorting and Other Classes of Algorithms. Nicki Dell Spring 2014

Cracking the Sudoku: A Deterministic Approach

isudoku Computing Solutions to Sudoku Puzzles w/ 3 Algorithms by: Gavin Hillebrand Jamie Sparrow Jonathon Makepeace Matthew Harris

Lecture 6: Latin Squares and the n-queens Problem

The most difficult Sudoku puzzles are quickly solved by a straightforward depth-first search algorithm

Cryptic Crosswords for Bright Sparks

ON 4-DIMENSIONAL CUBE AND SUDOKU

Solving Sudoku Using Artificial Intelligence

17. Symmetries. Thus, the example above corresponds to the matrix: We shall now look at how permutations relate to trees.

Sudoku Solver Version: 2.5 Due Date: April 5 th 2013

PART 2 VARIA 1 TEAM FRANCE WSC minutes 750 points

KenKen Strategies 17+

Zsombor Sárosdi THE MATHEMATICS OF SUDOKU

A Group-theoretic Approach to Human Solving Strategies in Sudoku

Math 3012 Applied Combinatorics Lecture 2

8. You Won t Want To Play Sudoku Again

CSE548, AMS542: Analysis of Algorithms, Fall 2016 Date: Sep 25. Homework #1. ( Due: Oct 10 ) Figure 1: The laser game.

Grade 6 Math Circles February 15, 2012 Math Puzzles

Physical Zero-Knowledge Proof: From Sudoku to Nonogram

KenKen Strategies. Solution: To answer this, build the 6 6 table of values of the form ab 2 with a {1, 2, 3, 4, 5, 6}

Solving and Analyzing Sudokus with Cultural Algorithms 5/30/2008. Timo Mantere & Janne Koljonen

WPF SUDOKU/PUZZLE GRAND PRIX 2014 WPFSUDOKU GP 2014 COMPETITIONBOOKLET ROUND6. Puzzle authors: Bulgaria Deyan Razsadov.

IN THIS ISSUE

WPF SUDOKU GP 2014 ROUND 2 WPF SUDOKU/PUZZLE GRAND PRIX Puzzle authors: Serbia. Organised by

INTRODUCTION TO COMPUTER SCIENCE I PROJECT 6 Sudoku! Revision 2 [2010-May-04] 1

Taking the Mystery Out of Sudoku Difficulty: An Oracular Model

Preview Puzzle Instructions U.S. Sudoku Team Qualifying Test September 6, 2015

MATHEMATICS ON THE CHESSBOARD

SUDOKU1 Challenge 2013 TWINS MADNESS

puzzles may not be published without written authorization

Applications of AI for Magic Squares

Easy Games and Hard Games

expert sudoku C08AF111E38FF93DB6AF118C2DC9B2A6 Expert Sudoku 1 / 6

DEVELOPING LOGICAL SKILLS WITH THE HELP OF SUDOKU. Radost Nicolaeva-Cohen, Andreea Timiras, Adrian Buciu, Emil Robert Rudi Wimmer

Investigation of Algorithmic Solutions of Sudoku Puzzles

This chapter gives you everything you

Canadian Mathematics Competition An activity of The Centre for Education in Mathematics and Computing, University of Waterloo, Waterloo, Ontario

Sudoku Squares as Experimental Designs

SUDOKU X. Samples Document. by Andrew Stuart. Moderate

Solution Algorithm to the Sam Loyd (n 2 1) Puzzle

Sudoku Solver Manual. Version June 2017

WPF PUZZLE GP 2014 COMPETITION BOOKLET ROUND 1 WPF SUDOKU/PUZZLE GRAND PRIX 2014

Two Parity Puzzles Related to Generalized Space-Filling Peano Curve Constructions and Some Beautiful Silk Scarves

Daily Sudoku Answers. Daily Sudoku Answers

ENGR170 Assignment Problem Solving with Recursion Dr Michael M. Marefat

It Stands to Reason: Developing Inductive and Deductive Habits of Mind

Tetrabonacci Subgroup of the Symmetric Group over the Magic Squares Semigroup

Final Project: Verify a Sudoku Solution Due Fri Apr 29 (2400 hrs)? Wed May 4 (1200 hrs)? 1

MAS336 Computational Problem Solving. Problem 3: Eight Queens

SudokuSplashZone. Overview 3

Counting Sudoku Variants

NURIKABE. Mason Salisbury, Josh Smith, and Diyalo Manral

REVIEW ON LATIN SQUARE

To Your Hearts Content

JIGSAW ACTIVITY, TASK # Make sure your answer in written in the correct order. Highest powers of x should come first, down to the lowest powers.

Welcome to the Sudoku and Kakuro Help File.

Selected Game Examples

Ludoku: A Game Design Experiment

CPSC 217 Assignment 3 Due Date: Friday March 30, 2018 at 11:59pm

The Problem. Tom Davis December 19, 2016

Eight Queens Puzzle Solution Using MATLAB EE2013 Project

arxiv: v1 [math.gt] 21 Mar 2018

Modified Method of Generating Randomized Latin Squares

MAGIC SQUARES KATIE HAYMAKER

Table of Contents. Table of Contents 1

Mobile SuDoKu Harvesting App

1st UKPA Sudoku Championship

Round minutes. Best results:

1 Recursive Solvers. Computational Problem Solving Michael H. Goldwasser Saint Louis University Tuesday, 23 September 2014

NEGATIVE FOUR CORNER MAGIC SQUARES OF ORDER SIX WITH a BETWEEN 1 AND 5

Melon s Puzzle Packs

5CHAMPIONSHIP. Individual Round Puzzle Examples SUDOKU. th WORLD. from PHILADELPHIA. Lead Sponsor

Task Scheduling. A Lecture in CE Freshman Seminar Series: Ten Puzzling Problems in Computer Engineering. May 2007 Task Scheduling Slide 1

Sudoku Solvers. A Different Approach. DD143X Degree Project in Computer Science, First Level CSC KTH. Supervisor: Michael Minock

Learning objective Various Methods for finding initial solution to a transportation problem

Transcription:

Chapter Sudoku The remarkably popular puzzle demonstrates man versus machine, backtraking and recursion, and the mathematics of symmetry. Figure.. A Sudoku puzzle with especially pleasing symmetry. The clues are shown in blue. You probably already know the rules of Sudoku, but figures. and. illustrate them. Figure. is the initial -by- grid, with a specified few digits Copyright c 00 Cleve Moler Matlab R is a registered trademark of The MathWorks, Inc. TM August 0, 00

Chapter. Sudoku known as the clues. I especially like the symmetry in this example, which is due to Gordon Royle of the University of Western Australia []. Figure. is the final completed grid. Each row, each column, and each major -by- block, must contain exactly the digits through. In contrast to magic squares and other numeric puzzles, no arithmetic is involved. The elements in a Sudoku grid could just as well be nine letters of the alphabet, or any other distinct symbols. Figure.. The completed puzzle. The digits have been inserted so that each row, each column, and each major -by- block contains through. Sudoku is actually an American invention. It first appeared, with the name Number Place, in the Dell Puzzle Magazine in. The creator was probably Howard Garns, an architect from Indianapolis. A Japanese publisher, Nikoli, took the puzzle to Japan in and eventually gave it the name Sudoku, which is a kind of kanji acronym for numbers should be single, unmarried. The Times of London began publishing the puzzle in the UK in 00 and it was not long before it spread back to the US and around the world. The fascination with solving Sudoku by hand derives from the discovery and mastery of a myriad of subtle combinations and patterns that provide tips toward the solution. The Web has hundreds of sites describing these patterns, which have names like hidden quads, X-wing and squirmbag. It is not easy to program a computer to duplicate human pattern recognition capabilities. Most Sudoku computer codes take a very different approach, relying on the machine s almost limitless capacity to carry out brute force trial and error. Our Matlab program, sudoku.m, uses only one pattern, singletons, together with recursive backtracking. To see how our sudoku program works, we can use Shidoku instead of Sudoku. Shi is Japanese for four. The puzzles, which are almost trivial to solve by

Figure.. Shidoku Figure.. Candidates Figure.. Insert singleton Figure.. Solution hand, use a -by- grid. Figure. is our first Shidoku puzzle and the next three figures show steps in its solution. In figure., the possible entries, or candidates, are shown by small digits. For example, row two contains a and column one contains a so the candidates in position (,) are and. Four of the cells have only one candidate each. These are the singletons, shown in red. In figure., we have inserted the singleton in the (,) cell and recomputed the candidates. In figure., we have inserted the remaining singletons as they are generated to complete the solution.

Chapter. Sudoku Figure.. diag(:) Figure.. No singletons. Figure.. Backtrack step. Figure.0. Solution is not unique. The input array for figure. is generated by the MATLAB statement X = diag(:) As figure. shows, there are no singletons. So, we employ a basic computer science technique, recursive backtracking. We select one of the empty cells and tentatively insert one of its candidates. We have chosen to consider the cells in

the order implied by Matlab one-dimensional subscripting, X(:), and consider the candidates in numerical order, so we tentatively insert a in cell (,). This creates a new puzzle, shown in figure.. Our program is then called recursively. In this example, the new puzzle is easily solved and the result is shown in figure.0. However, the solution deps upon the choices that we made before the recursive call. Other choices can lead to different solutions. For this simple diagonal initial condition, the solution is not unique. There are two possible solutions, which happen to be matrix transposes of each other. >> Y = shidoku(diag(:)) Y = >> Z = shidoku(diag(:) ) Z = Mathematicians are always concerned about existence and uniqueness in the various problems that they encounter. For Sudoku, neither existence nor uniqueness can be determined easily from the initial clues. With the puzzle in figure., if we were to insert a, or in the (,) cell, the row, column and block conditions would still be satisfied, but it turns out that the resulting puzzle has no solution. It would be very frustrating if such a puzzle were to show up in your daily newspaper. Backtracking generates many impossible configurations. The recursion is terminated by encountering a puzzle with no solution. Uniqueness is also a elusive property. In fact, most descriptions of Sudoku do not specify that there has to be exactly one solution. Again, it would be frustrating to find a different solution from the one given by your newspaper. The only way that I know to check uniqueness is to exhaustively enumerate all possibilities. A number of operations on a Sudoku grid can change its visual appearance without changing its essential characteristics. All of the variations are basically the same puzzle. These equivalence operations can be expressed as array operations in Matlab. For example p = randperm() z = find(x > 0) X(z) = p(x(z)) permutes the digits representing the elements. Other operations include

Chapter. Sudoku X rot0(x,k) flipud(x) fliplr(x) X([: :],:) X(:,[randperm() :]) If we do not count the comments and GUI, sudoku.m involves less than 0 lines of code. The outline of the main program is: Fill in all singletons. Exit if a cell has no candidates. Fill in a tentative value for an empty cell. Call the program recursively. Here is the code for the main program. All of the bookkeeping required by backtracing is handled by the recursive call mechanism in Matlab and the underling operating system. function X = sudoku(x) % SUDOKU Solve Sudoku using recursive backtracking. % sudoku(x), expects a -by- array X. % Fill in all "singletons". % C is a cell array of candidate vectors for each cell. % s is the first cell, if any, with one candidate. % e is the first cell, if any, with no candidates. [C,s,e] = candidates(x); while ~isempty(s) && isempty(e) X(s) = C{s}; [C,s,e] = candidates(x); % Return for impossible puzzles. if ~isempty(e) return % Recursive backtracking. if any(x(:) == 0) Y = X; z = find(x(:) == 0,); % The first unfilled cell.

for r = [C{z}] X = Y; X(z) = r; X = sudoku(x); if all(x(:) > 0) return % Iterate over candidates. % Insert a tentative value. % Recursive call. % Found a solution. The key internal function is candidates. function [C,s,e] = candidates(x) C = cell(,); tri = @(k) *ceil(k/-) + (:); for j = : for i = : if X(i,j)==0 z = :; z(nonzeros(x(i,:))) = 0; z(nonzeros(x(:,j))) = 0; z(nonzeros(x(tri(i),tri(j)))) = 0; C{i,j} = nonzeros(z) ; L = cellfun(@length,c); % Number of candidates. s = find(x==0 & L==,); e = find(x==0 & L==0,); % candidates For each empty cell, this function starts with z = : and uses the numeric values in the associated row, column and block to zero elements in z. The nonzeros that remain are the candidates. For example, consider the (,) cell in figure.. We start with z = The values in the first row change z to z = 0 0 0 Then the first column changes z to z = 0 0 0 0 0 The (,) block does not make any further changes, so the candidates for this cell are C{,} = [ ].

Chapter. Sudoku Figure.. The initial candidates for the puzzle in figure.. There are no singletons. Figure.. The first step in the backtracking. Figure. is actually a very difficult puzzle, either by hand or by machine. Figures. through. are a few snapshots of the solution process. The initial candidates are shown in figure.. There are no singletons, so the first recursive step, shown in figure., happens immediately. (We know that this puzzle with a in the (,) cell has no solution, but the program needs to rediscover that fact every time.) Figure. shows how the first column is filled in for the first time at

Figure.. After steps the first column has been filled with possible values, but this track will eventually fail. The cyan values are generated by the backtracking and the green values are implied by the others. Figure.. After, steps, we appear to be close to a solution, but it is impossible to continue. The eventual solution is in figure.. step. The elements in cyan are the tentative values from the backtracking and the elements in green are implied by those choices. But we re still a long way from the solution. After, steps the recursion puts a in the (,) cell and after, steps it tries a. Figure. shows the situation after, steps. We appear

0 Chapter. Sudoku to be close to a solution because of the cells have been assigned values. But the first row and last column already contain all the of the digits from through, so there are no values left for the (,) cell in the upper right corner. The candidate list for this cell is empty and the recursion terminates. (There are also two cells in the third row showing the same singleton. This could be another reason to terminate the recursion, but it is not necessary to check.) Finally, after, steps backtracing finally tries a in cell (,). The is a good idea because less than 00 steps later, after, steps, the program reaches the solution shown in figure.. This is many more steps than most puzzles require. References [] Gordon Royle, Symmetry in Sudoku, http://people.csse.uwa.edu.au/gordon/sudoku/sudoku-symmetry.pdf [] Sudoku Squares and Chromatic Polynomials, http://www.ams.org/notices/000/tx00000p.pdf [] Strategy families, http://www.scanraid.com/strategy\_families [] Nikoli, http://www.nikoli.co.jp/en/misc/00speech\_from\_ussc.htm Exercises. Solve. Solve a Sudoku puzzle by hand.. sudoku puzzle. The EXM program sudoku_puzzle generates different puzzles. The comments in the program describe the origins of the puzzles. How many steps are required by sudoku.m to solve each of the puzzles?. Patterns. Add some human puzzle solving techniques to sudoku.m. This will complicate the program and require more time for each step, but should result in fewer total steps.. sudoku alpha. In sudoku.m, change intstr(d) to char( A +d-) so that the display uses the letters A through I instead of the digits through. See figure.. Does this make it easier or harder to solve puzzles by hand.. sudoku. Modify sudoku.m to solve -by- puzzles with -by- blocks.

F H D B C D G H G C A D F E E F D B C F H Figure.. Use the letters A through I instead of the digits through.