Proving NP-hardness
In the last lecture we learned about the complexity classes P and NP of decision problems. A problem in A \in \text{P} has a polynomial time algorithm. A problem A \in \text{NP} has a polynomial time verifier that can take an instance x of A along with a certificate/proof y and use y to verify that x is a Yes instance of A in polynomial time (however, no certificate y can “trick” the verifier into accepting if x is a No instance). We know \text{P} \subseteq \text{NP}, and most computer scientists believe \text{P} \neq \text{NP}.
A (decision) problem B is NP-hard if, for all problems A \in \text{NP}, there is a Karp reduction from A to B (denoted A \leq_P B). Problem B being NP-hard means it is “at least as difficult” as any problem in NP, including the ones that don’t have polynomial time algorithms assuming \text{P} \neq \text{NP}. In particular, B itself has no polynomial time algorithm unless \text{P} = \text{NP}. Finally, a decision problem is (in) NP-complete if it is both NP-hard and in NP (and yes, there are NP-hard problems that are definitively not in NP, but we haven’t discussed any yet). The NP-complete problems have polynomial time algorithm if and only if \text{P} = \text{NP}. As we’ll see, they’re also really convenient to work with when doing proofs of NP-hardness.
The Cook-Levin theorem gives the first known “natural” NP-complete problem: SAT. An instance of SAT is a Boolean formula \Phi in conjunctive normal form. The goal is to decide whether one can set the variables of \Phi so it evaluates to True. Unfortunately, the proof is beyond the scope of this class.
But now what? Do we have to come up with a bespoke complicated proof for every problem we believe to be NP-hard? Fortunately, we have the convenient fact that A \leq_P B and B \leq_P implies A \leq_P C, and therefore we can rely on the following rule:
To prove a problem B is NP-hard, reduce a known NP-hard problem A to B.
We (briefly) saw the rule applied once so far. 3SAT, the specialization of SAT that only takes CNF Boolean formula with exactly three variables per clause, is also NP-complete.
As we’ll see, restricting the number of variables per clause can be convenient for making additional NP-hardness proofs easier to understand.
Proof examples
IndependentSet and friends
Recall IndependentSet (IS): Given an undirected graph G = (V, E) and an integer k, is there an independent set in G with k vertices? Theorem: Independent Set is NP-complete.
Proof: We already saw IndependentSet is in NP, because we can use the an independent set of size k as the certificate.
To prove the problem is NP-hard, we’ll show \text{3SAT} \leq_P \text{IS}. Let \Phi be an instance of 3SAT, a 3CNF Boolean formula. Let n be the number of variables in \Phi and k be the number of clauses.
We’ll encode the formula as a graph where a large independent set corresponds to setting variables so that we have one true variable per clause. Specifically, we build a graph G = (V, E) with 3k vertices, where each vertex is a literal in \Phi. Two vertices are connected by an edge if and only if either
- they share a clause or
- they represent both a variable and its inverse.
The figure below shows the graph for the Boolean formula (a \lor b \lor c) \land (b \lor \bar{c} \lor \bar{d}) \land (\bar{a} \lor c \lor d) \land (a \lor \bar{b} \lor \bar{d}).

The independent set instance uses the integer parameter k. This reduction takes O(n) time, which is polynomial.
All that remains is to prove \Phi is satisfiable if and only if G has an independent set of size k.
- Suppose there exists a satisfying assignment for \Phi.
Let S contain exactly true literal per clause triangle in G. We have |S| = k, because \Phi is satisfied by the assignment. We don’t pick both sides of a literal-inverse edge, so S is independent. - Suppose G has an independent set S of size k. Set S has exactly one vertex per triangle. It does not contain any literal and its negation. Pick any assignment of \Phi’s variables that make the literals in S True. Each clause will have at least one True literal.
Again, we reduced from a known NP-hard problem 3SAT to the new problem IndependentSet in polynomial time. So now Independent Set is a known NP-hard problem. Using the obvious certificates and the reductions we saw last week, we immediately get the following corollaries. Corollary: Clique is NP-complete. Corollary: VertexCover is NP-complete.
Let’s keep going!
3Color
A proper k-coloring of a graph G = (V, E) assigns one of k distinct colors to each vertex so that every edge has two distinctly colored endpoints. I first heard of these in the context of map coloring, but they also comes up in compiler construction. You can model register allocation as having one vertex per value you need to track. There are edges between variables that should ideally be stored in registers at the same time, and your colors are register choices. It may not surprise you that computing the minimum number of colors to properly color a graph is NP-hard. It gets worse, though. In 3Color, we are given an undirected graph G = (V, E), and we need to decide if G specifically has a proper 3-coloring. Theorem: 3Color is NP-complete.
Proof: 3Color is in NP, because you can use the color assignments as the certificate.
While we have a small collection of graph problems to start with, we’ll show 3Color is NP-hard by doing another reduction from 3SAT. Let \Phi be an instance of 3SAT, a 3CNF Boolean formula. Let n be the number of variables in \Phi and k be the number of clauses.
We’ll encode \Phi by building an undirected graph G = (V, E) that has a proper 3-coloring if and only if \Phi is satisfiable. This time the construction is a bit more involved, though. It will help to think of us building up the graph bit-by-bit using little pieces called gadgets that capture different aspects of what it means to satisfy \Phi.
First, there is a single truth gadget, a triangle with three vertices T, F, and X standing for True, False, and Other.
They’re mutually connected, so any proper 3-coloring must give them all 3 distinct colors.
Assuming there exists a proper 3-coloring in the graph we’re building, we may as well refer to the colors themselves as True, False, and Other.

For each variable a in \Phi, we make a variable gadget which is a triangle containing the same vertex X as in the truth gadget connected to two new vertices a and \bar{a}.
In a proper 3-coloring of G, we have to assign color True to exactly one of a or \bar{a} and False to the other.

Finally, for each clause in \Phi, we make a clause gadget.
We take the three relevant literal nodes we put in the variable gadgets and hook them to the same T vertex as in the truth gadget.
There are also five additional unlabeled nodes and ten additional edges unique to each clause gadget.
For the clause (a \lor b \lor \bar{c}) we get the following clause gadget.

Each triangle along with the two adjacent vertices to its left can be thought of as a “majority gate”.
If the two vertices to the left have the same color in a proper 3-coloring, then the rightmost vertex of the triangle is forced to get that color also.
Otherwise, you can pick any color for the rightmost vertex of the triangle.

It follows that in a proper 3-coloring, you cannot color all the literal vertices of a clause False. Otherwise, the tips of both triangles would be False, contradicting T getting the color True. On the other hand, if at least one literal vertex is True, you can set its incident triangle tip to True and if necessarily propagate that color choice to the vertex T as well.
And that’s the whole reduction (whew).
Despite it sounding fairly complicated, it “only” takes polynomial O(n) time to build G.
Below is the entire graph for the 3CNF Boolean formula
(a \lor b \lor c) \land (b \lor \bar{c} \lor \bar{d}) \land (\bar{a} \lor c \lor d) \land (a \lor \bar{b} \lor \bar{d}).

I still need to argue that \Phi is satisfiable if and only if G has a proper 3-coloring, but I’ve done most of the work already while motivating the gadgets.
- Suppose \Phi has a satisfying assignment. Color the literal vertices according to that assignment. Every clause contains at least one True literal. As discussed above, we can propagate the literal colors to a proper 3-coloring of that clause.
- Suppose G has a proper 3-coloring. Set the variables according to which colors are assigned to their literals. As discussed above, no clause gadget has three False-colored literals, so every clause of \Phi is satisfied.
This proof is significantly more complicated than the ones you’ll be expected to do for your last homework and on the final exam, but the concept of gadgets may be helpful regardless.
DirHamCycle
A Hamiltonian cycle in a directed graph is a cycle that contains every vertex of the graph exactly once. In the DirHamCycle problem (sometimes called “HamiltonianCycle in directed graphs” in the course materials), we’re given a directed graph G = (V, E), and we need to decide if G contains a Hamiltonian cycle.
Theorem: DirHamCycle is NP-complete.
Proof: DirHamCycle is in NP, because we can use the permutation of the vertices in the cycle for the certificate.
The Erickson textbook contains two proofs of NP-hardness. For the sake of not showing you yet another reduction from 3SAT, I’ll show the reduction from VertexCover. Let G = (V, E) be an undirected graph and k be an integer. We’ll build a directed graph H = (V', E') that contains a Hamiltonian cycle if and only if G has a vertex cover (a subset of vertices with at least one incident to each edge of G) of size k. Again, we’ll build up the reduction by describing a bunch of gadgets.
For each undirected edge uv in G, we’ll add one edge gadget consisting of four vertices
- (u, v, \text{in}),
- (u, v, \text{out}),
- (v, u, \text{in}), and
- v, u \text{out})
connected by six directed edges
- (u, v, \text{in}) \to (u, v, \text{out}),
- (v, u, \text{in}) \to (v, u, \text{out}),
- (u, v, \text{in}) \to (v, u, \text{in}),
- (v, u, \text{in}) \to (u, v, \text{in}),
- (u, v, \text{out}) \to (v, u, \text{out}), and
- (v, u, \text{out}) \to (u, v, \text{out}).

The idea here is that if there is a Hamiltonian cycle in H, it must use exactly one of three routes through each edge gadget.
The route used will correspond to which of u or v to include in a vertex cover of G.
Also, observe how any Hamiltonian cycle enters the chain at (u, v, \text{in}) if and only if it leaves at (u, v, \text{out}).

For each vertex u in G, we’re going to connect the u sides of the edge gadgets for each edge uv into a single directed path we’ll call a vertex chain. Specifically, if u has d neighbors v_1, v_2, \dots, v_d, then we add to H a total of d- 1 additional edges (u, v_i, \text{out}) \to (u, v_{i + 1}, \text{in}) for each i \in \{1, \dots, d - 1\}.
Below is a example input for VertexCover that includes a vertex u with three adjacent edges.
It is followed by part of the resulting DirHamCycle instance focusing on the vertex chain for u.

The idea here is that a Hamiltonian cycle will either
- use all the (u, v_i, \text{out}) \to (u, v_{i + 1}, \text{in}) edges in the chain, signifying that a small vertex cover should include u, or
- it will visit all of the vertices in u’s chain by doing the little detours from the v_i side of all the chains, signifying that a small vertex cover will have to include the other side of those edges if it doesn’t include u.
Finally, H includes k additional cover vertices x_1, x_2, \dots, x_k. From each cover vertex is a directed edge to the first member of each vertex chain, and to each cover vertex is a directed edge from the last member of each vertex chain. In short, a Hamiltonian cycle must directly enter (and therefore leave) exactly k vertex chains.

Whew. It all takes polynomial O(|V| + |E|) time!
But we still need to prove G has a vertex cover of size k if and only if H has a Hamiltonian cycle.
- Suppose C = \{u_0, u_1, \dots, u_{k-1}\} is a vertex cover in G of size k.
For each i from 0 to k-1, walk from x_i to the chain for u_i to x_{i+1 \mod k}. As we go through an edge gadget for u_i v, we take the directed edge (u_i, v, \text{in}) \to (u_i, v, \text{out}) if v \in C. Otherwise, we take the detours shown earlier. So each cover vertex is visited exactly once, and each (u, v, \text{in}) and (u, v, \text{out}) is visited exactly once as part of u’s chain or v’s chain depending on which of u and v are in C. - Suppose H contains a Hamiltonian cycle C.
It must contain an edge from each cover vertex to the start of some vertex chain at which point it must traverse the entire vertex chain as explained above, finally leaving to another cover vertex. There are k cover vertices, so the cycle visits vertex chains for exactly k vertices of G. These k vertices form a vertex cover: the cycle must enter the edge gadget for each uv \in E at either (u, v, \text{in}) or (v, u, \text{in}) in order to use both of those vertices of H.

There is a similar problem, UndirHamCycle problem, where we’re given an undirected graph G = (V, E), and we need to decide if G contains a Hamiltonian cycle. The earlier lecture notes on Karp reductions show \text{DirHamCycle} \leq_P \text{UndirHamCycle}, proving UndirHamCycle is NP-complete as well. Also, the problems of deciding whether a given directed or undirected graph has a Hamiltonian path that includes every vertex exactly once are NP-complete (note that the inputs to the Hamiltonian path problems do not include specified start and end vertex for the path). There are relatively straightforward proofs from the DirHamCycle and UndirHamCycle, respectively.
SubsetSum
Let’s do one more NP-hardness reduction in detail. In the SubsetSum problem, we’re given a set X of positive integers and a non-negative integer T. We need to determine whether there is a subset of X (using each element at most once) whose members sum to exactly T.
Theorem: SubsetSum is NP-complete
Proof: SubsetSum is in NP, because we can use the subset as the certificate and verify that it sums to T in polynomial time.
For the proof of NP-hardness, we need to reduce from a known NP-hard problem to SubsetSum. Preferably the problem we reduce from involves picking some kind of subset. From what we’ve seen so far, one of the “pick some vertices from a graph” problems is probably our best bet. We could probably make a reasonable looking reduction from any of the three, but we’re going to use VertexCover again.
Let G = (V, E) be an undirected graph and k an integer. We’ll build an instance (X, T) of SubsetSum where a subset of X sums to T if and only if there is a vertex cover of size k in G. We’ll again use some gadgets to do the reduction, but they’re a lot simpler than what we dealt with for DirHamCycle or even 3Color.
First, number the edges of G arbitrarily from 0 to |E| - 1. For each edge i, add to set X an “edge gadget” integer b_i := 4^i.
Next, given vertex v, let \Delta(v) denote its incident edges. For each v \in V, we add a single “vertex gadget” integer a_v := 4^{|E|} + \sum_{i \in \Delta(v)} 4^i. That’s all the members of X. One way to interpret these gadgets is to imagine each integer in X as a number written in base 4 with (|E| + 1) crumbs (or quits or maybe digits if you’re wearing mittens).1 The |E|th crumb is 1 if the integer is a vertex gadget and 0 otherwise. For each i < |E|, the ith crumb is 1 if the integer represents edge i or one of its endpoints and is 0 otherwise.
Finally, we set T := k \cdot 4^{|E|} + \sum_{i = 0}^{|E| - 1} 2 \cdot 4^i.
For example, given that same instance to VertexCover as before,

we would create the following instance of SubsetSum: \begin{aligned} a_u := 100111_4 && b_{uv} := 000001_4 \\ a_v := 101001_4 && b_{uw} := 000010_4 \\ a_w := 110010_4 && b_{ux} := 000100_4 \\ a_x := 111100_4 && b_{vx} := 001000_4 \\ && b_{wx} := 010000_4 \end{aligned} T := k \cdot 100000_4 + 022222_4
This reduction takes only O(VE) time, even if we spend constant time per crumb.
We need to prove G has a vertex cover of size k if and only if there is a subset of X summing to T.
-
Suppose G has a vertex cover C of size k. Let S = \{a_v \mid v \in C\} \cup \{b_i \mid \text{edge $i$ has exactly one endpoint in $C$}\}. For all i < |E|, the ith crumb is equal to 2. And we have exactly k vertex gadgets contributing a |E|th crumb of 1. Summing those crumbs gives us k \cdot 4^{|E|}. So the sum of elements in S is exactly T.
In the example above, the vertex cover \{u, x\} corresponds to the subset \{a_u, a_x, b_{uv}, b_{uw}, b_{vx}, b_{wx}\}.
-
Suppose there is a subset S \subseteq X whose elements sum to exactly T. We must have subsets V' \subseteq V and E' \subseteq E where \sum_{v \in V'} a_v + \sum_{i \in E'} b_i = T. Summing any subset of X will result in no carries for any crumb i < |E|, because only three members of X have that crumb equal to 1. Therefore exactly two members of S have the ith crumb equal to 1. At least one of those two is a vertex gadget a_v where v is incident to edge $i, because there is only one edge gadget with the ith crumb set to 1. We conclude V' is a vertex cover. Also, |V'| = k, because the vertex gadgets’ 4^|E| contributions sum to exactly k \cdot 4^|E|.
Why crumb? Because it uses half as many bits as a nibble. Why a nibble? Because it uses half as many bits as a byte.
If you did some extra reading, you may have noticed back in the dynamic programming notes, there are the beginnings of an O(nT) time algorithm for SubsetSum. But we also just proved it’s NP-hard. Did we just prove \text{P} = \text{NP}?
Unfortunately, no. The explanation is that nT isn’t really a polynomial in SubsetSum’s input size, because T can be exponential in the number of bits (or crumbs) used to encode members of X or T itself. Algorithms like the one for SubsetSum are said to run in pseudo-polynomial time. Likewise, we call SubsetSum weakly NP-hard because our chain of polynomial time reductions from arbitrary members of NP required us to create exponentially large numbers at some point.
If we can reduce to a problem that doesn’t use numbers or if we can still do the reductions while writing the numbers in unary, then we call the problem strongly NP-hard. TravelingSalesman (which we won’t cover directly) is an example of a problem with numbers that is strongly NP-hard, because the graph you build during the reduction from Hamiltonian Cycle uses only weights of 1 and 2.
(Neat fact in case you want to search what this problems is: In Karp’s big paper of NP-complete problems, he proved the problem MaxCut to be weakly NP-hard. It was only later proven to be strongly NP-hard. In fact, MaxCut doesn’t even need edge weights to be hard!)
So why bother?
We’ve now spent nearly two weeks discussing reductions that merely show some problems cannot be solved quickly. But why? From a pedagogical standpoint, it’s yet another opportunity to practice something that has come up over and over this semester: reductions and transforming problems. And that’s something you do a lot in computing whether or not your intention is to do theory.
More practically, what happens if you do prove a problem is NP-hard?
- Well, now you know why it was so hard to find an efficient algorithm for it. So you can safely stop trying.
- Or maybe you’re not solving quite the right problem? Try a different problem or approach for whatever your real goal happens to be.
- Or maybe you are trying to solve the right problem, but the kinds of “worst case” inputs you need to prove NP-hardness don’t actually come up. Can you specialize to a still useful version of the problem that isn’t NP-hard?
- If you’re doing some kind of optimization, maybe you can find a provably good solution whose value is a close approximation to optimal.
- Or maybe you can just do some heuristics that seem to work well for your inputs in practice. You should try to be correct and precise, but don’t make perfection the enemy of good.
Reduction strategies
Now, let’s say you’re working with some problem B and you want to know if it’s NP-hard. Half the challenge is choosing a problem to reduce from, and to that end, let’s review some problems we’ve seen plus a couple others that might be useful. I’ve left out some that are merely harder to use versions of other problems on the list. I’ve also loosely categorized them by goal in the problem you’re reducing to.
- Binary choices:
- 3SAT
- Large subsets:
- IndependentSet
- Clique
- Small subsets:
- VertexCover
- SetCover (from VertexCover)
- Given a set X and several subsets of X, pick as few subsets as possible to cover all members of X.
- Partition into 3 or more subsets:
- 3Color
- Order things or make pretty subgraphs:
- Directed/Undirected Hamiltonian Cycle/Path
- Long Cycle/Path (from Hamiltonian Cycle/Path)
- Traveling Salesman Problem/World Tour (from Undirected Hamiltonian Cycle)
- Select “balanced” subsets
- SubsetSum
- Partition (from SubsetSum)
- Can a given subset of integers be partitioned into two subsets with equal sums?
- 3DMatching/Exact3DMatching/X3M (from 3SAT)
- Given a three sets A, B, and C of n elements each along with triples from A \times B \times C, can you pick exactly n triples to cover every element?
- 3Partition (from 3DMatching)
- Given 3n integers, can they be partitioned into n subsets of size 3
The idea behind this list is that the problem Y you’re trying to reduce to is likely to have one or more of the features listed above. It’s going to be a lot easier going from say, one partitioning problem to another (especially if they both involve 3 subsets or subsets of size 3), than it is to make up weird gadgets to switch between problem types.
Something else to consider when picking problems and designing the reduction is how complicated your inputs need to be. To prove problem B is NP-hard, your goal is to take an arbitrary instance x of some known NP-hard problem A and reduce to one instance y of problem B.
- You have to deal with any input to problem A. So, while problem type is likely your most important consideration, also try to make the known NP-hard problem X as tightly constrained as possible so there’s fewer things to mess with during the reduction. For example, use 3SAT, not SAT. Use 3Color if you can, not some arbitrary kColor. Use VertexCover, not SetCover.
- You are in control of the input to B. Build a very very special input y so that the “good” things/certificates have enough structure that you can argue they imply “good” inputs/certificates for the input to A. The DirHamCycle input was a beast, but I still made sure Hamiltonian cycles had a very predictable structure if they existed at all. The SubsetSum instance had very peculiar integers that encoded exactly which edge or vertex they came from. Use those gadgets!
NP-hardness is weird and hard (at first). With enough practice, it eventually becomes like solving a puzzle. There’s a reason MIT has a research group devoted to NP-hardness, and half their output is showing certain real life puzzle games are NP-hard when extended to arbitrarily large instances of the puzzles.