Reaves.dev

v0.1.0

built using

Phoenix v1.7.20

Satisfiability

Stephen M. Reaves

::

2025-02-15

Graph algorithms to satify logic equations in CNF

Summary

We can build a graph from a logical statement. Once we have that graph, we can build a system of Strongly Connected Components (SCCs) to determine a satisfying assignment for the original statement.

Contents

Notation

Boolean formula

  • n variables
    • x1,x2,,xn x_1, x_2, \ldots, x_n
  • 2n literals
    • x1,x1ˉ,x2,x2ˉ,,xn,xnˉ x_1, \bar{x_1}, x_2, \bar{x_2}, \ldots, x_n, \bar{x_n}
  • AND
    • \land
  • OR
    • \lor
  • Conjunctive Normal Form
    • Several clauses
      • OR of several literals
        • (x3x5ˉx1ˉx2) \left( x_3 \lor \bar{x_5} \lor \bar{x_1} \lor x_2 \right)
    • AND of several clauses
      • (x2)(x2ˉx3) \left( x_2 \right) \land \left( \bar{x_2} \lor x_3 \right)
    • F is satisfiable if every clause it true

SAT Problem

INPUT: Formula F in CNF with n variables and m clauses

OUTPUT: assignment (assign T or F to each variable) statisfying if one exists

f=(x1ˉx2ˉx3)(x2x3)(x3ˉx1ˉ)(x3ˉ)x1=Fx2=Tx3=F \begin{aligned} f &= \left( \bar{x_1} \lor \bar{x_2} \lor x_3 \right) \land \left( x2_ \lor x_3 \right) \land \left( \bar{x_3} \lor \bar{x_1} \right) \land \left( \bar{x_3} \right) \ x_1 &= F \ x_2 &= T \ x_3 &= F \end{aligned}

Simplifying input

Consider input f for 2-SAT problem

simplify unit clauses and remove them

(x3x2ˉ)(x1ˉ)(x1x4)(x4ˉx2)(x3ˉx4)(x3x2ˉ)(T)(x1x4)(x4ˉx2)(x3ˉx4)(x3x2ˉ)(T)(Fx4)(x4ˉx2)(x3ˉx4)(x3x2ˉ)(T)(FT)(x4ˉx2)(x3ˉx4)(x3x2ˉ)(T)(FT)(x4ˉx2)(x3ˉT) \begin{aligned} \left( x_3 \lor \bar{x_2} \right) &\land \underline{\left( \bar{x_1} \right)} &\land \left( x_1 \lor x_4 \right) &\land \left( \bar{x_4} \lor x_2 \right) &\land \left( \bar{x_3} \lor x_4 \right) \ \left( x_3 \lor \bar{x_2} \right) &\land \underline{\left( T \right)} &\land \left( x_1 \lor x_4 \right) &\land \left( \bar{x_4} \lor x_2 \right) &\land \left( \bar{x_3} \lor x_4 \right) \ \left( x_3 \lor \bar{x_2} \right) &\land \left( T \right) &\land \underline{\left( F \lor x_4 \right)} &\land \left( \bar{x_4} \lor x_2 \right) &\land \left( \bar{x_3} \lor x_4 \right) \ \left( x_3 \lor \bar{x_2} \right) &\land \left( T \right) &\land \underline{\left( F \lor T \right)} &\land \left( \bar{x_4} \lor x_2 \right) &\land \left( \bar{x_3} \lor x_4 \right) \ \left( x_3 \lor \bar{x_2} \right) &\land \left( T \right) &\land \left( F \lor T \right) &\land \left( \bar{x_4} \lor x_2 \right) &\land \underline{\left( \bar{x_3} \lor T \right)} \ \vdots \end{aligned}

We either end up with an empty formula (which is definitionaly true) or a formula with exactly 2 liters per clause

Graph of Implications

Take f with all clauses of size 2, n variables, m clauses

Create a directed graph with

  • 2n vertices
    • x1,x1ˉ,x2,x2ˉ,,xn,xnˉ x_1, \bar{x_1}, x_2, \bar{x_2}, \ldots, x_n, \bar{x_n}
  • 2m edges
    • implications per clause

(x1ˉx2ˉ)(x2x3)(x3ˉx1ˉ) \left( \bar{x_1} \lor \bar{x_2} \right) \land \left( x_2 \lor x_3 \right) \land \left( \bar{x_3} \lor \bar{x_1} \right)

x1=T    x2=F x_1 = T \implies x_2 = F

x2=T    x1=F x_2 = T \implies x_1 = F

Gcluster_tcluster_fx1x1x2bx2bx1->x2bx3bx3bx1->x3bx2x2x1bx1bx2->x1bx3x3x3->x1bx2b->x3x3b->x2

path =x1x2ˉx3x1ˉ \text{path } = x_1 \rightarrow \bar{x_2} \rightarrow x_3 \rightarrow \bar{x_1}

x1x1ˉ    x1=Tx1ˉ=T x_1 \leadsto \bar{x_1} \implies x_1 = T \land \bar{x_1} = T

x1x1ˉ    x1=Tx1=F \phantom{x_1 \leadsto \bar{x_1}} \implies x_1 = T \land x_1 = F

x1x1ˉ     \phantom{x_1 \leadsto \bar{x_1}} \implies \bot

x1x1ˉ&x1ˉx1     f is not satisfiable x_1 \leadsto \bar{x_1} & \bar{x_1} \leadsto x_1 \implies \text{ f is not satisfiable}

if xi&xiˉ x_i & \bar{x_i} are in same SCC, then f is not satisfiable

Finding assignments

Approach 1

  • Take sink SCC S
  • Set S = T
    • Satisfy all literals in S
  • Remove S
  • Repeat

It would be great if complement to S was a source, since it would have no incoming implications.

Approach 2

  • Take source SCC S’
  • Set S’ = F
  • Remove literals
  • Repeat

Starting with a source or a sink is the same thing

2-SAT Algorithm

If for all i, xi&xiˉ x_i & \bar{x_i} are in different SCCs, then S is a sink SCC     Sˉ is a source SCC S \text{ is a sink SCC } \implies \bar{S} \text{ is a source SCC}

2SAT(f):
  Simplify f to assume all clauses are of size exactly 2
  Construct graph G for f
  while G has nodes:
    Take a sink SCC S
    Set S = T
    Remove S