Boolean satisfiability, SAT for short, asks a simple question: given a formula over true/false variables, is there an assignment that makes it true? It is the textbook NP-complete problem, which is usually where the lesson stops. In theory it is hard. In practice, modern SAT solvers handle very large formulas, and they sit under package dependency resolvers, hardware verification tools and planners.
The gap between "exponential in theory" and "works in practice" is filled by a handful of specific ideas. I wanted to understand them, so I wrote sat-solver: a boolean satisfiability solver from scratch in Rust, in two steps. First the classic DPLL algorithm, then CDCL (conflict-driven clause learning) in the style of MiniSat. It reads and writes DIMACS CNF and includes a Sudoku solver as a demo. It has no dependencies. This post is about what I built and what it teaches.
What I built
The formula representation. cnf.rs stores a formula in conjunctive normal form: a list of clauses, each a list of literals. A literal is packed into one integer as 2*var + sign, so a variable and its negation sit next to each other. The DIMACS parser handles comments, clauses that span several lines, and the SATLIB % end marker.
DPLL, the baseline. dpll.rs is the classic algorithm: unit propagation to a fixpoint, branching on a literal from the shortest unsatisfied clause, and chronological backtracking. It is simple enough to trust, and that is its job here: it is the baseline the CDCL solver is checked against.
CDCL, the real solver. cdcl.rs has the techniques that make modern solvers work:
- Two watched literals with blocker literals, so most satisfied clauses are skipped without touching clause memory.
- 1-UIP conflict analysis with local clause minimization, and non-chronological backjumping to the second-highest decision level in the learnt clause.
- VSIDS variable activity, kept in an indexed binary heap, plus phase saving.
- Luby restarts, with a unit of 100 conflicts.
- Learnt clause deletion: the less active half is dropped once the database passes a growing limit (binary and locked clauses are kept), and then garbage collected.
Sudoku as a demo. sudoku.rs encodes a puzzle with 729 variables ("cell (r,c) holds d"), exactly-one constraints per cell, row, column and box, and the givens as unit clauses. is_unique blocks the first solution and solves again: if the second run is UNSAT, the puzzle has one solution.
Instance generators. gen.rs produces pigeonhole formulas (UNSAT when there are more pigeons than holes), n-queens, and uniform random k-SAT.
How to use it
Clone the repository and build it. The binary is called sat.
git clone https://github.com/AbdeldjalilChougui/sat-solver cd sat-solver cargo build --release S=target/release/sat $S solve tests/cnf/unsat_php_7_6.cnf # exit code 10 = SAT, 20 = UNSAT $S solve --dpll tests/cnf/sat_queens_10.cnf # use the DPLL baseline $S gen random 200 852 42 | $S solve - # random 3-SAT near the phase transition $S gen php 9 8 > php.cnf # pigeonhole instance (UNSAT) $S sudoku "8..........36......7..9.2...5...7.......457.....1...3...1....68..85...1..9....4.." Output follows the SAT competition format: an s SATISFIABLE line and v ... 0 lines with the model. Every model is checked against the formula before it is printed, so a "SAT" answer is never taken on faith.
The third command is worth a closer look. gen random 200 852 42 makes a random 3-SAT formula with 200 variables and 852 clauses, using seed 42. The ratio of about 4.26 clauses per variable is the region where random 3-SAT is hardest, the phase transition.
What I learned
Learning from conflicts is the big idea. DPLL, when it hits a contradiction, undoes its last decision and tries the other value. It learns nothing, so it can hit the same contradiction again in another part of the search. CDCL asks why the conflict happened. It walks back through the implications to a clause that sums up the cause, adds that clause to the formula, and jumps back, often over several decisions that had nothing to do with the problem. The search never makes that mistake again.
Most of the time is spent in propagation, so the data structure matters most. A naive solver visits every clause that contains a literal each time it is assigned. With two watched literals, a clause is only visited when one of its two watched literals becomes false, and nothing needs to be undone on backtracking. Blocker literals add a cheap check that often avoids opening the clause at all.
Heuristics carry real weight. VSIDS bumps the activity of variables involved in recent conflicts, so the solver keeps working where the trouble is. Phase saving remembers the last value of each variable. Restarts abandon the current search path but keep the learnt clauses. None of these changes what the solver can prove. All of them change how long it takes.
Forgetting is necessary. Learnt clauses pile up and slow propagation down. Dropping the less active half from time to time keeps the solver moving.
A simple solver is the best test for a complex one. CDCL has many places for subtle bugs. Checking it against DPLL on random instances, and verifying every model, catches them. An encoding is a program too: writing the Sudoku constraints showed me how much of "using SAT" is choosing the variables well.
Things to know
This is a learning project. The README is specific about where it stands:
- On a laptop it solves random 3-SAT with 200 variables at the threshold in well under a second, but it is roughly an order of magnitude slower per conflict than MiniSat.
- It lacks recursive minimization, LBD-based clause management, preprocessing and inprocessing.
- There are no incremental assumptions.
- There is no proof output (DRAT), so UNSAT answers are not independently certified. Only SAT models are checked.
What was tested. cargo test covers:
- the known instances in
tests/cnf/, where the file name prefixsat_orunsat_is the expected answer; - pigeonhole from 3 pigeons in 2 holes up to 8 in 7;
- n-queens for n = 1 to 12, which is UNSAT exactly for n = 2 and 3;
- CDCL and DPLL agreeing on 150 random 3-SAT instances in the hard region, with every model verified;
- larger random instances;
- known Sudoku puzzles, including Arto Inkala's "hardest Sudoku", with their published solutions and uniqueness checks;
- the CLI's exit codes.
Debug builds use smaller random instances. Run cargo test --release for the full sizes.
Why I built it
SAT solvers are a case where the engineering is as interesting as the theory. The algorithm on paper is short. What makes it work is a set of techniques that each sound minor and together change what is solvable. Reading about watched literals and 1-UIP learning does not show why they matter. Building DPLL first, then adding the CDCL parts one at a time, with the same instances to run at each step, makes the effect of each visible.
Doing it in two steps also gave me a working oracle at every stage, which is the reason I trust the result as far as the tests go.
Get the code
The source, the test instances and the generators are on GitHub: https://github.com/AbdeldjalilChougui/sat-solver. It's MIT licensed. Issues and pull requests are welcome.
Originally published at abdeldjalilchougui.biz.