{"id":21,"date":"2005-05-21T18:39:55","date_gmt":"2005-05-21T08:39:55","guid":{"rendered":"http:\/\/somethinkodd.com\/oddthinking\/?p=21"},"modified":"2007-10-07T20:59:03","modified_gmt":"2007-10-07T10:59:03","slug":"21","status":"publish","type":"post","link":"https:\/\/www.somethinkodd.com\/oddthinking\/2005\/05\/21\/21\/","title":{"rendered":"Pursuing Sudoku with Pseudo-code"},"content":{"rendered":"<p><!-- UnMarkedDown_2_01132526274--><\/p>\n<p>In this weekend&#8217;s edition of the <a href=\"http:\/\/www.smh.com.au\/\">Sydney Morning Herald<\/a>, they introduced and over-hyped a type of logic puzzle called <a href=\"http:\/\/www.sudoku.com\">Sudoku<\/a>.<\/p>\n<p>It is a logic puzzle with fairly simple rules. There is a 9&#215;9 grid, broken down into nine 3&#215;3 blocks. The aim is to layout the digits 0 to 9 such that they follow some simple constraints &#8211; no row, column or block may have a repeated digit.<\/p>\n<p><!--more--><\/p>\n<p>You can find far a better description at the <a href=\"http:\/\/www.sudoku.com\">Sudoku home page<\/a>.<\/p>\n<p>Okay, so a new type of puzzle?  I&#8217;m up for that. <a href=\"http:\/\/www.somethinkodd.com\/oddthinking\/2005\/05\/21\/puzzling-over-puzzles\/\">Earlier today<\/a>, I wrote about my approach to tackling such puzzles.<\/p>\n<p>Sudoku turns out to be a similar type of puzzle to the unfortunately-named <a href=\"http:\/\/www.geocities.com\/Heartland\/Plains\/4484\/logic.htm\">Logic Problems<\/a> class of puzzles &#8211; you know, the type with clues like &#8220;The butcher&#8217;s wife plays cribbage with the redhead on Thursdays.&#8221;  I have seen this type of problem solved by some software written by a colleague in 1992 as he experimented with the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Prolog\" title=\"Wikipedia definition of Prolog\" class=\"wikipedia\">Prolog programming language<\/a>.<\/p>\n<p>As I read the hype about the puzzle, I thought the puzzle would probably fall down to a similar method &#8211; which may, or may not, end up being brute-force &#8211; I could never be sure with Prolog software.<\/p>\n<p>Then something in the article caught my eye. Wayne Gould, a sudoku expert and owner of the <a href=\"http:\/\/www.sudoku.com\">Sudoku home page<\/a>, chided a beginner:<\/p>\n<blockquote><p>&#8220;So you tried trial and error, then? That&#8217;s not the way to go. One golden rule of sudoku is never guess.&#8221;<\/p><\/blockquote>\n<p>Never guess? That&#8217;s an excellent sign from the perspective of solving it automatically. However, a terrible thought occurred to me.  Could it be that this entire puzzle, despite all of the hype, is merely using simple elimination, with no higher thought processes?  Could the whole thing be that straight-forward?<\/p>\n<p>Let me give an example.<\/p>\n<p>Suppose we have five grid cells in a row A, B, C,  D and E which must be assigned unique values 1, 2, 3, 4 or 5.<\/p>\n<p>Suppose we already know some constraints:<\/p>\n<blockquote><p>\nA is NOT 3, 4 or 5<br \/>\nB is NOT 3, 4 or 5<br \/>\nC is NOT 5<br \/>\nD is NOT 1 or 5<br \/>\nE is NOT 1\n<\/p><\/blockquote>\n<p>Now, we know that one of the cells must have the value 5, and only E can have the value 5, so by elimination on E must equal 5.<\/p>\n<p>That&#8217;s simple.<\/p>\n<p>It takes a little bit more thought to see that A and B both can only be 1 or 2, so therefore no other cell can have the value 1 or 2 &#8211; that would mean that there wasn&#8217;t room for both A and B.  <\/p>\n<p>We can conclude that:<\/p>\n<blockquote><p>\nA is NOT 3, 4 or 5<br \/>\nB is NOT 3, 4 or 5<br \/>\nC is NOT <bold>1, 2 or 5<br \/>\nD is NOT 1<\/bold><bold>, 2<\/bold> or 5<br \/>\nE is <bold>5<\/bold>\n<\/p><\/blockquote>\n<p>You could draw this same conclusion by backtracking;  guessing that D = 2 would eventually lead to the same conclusion. However recognising this pattern generally is a bit harder than simple elimination.<\/p>\n<p>For all I knew, the general Sudoku problem <em>could<\/em> require brute-force to solve (i.e. it could be <a href=\"http:\/\/en.wikipedia.org\/wiki\/NP-complete\" title=\"Wikipedia definition of NP-complete\" class=\"wikipedia\">NP-complete<\/a>). Alternatively, they <em>could<\/em> require more sophisticated logic than simply ticking off items until a conclusion could be drawn. <\/p>\n<p>However, it could be that the subset of puzzles that actually get printed in the newspaper might not be so hard. The hype just seemed too much; perhaps the printed puzzles were actually trivial.<\/p>\n<p>The weekend SMH ones are supposed to be the hardest ones they publish.  If my theory was right, even the hardest published ones would fall easily &#8211; not to brute-force guessing. but to methodical elimination.<\/p>\n<p>So away I went.<\/p>\n<p>I wrote 150 lines of Python.<\/p>\n<p>The design consists of three classes, Cells, Neighbourhoods (representing rows, columns and blocks) and the Big Grid.<\/p>\n<p>The Big Grid is just a container which creates the cells and neighbourhoods, and helps them link together, so each cell knows of its three neighbourhoods, and each neighborhood knows of its nine cells.<\/p>\n<p>Each cell is responsible for tracking what values are still legal for that cell. If it every works out by eliminiation what the correct value was for that cell, it notifies its three neighbourhoods of the happy news.<\/p>\n<p>Each neighbourhood is responsible for two tasks &#8211; (i) informing the unsolved cells of values that are no longer legal (because they were used by others cells in the neighbourhood) and (ii) watching out for the case that only one cell in the neighbourhood could have a particular value, meaning it could be resolved by elimination.<\/p>\n<p>Notifications go back and forth between the cells informing their neighbourhoods of news, who in turn notify the neighbouring cells, who themselves draw further conclusions and call back to the neighbourhoods. <\/p>\n<p>The system was very recursive &#8211; the recursion tails out when a cell is informed of a fact of which it is already aware. Cells also unsubscribe to notifications once they are resolved.<\/p>\n<p>An optimisation would be to push all notifications on a stack and then iterate through them rather than recursing. With this technique, it would be possible to prioritise notifications (&#8220;You are a 6! You can stop listening now.&#8221; is a more important message than &#8220;You are not a 3.&#8221;), to consolidate them (&#8220;Here is a bunch of messages at once for this cell.&#8221;), and to dismiss them faster (&#8220;This cell is no longer subscribing &#8211; discard the message.&#8221;)<\/p>\n<p>However, the speed of the application was not slow, and these optimisations were not required.<\/p>\n<p>So that was my experimental method &#8211; what were the results?<\/p>\n<p>An empty grid has 91 cells.  A total of 31 clues were provided. <\/p>\n<p>After entering 29 of the clues, the software had still failed to fill in additional cells.  After entering 30 of them, it was able to work out only a few of the cells.<\/p>\n<p>After entering the final clue, it worked out a few more &#8211; a total of 12 cells of a possible 50.<\/p>\n<p>My hypothesis was wrong: Sudoku is not a trivial puzzle, relying only on elimination.<\/p>\n<p>Only after getting this far, did I let myself search the web to see what other Sudoku solvers are out there.<\/p>\n<p>The first I looked at was <a href=\"http:\/\/www.sudoku-solver.com\/\">Sudoku Solver<\/a> (I didn&#8217;t install it. Install at own risk.) <\/p>\n<p>The techniques Sudoku Solver uses are not surprising. The manual states:<\/p>\n<blockquote><p>The system will then attempt to solve the puzzle deterministically. For most puzzles this will yield the solution. For some puzzles the deterministic capability produces no further progress, either because the puzzle has more than one solution or because it is very subtle. At this point the system does a structured exploration of solutions, looking for inconsistencies until the solution is found.<\/p><\/blockquote>\n<p>A far more interesting site is the similarly named <a href=\"http:\/\/www.sudokusolver.co.uk\/\">Sudoku Solver by logic<\/a>. <\/p>\n<p>While this solver, too, will resort to backtracking  (They call it &#8220;guess-and-check&#8221;.) where necessary, the authors seem to have a similar aversion to it.  Their web-site explains:<\/p>\n<blockquote><p>There&#8217;s an argument as to whether [guess-and-check]  is &#8216;logic&#8217;, so we have made it an option using a tickbox.<\/p><\/blockquote>\n<p>Even with backtracking turned off, their software quickly solved the puzzle in the SMH that had thwarted my software.  The solution even showed that my half-finished initial practice attempt at solving the same Sudoku manually had a mistake in it.<\/p>\n<p>The authors take the time to <a href=\"http:\/\/www.sudokusolver.co.uk\/solvemethods.html\">explain their methods<\/a>. They seem to have neglected to mention the  most obvious method which they have clearly implemented. (i.e If a cell has eight unique neighbours, it must be the only unused number.)  Using their terminology, I only implemented that obvious method, plus Method A. My example above, demonstrating more sophisticated logic would have been cracked by their Method C.<\/p>\n<p>They also admit that they haven&#8217;t completely cracked it, and <a href=\"http:\/\/www.sudokusolver.co.uk\/challenge.html\">offer you the chance to help<\/a> &#8211; take a stab at one of the solutions they can&#8217;t solve automatically, describe how you solved it, and they will try to implement that method.<\/p>\n<p>An exciting idea, until a horrible thought occurs &#8211; what if the problem is <a href=\"http:\/\/en.wikipedia.org\/wiki\/NP-complete\" title=\"Wikipedia definition of NP-complete\" class=\"wikipedia\">NP-complete<\/a>?<\/p>\n<p>Sure enough, the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Sudoku\" title=\"Wikipedia definition of Sudoku\" class=\"wikipedia\">Wikipedia article on Sudoku<\/a> confirms the bad news:  Sudoku is NP-complete.  Elegant logic may speed up the processing a little, but when it all boils down to it, you are going to have to try out values one by one until you find one that works.<\/p>\n<p>Wayne Gould, you&#8217;re wrong! The golden rule of sudoku is, sometimes, guessing is the only way forward.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A study of the solvability of a puzzle that growing in popularity<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"","footnotes":""},"categories":[35,21,33,34],"tags":[91,102,119],"class_list":["post-21","post","type-post","status-publish","format-standard","hentry","category-heroic-failures","category-observation","category-puzzle-solving","category-software-development","tag-puzzles","tag-solutions","tag-sudoku"],"_links":{"self":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/21","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/comments?post=21"}],"version-history":[{"count":0,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/21\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/media?parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/categories?post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/tags?post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}