E ExamMaster

Data Structures & Algorithms · Data Structures & Algorithms

P, NP and NP-Completeness

Decision versus optimisation, the classes P, NP, NP-hard and NP-complete, polynomial reductions, and Cook's theorem.

Six concepts on NP-completeness — decision versus optimisation, the four class names, the direction of a polynomial reduction, Cook's theorem, the P = NP collapse, and why NP-hard is broader than NP-complete. Graph-shaped NPC problems (clique, chromatic number, TSP-decision) are the next topic.

  • Data Structures & Algorithms
  • Hard level
  • 6 concepts

1Decision problems, not search problems

P, NP and NP-complete are classes of decision problems: each instance is a yes or a no. 'Is there a tour of cost at most K?' is a decision problem. 'Find the shortest tour' is an optimisation problem. The two are close — if you can find the shortest tour you can answer every K, and if you can answer every K you can binary-search the optimum — but they do not sit in the same class, because NP is a class of languages.

Certificates attach to yes-instances of decision problems. A tour of cost \le K is a certificate for a yes. There is no single certificate convention for 'the shortest tour' that puts the optimisation problem in NP.

Decision questions answer yes or no: on the four-city instance, tour-at-most-17 is no and tour-at-most-18 is yes. The optimisation question answers with the number 18. Those outputs do not share a scale, which is why they sit in different classes.

Split one informal problem

  1. Name the objectA tour, a clique, a colouring, a packing.
  2. DecisionDoes there exist one with cost / size / colours \le or \ge a given K?
  3. OptimisationFind the best such object. NP-hard, typically, but not 'in NP'.
Same story, two questions
Informal nameDecision (in NP or not)Optimisation
TSPTour of cost \le K? (NPC)Shortest tour (NP-hard)
CliqueClique of size \ge k? (NPC)Largest clique (NP-hard)
KnapsackValue \ge V at weight \le W? (NPC)Maximum value (NP-hard)

Four cities, K = 17 and K = 18

Shortest tour on the four-city instance is 18. Answer the decision questions 'tour \le 17?' and 'tour \le 18?'.

  • shortest tour18
  • exists tour \le 17?no
  • exists tour \le 18?yes — the tour of cost 18 is a certificate

Pro tip. The certificate for a yes is the tour itself. A verifier adds four edge weights and compares with K — polynomial work, no search.

The NP-complete version of TSP is
  1. The decision question: is there a tour of cost \le K?
  2. The optimisation question: find the shortest tour
  3. Both, because they are polynomial-time equivalent

NP-complete is a class of decision problems. Optimisation TSP is NP-hard. Poly-time equivalence of opt and decision does not put the optimisation problem into NP.

2P, NP, NP-hard, NP-complete

P is the decision problems solvable in time polynomial in the input size — O(n^k) for some constant k. Sorting-related decisions, shortest paths in graphs with nonnegative weights, and 2-colouring sit here.

NP is the decision problems with a polynomial-time verifier: every yes-instance has a short certificate that the verifier accepts, and the verifier rejects every certificate on a no-instance. NP does not mean 'non-polynomial'. Every problem in P is in NP, because the solver itself is a verifier that ignores the certificate.

NP-hard means every problem in NP polynomial-time reduces to it — at least as hard as all of NP. NP-complete means in NP and NP-hard. The NP-complete problems are the hardest problems that are still in NP.

Figure. Schematic containment, not to scale and not a proof that P \neq NP. NP-complete problems sit in the overlap of NP and NP-hard. Halting is NP-hard and outside NP.

Place a problem

  1. In P?Is there a poly-time algorithm that always answers yes or no?
  2. In NP?Is there a short certificate a poly-time verifier can check on yes-instances?
  3. NP-hard?Does every NP problem reduce to it? If also in NP, it is NP-complete.
Four labels
ClassRequiresTypical member
PPoly-time algorithmShortest path (nonnegative)
NPPoly-time verifiable yes-certificateSAT, clique decision, TSP decision
NP-hardEvery NP problem reduces to itSAT, TSP-opt, Halting
NP-completeIn NP and NP-hardSAT, clique decision, TSP decision
A problem in P is also
  1. In NP — a solver is a verifier that can ignore the certificate
  2. Outside NP — P and NP are disjoint
  3. NP-complete — every poly-time problem is hardest

P \subseteq NP. They are not known to be equal and are not disjoint. NP-complete problems are the hardest in NP; shortest path is not one of them.

3Reduction: A ≤p B points at the harder one

A polynomial-time reduction from A to B is a function f, computable in polynomial time, that turns every instance x of A into an instance f(x) of B so that x is a yes if and only if f(x) is a yes. We write A \le_p B. If you can solve B in polynomial time, you can solve A in polynomial time: compute f(x), then run the B-solver.

Hardness travels the other way. If A is already NP-hard and A \le_p B, then B is NP-hard. The usual mistake is to reduce the new problem to the old one and claim a hardness proof.

Figure. The reduction arrow goes from the known-hard problem to the new one. Hardness flows against the arrow: B is at least as hard as A.

To prove B is NP-hard

  1. Pick a known-hard ASAT after Cook's theorem; later, any NP-complete problem.
  2. Build fA poly-time map from A-instances to B-instances that preserves yes/no.
  3. ConcludeA \le_p B, so B is at least as hard as A. If B is also in NP, B is NP-complete.

Using a reduction

def solve_A(x, solve_B, reduce_A_to_B):
    y = reduce_A_to_B(x)   # poly-time f
    return solve_B(y)      # A is yes iff B is yes
To prove B is NP-hard given that A is NP-hard, you show
  1. A \le_p B — reduce the known-hard problem to B
  2. B \le_p A — solve B by calling A
  3. A \in P and B \in NP

A \le_p B means B is at least as hard as A. B \le_p A is the direction you use to reuse an A-solver, not to prove B hard. P-versus-NP membership is a different claim.

4Cook's theorem: SAT is NP-complete

Cook–Levin, the result this syllabus calls Cook's theorem, says that SAT is NP-complete. SAT is the decision problem: given a Boolean formula, does there exist a truth assignment that makes it true? SAT is in NP because a guessed assignment is a certificate the verifier can evaluate in polynomial time.

The hardness half is the content of the proof: every language L in NP has a polynomial-time verifier, and that verifier's computation on an input x and a certificate c can be encoded as a Boolean formula \varphi_{x} that is satisfiable if and only if some certificate makes the verifier accept. The map x \mapsto \varphi_{x} is the reduction L \le_p SAT. You do not need the tableau details to use the theorem: after it, SAT is a legal starting point for every later NP-completeness proof.

Figure. Cook–Levin encodes an arbitrary NP verifier on x and certificate c as a formula φ_x that is satisfiable exactly when some certificate is accepted. SAT is therefore NP-complete and later proofs may reduce from SAT.

What the theorem gives you

  1. SAT is in NPA truth assignment is a short certificate; evaluating the formula is poly-time.
  2. Every L \in NP reduces to SATEncode the verifier's computation as a formula. That is the proof, not a second definition of NP.
  3. AfterwardsTo prove a new B is NPC, show B \in NP and SAT \le_p B (or C \le_p B for any already-NPC C).
Cook's theorem (Cook–Levin) states that
  1. SAT is NP-complete
  2. P \neq NP
  3. Every NP-complete problem reduces to sorting

The theorem is SAT's NP-completeness. It does not settle P versus NP. Sorting is in P and is not a target of NPC reductions.

5One NPC algorithm in P would collapse P = NP

Suppose some NP-complete problem X had a polynomial-time algorithm. Take any L in NP. By NP-hardness of X we have L \le_p X, so L is solvable in polynomial time too: reduce, then run the X-solver. Therefore NP \subseteq P. Combined with the always-true P \subseteq NP, we get P = NP.

That is why a claimed poly-time algorithm for SAT, for clique, or for TSP-decision is a claim that P = NP. Nobody has one that has survived checking, and nobody has a proof that none exists. The working stance in algorithm design is: treat NPC problems as ones you solve exactly only on small n, or approximately, or by exploiting special structure (bipartite, bounded treewidth, tiny k).

Figure. If an NPC X is in P, every L in NP rides the reduction into X and comes out in P.

The collapse

  1. Assume X \in P and X is NPCX has a poly-time solver, and every L \in NP reduces to X.
  2. TransferEach L is 'reduce, then solve X' — still poly-time.
  3. ConcludeNP \subseteq P, hence P = NP.
A polynomial-time algorithm for SAT would imply
  1. P = NP — every NP problem reduces to SAT
  2. Only SAT is in P; other NPC problems stay hard
  3. NP-hard problems outside NP, such as Halting, become decidable

SAT is NP-complete, so every NP problem reduces to it. Other NPC problems would fall with it. Halting is undecidable and NP-hard; a SAT solver does not decide the Halting problem.

6NP-hard need not be in NP

NP-complete is a narrower badge than NP-hard. Completeness requires membership in NP. Hardness does not. The Halting problem is NP-hard — every NP problem reduces to it — and it is undecidable, so it is not in NP. Optimisation TSP is NP-hard and is not a decision language, so it is not NP-complete.

Clique, chromatic-number and TSP-decision — the three syllabus graph problems — are NP-complete and each has a short certificate. Their optimisation twins (largest clique, \chi(G), shortest tour) stay NP-hard and outside the NPC roster.

Figure. Bars are a badge count, not a hardness ranking: 2 means in NP and NP-hard (NPC); 1 means NP-hard only.

Which badge

  1. In NP and NP-hardNP-complete — SAT, clique-decision, TSP-decision, 3-colouring.
  2. NP-hard, not in NPHalting; optimisation TSP; 'find the largest clique'.
  3. In NP, not known hardEvery problem in P; also problems in NP that are not known to be NPC (the P-versus-NP gap).
Hard versus complete
ProblemIn NP?NP-hard?NPC?
SATyesyesyes
TSP decisionyesyesyes
TSP optimisationno (not a language)yesno
Haltingno (undecidable)yesno
Optimisation TSP is
  1. NP-hard, but not NP-complete, because it is not a decision problem
  2. NP-complete, because it is equivalent to TSP-decision
  3. In P, because branch and bound solves it

NP-complete requires membership in NP. Equivalence of opt and decision does not make the opt problem a language. Branch and bound is exponential in the worst case, not a P algorithm.

Notes

  • A decision problem answers yes or no. An optimisation problem asks for a best object. Complexity classes P and NP are defined on decision problems.
  • P is the class of decision problems solvable in polynomial time. NP is the class of decision problems whose yes-instances have a certificate that a polynomial-time verifier can check — not 'non-polynomial'.
  • Problem A polynomial-time reduces to problem B (A \le_p B) when a poly-time function maps instances of A to instances of B so that the answers match. Then B is at least as hard as A (up to a polynomial).
  • NP-hard means every problem in NP reduces to it. NP-complete means it is in NP and NP-hard.
  • Cook–Levin (Cook's theorem on this syllabus): SAT is NP-complete. It is the first natural NP-complete problem; every language in NP reduces to it.
  • If any NP-complete problem is in P, then P = NP. That is why a polynomial algorithm for one NPC problem would collapse the classes.

Formulas

  • P \subseteq NP (every problem you can solve in poly time, you can verify in poly time).
  • A \le_p B and B \in P imply A \in P.
  • NP-complete = NP \cap NP-hard.
  • If any NP-complete X is in P, then P = NP.
  • SAT = \{ Boolean formulas that have a satisfying assignment \} is NP-complete (Cook–Levin).

Exam traps & shortcuts

  • NP does not mean 'non-polynomial'. Many NP problems are in P. NP means 'yes-answers have short, quickly checked proofs'.
  • To show B is NP-hard, reduce a known NP-hard A to B — A \le_p B. The arrow points toward the problem you are proving hard.
  • Optimisation TSP is NP-hard; decision TSP ('is there a tour of cost \le K?') is NP-complete. Do not quote them as the same class membership.
  • Cook's theorem gives you SAT as the starting NPC problem. Later NPC proofs reduce from SAT (or from another NPC problem), they do not replay Cook's proof.

Reference tables

Restated from the class and hard-versus-complete concepts.

Class roster
LabelMeans
PDecidable in polynomial time
NPYes-instances have a poly-time-checkable certificate
A \le_p BPoly-time f with x \in A iff f(x) \in B
NP-hardEvery problem in NP reduces to it
NP-completeIn NP and NP-hard
Cook–LevinSAT is NP-complete

Which way the arrow goes.

Direction cheatsheet
You wantYou show
B is NP-hardA \le_p B for a known-hard A
A is in P, given B in PA \le_p B
P = NP from one algorithmthat algorithm puts some NPC problem in P

Recap

Night-before NP pegs.

Decision
NPC is a class of yes/no problems. 'Find the shortest tour' is NP-hard, not NPC.
NP
Short, quickly checked yes-certificate. Not 'non-polynomial'. P \subseteq NP.
Reduce
A \le_p B means B is at least as hard. Prove B hard by reducing from a known-hard A.
Cook
SAT is NP-complete. Later proofs start from SAT (or another NPC problem).
Collapse
One NPC problem in P \Rightarrow P = NP. Halting stays undecidable.
Hard \neq complete
Halting and TSP-opt are NP-hard and not NPC.

Practise P, NP and NP-Completeness

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • A 6-question practice set that ends the chapter
  • 6 quick checks with worked explanations
  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.