Sub topic 11
Building Gadgets: Reducing 3-SAT to Independent Set
This chapter assumes you know what a many-one reduction is and how to prove one correct in both directions. It also assumes you know that 3-SAT is NP-complete (from the Cook-Levin chapter) and that Independent Set is in NP. You should be able to read Python that uses itertools.
So far, the reductions in this tutorial have connected problems that look alike. Independent Set, Vertex Cover, and Clique are all about choosing vertices in a graph. The reduction in this chapter connects a logic problem to a graph problem, and the two look nothing alike. To bridge them we build gadgets. A gadget is a small piece of the output instance that copies one feature of the input instance and forces a specific local behaviour.
The two problems
A 3-CNF formula is an AND of clauses, where each clause is an OR of exactly three literals. A literal is a variable x or its negation ¬x. 3-SAT asks whether some true/false assignment to the variables makes every clause true.
An independent set in a graph is a set of vertices with no edge between any two of them. The decision problem Independent Set takes a graph G and a number k and asks whether G has an independent set of size at least k.
We want a polynomial-time function that maps a formula φ with m clauses to a pair (G, k) such that:
φ is satisfiable ⇔ G has an independent set of size k.
The construction
Think about what a satisfying assignment does. In every clause it makes at least one literal true. If we pick one true literal from each clause, we get m choices that obey two rules:
- Exactly one literal is chosen per clause.
- No two chosen literals contradict each other. We never pick both
xand¬x.
The graph encodes these two rules with two kinds of edges.
- Clause gadget. For clause
i, create three vertices, one for each literal occurrence, and join them into a triangle. An independent set can contain at most one vertex of a triangle, so it picks at most one literal per clause. - Conflict edges. For every pair of vertices whose literals are
xand¬x, add an edge, even if they sit in different clauses. An independent set can then never choose a literal together with its negation. - Target. Set
k = m.
The vertices are literal occurrences, not variables. If x1 appears in three clauses, it gets three separate vertices.
In the diagram, picking the two x2 vertices gives an independent set of size 2. That matches the assignment x2 = true, which satisfies both clauses.
Proving both directions
(⇒) Satisfiable implies an independent set of size m. Fix a satisfying assignment. In each clause, choose one vertex whose literal is true under it, and call the chosen set S, which has m vertices. No triangle edge lies inside S because S takes one vertex per triangle. No conflict edge lies inside S either, because a conflict edge joins x to ¬x, and the assignment cannot make both true. So S is independent.
(⇐) An independent set of size m implies satisfiable. Let S be independent with |S| = m. Each triangle holds at most one vertex of S, and there are exactly m triangles, so S holds exactly one vertex from each. Set each variable whose positive literal appears in S to true, and each variable whose negated literal appears in S to false. Because of the conflict edges, S never contains both x and ¬x, so this assignment is consistent. Any variable that S does not mention can take either value. Each clause contains a vertex of S, and that vertex's literal is true, so every clause is satisfied.
The counting step in (⇐) matters. The triangles only give "at most one per clause". The target k = m turns that into "exactly one per clause", and that is what forces every clause to be satisfied.
Running time
With m clauses, the graph has 3m vertices. It has 3m triangle edges and at most (3m)²/2 conflict edges. Building it with a double loop over vertices takes O(m²) time, which is polynomial in the size of the formula.
The code, with a brute-force check
The script below implements the reduction. It then checks the equivalence on small formulas by solving both sides with exhaustive search. Brute force is exponential and only useful for testing. The reduction itself is the polynomial part.
from itertools import combinations, product
def three_sat_to_independent_set(clauses):
# clauses: list of tuples of nonzero ints, e.g. (1, -2, 3) = x1 OR NOT x2 OR x3
vertices, edges = [], []
for i, clause in enumerate(clauses):
nodes = [(i, j, lit) for j, lit in enumerate(clause)]
vertices.extend(nodes)
edges.extend(combinations(nodes, 2)) # clause gadget: a triangle
for a, b in combinations(vertices, 2):
if a[2] == -b[2]:
edges.append((a, b)) # conflict edge: x vs NOT x
return vertices, edges, len(clauses)
def satisfiable(clauses):
n = max(abs(lit) for c in clauses for lit in c)
for bits in product([False, True], repeat=n):
if all(any(bits[abs(l) - 1] == (l > 0) for l in c) for c in clauses):
return True
return False
def find_independent_set(vertices, edges, k):
edge_set = {frozenset(e) for e in edges}
for s in combinations(vertices, k):
if all(frozenset(p) not in edge_set for p in combinations(s, 2)):
return s
return None
def assignment_from_set(s):
# Map an independent set back to a (partial) truth assignment
return {abs(lit): lit > 0 for (_, _, lit) in s}
sat_formula = [(1, 2, -3), (-1, 2, 3), (1, -2, -3)]
# All 8 sign patterns over x1, x2, x3: every assignment falsifies one clause
unsat_formula = [tuple(s * v for s, v in zip(signs, (1, 2, 3)))
for signs in product([1, -1], repeat=3)]
for formula in (sat_formula, unsat_formula):
V, E, k = three_sat_to_independent_set(formula)
s = find_independent_set(V, E, k)
print(len(V), "vertices,", len(E), "edges, k =", k)
print("satisfiable:", satisfiable(formula), "| independent set found:", s is not None)
assert satisfiable(formula) == (s is not None)
if s is not None:
print("assignment:", assignment_from_set(s))The unsatisfiable case searches about 735,000 subsets of size 8, so it takes a few seconds. The assignment_from_set function shows that the reduction carries a solution back as well as a yes/no answer. The (⇐) proof is effectively an algorithm that turns an independent set into an assignment.
Edge cases worth checking
| Situation | What the construction does | Why correctness survives |
|---|---|---|
| Repeated literal, e.g. `(x1 ∨ x1 ∨ x2)` | Two `x1` vertices in one triangle | The triangle still allows one pick, and the two copies have no conflict edge between them |
| Clause with `x` and `¬x` | Triangle edge plus a duplicate conflict edge | Duplicate edges change nothing, and the clause is always satisfiable through either literal |
| Variable appears with only one sign | No conflict edges for it | Nothing can contradict it, so any choice of it is consistent |
| Variable never chosen in `S` | Left unassigned by (⇐) | It can take any value, since every clause is already satisfied by its chosen literal |
| Clauses with fewer than 3 literals | Gadget becomes an edge or a single vertex | The proof never used the number 3, so the reduction works for general CNF-SAT |
What this buys you
Independent Set is in NP, and 3-SAT reduces to it, so Independent Set is NP-complete. Reductions compose, so the earlier reductions from Independent Set to Vertex Cover and from Independent Set to Clique now make those two problems NP-complete as well. The same pattern of one gadget per constraint and one per variable choice appears in reductions to 3-Coloring, Hamiltonian Cycle, and Subset Sum. Those gadgets are more elaborate, but you check them the same way: confirm each local rule, then prove both directions.
Exercise
Suppose you dropped the triangle edges but kept the conflict edges and k = m. Give a formula where the graph has an independent set of size m but the formula is unsatisfiable.
Show answer
Take the two clauses (x1 ∨ x1 ∨ x1) and (¬x1 ∨ ¬x1 ∨ ¬x1), which form an unsatisfiable formula with m = 2. Without triangles, the three x1 vertices are pairwise non-adjacent, so {x1, x1} (two occurrences from clause 1) is an independent set of size 2. The (⇐) proof breaks because an independent set can now take two vertices from one clause and none from another.
Exercise
For a 3-CNF formula with m clauses where each variable appears positively a times and negatively b times, how many conflict edges does the variable contribute? Use this to bound the total edge count.
Show answer
It contributes a·b conflict edges, one for each pair of a positive and a negative occurrence. Summed over all variables, this is at most the number of pairs among the 3m vertices, about (3m)²/2. Together with the 3m triangle edges, the graph has O(m²) edges, so the reduction runs in polynomial time.
Exercise
Write the graph and k for the formula (x1 ∨ x2 ∨ x3) ∧ (¬x1 ∨ ¬x2 ∨ ¬x3), and name one independent set of size k and the assignment it gives.
Show answer
The graph has six vertices in two triangles, {x1, x2, x3} and {¬x1, ¬x2, ¬x3}, plus three conflict edges: x1–¬x1, x2–¬x2, and x3–¬x3. The target is k = 2. One independent set is {x1 from clause 1, ¬x2 from clause 2}, which gives x1 = true and x2 = false, with x3 free. Both clauses are satisfied.