NP-hard vs NP-complete
A problem is NP-hard if it is at least as hard as every problem in NP: any NP problem can be translated into it in polynomial time. A problem is NP-complete if it is NP-hard and also in NP, meaning a proposed solution can be checked quickly. So every NP-complete problem is NP-hard, but NP-hard problems need not be in NP, and some are not even decidable.
The short version
Start with the two building blocks. P is the class of yes/no problems an algorithm can solve in polynomial time, roughly "quickly." NP is the class of yes/no problems where a "yes" answer comes with a certificate, such as a proposed solution, that can be checked in polynomial time. If you need the background first, read P vs NP explained.
The two "hardness" labels are defined on top of these:
- NP-hard: every problem in NP reduces to it in polynomial time. Solving it quickly would let you solve everything in NP quickly. It says nothing about whether the problem itself is in NP.
- NP-complete: in NP and NP-hard. These are the hardest problems inside NP.
If any NP-complete problem has a polynomial-time algorithm, then P = NP. That is why they sit at the center of the P vs NP question.
Reductions, explained plainly
A reduction from problem A to problem B is a fast recipe that turns any instance of A into an instance of B with the same answer. If you have one, then a fast algorithm for B gives a fast algorithm for A: translate, then solve. In this sense B is "at least as hard as" A.
To prove a new problem X is NP-hard, you reduce a problem already known to be NP-hard to X, not the other way around. Getting this direction backwards is one of the most common mistakes in homework and in claimed proofs.
A concrete sketch: 3-SAT to clique
3-SAT asks whether a formula made of clauses, each an OR of three literals (a variable or its negation), can be made true. Clique asks whether a graph contains k vertices that are all connected to each other. Karp's 1972 reduction goes like this:
- For a formula with k clauses, create one vertex for each literal in each clause.
- Connect two vertices if they come from different clauses and are not negations of each other (not x and ¬x).
- Ask whether the graph has a clique of size k.
Take (x ∨ y ∨ z) ∧ (¬x ∨ ¬y ∨ z) ∧ (x ∨ ¬y ∨ ¬z), so k = 3. The assignment x = true, y = false satisfies it. Picking x from clause 1, ¬y from clause 2 and x from clause 3 gives three vertices that are pairwise connected: a clique of size 3.
It works in general. A clique of size k must take exactly one vertex from each clause, since vertices in the same clause are never connected, and it never contains both x and ¬x, so setting those literals true satisfies every clause. Conversely, a satisfying assignment gives one true literal per clause, and those vertices form a clique. The graph has 3k vertices, so building it takes polynomial time. Because 3-SAT is NP-complete, clique is NP-hard, and since a proposed clique is easy to check, clique is NP-complete.
P, NP, NP-complete and NP-hard compared
| Class | Definition | Can a "yes" answer be checked quickly? | Example problems |
|---|---|---|---|
| P | Solvable in polynomial time | Yes, and it can be solved quickly too | Shortest path, minimum spanning tree, linear programming |
| NP | "Yes" answers have a certificate checkable in polynomial time | Yes, by definition | Everything in P, plus SAT, Hamiltonian cycle, factoring (decision version) |
| NP-complete | In NP and NP-hard | Yes | SAT, 3-SAT, clique, vertex cover, Hamiltonian cycle, subset sum, knapsack (decision version) |
| NP-hard | Every NP problem reduces to it in polynomial time | Not necessarily | All NP-complete problems, shortest travelling salesman tour, halting problem |
Note that NP-complete is the overlap of NP and NP-hard. Factoring (asking whether a number has a factor below some bound) is in NP but is not known to be NP-complete, so "in NP and not known to be in P" does not mean NP-complete.
Classic NP-complete problems, and one NP-hard problem that is not
- SAT: can the variables of a Boolean formula be set so that it is true? The first problem shown NP-complete. See the SAT problem.
- 3-SAT: SAT restricted to clauses of three literals. Still NP-complete, and a convenient starting point for reductions.
- Clique: does a graph contain k vertices that are all mutually connected?
- Vertex cover: can k vertices touch every edge of a graph?
- Hamiltonian cycle: is there a cycle that visits every vertex exactly once?
- Subset sum: does some subset of a list of integers add up to a target value?
- Knapsack (decision version): can items be chosen within a weight limit so that their total value reaches at least V?
- Halting problem: does a given program eventually stop? It is NP-hard, because any NP problem can be turned into a question about whether a search program halts, but it is undecidable, so it is not in NP and not NP-complete.
Where the idea came from: Cook–Levin and Karp's 21
Stephen Cook's 1971 paper showed that satisfiability and 3-SAT are NP-complete. Leonid Levin, working independently in the Soviet Union, defined a closely related notion of "universal search problems" and published examples including satisfiability in 1973. The result that SAT is NP-complete is now called the Cook–Levin theorem.
In 1972 Richard Karp used reductions to show that 21 natural combinatorial problems are NP-complete, including clique, vertex cover, Hamiltonian circuit, knapsack, set cover and graph coloring (chromatic number). Karp also introduced the notation P and NP and the polynomial-time many-one reduction that became the standard definition. His paper showed that NP-completeness was not a curiosity of logic but a common feature of practical problems.
Common misconceptions
"NP means non-polynomial." It stands for nondeterministic polynomial time. Every problem in P is also in NP, so NP problems are not by definition slow.
"NP-hard means harder than NP-complete." NP-hard includes all NP-complete problems. Some NP-hard problems are much harder (the halting problem cannot be solved at all), but the label itself only sets a lower bound on difficulty.
"The travelling salesman problem is NP-complete." Only its decision version is: "Is there a tour of length at most L?" That is in NP, since a proposed tour is easy to check. The optimization version, "find the shortest tour," is NP-hard. It is not a yes/no question, so it cannot be in NP as defined, and checking that a tour is truly the shortest is not known to be easy. The same split applies to knapsack and many scheduling problems. See the travelling salesman problem for more.
"NP-hard means hopeless in practice." NP-hardness is about the worst case. Modern SAT solvers routinely handle many large real-world instances, even though no algorithm is known to handle every instance in polynomial time.
"Someone proved an NP-complete problem is easy." If that were true and correct, it would show P = NP. Nobody has done so; see the current status of P vs NP.
Questions
Is every NP-hard problem NP-complete?
No. An NP-hard problem only has to be at least as hard as everything in NP. The halting problem is NP-hard but undecidable, so it is not in NP and not NP-complete.
Is every NP-complete problem NP-hard?
Yes. NP-complete means in NP and NP-hard, so every NP-complete problem is NP-hard by definition.
What was the first NP-complete problem?
Boolean satisfiability (SAT). Stephen Cook proved it NP-complete in 1971, and Leonid Levin found an equivalent result independently, published in 1973.
Is the travelling salesman problem NP-complete or NP-hard?
Both, depending on the version. Asking whether there is a tour of length at most L is NP-complete; finding the shortest tour is an optimization problem and is NP-hard.
Does NP mean non-polynomial?
No. NP stands for nondeterministic polynomial time, and every problem that can be solved in polynomial time is also in NP.
Sources
- Stephen Cook, The complexity of theorem-proving procedures (STOC 1971)
- Richard Karp, Reducibility Among Combinatorial Problems (1972)
- Stephen Cook, The P versus NP Problem (official Clay problem description)
- Wikipedia, Cook–Levin theorem
- Wikipedia, Karp's 21 NP-complete problems
- Wikipedia, NP-hardness
- Wikipedia, NP-completeness
- Wikipedia, Clique problem (Karp's reduction from satisfiability)
- Wikipedia, Travelling salesman problem
- Wikipedia, Integer factorization
- Wikipedia, SAT solver
Get one email the day P vs NP changes status.
Get the alert