2-CNF Satisfiability is Poly-time Decidable

Mar. 20, 2023

2-CNF is CNF that every clause has at most two literals. The satisfiability of CNF is NP-complete in general, however 2-CNF satisfiability is special becuase it is solvable in polynomial time. Let’s call the satisfiability problem of 2-CNF as 2-SAT.

Definition. (complement) For literal LL, define its complement L\overline{L} as following:

L:={pif L=¬p.¬pif L=p. \overline{L} := \begin{cases} p & \text{if } L = \neg p. \\ \neg p & \text{if } L = p. \end{cases}

Definition. (implication graph) The implication graph of a 2-CNF formula FF is a directed graph G=(V,E)G = (V, E). For propositional variables p1,,pnp_1, \cdots, p_n,

V:={p1,,pn}{¬p1,,¬pn} V := \{p_1, \cdots, p_n\} \cup \{\neg p_1, \cdots, \neg p_n\}

E={(L,M):clause (LM) or (LM) appears in F} E = \{(L, M) : \text{clause } ( \overline{L} \vee M ) \text{ or } (L \vee \overline{M}) \text{ appears in } F \}

The edge (L, M) represents the implication LML \to M. For example, consider following 2-CNF formula.

(p0p2)(p0¬p3)(p1¬p3)(p1¬p4)(p2¬p4)(p0¬p5)(p1¬p5)(p2¬p5)(p3p6)(p4p6)(p5p6) (p_0 \vee p_2) \wedge (p_0 \vee \neg p_3) \wedge (p_1 \vee \neg p_3) \wedge (p_1 \vee \neg p_4) \wedge (p_2 \vee \neg p_4) \\ \wedge (p_0 \vee \neg p_5) \wedge (p_1 \vee \neg p_5) \wedge (p_2 \vee \neg p_5) (p_3 \vee p_6) \wedge (p_4 \vee p_6) \wedge (p_5 \vee p_6)

Then we can draw an implication graph as follows.

Example Implication Graph

For instance, the formula has (p0p2)(p_0 \vee p_2) clause, then there are two implications, ¬p0p2\neg p_0 \to p_2 and ¬p2p0\neg p_2 \to p_0. These two edges exists in the graph.

Definition. (consistent) Implication graph is consistent if there is no propositional variable pp with paths from p¬pp \to \neg p and ¬pp\neg p \to p.

In other words, the formula FF is consistent if its implication graph has no pp that cycle having nodes pp and ¬p\neg p exists.

Theorem. There is polynomial time algorithm that checks if implication graph GG is consistent.

It is well-known that we can build strongly connected components from directed graph GG in polynomial time. Also, checking if there is a component containing pp and ¬p\neg p for arbitrary variable pp takes linear time.

Finally, we can show 2-SAT is solvable in polynomial time if we show the following.

Theorem. 2-CNF formula FF is satisfiable iff its implication graph GG is consistent.

(\Rightarrow) This direction is quite trivial. Suppose FF is satisfiable, and implication graph GG is not consistent. Then there exists an satisfying assignment AFA \models F. If GG is not consistent, a propositional variable pp such that implications p¬pp \to \neg p and \neg p \to p $ holds. If $A(p) = true, then p¬pp \to \neg p does not hold. If A(p)=falseA(p) = false, then ¬pp\neg p \to p does not hold. Contradiction.

(\Leftarrow) We need to show that if implication graph is consistent, then the formula is satisfiable. Claim that following algorithm always return satisfying assignment if GG is consistent.

Empty assignment means, A(p)A(p) is not defined for every assignment pp. To prove this algorithm works, we will show two invariants first.

If outer loop invariant is true and all variables are assigned, then for all implications MNM \to N, there is no case that A(M)=1A(M) = 1 and A(N)=0A(N) = 0.