Sudoku as a SAT problem

April 8th, 2007 at 9:51 am

I have just posted a new program (a collection of Ruby scripts, really) to the programs and code page. It is an implementation of a Sudoku solver - an unusual one. The Sudoku is converted into a boolean satisfiability problem (SAT), which is then solved with a SAT solver.

Once I had a task at work of doing some coding for a SAT solver.

To learn more about SAT solvers, I read some papers and tutorials about DPLL - an algorithm to solve SAT which is used as the basis for many modern solvers. To consolidate my understanding of the algorithm, I wrote a short document (dpll.pdf in the package) and coded the algorithm in Perl. The implementation is very simplistic because no heuristic is used. Real SAT solvers are all about complex, sophisticated heuristics.

When Sudoku became popular, I solved a few and quickly reached a conclusion that I don’t find these puzzles interesting. The solution process just felt too mechanic, and reminded me of manually solving a boolean formula for satisfiability (SAT) - the same approach of applying clearly defined inference rules over and over again until a solution is reached. So I searched the net a little (the Google search “sudoku sat” should get you started) and
came upon a few interesting websites and articles that discuss representing Sudoku as a SAT problem. Since I already had a (very) modest SAT solver of my own, I decided to implement the other half of the problem and make it all work together to actually solve Sudokus.

At the same time I started being interested in Ruby, so I wanted to combine the interests and code the solver in that language. I’ve rewritten my Perl SAT solver in Ruby and added a Sudoku solver, which is capable of solving even the hardest Sudokus without any guessing, as far as I know of.

Download the package from here.

Related posts:

  1. ruby
  2. Blogger, Atom and HTTP clients

6 Responses to “Sudoku as a SAT problem”

  1. sick_oscarNo Gravatar Says:

    very good point!

  2. PhilNo Gravatar Says:

    Looking at the code, where do you perform the rule 3 resolution?

  3. elibenNo Gravatar Says:

    Phil,

    I don’t implement the resolution in my SAT solving code, because the data structure I currently use makes it very inefficient. My SAT solving code is trivial, and the data structure is trivial - a mere array of arrays. Real SAT solvers keep separate track for each literal of its appearances. Then, resolution can be actually applied efficiently.

  4. I like sudokuNo Gravatar Says:

    Sudoku is very funny

  5. LoiNo Gravatar Says:

    can u give a good reference for satisfiability problem?
    this is because i have to report for this and i want to have a thorough understanding on it first

  6. elibenNo Gravatar Says:

    Loi,

    Start with Wikipedia: http://en.wikipedia.org/wiki/Boolean_satisfiability_problem

Leave a Reply

To post code with preserved formatting, enclose it in `backticks` (even multiple lines)


generic acomplia purchase cialis overnight delivery cheap acomplia online buy generic clomid buy cialis low price viagra without prescription where to buy cialis lowest price levitra where to buy propecia cheap cialis from canada lasix no prescription viagra without rx cheap accutane tablets viagra online without prescription viagra no rx buying cialis online zithromax viagra in uk free cialis cialis us where to buy acomplia find cialis online buy viagra lowest price accutane prescription buy cheap accutane online cialis buy buy generic cialis online acomplia order propecia online lowest price synthroid synthroid without a prescription synthroid online buy propecia online cheap levitra online where to buy levitra cialis online review synthroid prices cialis generic cialis buy drug buy viagra on line viagra pharmacy cialis for order price of levitra zithromax online where to buy synthroid soma generic generic clomid propecia online stores viagra cheap drug cheap generic soma cialis cheap zithromax online cheap order accutane online purchase zithromax online purchase viagra online buy cheap clomid cheap generic propecia zithromax pharmacy online pharmacy cialis cheapest acomplia cost of cialis no prescription viagra free viagra purchase lasix online cialis from india viagra from india order discount cialis soma online stores find no rx cialis cialis no rx required find viagra without prescription approved cialis pharmacy lasix discount