A Systematic Approach to Algorithms

Vijay K. Garg · The University of Texas at Austin

Chapter 18. Horn and 2-SAT Satisfiability

Classical forward-chaining and implication-graph algorithms for Horn and 2-SAT satisfiability.

This page: Classical forms. View LLP forms »

This page collects the classical / sequential implementations of the algorithms developed in this chapter. The lattice-linear (LLP) reformulations and chapter setup live on the LLP companion page.

Horn formulas

A Horn formula is a CNF where every clause has at most one positive literal. Each clause can be written as an implication $(x_{i_1} \wedge \cdots \wedge x_{i_k}) \to h$, where $h$ is a variable (a definite clause), a unit fact ($\top \to y$), or $\bot$ (a goal or negative clause). Horn formulas are central in logic programming and database theory.

The key structural property is the meet-closure: if $G$ and $H$ both satisfy a Horn formula, then so does $G \sqcap H$ (the componentwise AND). This means the set of satisfying assignments forms a lattice, and the least model (the smallest satisfying assignment) is unique and computable in linear time.

2-SAT formulas

A 2-SAT formula is a CNF where every clause has exactly two literals. Unlike 3-SAT (which is NP-complete), 2-SAT is solvable in linear time via the implication graph: each clause $(a \lor b)$ generates two implications $(\lnot a \to b)$ and $(\lnot b \to a)$. The formula is satisfiable iff no variable and its negation belong to the same strongly connected component.

2-SAT

Satisfiability via implication graph and SCC detection. Build the directed graph of implications, compute strongly connected components using Kosaraju's algorithm, check that no variable and its negation share a component, and assign truth values in reverse topological order. Running time: $O(n + m)$.

Time complexity: $O(n + m)$ via SCC on the implication graph.

boolean[] TwoSAT(int[] clauseA, int[] clauseB) {
  // Build implication graph from clauses (a ∨ b):
  //   edges (¬a → b) and (¬b → a)
  // Compute SCCs via Kosaraju's algorithm.
  // UNSAT iff x_i and ¬x_i share an SCC.
  // Assign: x_i = true when comp(x_i) > comp(¬x_i).
  ...
}