A Gallery of NP-Completeness Reductions
From 3-SAT to independent set, vertex cover, Hamiltonian cycle and subset sum.
Once one problem is known to be NP-complete, proving the next is a matter of construction: build gadgets that make one problem impersonate another, then prove that nothing is gained or lost in translation. Karp did it 21 times in one 1972 paper, and NP-completeness turned from a curiosity about logic into the map of hard problems.
What you’ll learn
- Follow the recipe for an NP-completeness proof, and avoid its classic mistakes, starting with the direction of the reduction.
- Prove that independent set, vertex cover and clique reduce to one another, and that 3-SAT reduces to 3-coloring through gadgets.
- Reduce 3-SAT to subset sum with decimal digits, and explain weak versus strong NP-hardness.
- Sketch the reduction from vertex cover to Hamiltonian cycle, and prove that Hamiltonian cycle reduces to the traveling salesman, which therefore has no constant-factor approximation unless P = NP.
The recipe, and Karp’s 21
Every proof that a problem is NP-complete follows the same five steps.
- is in NP. Name the certificate and the polynomial-time check.
- Choose a known NP-complete problem whose structure resembles : 3-SAT for choices under constraints, vertex cover or independent set for selections in graphs, subset sum or partition for numbers, Hamiltonian cycle for orderings.
- Construct a map from instances of to instances of , computable in polynomial time. Watch the size of what you build, and how its numbers are written.
- Prove , usually by carrying a certificate of forward.
- Prove . This is usually the hard direction: every solution of the built instance, including ones you did not intend, must decode into a solution of . Gadgets exist to force solutions into the intended shape.
Karp's 1972 paper applied the recipe to 21 problems: satisfiability, 0–1 integer programming, clique, set packing, vertex cover, set covering, feedback vertex set, feedback arc set, directed and undirected Hamiltonian cycle, satisfiability with at most three literals per clause, chromatic number, clique cover, exact cover, hitting set, Steiner tree, 3-dimensional matching, knapsack, job sequencing, partition and max cut. Garey and Johnson (1979) later singled out six "basic" NP-complete problems as the usual starting points (3-SAT, 3-dimensional matching, vertex cover, clique, Hamiltonian cycle and partition) and cataloged hundreds more.
You find a polynomial-time reduction from your problem to 3-SAT. What have you shown?
- That is NP-hard
- That is in NP, so a SAT solver can decide it after translation, but nothing about how hard is
- That is NP-complete
- That is in P
Show the answer
B. That is in NP, so a SAT solver can decide it after translation, but nothing about how hard is
says is no harder than 3-SAT. Since NP is closed under polynomial-time reductions, is in NP, and translating then calling a SAT solver decides it. Easy problems reduce to 3-SAT too; hardness needs the opposite direction.
Graphs: independent set, vertex cover, clique and 3-coloring
Independent set and vertex cover. An edge has both ends in exactly when it has no end in :
Independent set and clique. Two vertices of are non-adjacent in exactly when they are adjacent in :
Cost. The maps and take time.
Colors get names. In any 3-coloring the palette triangle uses all three colors; call them , and after its vertices. Each and is adjacent to and to each other, so one is colored and the other . Read as true exactly when is colored :
The OR-gadget lemma. If both inputs are colored , then and avoid , so they take and and force . If at least one input is colored , the gadget can be colored with : for inputs use , ; for use , ; for use , .
Colorable gives satisfiable. If some clause had all three literals false, its literal vertices would all be colored ; the lemma forces , then , but is adjacent to the palette vertex . So every clause has a true literal.
Satisfiable gives colorable. Color the palette, then with and with when is true, the reverse otherwise. In each clause, if or is true, color the first gadget with ; otherwise is forced and is true. Either way the second gadget has an input colored , so it can be colored with , which its edges to and allow.
Gadgets are where reductions break, so check them by machine. Exhaustive search confirms that the clause gadget can be completed for exactly 7 of the 8 truth patterns of its literals, all but all-false, and that the whole reduction agrees with a brute-force 3-coloring search on 300 random formulas, 33 of them unsatisfiable.
Numbers: subset sum, and weak versus strong hardness
Subset sum asks whether some of the given positive integers add up exactly to a target . To reduce 3-SAT to it, write numbers in base 10 with one digit per variable and one per clause, digits in all:
- for each variable , a number (" true") with a 1 in digit and in the digit of every clause containing , and a number (" false") with a 1 in digit and in the digit of every clause containing ;
- for each clause, two slack numbers with a single 1 in that clause's digit;
- the target : a 1 in every variable digit and a 3 in every clause digit.
| Number | |||||
|---|---|---|---|---|---|
| 1 | 0 | 0 | 1 | 0 | |
| 1 | 0 | 0 | 0 | 1 | |
| 0 | 1 | 0 | 1 | 0 | |
| 0 | 1 | 0 | 0 | 1 | |
| 0 | 0 | 1 | 1 | 1 | |
| 0 | 0 | 1 | 0 | 0 | |
| slacks for (two) | 0 | 0 | 0 | 1 | 0 |
| slacks for (two) | 0 | 0 | 0 | 0 | 1 |
| target | 1 | 1 | 1 | 3 | 3 |
No carries. A variable column receives at most 2 (from and ), and a clause column at most 3 from literal numbers plus 2 from its slacks. Since , a subset reaches exactly when every column reaches its digit of :
Satisfiable gives a subset. Take for every true and for every false one: each variable column sums to 1, and each clause column to the number of true literals in its clause, between 1 and 3. Add 2, 1 or 0 of that clause's slack numbers to reach 3.
A subset gives a satisfying assignment. Each variable column must sum to exactly 1, so the subset contains exactly one of and : that is an assignment. Each clause column must reach 3 with at most 2 from slacks, so some chosen literal number has a 1 there, and its literal is true.
Size. There are numbers of digits, digits in all, so the construction is polynomial. The values, though, are as large as , which matters next.
import itertools, random
def sat_to_subset_sum(clauses, n):
"""Base-10 digits: one per variable, then one per clause. Returns (numbers, target)."""
m = len(clauses)
def number(var, clause_digits):
d = [0] * (n + m)
if var:
d[var - 1] = 1
for j in clause_digits:
d[n + j] += 1
return int(''.join(map(str, d)))
nums = []
for i in range(1, n + 1):
nums.append(number(i, [j for j, c in enumerate(clauses) if i in c])) # y_i: x_i true
nums.append(number(i, [j for j, c in enumerate(clauses) if -i in c])) # z_i: x_i false
for j in range(m):
nums += [number(0, [j]), number(0, [j])] # two slacks per clause
return nums, int('1' * n + '3' * m)
def satisfiable(clauses, n):
return any(all(any((l > 0) == b[abs(l) - 1] for l in c) for c in clauses)
for b in itertools.product([False, True], repeat=n))
def reachable_sums(nums):
sums = {0}
for x in nums:
sums |= {s + x for s in sums}
return sums
print(sat_to_subset_sum([[1, 2, 3], [-1, -2, 3]], 3)) # ([10010, 10001, 1010, 1001, 111, 100, 10, 10, 1, 1], 11133)
random.seed(2)
for _ in range(2000):
n, m = random.randint(1, 3), random.randint(1, 5)
F = [[random.choice([1, -1]) * random.randint(1, n) for _ in range(3)] for _ in range(m)]
nums, t = sat_to_subset_sum(F, n)
assert satisfiable(F, n) == (t in reachable_sums(nums))
print("agrees with brute force on 2,000 random formulas")The 2,000 random formulas include 67 unsatisfiable ones, so both directions of the proof are tested.
Subset sum has an dynamic program: polynomial in the value of the target but exponential in its number of digits, a pseudo-polynomial algorithm. The reduction above needs exponentially large values, and it has to: when every number is bounded by a polynomial in , subset sum is in P. Problems like this are weakly NP-hard. One extra number also reduces subset sum to partition (split a set into two halves of equal sum): add , where is the total of the numbers.
A problem is strongly NP-hard if it stays NP-hard when all its numbers are bounded by a polynomial in the input size (Garey & Johnson, 1978). Bin packing and 3-partition are, and so is the traveling salesman, whose hardness proof below uses only small weights. Unless P = NP, strongly NP-hard problems have no pseudo-polynomial algorithms; the lesson Pseudo-Polynomial Time develops the distinction.
Tours: Hamiltonian cycle and the traveling salesman
Vertex cover to Hamiltonian cycle needs a cleverer gadget (the version given by Garey & Johnson and by Cormen et al.). Each edge of becomes 12 vertices in two rows of six, one row for and one for , cross-linked so that a cycle visiting all 12 can pass through in exactly three ways: along 's row alone (crossing over to 's row and back), along 's row alone, or along both rows separately. For each vertex , the gadgets of 's edges are chained along 's rows, and extra selector vertices are joined to the two ends of every chain. A Hamiltonian cycle then makes chain traversals, one per selector, and it reaches every gadget exactly when every edge has an endpoint among the chosen vertices: the cycle exists if and only if has a vertex cover of size .
A Hamiltonian cycle is a tour of cost . Following it uses edges of weight 1, and no tour has fewer than legs:
Without one, every tour uses a non-edge. A tour with all legs in would be a Hamiltonian cycle, so some leg costs :
Hardness and inapproximability. With , asking "is there a tour of cost at most ?" decides Hamiltonian cycle. For any , a -approximation returns a tour of cost at most exactly when has a Hamiltonian cycle, so comparing its answer with decides the question. The weights have bits, so the construction is polynomial.
Why metric TSP escapes. These weights violate the triangle inequality: two edges of weight 1 make a detour of cost 2 around a non-edge of weight . Weights 1 and 2 are metric, so metric TSP is still NP-hard, but it can be approximated within 1.5 (the lesson Approximating the Traveling Salesman).
Key takeaways
- An NP-completeness proof shows membership in NP, then reduces a known NP-complete problem to the new one in polynomial time, proving both directions and watching the size of the construction and the encoding of its numbers.
- Complements tie independent set, vertex cover and clique together, and gadgets (a palette triangle, complementary variable pairs, OR-gadgets) carry 3-SAT into 3-coloring.
- Decimal digits without carries carry 3-SAT into subset sum, but only with exponentially large values: subset sum is weakly NP-hard, with a pseudo-polynomial DP, while strongly NP-hard problems such as bin packing stay hard with small numbers.
- Weights 1 and turn Hamiltonian cycle into the traveling salesman: TSP is NP-hard and has no constant-factor approximation unless P = NP. The triangle inequality is exactly what metric TSP adds.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
A graph on 10 vertices has a largest independent set of size 4. What is the size of its smallest vertex cover?
- 4
- 6
- 10
- 14
Show the answer
B. 6
is independent exactly when is a vertex cover, so the smallest cover is the complement of the largest independent set: .
In the reduction from 3-SAT to subset sum, why does writing the numbers in base 10 work?
- Because subset sum is defined for decimal numbers
- Because every column sums to at most 5, so no carry mixes columns and the target can only be met column by column
- Because base 10 keeps the numbers small
- It is arbitrary: base 2 would work just as well
Show the answer
B. Because every column sums to at most 5, so no carry mixes columns and the target can only be met column by column
A clause column gets at most 3 from literal numbers and 2 from its slack numbers, a variable column at most 2. Any base of at least 6 keeps the columns independent; 10 is just convenient.
Which problem stays NP-hard even when every number in the input is bounded by a polynomial in the input size?
- Subset sum
- 0/1 knapsack
- Partition
- Bin packing
Show the answer
D. Bin packing
Bin packing, like 3-partition, is strongly NP-hard. The other three have pseudo-polynomial dynamic programs, or , which run in polynomial time once the numbers are polynomially bounded.
The reduction from Hamiltonian cycle shows that general TSP has no polynomial-time -approximation for any constant , unless P = NP. Why does the argument fail for metric TSP?
- Metric instances are always small
- The weights it builds, 1 and , violate the triangle inequality
- Hamiltonian cycle is easy when distances are metric
- Metric TSP can be solved exactly in polynomial time
Show the answer
B. The weights it builds, 1 and , violate the triangle inequality
Two edges of weight 1 make a detour of cost 2, while the direct non-edge costs : the triangle inequality fails. With it, approximations exist, such as Christofides's algorithm, within a factor 1.5.
Further reading
- R. M. Karp, "Reducibility among combinatorial problems," in R. E. Miller & J. W. Thatcher (eds.), Complexity of Computer Computations (Plenum, 1972), 85–103.
- M. R. Garey & D. S. Johnson, Computers and Intractability: A Guide to the Theory of NP-Completeness (W. H. Freeman, 1979).
- M. R. Garey & D. S. Johnson, "'Strong' NP-completeness results: motivation, examples, and implications," Journal of the ACM 25(3) (1978).
- T. J. Schaefer, "The complexity of satisfiability problems," Proceedings of STOC (1978).
- T. H. Cormen, C. E. Leiserson, R. L. Rivest & C. Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), chapter 34.