Sub topic 11
Building Gadgets: Reducing 3-SAT to Independent Set
This chapter assumes you know polynomial-time reductions, the definition of NP-completeness, and that 3-SAT is NP-complete, which follows from Cook-Levin. You should also know the Independent Set problem from the earlier graph chapter.
The reductions between Independent Set, Vertex Cover, and Clique kept the problem in the world of graphs. Reducing 3-SAT to Independent Set crosses from logic to graphs. To do that, we build small pieces of graph called gadgets. Each gadget forces a local rule of the formula to hold.
The construction
A 3-SAT instance is a formula in conjunctive normal form with m clauses, each having at most three literals. We build a graph G and set k = m.
- Clause gadget. For each clause, create one vertex per literal and connect them all into a triangle. Any independent set can take at most one vertex from each triangle.
- Conflict edges. Connect every vertex labeled x to every vertex labeled ¬x, across all clauses. An independent set can never pick a variable and its negation together.
Choosing a vertex means "this literal is the one that makes its clause true."
Code with a brute-force check
from itertools import combinations, product
def three_sat_to_independent_set(clauses):
# clauses: 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 between x and NOT x
return vertices, edges, len(clauses)
def has_independent_set(vertices, edges, k):
for s in combinations(vertices, k):
chosen = set(s)
if all(not (a in chosen and b in chosen) for a, b in edges):
return True
return False
def satisfiable(clauses):
names = sorted({abs(l) for c in clauses for l in c})
for bits in product([False, True], repeat=len(names)):
val = dict(zip(names, bits))
if all(any(val[abs(l)] == (l > 0) for l in c) for c in clauses):
return True
return False
for formula in [[(1, -2, 3), (-1, 2, 3)], [(1,), (-1,)], [(1, 2), (-1,), (-2,)]]:
g = three_sat_to_independent_set(formula)
assert satisfiable(formula) == has_independent_set(*g)
print("reduction agrees with brute force")The brute-force check runs in exponential time and is only there for testing. The reduction itself runs in O(m²) time, because it compares every pair of vertices, and there are at most 3m of them.
Proving both directions
Satisfiable ⇒ independent set of size m. Fix a satisfying assignment. In each clause, pick one literal that is true and take its vertex. You picked exactly one vertex per triangle, so no triangle edge has both ends chosen. Every chosen literal is true under one consistent assignment, so you never chose both x and ¬x, and no conflict edge has both ends chosen either.
Independent set of size m ⇒ satisfiable. The triangles allow at most one vertex each, and there are m triangles, so the set has exactly one vertex in every clause. Conflict edges mean no variable shows up in the set both positive and negated. Make every chosen literal true and give any remaining variables arbitrary values. Every clause now contains a true literal.
Independent Set is in NP, and 3-SAT reduces to it in polynomial time, so Independent Set is NP-complete. By the earlier reductions, Vertex Cover and Clique are NP-complete too.
Exercise
Why does the reduction set k equal to the number of clauses rather than the number of variables?
Show answer
The triangles limit any independent set to one vertex per clause, so reaching size m forces every clause to have a chosen literal, which means every clause is satisfied. The number of variables plays no role in that counting argument.
Exercise
Suppose you drop the conflict edges. Give a formula where the reduction then gives the wrong answer.
Show answer
Take (x1) ∧ (¬x1). It is unsatisfiable. Without conflict edges, the graph is two isolated vertices, which form an independent set of size 2 = k. The reduction would wrongly answer yes.