Graphs/2-SAT via Implication Graphs

Lesson 6.81,291 words

2-SAT via Implication Graphs

A boolean formula whose every clause has exactly two literals can be solved in linear time — even though its three-literal cousin is NP-complete. The trick is to read each clause as a pair of implications, build a directed graph on the 2n2n literals, and ask a question we already know how to answer: which literals share a strongly connected component?

╌╌╌╌

The previous lesson gave us strongly connected components: the maximal sets of vertices in a directed graph that can all reach one another, computable in by a two-pass depth-first search. It is a beautiful algorithm, and this lesson is its most surprising payoff. We will take a problem that looks like it belongs to the world of intractable boolean satisfiability and show that one special case of it collapses to a single SCC computation.

The problem is 2-satisfiability (2-SAT). We are given boolean variables and a formula in conjunctive normal form where every clause has exactly two literals, a literal being a variable or its negation :

We must decide whether some assignment of true/false to the variables makes every clause true at once, and if so, produce one. Allowing three literals per clause gives 3-SAT, which is NP-complete, the canonical hard problem we will meet in the intractability module. The jump from two literals to three is the jump from to (as far as anyone knows) exponential. 2-SAT sits firmly in , and the reason is entirely graph-theoretic.1

Reading a clause as two implications

The key observation is that a two-literal disjunction is logically the same as a pair of implications. The clause asserts that at least one of , is true. So if happens to be false, is forced true; and symmetrically if is false, is forced. In symbols,

This rewriting is the whole idea. Build a directed implication graph on vertices, one for each literal and one for its negation . For every clause in , add the two edges

An edge reads if is true, then must be true. Because implication is transitive, a directed path means that committing to forces : reachability in is exactly the relation forces. The graph is skew-symmetric by construction. The contrapositive means every edge has a mirror edge , and this symmetry is what makes the assignment step work.

skew-symmetry: every edge has a mirror edge
clause

The two highlighted edges are the implication pair of the clause . Mirror edges always come in such pairs.

When is the formula satisfiable?

A satisfying assignment must respect every forced implication: if it sets true and , it must set true. The danger is a cycle of forcing that loops a literal back to its own negation. That danger is exactly an SCC.

UNSAT: and put both in one SCC, forcing and

The converse, that if no variable collides with its negation in an SCC then a satisfying assignment exists, is the more delicate half. We do not just assert it; the construction below builds an explicit assignment and the same skew-symmetry argument proves it consistent, which establishes the converse constructively.2

Constructing a satisfying assignment

Suppose the test passes: no and share an SCC. Contract each SCC to a single super-vertex; the result is the condensation of , which is always a directed acyclic graph (any cycle among components would have merged them). A DAG has a topological order, and topological order is what we assign by.

A two-pass SCC algorithm (Kosaraju or Tarjan) already numbers the components in a reverse topological order: Tarjan emits components sink-first, and Kosaraju's second pass discovers them in the order of decreasing first-pass finish time. So the comparison costs nothing extra: we set a literal true iff its component is discovered before its negation's in that reverse order (i.e. later topologically).

Algorithm:TwoSat(n,clauses)\textsc{TwoSat}(n, \text{clauses}) — decide and assign in O(n+m)O(n+m)
  1. 1
    build implication graph GG on 2n2n literal-vertices
  2. 2
    for each clause (ab)(a \vee b) do
  3. 3
    add edge ¬ab\lnot a \to b and edge ¬ba\lnot b \to a
  4. 4
    comp[]StronglyConnectedComponents(G)comp[\cdot] \gets \textsc{StronglyConnectedComponents}(G)
    comp in reverse topo order
  5. 5
    for i1i \gets 1 to nn do
  6. 6
    if comp[xi]=comp[¬xi]comp[x_i] = comp[\lnot x_i] then
  7. 7
    return Unsatisfiable
  8. 8
    for i1i \gets 1 to nn do
  9. 9
    value[xi](comp[xi]<comp[¬xi])value[x_i] \gets (comp[x_i] < comp[\lnot x_i])
    later topo \Rightarrow true
  10. 10
    return valuevalue

Why the rule is consistent. We must check the assignment never violates an implication edge, never sets some true and a forced false. Two facts make this automatic. First, edges of run from earlier components to later ones in topological order (that is what topological order means for a DAG), so an edge has no later than . Second, the skew-symmetry of guarantees that the condensation has a matching central symmetry: the component of sits in the mirror position to the component of . Concretely, if 's component is topologically later than 's, so we set true, then for any edge the mirror edge forces 's component to be no later than 's, hence 's component is no earlier than 's, so is also assigned true. The edge is satisfied. No clause can be violated, and the converse of the theorem holds.3

assign by reverse topological order of SCCs

Here sits in an earlier component than , so is set true (its SCC, highlighted, is later); likewise precedes , so is true. The whole pipeline (build , run one SCC computation, scan the variables twice) is time and space, matching the cost of the SCC algorithm it rests on.

Where 2-SAT shows up

The pattern to recognize is: each item has exactly two states, and the constraints are pairwise. Then every constraint becomes a two-literal clause and the whole problem becomes one implication graph. This covers a surprising range: placing labels on a map so adjacent labels do not collide (each label goes left-or-right), scheduling tasks each offered in one of two slots, two-coloring under these two must differ / must agree rules, and consistency checking in hardware and program verification, where 2-SAT is a standard subroutine. Pure 2-SAT rarely appears verbatim on LeetCode, but the implication-graph and pairwise- constraint shape is common: Satisfiability of Equality Equations is a union-find consistency check that is 2-SAT with only equalities and disequalities; Possible Bipartition asks for a two-coloring under must differ constraints, exactly the constraint-graph reduction; and Divide Nodes Into the Maximum Number of Groups layers a bipartiteness/BFS-distance argument on top. Knowing the implication-graph technique is what lets you see these as one family, and in competitive programming and formal verification 2-SAT in its raw form is common too.

Takeaways

  • 2-SAT (CNF satisfiability with exactly two literals per clause) is solvable in , unlike NP-complete 3-SAT; the gap from two to three literals is the gap from polynomial to (conjecturally) exponential.
  • Each clause is the implication pair and ; collecting them builds a skew-symmetric implication graph on the literals, where reachability = forcing.
  • Satisfiability theorem: is satisfiable iff no variable shares an SCC with its own negation; a collision means and , an outright contradiction.
  • An assignment is read straight off the condensation DAG: set each literal true iff its SCC is topologically later than its negation's; skew-symmetry proves this never violates an implication edge.
  • The whole algorithm is one strongly-connected-components computation plus two linear scans, a clean and surprising payoff of the SCC machinery from the previous lesson.

Footnotes

  1. Skiena, § — Satisfiability: 2-SAT is polynomial via implication graphs, while general SAT and 3-SAT are NP-complete.
  2. Erickson, Ch. — Strong Connectivity / Applications: the implication-graph reduction and the SCC characterization of 2-SAT satisfiability.
  3. CLRS, Ch. 20 — (SCC applications): strongly connected components and the condensation DAG, the substrate the 2-SAT assignment rule runs on.
Practice

╌╌ END ╌╌