NP-Completeness
Introduction
So far, we have designed efficient algorithms for a wide range of problems. But not all problems are so accomodating. In this section, we examine a class of problems for which no polynomial time algorithm is known (and for which there is strong theoretical reason to believe none exists).
Definitions
We study NP-completeness in the setting of decision problems, or problems with a yes/no answer. This is not much of a restriction, since most optimization problems that we have previously studied have a natural decision version (e.g. "for a graph , does there exist a traveling salesman tour of cost at most ?").
We say a (decision) problem is in NP if a proposed solution can be verified in polynomial time. More precisely, there exists a polynomial-time algorithm that, given an instance of and a certificate , correctly determines whether proves that is a yes-instance of problem . For example, a certificate (proposed solution) for a SAT instance is a truth assignment to its variables; we can check in polynomial time whether this assignment satisfies the SAT formula.
A polynomial reduction reduction from problem to problem , written as , is a polynomial-time computable function such that for any instance of , is a yes-instance of if and only if is a yes-instance of . Intuitively, this means is "no harder" than ; given an efficient algorithm for , we obtain an efficient algorithm for by transforming instances via and running 's algorithm on the result. Note the direction; means is at least as hard as , a fact we will use when proving NP-hardness.
We say a problem is NP-hard if for every problem . Intuitively, is at least as hard as every problem in NP; solving efficiently would let us solve all of efficiently.
We say is NP-Complete if and only if the following conditions are true:
- : there exists a verifier that can check proposed solutions to an instance of in polynomial time.
- is NP-hard. To show this, we produce a polynomial-time reduction from some problem already known to be NP-hard. By transitivity of , since every problem in NP reduces to B (as B is NP-hard) and , every problem in NP also reduces to ; so is NP-hard.
General Framework
NP-completeness proofs are an important part of CS4820, and they are sure to show up on exams. If you want to show that some decision problem is NP-complete, the proof generally has 4 steps, as follows:
- . Show that there is a polynomial-time verifier for . It should take in an instance of the problem Foo and a certificate (possible solution) to that instance, answering 'yes' if certificate is a correct solution and 'no' otherwise. This is usually straightforward and should almost always be done first. Don't forget to argue why the verifier runs in polynomial time!
- Reduce from some NP-complete problem to (i.e. showing ). You should pick a known NP-complete problem . Your goal is to solve using an algorithm that solves . You should do this by "reducing" or translating an arbitrary instance of into an equivalent instance of . This way, if you have an algorithm that solves , you can solve by reducing the problem to and running the algorithm for .
- Show that the reduction takes polynomial time. Make sure you aren't creating exponentially many gadgets/nodes!
- Show that your reduction is equivalent. In other words, is a yes-instance of iff is a yes-instance of (where is our reduction function). This has two parts.
- If is a yes-instance of , then is a yes-instance of . Typically, you show this by taking an arbitrary solution to and explicitly constructing a corresponding solution to .
- If is a yes-instance of , then is a yes-instance of . You show this by taking a solution to and explicitly constructing a corresponding solution to . This is often the harder direction, since you need to consider all possible solutions to (not just the ones you had in mind while making the reduction construction).
On the Direction of the Reduction
The most common way to lose points on an NP-completeness proof is to reduce the wrong way. If the reduction direction is flipped, then the entire content of the argument will be incorrect.
Recall what NP-hardness of requires: a reduction from a known NP-hard problem into your target . The operational meaning is that a fast algorithm for would yield a fast algorithm for (compute reduction , then call a solver for ). Since is already known to be hard, this transfers that hardness onto .
Reducing the other way, , proves the opposite kind of statement. It says a fast algorithm for would solve ; that is, is no harder than . This is an upper bound on 's difficulty, not a lower bound. It places no floor on how hard is, so it establishes nothing about NP-hardness.
Example Problem: Independent Set
Independent Set: Given an undirected graph and integer , we say that a set of nodes is independent if no two nodes in are joined by an edge. Does there exist an independent set of size at least ?
Show that Independent Set is NP-Complete.
We first show that Independent Set is in NP. Given a graph instance and certificate (proposed solution) , we check that the following conditions are true. If any of them do not hold, then we output "no". Otherwise, we output "yes".
- Check that is at least size (verify ).
- For each pair of nodes with , ensure . In other words, no node pair in should form an edge in .
We observe that this verifier runs in polynomial time. Since , checking the first condition takes time. For the second condition, we consider all pairs of nodes in and check membership in ; there can be at most pairs and a naive membership check in takes time. Thus, our verifier runs in time, which is indeed polynomial.
We show that Independent Set is NP-Hard by showing a reduction from 3SAT.
Recall that 3SAT gives a 3CNF (AND of ORs) of clauses with exactly three variables, where the 3CNF is satisfiable if there exists a boolean assignment of variables that satisfy all clauses. For example, the following 3CNF is satisfiable by setting and to be true and to be false (there are other satisfying sets of variable assignments).
Given a 3CNF of clauses and variables, we create a graph that we can run an algorithm for Independent Set on. For clause , we create 3 nodes , , and , representing the three literals in clause . We connect these three nodes with edges forming a triangle. Additionally, for every pair of nodes representing a variable and its negation (i.e. and ) across all clauses, we add an edge between them. We set .
We argue that this reduction takes polynomial time. For each of the clauses, we create 3 nodes and 3 edges (to connect them all together) in our graph. In addition, we connect all pair of nodes representing variables and their negation. There are at most pairs possible, so we can reduce our 3SAT instance to an Independent Set instance in time, which is polynomial.
The intuition is that each clause is represented by a gadget of "nodes" in our graph . Since in our Independent Set instance, we must pick exactly one node from each clause gadget to be in the independent set (the triangle edges prevent picking two nodes from the same gadget). Picking a node corresponds to setting that literal to true. The consistency edges between contradictory literal pairs ensure we never pick both a variable and its negation (which would violate the independent set property), so the assignment is well-defined and satisfies every clause.
The visualization below walks through this construction on a small 3CNF. Step through it to see the clause gadgets, triangle edges, and consistency edges appear.
3SAT → Independent Set
One gadget per clause
Each clause becomes a gadget of three nodes, one per literal. We have three clauses, so nine nodes in total. Nothing is connected yet.
Suppose the 3CNF is satisfiable. We show that a satisfying assignment of variables to a 3SAT instance implies an independent set solution to our constructed graph. Fix a satisfying assignment. For each clause , at least one of its three literals is true under this assignment; pick one such true literal and include its corresponding node in . This gives .
We check that is independent. No two nodes lie in the same clause gadget, since we picked exactly one per gadget (no triangle edges are present within ). Also, no two chosen nodes correspond to a variable and its negation: since both nodes would have to present true literals under the same assignment, a variable and its negation cannot both be true. Hence, no consistency edge connects two nodes of . So is an independent set of size .
Suppose has an independent set with (we know it cannot go any higher because exactly one node can be picked per clause gadget, and there are of them).
Define a variable assignment by setting each literal corresponding to a node in to true (if a variable has neither literal represented in , then assign it arbitrarily). This assignment is well defined; if both and were in , it would not be an independent set because they are connected by an edge. So no variable is assigned conflicting values. Since contains one node per clause gadget, every cluase has at least one literal set to true, so every clause is satisfied. Hence is satisfiable.
Therefore, our 3CNF is satisfiable if and only if has an independent set of size , completing the reduction. Combined with the polynomial-time verifier shown earlier, Independent Set is NP-complete.
Known NP-Complete Problems
The known NP-complete problems fall into several natural categories. On assignments or exams, you may freely cite NP-complete problems that have appeared in class, previous assignments, or in the textbook.
Packing Problems
If your problem has a parameter for which as gets larger the problem gets harder, try reducing from:
- Independent Set: Given an undirected graph and integer , we say that a set of nodes is independent if no two nodes in are joined by an edge. Does there exist an independent set of size at least ?
- Set Packing: Given a universe , subsets , and an integer , does there exists a subcollection of of at least pairwise-disjoint subsets?
Covering Problems
If your problem has a parameter for which as gets larger the problem gets easier, try reducing from:
- Vertex Cover: Given an undirected graph and an integer , we say that a set of nodes if every edge has at least one endpoint in . Does there exist a vertex cover of size at most ?
- Set Cover: Given a universe , subsets , and an integer , does there exist a subcollection of of at most sets whose union is all of ?
Partitioning Problems
If your problem involves dividing elements into disjoint groups, try reducing from:
- 3D Matching: Given disjoint sets each of size , a set of triples , does there exist a set of triples in such that every element of is contained in exactly one of these triples?
- 3-Coloring: Given an undirected graph , does have a 3-coloring?
Sequencing Problems
If your problem involves giving an ordering over items, try reducing from:
- Hamiltonian Cycle: Given a graph (either directed or undirected), is there a Hamiltonian cycle (a path starts and ends with the same node and visits every other node exactly once)?
- Hamiltonian Path: Given a graph (either directed or undirected), start node and end node , is there a Hamiltonian path starting at and ending at ?
Other
If your problem involves summing over numeral values to reach a target, try reducing from:
- Subset Sum: Given nonnegative integers and a target integer , does there exist such that ?
If all else fails, try 3-SAT!
- 3SAT: Given a CNF with clauses of 3 literals each, does there exist a satisfying assignment?
Practice
Problem 1. [Graph Selection] Suppose we have a graph . You are given two disjoint sets and , and a vertex set . The Graph-Selection problem is to determine whether there exists a vertex set such that:
- For all , there is a such that
- For all , either or
Prove that Graph-Selection is NP-Complete. (Source: CS 4820 FA25 Prelim 2 Review)
Hint 1
Could we try a reduction from 3-SAT?
Hint 2
With our reduction from 3-SAT, create two nodes for setting a variable either true or false.
Hint 3
With the previous setup, what should our bad edge set be?
Solution
We first show that Graph Selection is in NP. Given a Graph Selection instance consisting of graph , edge sets and , and set , we take in a certificate set and check the following conditions. If any of them are false, we reject (outputting "no"). Otherwise, if all conditions hold, output "yes".
- . This takes time.
- For all , there is a such that . We can check this by fixing each and checking through all with to see if ; if no satisfies this, then we can reject . We enumerate of all possible pairs in , so we take time for this check.
- For all , either or . We check over all edges . If there exists such that and , then we reject . Since , this step takes time.
Our verifier has an overall runtime of , which is indeed polynomial.
We show that Graph Selection is NP-Hard by showing a reduction from 3SAT.
Given a 3CNF of clauses and variables, we create a graph that we can run an algorithm for Graph Selection on. We create the following nodes
- For each variable , we create two nodes and .
- For each clause , create a clause node .
- Let .
Next, we create the following edges.
- For every variable , create an edge . Let be the set of all these edges.
- For every clause with corresponding node , we create three edges and place them all in . Let be the 3 variables in the clause. For each of the three variables: if some literal is in the clause, we put in edge ; otherwise, if some literal is in the clause, we put in edge .
We run Graph-Selection on this input and output whatever it outputs. This reduction runs in polynomial time. We create nodes and edges, which is polynomial in the size of the input 3CNF.
We now show that the original 3SAT instance has a solution iff this instance of Graph-Selection has a solution.
Satisfiable gives a valid
Fix a satisfying assignment and let
Condition 1. by construction.
Condition 2. Let .
- If : the assignment satisfies , so some literal of is true. Let be its node ( if , else ). Then by construction, and since is true.
- If is a literal node: after preprocessing its literal occurs in some clause , so and .
Condition 3. , and the assignment gives each exactly one value, so at most one of lies in . No bad edge has both endpoints in .
Hence is a valid selection.
A valid gives a satisfying assignment
Given a valid , define
This is well defined, and it assigns every variable. Observe that forces , since and satisfies condition 3. So
Fix a clause . Since , condition 2 gives some with . Every good edge at leads to a node of one of 's three literals, so is such a node.
- : then , and the edge exists only because the literal occurs in . Clause satisfied.
- : then by the observation, and occurs in . Clause satisfied.
Every clause is satisfied, so is satisfiable.
Problem 2. [Monotone SAT] Consider the following variant of SAT. We say that a boolean formula is monotone if no variable appears negated; i.e. every literal is a positive occurence of some variable . For example, the following SAT formula is monotone:
We take as input a montone CNF formula over variables , where each consists of ORs of non-negated literals. We also take an input number . The Monotone SAT decision problem asks whether is satisfiable by setting at most variables equal to true.
Prove that Monotone SAT is NP-Complete.
Hint
Use a reduction from Vertex Cover.
Solution
We first show that Monotone SAT is in NP. Given a Monotone SAT instance with boolean formula (made up of clauses of only positive literals) and a parameter , we take a variable assignment as our certificate. We check the following conditions: the amount of variables set to true in the assignment is less than or equal to , and every clause is satisfied. If any of these conditions are false, we reject the certificate. Otherwise, we accept.
We claim that this verifier runs in polynomial time. To check the first condition, we can perform a linear scan through all the variable assignments and count how many variables are assigned to true. For the second condition, checking each clause requires scanning its literals. For a formula with variables and clauses, this runs in time, which is polynomial. Thus, Monotone SAT is in NP.
Next, we show that Monotone SAT is NP-Hard by showing a reduction from Vertex Cover.
Recall the Vertex Cover problem: given a graph and an integer , decide whether there is a set with such that every edge has at least one endpoint in .
For our reduction, consider an instance of Vertex Cover with graph and parameter with . We create a variable for each vertex . For each edge , create the clause . Then, let
and keep the same. Every literal is a positive occurence of a variable, so is monotone. The construction produces one variable per vertex and one clause per edge, so it runs in polynomial time.
For correctness, we show that is a yes-instance of Vertex Cover iff is a yes-instance of Monotone SAT.
Suppose is a vertex cover with . Set true exactly when . This sets at most variables true. For any clause corresponding to edge , since covers this edge, at least one of , so at least one of is true and the clause is satisfied. Thus, is satisfied with at most true variables.
Suppose is satisfied by an assignment with at most true variables. Let |S| \le k(v_i, v_j)(x_i \lor x_j)x_i, x_jv_i, v_j \in SSk$.
Since the reduction runs in polynomial time and is correct, Vertex Cover Monotone SAT, so Monotone SAT is NP-Hard.
Monotone SAT is in NP and is NP-Hard, so it is NP-Complete.
Problem 3. [3-Coloring] Given a graph , the 3-Coloring problem asks if there is an assignment of colors to the nodes of the graph such that no two adjacent vertices have the same color, and at most 3 colors are used.
Prove that 3-Coloring is NP-Complete.
Solution
We first show that 3-Coloring is in NP. Given a 3-Coloring instance of a graph , we take as input to our verifier a coloring assignment . The verifier iterates over every edge and checks that . If every edge has properly colored endpoints, accept; otherwise, reject. This runs in time, so our verifier is indeed polynomial.
We now show that 3-Coloring is NP-Hard by providing a reduction from 3SAT. Let be a 3CNF formula.
In progress!
Problem 4. [Partition] The input to the Partition problem is a set of large integers . The problem is to decide if there is a subset such that .
Show that Partition is NP-Complete.
Hint
Reduce from Subset Sum.
Solution
We first show that Partition is in NP. Given a Partition instance of large numbers , our certificate is a set . We can simply check if holds (if yes, then accept; otherwise, reject this certificate). If we use , then doing addition operations and checking if the sums are equivalent takes time. This is polynomial in the input size.
We show that Partition is NP-Hard by showing a reduction from Subset Sum.
Let numbers with target sum be a Subset Sum instance. First, we add up all numbers and let . We assume without loss of generality that . We create an input to Partition that uses numbers: from Subset Sum and one additional number .
Letting , this reduction takes , as we need to do addition/subtraction operations to create our new element (since these are large numbers, we take time for each addition operation). Therefore, our reduction runs in polynomial time.
The intuition for this reduction is as follows: if there exists such a subset such that , then the remaining elements sum to . So a yes-instance of Subset Sum splits the numbers into two groups with sums and . This is almost a partition; both sides aren't always balanced, they differ by . Adding in an extra number closes this gap on the side and makes the two sums equal to . In other words, is a "balancing weight" that turns an uneven Subset Sum split into an even partitioning.
Suppose there is a subset of the subset sum problem with total sum . Then this subset satisfies the parition rule for the input we created. Since we have , the remaining items not selected in will sum to . Thus, we have a solution to Partition.
Suppose our constructed Partition instance has a partition of the numbers with equal sums. Note that implies that both sides sum to . We simply chose subset to be the side of the partition not containing the newly created item , so is a solution to the Subset Sum problem.
Since we have shown Partition to be in NP and NP-Hard, this problem is NP-Complete as required.