Escolar Documentos
Profissional Documentos
Cultura Documentos
GeneratorReport
CSE1370AdvancedFirstyearProject
Semester2,2007
DavidWarner20727356
24/10/2007
Supervisor:RezaRafeh
DavidWarner20727356
SudokuSolverandGeneratorReport
TableofContents
INTRODUCTION............................................................................................................................................3
WHATISSUDOKU?..................................................................................................................................................3
EXAMPLEPUZZLE....................................................................................................................................................3
VARIATIONS...........................................................................................................................................................3
MOTIVATIONANDGOALS.........................................................................................................................................3
SOLVINGMETHODS.....................................................................................................................................4
BRUTEFORCESOLVINGMETHOD...............................................................................................................................4
Method1........................................................................................................................................................4
Method2........................................................................................................................................................6
Method3........................................................................................................................................................8
LOGICSOLVINGMETHODS........................................................................................................................................9
SingletonDomains.......................................................................................................................................10
UniqueNumbersinDomainsofaRow,ColumnorBox...............................................................................11
PointingPairs.............................................................................................................................................12
NakedPairs/Triples/Quads........................................................................................................................13
Box/LineReduction....................................................................................................................................14
HiddenPairs.................................................................................................................................................15
GENERATION.............................................................................................................................................16
IMPLEMENTATION.....................................................................................................................................17
LANGUAGEOFIMPLEMENTATION.............................................................................................................................17
SOLVERIMPLEMENTATION......................................................................................................................................18
Measurements.............................................................................................................................................18
MeasurementLimitations............................................................................................................................22
GENERATORIMPLEMENTATION................................................................................................................................24
Remarks.......................................................................................................................................................24
GeneratingaGrid.........................................................................................................................................24
DifferentGridVarieties................................................................................................................................25
Measurements.............................................................................................................................................27
CONCLUSIONS............................................................................................................................................29
ACHIEVEMENTS.....................................................................................................................................................29
PROJECTSTATUS...................................................................................................................................................29
REFERENCES...............................................................................................................................................29
Page
2
DavidWarner20727356
SudokuSolverandGeneratorReport
Introduction
WhatisSudoku?
Sudokuisalogicpuzzlethatrequirestheplayertofilla99gridwiththedigits1to9sothateach
row,columnand33regioncontainseachdigitexactlyonce.Initiallythegridispartiallyfilledthe
puzzlesinthelocalpapersTheAgeandTheAustraliantendtohave26or28hints(squaresinitially
filled),butthisnumbercanbeaslowas17.Fromthisinitialstate,theplayershouldbeableto
progressivelydeducethevaluesoftheblanksquares.
ExamplePuzzle
Anexamplegrid(foundinTheAge)anditssolution:
5 4
7 9
6 3 8 7
8 5 6 2
6 4 7 1
1 4 7 6
8
7
9
1
6
5
3 7 4
9
5 8
3 7 8
4 6 9
2 5 1
3
1
4
8
2
9
2
5
6
7
4
3
9
6
2
5
3
1
7
8
1
6
9
4
5
4
3
2
8
7
1
9
5
3
7
2
4
3
8
9
5
6
6
2
7
4
1
8
4 2 9 6 1 5
7 5 1 8 2 3
8 3 6 4 7 9
Eachrow,columnand33regioncontainsthenumbers1to9exactlyonce.
Variations
Therearemanyvariationsofthetraditional99grid,suchas44grids(with22regions)and1616
grids(with44regions).Thesecanbesolvedinexactlythesamewayasnormalgrids,justusing
smaller(orlarger)domains(e.g.1to16insteadof1to9).Furthermore,therearevariantsthat
introduceextraconstraintsorhavenonsquareregions.Thisreportfocuseson99grids,however.
MotivationandGoals
TheaimofthisCSE1370projectwastoimplementasolver(specifically,astepbysteplogicsolver)
andagenerator.Althoughtherearemanysuchprogramsinexistence,bycreatingonefromscratchI
soughttotestandexpandmyprogrammingknowledge.
Page
3
DavidWarner20727356
SudokuSolverandGeneratorReport
SolvingMethods
BruteForceSolvingMethod
AsimplewaytosolveaSudokupuzzleistosimplytryfillingeachblanksquarewiththenumbers1to
9untilavalidsolutionisfound.Iendedupdevisingthreedifferentimplementationsofthismethod,
asshownbelow.
Method1
Thefirstandmostnavesolverstartsatthetopleftsquareandmovesfromlefttoright,topto
bottom,fillingeachblanksquarewithanumber1to9untilthegridisinvalid(thatis,anumberis
duplicatedinarow,columnor33region)oruntilthegridisfilledandvalid(thatis,solved).Ifthe
gridisinvalid,thesolverwillbacktrackuntilitisvalidandcontinueforwardagain.
Toillustrate,thisistheprocessona44grid:
4 3
2 1 3
3 4 2 1
1 1
2 1
3 4
1
4 3
2 1 3
3 4 2 1
1 2
2 1
3 4
1 3 1
4 3
2 1 3
3 4 2 1
3
4 3
3
2 1
1 3
2 1
3 4
1 2
2 1
3 4
4
4 3
3
2 1
2
4 3
3
2 1
1 3
2 1
3 4
3
4 3
3
2 1
1 3 4
4 3
2 1 3
3 4 2 1
4 3
3
2 1
3
2 1
3 4
Page
4
1 3
4 3
2 1 3
3 4 2 1
2
2 1
3 4
1 2 1
4 3
2 1 3
3 4 2 1
1 4
4 3
2 1 3
3 4 2 1
4 3
3
2 1
1 2
2 1
3 4
1 2 2
4 3
2 1 3
3 4 2 1
4 3
3
2 1
4 3
3
2 1
4
4 3
2 1 3
3 4 2 1
DavidWarner20727356
SudokuSolverandGeneratorReport
4 1
4 3
2 1 3
3 4 2 1
4 2
2 1
3 4
1 3
4 3
3
2 1
4
4 3
3
2 1
1 2
4 3
3
2 1
1 2
4 3
3
2 1
4
1
2
3
1
4
3
2
2
3
4
1
3
2
1
4
4
1
2
3
1 2
4 3
3
2 1
4
1
2
3
1
4
3
2
2
3
1
1
4
1
2
3
3
2
1
4
4 3 1
4 3
2 1 3
3 4 2 1
4 3
1
2 1
3 4
3
2
1
4
4 3
3
2 1
4 3
2 1
3 4
4 2 2
4 3
2 1 3
3 4 2 1
4 3
2 1
3 4
4 3 1 1
4 3
2 1 3
3 4 2 1
1 4
4 3
3
2 1
4 2
2 1
3 4
4 2 1 1
4 3
2 1 3
3 4 2 1
4 2
2 1
3 4
4 2 3
4 3
2 1 3
3 4 2 1
1
4 3
3
2 1
4 2
2 1
3 4
4
1
2
3
4 2
2 1
3 4
4 2 1 2
4 3
2 1 3
3 4 2 1
4
1
2
3
4 3
3
2 1
3
2
1
4
1
4
3
2
2
3
4
1
3
1
1
4
1 2
4 3
3
2 1
3
2
1
4
1
4
3
2
3
2
1
4
1
4
3
2
2
3
2
1
4
1
2
3
2
3
3
1
PerformanceDiscussion
Evenwithoutothermethodsforcomparison,thisfirstimplementationappearstobefairly
inefficient.Tofindthesolutionofthissimple44gridtook37numberplacements.Whensolvinga
99grid,thenumberofnumberplacementscanreachthehundredsofthousands.Whileeventhis
highnumberofguessesisfasttoexecuteonamoderncomputer,therearesomeobvious
improvementsthatcanbemadetothealgorithm.Forinstance,intheillustrationsfirsttwogrids,
thenumber1isplacedimmediatelybesideanothernumber1,clearlybreakingtheruleofSudoku.
Suchimprovementsarecoveredintheimplementationsdisplayedbelow.
Page
5
DavidWarner20727356
SudokuSolverandGeneratorReport
Method2
Thesecondimplementationintroducestheconceptofdomains.Eachsquareinagridhasadomain
ofupto9values(1to9)thatisreducedaccordingtonumbersalreadypresentintheintersecting
row,columnandregion.
4 3
2 1 3
3 4 2 1
Inthisgrid,wecanreducethedomainofthetopleftsquareto{1,4}since2and3alreadyappearin
thefirstcolumn.Likewise,wecanrestrictthedomainofthetoprightsquareto{2},since3and1
occurintherightmostcolumnand4appearsinthetoprightregion.
Soinitially,thedomainsofthegridare:
{1,4}
{1}
2
3
{2,3}
{2}
1
4
{1}
4
3
2
{2}
3
{4}
1
Thissolveractsinthesamewayasthepreviousimplementationexceptthatitrestrictsthedomains
ofthegridbeforeitstarts,onlyusingnumberspresentintheinitialdomains.Whilethedomain
restrictionrequirescomputation,itshouldresultinsignificantlylessbacktrackingtofindthesolution
ofagrid.
Illustratingtheprocess:
1
4 3
2 1 3
3 4 2 1
1 2
2 1
3 4
4
2 1
3 4
1 3
4 3
2 1 3
3 4 2 1
4 3
3
2 1
4 2
2 1
3 4
4 3
3
2 1
4 3
2 1
3 4
1
4 3
3
2 1
4 2 1 2
4 3
2 1 3
3 4 2 1
1 2
2 1
3 4
1 3 1
4 3
2 1 3
3 4 2 1
4 3
3
2 1
4 3
3
2 1
4 3
2 1
3 4
Page
6
4 2 1
4 3
2 1 3
3 4 2 1
1
4 3
3
2 1
4 3 1 2
4 3
2 1 3
3 4 2 1
DavidWarner20727356
SudokuSolverandGeneratorReport
4 3 2 1
1 4 3
2 1 3
3 4 2 1
4
1
2
3
3
2
1
4
2 1
4 3
3
2 1
4
1
2
3
3
2
1
4
2
4
3
2
1
3
4
1
4
1
2
3
3
2
1
4
2
4
3
2
1
3
4
1
PerformanceDiscussion
Incomparisontothefirstimplementation,thisversionappearstobemuchbetter:insteadof37
numberplacementstofindthesolution,only11arerequired.Thereissometradeoffinthatthe
domainsmustbecalculatedonce,initially,requiringsomecomputation.However,theideabehind
thisimplementationisthatthereducednumberofguesseswillmorethanmakeupforthat.
Page
7
DavidWarner20727356
SudokuSolverandGeneratorReport
Method3
Myfinalbruteforcesolverbuildsonthesecondimplementationbyrestrictingthedomainsofthe
grideverytimeanumberisentered.Thisagainshouldreducebacktrackingconsiderably,butthere
willbesometradeoffforcomputingthedomains.
Toillustrateusinga9x9grid:
5 4
7 9
6 3 8 7
8 5 6 2
6 4 7 1
1 4 7 6
3 7 4 6
9 3
5 8
Theinitialdomainsofthetop3regionsare:
{1,2,8,9} {1,2,3,9} {1,2,3,8} {2,6,7,9} {1,2,7,8,9}
5
{1,2,3}
4
{2,6}
7
{1,2,3,4} {1,2,3,5,8}
{2,6}
{1,2,8}
{1,4,6,8}
9
{1,2,3,5} {2,5,6}
{1,2,4,5,9} {1,2,4,9}
6
{2,9}
{1,2,9}
3
{1,2,5}
8
7
Afterthefirstboxisset,thedomainsoftherow,columnandregionarereducedaccordingly:
1
7
{2,4,5,9}
{2,3,9}
{2,3,4}
{2,4,9}
{2,3,8}
{2,3,5,8}
6
{2,6,7,9}
{2,6}
{2,9}
{2,7,8,9}
{1,2,8}
{1,2,9}
5
{2,3}
4
{2,6}
{1,4,6,8}
9
{1,2,3,5} {2,5,6}
3
{1,2,5}
8
7
{7,8,9}
{1,2,8}
{1,2,9}
5
{3}
4
{6}
{1,4,6,8}
9
{1,2,3,5} {2,5,6}
3
{1,2,5}
8
7
Thiswillhappeneverytimeasquareisfilled:
1
7
{4,5,9}
2
{3,4}
{4,9}
{3,8}
{3,5,8}
6
{6,7,9}
{2,6}
{2,9}
PerformanceDiscussion
Aspredicted,thisfinalimplementationresultsinasignificantlyreducedsearchspace.
Page
8
DavidWarner20727356
SudokuSolverandGeneratorReport
LogicSolvingMethods
Thebruteforcesolvingmethods,whilereasonablyquickonamoderncomputerandsimpleto
implement,arenotusedbyhumansolvers.Instead,humanplayersuseavarietyoftechniquesto
logicallydeducethevalueofblanksquares.Thesetechniquesarehardertoprogramthanthebrute
forcemethods,butsinceahumansolvercanusethemtosolvegridsinafairlyshorttime,itshould
followthatacomputerwithitssuperiorcalculatingpowercouldusethemtosolvegridsfasterthan
withthebruteforcemethods.
Furthermore,byusingthesamemethodsasahumansolver,theprogramcanprovideastepbystep
accountofhowtosolveagrid,whichismuchmoreusefultoaplayerthansimplybeingprovided
withthesolvedgrid.
InthisreportIdetail6differenttechniquesforsolvingpuzzles.Therearemanyother,more
advancedtechniques,butthe6methodsdescribedbelowaresufficienttosolvemanypuzzles(only
verydifficultpuzzlescannotbesolvedusingthem).
Page
9
DavidWarner20727356
SudokuSolverandGeneratorReport
SingletonDomains
Whenasquarehasadomainwithonly1numberinit,wecancertainlysetthevalueofthatsquare
totheremainingnumber.
Forexample,inthegrid:
9 6 2 1 8
1 4 9
2 5 9
5 2 6 3
4 9 7
1
4 9 7
7 1 6 8
Thehighlightedsquarehasonlyonenumberleftinitsdomain(afour),sincethenumbers1and8
arealreadypresentinitscolumn,5,2,6and3arealreadypresentinitsrow,and9and7arealready
presentinits33region.Sowecanbecertainthatthehighlightedsquarecancontainnothingother
thana4.
Page
10
DavidWarner20727356
SudokuSolverandGeneratorReport
UniqueNumbersinDomainsofaRow,ColumnorBox
Whenanumberappearsonlyonceinthedomainsofanentirerow,columnor33region,wecan
setthevalueofthesquarewiththatnumberinitsdomain.
Toillustrate,usingthegrid:
2
1
6
3 6 1
8 6
5 9
1 2 4 6
9 7 2
9 5 8
1 7 2
2 1 3 7
Thedomainsofcolumn8(thecolumncontainingtheredhighlightedsquare)are:
{1}
{7,9}
{7,8}
{4}
{8,9}
{2}
{5}
{6,9}
{3}
Thenumber6onlyappearsonceinthe8throwsowecansetthevalueofthatsquare(8,8)to6
andadjustthesurroundingdomainsaccordingly.
Thebluehighlightedsquarecontainsa2(uniqueinthecolumn)andthetwogreenhighlighted
squarescontain1s(uniqueintheirrespectiverows).
Page
11
DavidWarner20727356
SudokuSolverandGeneratorReport
PointingPairs
Ifanumberappearstwiceinaroworcolumn,andthoseoccurrenceshappentobebothinthesame
33regionandbetheonlyoccurrencesinthatregion,wecanremovethatnumberfromanyother
domainsinthatroworcolumn.
Usingthesamegridasbefore:
2
1
6
3 6 1
8 6
5 9
1 2 4 6
9 7 2
9 5 8
1 7 2
2 1 3 7
Thetwohighlightedsquaresconstituteapointingpairof8s.Thiscanbeseenbylookingatthe
domainsofthetopleftregionandthethirdcolumn:
{2}
{1}
{6}
{3}
{4,7}
{5}
{4,7,8,9}
{4,7,9}
{4,7,8}
{4,7,8,9}
{4,7,9}
{4,7,8}
{2}
{3,5,6,7,8}
{1,3,5,6}
{3,4,6,7}
{1}
{4,5,6,8,9}
Sincethenumber8mustbeineitherrow1or3(demonstratedbythedomainsofthetopleft
region),wecanremovetheother8sinthedomainsofthecolumn.
Theupdateddomainsofcolumn3are:
{4,7,8,9}
{4,7,9}
{4,7,8}
{2}
{3,5,6,7}
{1,3,5,6}
{3,4,6,7}
{1}
{4,5,6,9}
Page
12
DavidWarner20727356
SudokuSolverandGeneratorReport
NakedPairs/Triples/Quads
Whenapairofnumbersaretheonlymembersoftwodomainsinarow,columnor33region,those
numberscanberemovedfromtheotherdomainsinthatrow,columnorregion.
Takethedomainsofthisrow:
{6}
{5}
{7,8}
{9}
{3,4,7}
{1}
{2}
{7,8}
{3,4}
Thetwohighlightedsquaresconstituteanakedpair.Thenumbers7and8mustbeineither
column3orcolumn8(sincesettingthevalueofeithersquarewillreducetheothertohavinga
singletondomain),sowecanremoveany7sand8sleftintherow.Inthiscase,wecanremovethe7
fromthedomainofcolumn5:
{6}
{5}
{7,8}
{9}
{3,4}
{1}
{2}
{7,8}
{3,4}
Thisideacaninfactbeextendedtogroupsof3and4numbers.
Takethefollowingdomainsofacertainrow:
{1}
{4,7}
{4,7,9}
{8}
{2,3,4,5,7}
{2,3,4,5}
{6}
{7,9}
{3,4,5,9}
Thethreehighlightedsquaresconstituteanakedtriple.Importantly,thethreesquaresneednt
haveallthreenumbersintheirdomainsjusttheunionofthethreesquaresmusthave3elements.
Thenumbers4,7and9canberemovedfromtheotherdomainsintherow:
{1}
{4,7}
{4,7,9}
{8}
{2,3,5}
{2,3,5}
Page
13
{6}
{7,9}
{3,5}
DavidWarner20727356
SudokuSolverandGeneratorReport
Box/LineReduction
Thistacticissimilartopointingpairs.Ifanumberappearsonlytwiceinthedomainsofarowor
column,andthoseoccurrenceshappentobeinthesame33region,thatnumbercanberemoved
fromthedomainsofthatregion.
Takethedomainsofthefollowingregionandrow(therowcorrespondstorow2ofthebox):
{2}
{1}
{6}
{3}
{4,7}
{5}
{4,7,8,9}
{4,7,9}
{4,7,8}
{1}
{4,7}
{4,7,9}
{8}
{2,3,5} {2,3,5}
{6}
{7,9}
{3,5}
Thenumber4appearsonlytwiceintherow,sowecanremove4fromthedomainsofthesquare:
{2}
{1}
{6}
{3}
{4,7}
{5}
{7,8,9}
{4,7,9}
{7,8}
Page
14
DavidWarner20727356
SudokuSolverandGeneratorReport
HiddenPairs
Whenapairofnumbersoccurexactlytwiceinthedomainsofa33region,theothernumbersin
thedomainsofthosetwosquaresmayberemoved.
Takethedomainsofthisexampleregion:
{1,5,7,8} {1,5,8,9}
{1,2,7}
{1,2,9}
{3}
{1,2,9}
{1,4}
{6}
{1,2,4}
Noticethatthenumbers5and8appearonlytwiceeachintheregion,inthetopleftandtopmiddle
squares.Wecanreducethedomainsofthosetwosquares:
{5,8}
{1,2,7}
{3}
{5,8}
{1,2,9}
{1,2,9}
{1,4}
{6}
{1,2,4}
Page
15
DavidWarner20727356
SudokuSolverandGeneratorReport
Generation
GenerationistheprocessofcreatinganemptySudokupuzzlegrid.Agridshouldhaveaunique
solutionifitistobesolvablebylogic.ThemethodRezaandIagreedontocreategridswastostart
withafilledgridandremovenumbersuntilthenumberoffilledsquaresreachesthedesiredfigure
(mostpuzzlesinTheAustralianandTheAgehave26or28hints,sothisseemstobeareasonable
benchmark).
Duringthesquareremovalprocess,ifthegridatanypointcouldnolongerbesolvedwithlogic,the
generatorshouldbacktrackandtryremovingadifferentnumber.Thustheprocessshouldeventually
terminateeitherwhenallthesquareshavebeenexaminedorwhenthedesirednumberofhintsis
reached.
Page
16
DavidWarner20727356
SudokuSolverandGeneratorReport
Implementation
LanguageofImplementation
ForthisprojectIchosetouseJavaastheprogramminglanguage.Thiswassimplyduetoitbeingthe
languageIhadbeenusingmostrecently.AConstraintProgramminglanguagelikePrologwould
probablyhavebeenbettersuitedtothistasksincesuchlanguagesincludetheabilitytodosomeof
thesolvingworkautomatically.However,Ihavenoexperienceinusingthisprogrammingparadigm.
Additionally,Javahasthebenefitofbeingaportablelanguage(providedthehostmachinehasaJava
VirtualMachineinstalled)althoughthecodewaswritteninWindows,itshouldrunonother
platformswithminimaladjustment.
Page
17
DavidWarner20727356
SudokuSolverandGeneratorReport
SolverImplementation
Ofthe6techniquesshownabove(SingletonDomains,UniqueDomains,PointingPairs,Naked
Pairs/Triples/Quads,Box/LineReduction,HiddenPairs),IimplementedallbutHiddenPairs.Inallthe
gridsItested,itseemedtheHiddenPairstechniquewasnotrequiredtoforfindingthesolution(the
othermethodsweresufficient).
Measurements
BruteForce
Thisgraphgivestheaverageperformanceofthethreeimplementationson4differentgrids.
7000
6000
TimetoSolve(ms)
5000
4000
Implementation1
3000
Implementation2
Implementation3
2000
1000
0
1
TestGridNumber
Theperformanceisasexpected:implementation1istheslowest,andimplementation3isthe
fastest.
Theexactvaluesare:
Grid Number
Implementation 1 (ms)
1
2
3
4
Implementation 2 (ms)
457
170
6375
959
268
131
5187
582
Page
18
Implementation 3 (ms)
147
12
882
267
DavidWarner20727356
SudokuSolverandGeneratorReport
LogicSolver
Thefirsttwomostbasictechniques,SingletonDomainsandUniqueDomains,areveryfasttorunin
myimplementation:bothtake0msaccordingtomymeasurements(unfortunatelyIcouldnot
measureinunitssmallerthanmilliseconds).However,thethreeothertechniquestakesignificantly
longer,averagingabout10msoneachexecution.
12
AverageTimeTaken
10
0
SingletonDomains UniqueDomains
PointingPairs
Naked
Pairs/Triples
Box/Line
Reduction
Page
19
DavidWarner20727356
SudokuSolverandGeneratorReport
HybridSolver
Thissolverimplementationresortstobruteforceifthelogictechniquesarenotsufficienttosolve
thegrid.
Thefollowinggraphshowstheperformanceofthehybridsolverover4differenttestgrids.
1000
900
TimetoSolve(ms)
800
700
600
500
BruteForce
400
Logic
300
200
100
0
1
TestGridNumber
Thistableliststhenumberofnumberplacements(timesanumberisplacedinasquarebythe
solver)foreachpartofthesolveracrossthe4grids:
Grid Number
1
2
3
4
Logic
Brute Force
0
146
0
5279
53
53
55
10
Page
20
DavidWarner20727356
SudokuSolverandGeneratorReport
HybridSolver(BasicLogicTechniquesOnly)
Thissolveristhesameastheversiondescribedaboveexceptthatonlythebasic(Singleton
Domains/UniqueDomains)logictechniquesareusedbeforefallingbacktobruteforce.Therationale
behindthisisthatthosetwotechniquestakeverylittletime(0msonaverage)comparedtothe
otherthreetechniques.
Thefollowinggraphshowstheperformanceofthesolveroverthesame4testgrids.
350
300
TimetoSolve(ms)
250
200
BruteForce
150
Logic
100
50
0
1
TestGridNumber
Thistableliststhenumberofnumberplacements(timesanumberisplacedinasquarebythe
solver)foreachpartofthesolveracrossthe4grids:
Grid Number
1
2
3
4
Logic
Brute Force
53
4
55
10
0
146
0
5279
Fromthegraphandthetable,wecanseethatthetradeoffseemedtoresultinperformancegains
fortestgrids2and4:althoughthebruteforcetimetakenforeachislonger,theoveralltimeis
shorter(sincethecomputationallyexpensivelogictechniquesarenotused).
Page
21
DavidWarner20727356
SudokuSolverandGeneratorReport
MeasurementLimitations
ForthemeasurementsIusedasetof4testgridsselectedfromvarioussources.Theseareshown
below.
TestGrid1(28hints,fromTheAge)
7 5 1 4 6
8 4 9 3 2
1 3 5 8 7
6 2 3 9 4
9 8 7 5 1
4 7 2 6 8
2 9 6 1 5
5 1 8 2 3
3 6 4 7 9
ThisgridcanbesolvedusingonlytheSingletonDomains/UniqueDomainslogictechniques.
5 4
7 9
6 3 8 7
8 5 6 2
6 4 7 1
1 4 7 6
3 7 4 6
9 3
5 8
8
7
9
1
6
5
3
4
2
3
1
4
8
2
9
7
6
5
2
5
6
7
4
3
8
9
1
9
6
2
5
3
1
4
7
8
2
1
6
7
8
4
3
9
5
3
4
5
1
6
9
7
8
2
8
9
7
2
5
3
6
1
4
7
8
9
5
4
1
2
3
6
6
2
3
9
7
8
4
5
1
4
5
1
3
2
6
9
7
8
5
6
2
8
3
7
1
4
9
1
7
8
4
9
2
5
6
3
9
3
4
6
1
5
8
2
7
4
5
9
7
3
6
8
1
2
8
6
1
2
4
3
9
5
7
9
2
4
8
7
5
1
6
3
7
3
5
1
6
9
2
8
4
6
4
7
3
2
8
5
9
1
5
9
3
6
1
4
7
2
8
1
8
2
9
5
7
3
4
6
TestGrid2(28hints,fromTheAge)
2
1
6
3 6 1
8 6
5 9
1 2 4 6
9 7 2
9 5 8
1 7 2
2 1 3 7
Thisgridneedsall5implementedlogictechniquestobesolved.
TestGrid3(26hints,fromTheAustralian)
4 9 6 1
1 5
2 2
5 8 6
4 6
2 5 4
9
9 2 4
2 3 1
2
7
8
4
9
1
6
3
5
Thisgridhasarelativelylownumberofhints.
Page
22
3
1
6
5
8
2
4
7
9
DavidWarner20727356
SudokuSolverandGeneratorReport
TestGrid4(29hints)
4 6
3 7
6 7
7 3
5 4
6 4
3 1
8 4
6 5 1
1
4 9
6 5
1
8 9
5 8
3
4
1
7
5
6
2
9
8
8
5
6
1
9
2
3
7
4
2
9
7
3
8
4
1
5
6
4
3
2
9
6
1
7
8
5
9
1
5
8
4
7
6
2
3
6
7
8
2
3
5
9
4
1
1
2
4
6
7
8
5
3
9
5
6
9
4
2
3
8
1
7
7
8
3
5
1
9
4
6
2
Thisgridcannotbesolvedusingthelogicmethodsusedbymysolver.
Isoughtthatthetestgridsmightvaryinnature,but4isstillaverysmallsampleoftheover5billion
possiblegrids.Withmoretime,Iwouldtestmanymoregrids.
Page
23
DavidWarner20727356
SudokuSolverandGeneratorReport
GeneratorImplementation
Remarks
Thegeneratordemonstratesthatthenumberofhintsgiveninapuzzleisnotnecessarilyagood
measureofhowdifficultapuzzleisforahumanplayertheexampleEasygridbelowhasonly24
hintsandyetrequiresnotechniquesotherthanthebasicunique/singletondomainmethods,while
theexampleHardgridhas26hintsandrequirestheothertechniquestobeusedseveraltimes.
GeneratingaGrid
GeneratingtheFilledGrid
Creatingafilledgridturnedouttobeaneasytasksimplyrunningthebruteforcesolveronan
empty(0squaresfilled)gridwillproduceavalidsolvedgrid.However,intheoriginal
implementationthesolverwouldproducethesamegrideverytime(sinceitrunsfromlefttoright,
toptobottomalways).ByinsteadtakingarandompaththroughthegridarandomsolvedSudoku
gridmaybeproduced.
FirstSquareRemovalAttempt
Myfirstattempttoprogressivelyremovesquaresfromthefilledgriddidnotfullysucceed.My
methodwastoremoveasquare,resetthedomainsoftherow,columnand33regionintersecting
thatsquareandthencheckifthevalueofthatsquarewasstilldeduciblebylogic.Thisworkedfor
hintcountsofabout35andupwards,butneverproducedanygridswithfewerhints.Furthermore,
thegridsproducedwerealwayseasythatistosay,theyneverrequiredthemoreadvancedlogic
techniques(pointingpairs,etc.)tobesolved.
SecondSquareRemovalAttempt
Mysecondapproachwastosimplyremovenumbersandcheckwiththebruteforcesolverwhether
thegridstillhadauniquesolutionifitdid,anothernumberwouldberemoved,ifnot,adifferent
numberwouldberemoved.Theprocesswouldrepeatuntilthedesirednumberoffilledsquareswas
reached.
Thismethodworkedreasonablywellitproducedgridswitharelativelysmallnumberofhints
(downtoabout25).Unfortunately,thetimetakentoproduceagridwasextremelyvariable
anywherefromafewsecondstomanyminutes.Thevariationiscausedbytherandompaththatthe
generatorusesthedecisionastowhichsquaretoremoveisentirelyrandom.Thusthegenerator
mightgetluckyandhappentosuccessivelypickhintsthatmaintainauniquesolution,quickly
findinganemptypuzzle,orthegeneratormightdothereverseandconsistentlypicksquareswhich
cannotbeemptiedwithoutincreasingthesolutioncount.Furthermore,thebruteforcesolverwas
beingrunagainandagain,increasingthetimetakentofindagrid.
ThirdSquareRemovalAttempt
Thistime,insteadofusingthebruteforcesolvertocheckwhetherthegeneratedgridhadaunique
solution,thelogicsolverwasusedtocheckifthegridwassolvable.Sincethelogicsolverisnearly
alwaysfasterthanthebruteforcesolver,thisshoulddecreasethetimetakentofindagrid.However,
thevariabilityremainsthegeneratorstillremovessquaresatrandom.
Page
24
DavidWarner20727356
SudokuSolverandGeneratorReport
DifferentGridVarieties
DifficultyLevel
Thegeneratorprovides2differentdifficultylevels:easyandhard.Easygridsaresolvablebyusing
onlytheuniqueandsingletondomaintechniques(seetheLogicSolversectionofthisreport),while
hardgridsrequireatleastoneuseoftheothertechniques.
Symmetry
TheSudokupuzzlespublishedinTheAgeandTheAustralianhavetheirhintsdistributedina
symmetricpatternacrosstheboardforaestheticreasons.Thegeneratorprovidestheoptionof
generatingsuchgrids.However,thenumberofpossibleemptygridsthatcanbemadefromafilled
gridissmallerifsymmetryisrequired,sousingthesymmetricaloptioncanslowdowngeneration.
ExampleGeneratedGrids
EasyGrid(24hints)
2
7
9 7 1
3 9 5
1
8 3 2
8
7 2 3
2 5 4 1
8 5
8
4
9
7
2
3
5
6
1
2
7
6
9
1
5
4
8
3
5
3
1
8
6
4
7
2
9
4
9
2
3
5
6
1
7
8
6
5
7
1
8
2
9
3
4
3
1
8
4
9
7
6
5
2
9
6
5
2
3
1
8
4
7
7
8
3
5
4
9
2
1
6
1
2
4
6
7
8
3
9
5
4
1
6
9
7
2
3
8
5
8
2
5
3
1
4
9
6
7
9
7
3
8
5
6
2
1
4
1
5
2
7
8
9
4
3
6
3
8
4
6
2
5
7
9
1
7
6
9
1
4
3
8
5
2
5
3
7
2
9
1
6
4
8
2
9
1
4
6
8
5
7
3
6
4
8
5
3
7
1
2
9
HardGrid(26hints)
9 1
1 5 8 6
3 9
3 6 4
1 2 9
9 8 6
1 3 7
5
6
4
7
1
2
Page
25
DavidWarner20727356
SudokuSolverandGeneratorReport
EasySymmetricGrid(27hints)
6
3
8 2
8
7 5
2 8
7 3
4
3 1 6
9 8
3
4
3
3
1 8
5
1
9
5
1
9
4
6
3
2
7
8
8
6
4
2
7
1
3
9
5
2
3
7
8
5
9
4
6
1
3
2
5
1
4
7
9
8
6
7
9
1
6
3
8
5
4
2
4
8
6
5
9
2
1
3
7
1
7
8
9
2
4
6
5
3
6
4
3
7
1
5
8
2
9
9
5
2
3
8
6
7
1
4
1
6
2
9
3
5
8
7
4
4
1
9
5
7
2
6
8
3
6
7
5
3
1
8
2
4
9
2
3
8
4
6
9
5
1
7
HardSymmetricGrid(30hints)
4
2
6
7
2
4
1
5
4 6
9
6 9 5
7 1
6 7 8
4 3
6
7
5
1
4 1
3
5
4
1
2
6
7
9
8
Page
26
9
8
1
7
5
3
4
2
6
7
2
6
8
9
4
1
3
5
5
4
3
2
8
7
9
6
1
8
9
7
6
4
1
3
5
2
DavidWarner20727356
SudokuSolverandGeneratorReport
Measurements
Asmentionedabove,eveninitsfinalimplementationthegeneratorisextremelyvariableastohow
longittakestogenerateagridwithagivennumberofhintsasitclearssquaresinarandomorder.
Assuchasimpleaverageisnotanappropriatemeasure.Instead,Iranthegenerator10timesfor
variousgridtypesandrecordedthetimes.
EasyGrid(24hints)
Attempt TimeTaken
1
2
3
4
5
6
7
8
9
10
168ms
139ms
1445ms
142ms
146ms
127ms
2260ms
23741ms
107ms
114ms
Althoughthenumbersgenerallyhoveredaroundthe140msmark,attempts3,7andparticularly8
demonstratetheextremevariabilityofthegenerator.
HardGrid(28hints)
Attempt TimeTaken
1
2
3
4
5
6
7
8
9
10
6878ms
11422ms
25651ms
16665ms
10757ms
17517ms
26599ms
12683ms
24456ms
43491ms
Thetimetakentogenerateahardgridisonaverageveryhighcomparedtogeneratinganeasygrid,
butthetimeisstillquitevariable.
Page
27
DavidWarner20727356
SudokuSolverandGeneratorReport
EasySymmetricGrid(27hints)
Attempt TimeTaken
1
2
3
4
5
6
7
8
9
10
207ms
605451ms
200ms
68ms
78ms
72ms
47ms
53ms
66ms
53ms
Attempt#2isagoodexampleofhowtheprocessmaytakeaverylongtimetocompletebutthatit
willeventuallyterminate.
HardSymmetricGrid(29hints)
Attempt TimeTaken
1
2
3
4
5
6
7
8
9
10
3194ms
770376ms
12229ms
17060ms
12047ms
3430ms
14797ms
26434ms
11047ms
16254ms
Onceagain,thetimesvarysignificantly.
Page
28
DavidWarner20727356
SudokuSolverandGeneratorReport
Conclusions
Achievements
ThroughgeneratingthisSudokusolverandgeneratorIfeelIhaveimprovedmyprogrammingability.
ThiswasperhapsthelargestprogramintermsoftimeinvestedandlinesofcodewrittenthatIhave
created.Thecodeisnotofthehighestquality,anditisseverelylackingindocumentation,butsome
oftheproblemsposedbytheprojectwereagoodchallengetosolve.Writingthesolverhas
demonstratedtheadvantagesofsmartalgorithmsovernavealgorithmsevidencedinthe
measurementsabove.Finally,Ihaveexperiencedparticipatinginwhatcouldbecalledasmall
researchproject(usefulforfuturework).
ProjectStatus
ThelogicsolverportionoftheprogramissufficientforsolvingmanySudokupuzzles.However,the
implementationcouldlikelybeimprovedtoexecutefaster.Furthermore,therearemanylogic
solvingtechniquesthathavenotbeenimplemented.
Thegenerator,ontheotherhand,iscurrentlyratherpoorlyimplemented.Itworksinthatit
successfullygeneratesgridsofagivennumberofhints,butthevariabilityofthetimetakento
generateisasignificantweakness.Additionally,thegeneratorcurrentlyonlyofferstwolevelsof
puzzledifficulty(easyandhard)ascaleofdifficultieswouldbeperhapsmoreuseful.
References
Bau,D.(2006,September4).SudokuGenerator.RetrievedOctober24,2007,fromdavidbau.com:
http://davidbau.com/archives/2006/09/04/sudoku_generator.html
Stuart,A.(2006,November21).SudokuSolver.RetrievedOctober24,2007,fromScanraidLtd:
http://www.scanraid.co.uk/sudoku.htm
Page
29