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 idea is to read each clause as a pair of implications, build a directed graph on the 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. This lesson is its most direct payoff: a problem that looks like intractable boolean satisfiability, but whose two-literal special case 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
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
where and range over literals and double negation cancels ( is ). In figures we write for . The construction rules, spelled out:
- Clause with two positive literals: edges and . Nothing else — in particular not .
- Clause , i.e. the implication : edges and . Encoding an implication directly still produces two edges; the contrapositive comes along whether you write it or not.
- Clause (
not both
): edges and . - Unit clause : treat it as , contributing the single edge , which forces true (we will see why in a moment).
- Constraint
and must differ
: two clauses and , four edges;must agree
is and .
Every clause thus contributes exactly two edges (a duplicated edge for a unit clause), so the graph has vertices and 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 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.
When is the formula satisfiable?
A satisfying assignment must respect every forced implication: if it sets true and , it must set true. The failure mode is a cycle of forcing that loops a literal back to its own negation — and such a cycle is exactly an SCC.
The no collision
test is a decision procedure — it answers satisfiable? — so its
correctness splits into the two halves we named in
the foundations: the test must never
report satisfiable when no assignment exists (soundness), and must never miss a
formula that is satisfiable (completeness).
The two directions of the iff give exactly these two guarantees. The forward direction
below — a collision forces a contradiction — is completeness: every truly
unsatisfiable formula does produce a collision, so a satisfiable one never gets
rejected. The converse, built constructively in the next section, is soundness:
when the test passes we exhibit an assignment that really satisfies , so a
satisfiable
verdict is never a false positive.
The converse, that if no variable collides with its negation in an SCC then a satisfying assignment exists, is the more delicate half. The construction below builds an explicit assignment, and the same skew-symmetry argument proves it consistent, establishing 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).
- 1build implication graph on literal-vertices
- 2for each clause do
- 3add edge and edge
- 4comp in reverse topo order
- 5for to do
- 6if then
- 7return Unsatisfiable
- 8for to do
- 9later topo true
- 10return
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.
A complete worked example
We run the pipeline once end to end. Take three variables and four clauses:
Build the implication graph. Each clause contributes and . Working clause by clause (writing for ):
| Clause | First edge | Second edge |
|---|---|---|
That is eight edges on the six literal-vertices :
Run SCCs. The edges close two directed triangles: is one strongly connected component, and is another. So
No variable meets its own negation: while , and likewise for and . The collision test passes on all three variables, so is satisfiable.
Read the assignment. The condensation has just two super-vertices, and
, joined by the cross edges and , both
running . So is topologically later (the sink). The rule assign true to whichever of , lies in the later SCC
makes every
variable read straight off which component holds its positive literal:
- (later) .
- (earlier), so is later .
- (later) .
Check it. The assignment satisfies every clause: , , , and . No search was needed.
For contrast, add the clause — forbidding and
from both being true. Its edges and
now run , and the graph already had the cross edges
and running . Edges in both directions collapse
and into a single strongly connected component containing all six literals —
in particular and together. Once that happens the collision test fires and the formula is
correctly reported unsatisfiable — no assignment can honor or
,
or
, not both and
, , and
not both and
at once.
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 wide
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. The implication-graph technique unifies these as one family, and in
competitive programming and formal verification 2-SAT in its raw form is common
too.
The boundary of tractability
2-SAT is a boundary case: almost any modification of the problem is NP-hard.
The canonical linear algorithm. The implication-graph reduction and the later SCC wins
assignment rule are due to Aspvall, Plass, and Tarjan (1979), who packaged the whole thing as a single procedure.4 Their paper actually solves the more general quantified 2-SAT, but strip the quantifiers and what remains is the algorithm here: build , one SCC pass, two scans. It remains the standard method, used verbatim in competitive programming and as a subroutine in SAT solvers' preprocessing.
Randomized 2-SAT. A different linear-expected-time algorithm ignores the graph entirely. Papadimitriou's random-walk method starts from any assignment and, while some clause is unsatisfied, picks one and flips a uniformly random one of its two literals.5 Each flip is a step in a random walk on the number of variables that agree with a fixed satisfying assignment; because a two-literal clause guarantees at least a chance of stepping toward the target, the walk reaches a satisfying assignment in expected flips. The same idea with three literals only steps toward the target with probability , and the walk drifts — this is why Schöning's randomized 3-SAT algorithm runs in exponential (though better-than-brute-force) time.
Everything nearby is hard. The two-to-three-literal boundary is the most famous, but not the only one:
- 3-SAT is NP-complete (Cook-Levin), the archetypal hard problem of the intractability module.
- MAX-2-SAT — satisfy as many clauses as possible when you cannot satisfy them all — is NP-hard even though plain 2-SAT is easy; the Goemans-Williamson semidefinite-programming relaxation gives the best known approximation.
- Weighted / quantified variants and counting the number of satisfying assignments (#2-SAT) are all intractable.
2-SAT is a different problem from general SAT, not merely a smaller one: its special structure (every clause is an implication, so forcing is a reachability relation) is what collapses it into a graph question. Without that structure the problem is NP-hard.
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 direct payoff of the SCC machinery from the previous lesson.
Footnotes
- Skiena, § — Satisfiability: 2-SAT is polynomial via implication graphs, while general SAT and 3-SAT are NP-complete. ↩
- Erickson, Ch. — Strong Connectivity / Applications: the implication-graph reduction and the SCC characterization of 2-SAT satisfiability. ↩
- CLRS, Ch. 20 — (SCC applications): strongly connected components and the condensation DAG, the substrate the 2-SAT assignment rule runs on. ↩
- Aspvall, B., Plass, M. F. & Tarjan, R. E. (1979),
A linear-time algorithm for testing the truth of certain quantified boolean formulas,
Information Processing Letters 8(3), 121–123 — the implication-graph SCC algorithm for 2-SAT. ↩ - Papadimitriou, C. H. (1991),
On selecting a satisfying truth assignment,
Proc. FOCS 1991, 163–169 — the random-walk algorithm solving 2-SAT in expected time. ↩
╌╌ END ╌╌