Formal Methods 101: Boolean Satisfiability (SAT)

This is part 6 of the Formal Methods 101 series. A Boolean-valued logic formula (predicate) is satisfiable (SAT) if there is a valid assignment of values to the predicate's variables, also known as a model, that makes the formula evaluate to TRUE.

Satisfiable and unsatisfiable predicates

Example 1: satisfiable

P = (A ∧ ¬B)

This predicate is satisfiable (SAT). Proof, by giving a model:

A = TRUE, B = FALSE
P = (TRUE ∧ ¬FALSE)
P = (TRUE ∧ TRUE)
P = TRUE

Example 2: unsatisfiable

P = (A ∧ ¬A)

This predicate is unsatisfiable (not SAT). Proof: the variable A would have to be both TRUE and FALSE at once to make this predicate evaluate to TRUE, so no model exists.

Truth tables

A common way to find all the possible states of a predicate is to build a truth table. The table lists every combination of values for the predicate's variables along with the resulting Boolean value. For the predicate (A ∧ ¬B) ∨ C it looks like this:

ABC(A ∧ ¬B) ∨ C
0000
0011
0100
0111
1001
1011
1100
1111

Predicates (Boolean-valued functions) can be specified by a truth table alone, without writing a formula. For example, here is a predicate P(a, b):

abP
000
010
101
110

This predicate may also be written as a formula:

P = (A ∧ ¬B)

Normal forms: CNF and DNF

Two standard shapes for a formula are used by SAT solvers.

  • Conjunctive normal form (CNF) is a conjunction (ANDs) of one or more clauses, where each clause is a disjunction (ORs) of literals.
  • Disjunctive normal form (DNF) is a disjunction (ORs) of one or more terms, where each term is a conjunction (ANDs) of literals.

CNF: (A ∨ B ∨ C) ∧ (D ∨ E ∨ F) ∧ (H ∨ I ∨ J)
DNF: (A ∧ B ∧ C) ∨ (D ∧ E ∧ F) ∨ (H ∧ I ∧ J)

Any propositional formula can be converted into CNF or DNF using these logical equivalences: double negation elimination, De Morgan's laws, and the distributive law. This also means CNF and DNF are interchangeable: one can be converted into the other with the same equivalences.

Solver algorithms

The next sections describe five approaches. The worked examples all use the same predicate, ¬A ∧ B ∧ C, whose only model is A = 0, B = 1, C = 1.

Depth-first search (DFS)

DFS is a search algorithm for tree or graph data structures. It starts at the root and explores as deep into a branch as possible before backtracking up the tree one level. Here each level of the tree assigns one variable, and a leaf is the value of the predicate for one full assignment.

A binary search tree over variables A, B and C for the predicate not-A and B and C. Depth-first search follows A = 0, B = 0, C = 0 and reaches a 0 leaf, backtracks, tries C = 1, another 0 leaf, backtracks to B, tries B = 1, C = 0 (a 0 leaf), backtracks, then C = 1 reaches the single 1 leaf, the satisfying assignment A = 0, B = 1, C = 1, after visiting 8 nodes. The A = 1 half is never explored.
Depth-first search. The amber ring is the search's current position and the dashed amber arrows are backtracks. The search runs into three dead ends before the satisfying leaf, and never needs to look at the A = 1 half of the tree.

Breadth-first search (BFS)

BFS is also a search algorithm for tree or graph data structures. It starts at the root and explores all of the neighbouring nodes before proceeding down to the next layer of the tree.

The same search tree explored breadth first. The numbers give the visit order: A first, then both B nodes, then all four C nodes, then the leaves from the left until the fourth leaf, the 1 leaf for A = 0, B = 1, C = 1, is reached as the 11th node visited. The last four leaves are never visited.
Breadth-first search on the same predicate. It finds the same model, but only after visiting every node on the levels above the leaves, 11 nodes in all against 8 for depth-first search here.

Bounded model checking (BMC)

BMC is a bounded-DFS approach: an iterative DFS to depth K, with backtracking. If no SAT is found, the search is repeated to depth K + D, or stops when necessary. The bound prevents the search from falling into the pitfall of infinite loops.

K = 2

With K = 2 the search goes only two edges deep, which is too short to evaluate the variable C:

The search tree explored depth first but cut off after two edges, so the search reaches every C node but never assigns C and never reaches a leaf. The ring visits A, B, C and backtracks through both halves of the tree and finishes without finding a satisfying leaf.
Bounded search with K = 2. The search visits all of the A, B and C nodes, backtracking each time, and stops at the bound without reaching a leaf.

The search terminates without a model. It has not proven that the formula is unsatisfiable; its depth was simply too short to evaluate C, so it ends without proving SAT.

K = 3

With K = 3 the leaves are within reach and the search finds the model:

The same depth-first search with the bound raised to three edges. The leaves are now reachable, so the search backtracks from dead ends and reaches the 1 leaf for A = 0, B = 1, C = 1 after visiting 8 nodes.
Bounded search with K = 3. Now the bound is deep enough to evaluate every variable, and the search finds the satisfying leaf.

Note that this example is a little deceiving, because it is not the true purpose of BMC. Usually, for BMC you search to a depth equal to the number of variables in the CNF, which is a standard DFS. The reason is that your CNF would be created by translating a state transition system into CNF up to K time steps.

Reduced ordered binary decision diagrams (ROBDDs)

ROBDDs are an approach to SAT (and to representing reachable state spaces). The procedure builds a compressed tree representation of the variable assignments and their results. It picks a variable as the root node and builds branches off it into the other variables, then merges isomorphic nodes (nodes for the same variable with the same children and results) and equivalent results (leaves).

The unreduced diagram

Before reduction, the diagram is the complete decision tree: every assignment has its own path and its own leaf.

A complete binary decision tree over A, B and C with eight leaves. Edges are labelled 0 and 1. Seven leaves are 0; only the leaf for A = 0, B = 1, C = 1 is 1, the satisfying assignment of not-A and B and C.
The unreduced diagram for ¬A ∧ B ∧ C: 2³ = 8 leaves, and exactly one of them is 1.

Reducing it

Reduction uses two steps, applied from the bottom layers up:

  1. Merge isomorphic subgraphs. The seven 0 leaves merge into one, and then the three C nodes whose edges both lead to that 0 leaf merge into one node.
  2. Eliminate nodes with two isomorphic children. A node whose 0 and 1 edges lead to the same place cannot affect the result, so it is removed and the edges into it are redirected to its child. This removes the merged C node, and then the B node that had only reached it.
Four panels. First, the complete decision tree for not-A and B and C with eight leaves. Second, the seven 0 leaves merged into one 0 leaf beside the single 1 leaf. Third, the three C nodes whose edges both reach the 0 leaf merged into one. Fourth, nodes whose two edges lead to the same place removed, leaving three nodes: A, whose 0 edge leads to B and whose 1 edge leads to the 0 leaf; B, whose 1 edge leads to C and whose 0 edge leads to the 0 leaf; and C, whose 1 edge leads to the 1 leaf and 0 edge to the 0 leaf.
The reduction, one rule at a time. The amber ring marks what was just merged. The last panel is the final ROBDD: one 1 leaf, one 0 leaf, and a single path from A to the 1 leaf.

Note that the variable (node) ordering can affect the size of the ROBDD for larger predicates.

Davis-Putnam-Logemann-Loveland (DPLL)

DPLL (sometimes used interchangeably with CNF-SAT) is a backtracking search algorithm that works on a formula in conjunctive normal form. It looks at all the CNF clauses, picks a variable (literal) or its negation and assumes it is TRUE, and sets aside the clauses that this assumption made true. In the leftover clauses it removes the opposite literal, then looks for conflicts among the remaining literals. For example, if clause 1 = (A) and clause 2 = (¬A), then A is in conflict. If a conflict occurs, it backtracks and flips the previous assumption; if no conflict occurs, it picks a new assumption among the leftover clauses. This repeats until SAT is proven or not proven.

Steps

  1. Choose a variable R from the CNF predicate P and assume it is TRUE, preferably one that occurs in many of the clauses. (The negation of R could also be assumed true.)
  2. Identify the clauses Y that do NOT contain R. If Y is the empty set ∅, terminate as SAT.
  3. Remove from these clauses any references to ¬R.
  4. Look for conflicts among the remaining variables of the clauses. (An example conflict is one clause containing only A and another containing only ¬A.)
  5. If a conflict exists, go back to step 1 and pick a variable L that you did not pick before and assume it is TRUE (that is, L ≠ R). Then repeat steps 2 to 4. If no such variable exists, terminate as NOT SAT.
  6. Otherwise repeat step 1, but pick a variable T, with T ≠ R, from Y, and assume it is TRUE.
  7. Repeat steps 2 to 6 with the variable T.

Worked example

The CNF predicate:

P = (¬A ∨ C) ∧ (¬A ∨ B ∨ ¬C) ∧ (A ∨ ¬B ∨ ¬C)

  1. Assume A is TRUE.
  2. The set of clauses of P without A is Y = (¬A ∨ C) ∧ (¬A ∨ B ∨ ¬C).
  3. Remove ¬A from Y and get (C) ∧ (B ∨ ¬C).
  4. Assume B is TRUE.
  5. The set of clauses of Y without B is W = (C).
  6. Assume C is TRUE.
  7. The set of clauses of W without C is the empty set ∅.
  8. Therefore this predicate is SAT, with A = 1, B = 1, C = 1.

Other variants of the SAT problem

  • MAJ-SAT: decide whether the majority of all assignments make the predicate TRUE.
  • #SAT: count the number of variable assignments that make the predicate true.
  • UNIQUE-SAT: decide whether a predicate has exactly one satisfying assignment.
  • UNAMBIGUOUS-SAT: a SAT problem in which the formula guarantees that there is only one satisfying assignment.
  • MAX-SAT: find the maximum number of clauses that can be satisfied by any one assignment.
  • And others.