SAT++ - What is a SAT solver?

Theme toggle button

SAT++ Article One: What is a SAT Solver?

Introduction

SAT solvers are incredibly useful pieces of modern day software. They are used in both software and hardware verification (circuitry testing), product configuration, package management, computational biology, particle physics, solving graph problems, and much much more. In a more computer science-focused world, they have applications in NP Completeness. Since (nearly) every problem has a reduction to a Boolean SAT problem, creating one really really good SAT solver lets us solve all those problems in a β€œfast” manner. So they are important everywhere despite the vast majority of us never really thinking about their existence.

What Do SAT Solvers Solve?

The answer to this question is very obvious: SAT solvers solve SAT problems! But what is a SAT problem? A SAT problem is a β€œsatisfiability” problem, more specifically a boolean satisfiability problem. In other words, SAT solvers solve problems that have boolean constraints on them.

If you are familiar with propositional logic, or boolean algebra, then you might understand what I’m talking about, but most likely I’m not explaining this well enough for anyone to understand. In boolean algebra, there are three operators: and (∧), or (∨), and not (Β¬). These operators operate on boolean variables, and the result is a new boolean. For instance, (T ∧ F) ≑ F, which in English means β€œtrue and false make false.”

These three operators are the foundation of boolean algebra, and the foundation of SAT solving. In order for us to solve a SAT problem, we first need to make one. However, SAT solvers don’t operate on just any Boolean algebra equation. No no no, they must be in a proper form, called CNF (more on that in the next section). CNF (Conjunctive Normal Form) equations are a β€œconjunction of disjunctions,” i.e., clauses of ors, combined with ands.

Some examples of CNF are as follows:

Some examples that are not CNF:

Now that we know what our inputs look like, the next question (I hope you’re asking questions) is β€œhow do we encode that?” Well good thing you’re reading this, because that’s what we’re here to talk about! Before we get to that, here are some definitions we will be using:

Clause: A clause is a collection of variables, in which each variable is separated by an or (∨), and each clause is separated by an and (∧).

Variable A variable is an element of a clause, taking on a value of True, False, or Unassigned. Each variable can be negated (Β¬) or not, affecting its evaluated value.

Literal A literal is an evaluated variable, evaluating to one of True, False, or Unassigned. Since a literal is an evaluated variable, it cannot be negated. A literal whose corresponding variable is unassigned evaluates to Unassigned, regardless of whether or not the variable is negated.

A quick example of the difference of a literal value, using all three of our definitions:

(¬ 𝐴 )

is a clause of one variable β€œA”, which is negated. If the variable 𝐴 = πΉπ‘Žπ‘™π‘ π‘’ , then its literal value is π‘‡π‘Ÿπ‘’π‘’ , because

Β¬ πΉπ‘Žπ‘™π‘ π‘’ ≑ π‘‡π‘Ÿπ‘’π‘’.

In other words, 𝐴 was assigned the value False, and then it evaluated to True. This will be very important in the future, so please take the time to understand it well enough.

As discussed earlier, a CNF equation only has three operators: and (∧), or (∨), and not (¬), but in propositional logic, there are many more operators, and their clauses are not guaranteed to be in the format we want.

DIMACS CNF Format

Now that we know what CNF looks like, we can now discuss how it’s formatted for a SAT solver’s consumption. Typically, CNF is formatted in DIMACS CNF form. Some rules for DIMACS CNF form are:

  1. The file may begin with comment lines. The first character of each comment line must be a lower case letter β€œc”. Comment lines typically occur in one section at the beginning of the file, but are allowed to appear throughout the file.

  2. The comment lines are followed by the β€œproblem” line. This begins with a lower case β€œp” followed by a space, followed by the problem type, which for CNF files is β€œcnf”, followed by the number of variables followed by the number of clauses.

  3. The remainder of the file contains lines defining the clauses, one by one.

  4. A clause is defined by listing the index of each positive literal, and the negative index of each negative literal. Indices are 1-based, and for obvious reasons the index 0 is not allowed.

  5. The definition of a clause may extend beyond a single line of text.

  6. The definition of a clause is terminated by a final value of β€œ0”.

  7. The file terminates after the last clause is defined.

There are some things about this syntax to be wary of:

  1. The definition of the next clause normally begins on a new line, but may follow, on the same line, the β€œ0” that marks the end of the previous clause.

  2. In some examples of CNF files, the definition of the last clause is not terminated by a final β€œ0”.

  3. In some examples of CNF files, the rule that the variables are numbered from 1 to 𝑁 is not followed. The file might declare that there are 10 variables, for instance, but allow them to be numbered 2 through 11.

  4. Comments can appear anywhere in the file, but the comment must be on its own line and it must start with β€œc”.

p cnf 4 2
1 2 -3 0
-1 4 0

And one with comments:

c this is a comment
p cnf 4 2
c this is another comment
1 2 -3 0
-1 4 0

In order to increase the compatibility of our SAT solver, we will be bending some rules.

We will firstly allow missing header lines. In the case where no header is passed in, we will instead infer the number of clauses and variables. We will also be very lenient in where comments can appear, but we will enforce that variables must be between 1 and 𝑁 .

  1. Comments can appear anywhere in the file, but they MUST be on their own line and start with β€œc”.

  2. A header is not necessary, and if one is not provided, then the number of variables and clauses shall be inferred. However, if one is provided, then the number of variables and clauses MUST match the header.

  3. All clauses, with the exception of the last clause, must end with β€œ0”, and can extend multiple lines.

  4. All variables are numbered 1 through 𝑁 , where 𝑁 is the number of variables. That is, if you say there are 10 variables, they must be labeled 1 , 2 , … , 10 and not 2 , 3 , … , 11 or any other convention.

  5. (For compatibility) Upon encountering a β€œ%” character outside of a comment, we will stop parsing immediately.

Thus, valid input can take on many forms:

p cnf 4 2
1 2 -3 0
-1 4 0
4 2
1 2 -3 0
-1 4 0
p cnf 4 2
c this comment doesn’t have to be at the top
1 2 -3 0
-1 4 0

Reading CNF Programmatically

Now that we know what our input looks like, we can finally discuss reading it programmatically. Firstly we must discuss how our SAT solver will be represented in code. At the moment, we just need to keep track of

So our SAT solver will be as follows:

SATSolver(string input)
bool solveCNF()
void parse(string equation)
int numVars
int numClauses
int[][] clauses
bool[] vars

Algorithm 1

Algorithm 2

With that, we will end this article off. We will continue in the next one, in which we will discuss our first SAT solving algorithm: brute force.