13
Modeling Sudoku as a CNF Formula Spring 2017 CSCE 235H Introduc7on to Discrete Structures URL: cse.unl.edu/~cse235h All ques7ons: Piazza

Modeling Sudoku as a CNF Formula - Computer Science and ...cse.unl.edu/~choueiry/S17-235H/files/SATslides02.pdf · CSCE 235H Modeling Sudoku 13 Solving Sudoku • Total number of

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Modeling Sudoku as a CNF Formula - Computer Science and ...cse.unl.edu/~choueiry/S17-235H/files/SATslides02.pdf · CSCE 235H Modeling Sudoku 13 Solving Sudoku • Total number of

ModelingSudokuasaCNF

Formula

Spring2017CSCE235HIntroduc7ontoDiscreteStructures

URL:cse.unl.edu/~cse235hAllques7ons:Piazza

Page 2: Modeling Sudoku as a CNF Formula - Computer Science and ...cse.unl.edu/~choueiry/S17-235H/files/SATslides02.pdf · CSCE 235H Modeling Sudoku 13 Solving Sudoku • Total number of

ModelingSudokuCSCE235H 2

SudokuRules: •  Each cell filled with a

number 1…9 •  Each

•  row, •  column, and •  3x3 box

contains all nine numbers (i.e., no duplicates)

Page 3: Modeling Sudoku as a CNF Formula - Computer Science and ...cse.unl.edu/~choueiry/S17-235H/files/SATslides02.pdf · CSCE 235H Modeling Sudoku 13 Solving Sudoku • Total number of

ModelingSudokuCSCE235H 3

SudokuRules: •  Each cell filled with a

number 1…9 •  Each

•  row, •  column, and •  3x3 box

contains all nine numbers (i.e., no duplicates)

Page 4: Modeling Sudoku as a CNF Formula - Computer Science and ...cse.unl.edu/~choueiry/S17-235H/files/SATslides02.pdf · CSCE 235H Modeling Sudoku 13 Solving Sudoku • Total number of

ModelingSudokuCSCE235H 4

DefiningtheVariables

•  assertsthanthecellinrowandcolumnisassignedvalue.p(i, j, n) i

j n

p(5, 8, 1)

Page 5: Modeling Sudoku as a CNF Formula - Computer Science and ...cse.unl.edu/~choueiry/S17-235H/files/SATslides02.pdf · CSCE 235H Modeling Sudoku 13 Solving Sudoku • Total number of

ModelingSudokuCSCE235H 5

AssigningNumbers(1)

•  Onenumberin1…9incellinrowi, column j

•  Everycellcontainsatleastonenumber:

p(i, j, 1) _ p(i, j, 2) _ . . . _ p(i, j, 9)

i=1

j=1

9_

n=1

p(i, j, n)

Page 6: Modeling Sudoku as a CNF Formula - Computer Science and ...cse.unl.edu/~choueiry/S17-235H/files/SATslides02.pdf · CSCE 235H Modeling Sudoku 13 Solving Sudoku • Total number of

ModelingSudokuCSCE235H 6

AssigningNumbers(2)

•  Thecellinrowi column j cannottaketwonumbers

x = 1, y = 2, 3, 4, 5, 6, 7, 8, 9

x = 2, y = 3, 4, 5, 6, 7, 8, 9

x = 3, y = 4, 5, 6, 7, 8, 9

x = 4, y = 5, 6, 7, 8, 9

x = 5, y = 6, 7, 8, 9

x = 6, y = 7, 8, 9

x = 7, y = 8, 9

x = 8, y = 9

(x, y 2 1 . . . 9) ^ x 6= y ⌘ x, y = x+ 1, . . . , 9

⌘ 8x, y(x 6= y ! (¬p(i, j, x) _ ¬p(i, j, y)))8x, y(x 6= y ! ¬(p(i, j, x) ^ p(i, j, y)))

Page 7: Modeling Sudoku as a CNF Formula - Computer Science and ...cse.unl.edu/~choueiry/S17-235H/files/SATslides02.pdf · CSCE 235H Modeling Sudoku 13 Solving Sudoku • Total number of

ModelingSudokuCSCE235H 7

AssigningNumbers(3)

•  Eachcellmustcontainanumber:

•  Everycellcontainsatmostonenumber:

i=1

j=1

9_

n=1

p(i, j, n)

i=1

j=1

x=1

y=x+1

(¬p(i, j, x) _ ¬p(i, j, y))

Page 8: Modeling Sudoku as a CNF Formula - Computer Science and ...cse.unl.edu/~choueiry/S17-235H/files/SATslides02.pdf · CSCE 235H Modeling Sudoku 13 Solving Sudoku • Total number of

ModelingSudokuCSCE235H 8

Restric7ngRows,Columns

•  Everyrowcontainseverynumber:

•  Everycolumncontainseverynumber:

i=1

n=1

9_

j=1

p(i, j, n)

j=1

n=1

9_

i=1

p(i, j, n)

Page 9: Modeling Sudoku as a CNF Formula - Computer Science and ...cse.unl.edu/~choueiry/S17-235H/files/SATslides02.pdf · CSCE 235H Modeling Sudoku 13 Solving Sudoku • Total number of

ModelingSudokuCSCE235H 9

Restric7ng3x3Boxes

•  Every3x3boxcontainseverynumber:

r=0

s=0

n=1

3_

i=1

3_

j=1

p(3r + i, 3s+ j, n)

Page 10: Modeling Sudoku as a CNF Formula - Computer Science and ...cse.unl.edu/~choueiry/S17-235H/files/SATslides02.pdf · CSCE 235H Modeling Sudoku 13 Solving Sudoku • Total number of

ModelingSudokuCSCE235H 10

RedundantClauses

•  Sudokuproblemcanbemodeledinmanyways

•  Mayinvolveredundantclausesthatcanberemovedtoobtainanequivalentformula

•  Canbegeneratedusinginferencerulesonotherclausesintheproblem

•  Redundantclausesmaybeusefulandspeedthesolvingprocess

Page 11: Modeling Sudoku as a CNF Formula - Computer Science and ...cse.unl.edu/~choueiry/S17-235H/files/SATslides02.pdf · CSCE 235H Modeling Sudoku 13 Solving Sudoku • Total number of

ModelingSudokuCSCE235H 11

DefiningIni7alSetup

•  Ini7alsetupisdefinedbyincludingunaryclausescontainingthevariablescorrespondingtothevaluesinthefilledcells ...

^p(2, 2, 8)^p(2, 3, 9)^p(2, 4, 4)^p(2, 5, 1)^p(3, 3, 6)^ . . .

Page 12: Modeling Sudoku as a CNF Formula - Computer Science and ...cse.unl.edu/~choueiry/S17-235H/files/SATslides02.pdf · CSCE 235H Modeling Sudoku 13 Solving Sudoku • Total number of

ModelingSudokuCSCE235H 12

SudokuCNFFormula

•  729variables•  324clauseswith9literals•  2916clauseswith2literals•  3240totalclauses(+clausesforini7alsetup)

Page 13: Modeling Sudoku as a CNF Formula - Computer Science and ...cse.unl.edu/~choueiry/S17-235H/files/SATslides02.pdf · CSCE 235H Modeling Sudoku 13 Solving Sudoku • Total number of

ModelingSudokuCSCE235H 13

SolvingSudoku

•  Totalnumberofpossibleassignments:•  Tes7ngonebillionassignmentsasecond:

•  ModernSATsolverscansolveSudokuinmillisecondsbyaggressivelypruningthesearchtree

2729 = 2.824014⇥ 10219

8.94876⇥ 10202 years