COMPUTER ENGINEER

← BlogDATA STRUCTURES / ALGORITHMSPython example pack ↓

Complexity and approximation · 46 / 53

P, NP, reductions and lower bounds

Understand → trace → implement → justify

3 min reading · allow extra time for practice

P contains decision problems solvable in polynomial time. NP contains decision problems whose yes answers have polynomial-size certificates verifiable in polynomial time; NP does not mean non-polynomial. P⊆NP, and whether P=NP remains open. NP-complete problems are both in NP and NP-hard. NP-hard problems need not belong to NP.

A polynomial reduction A≤pB transforms A into B. A fast B solver would solve A. To prove B hard, reduce a known hard A to B, not merely the reverse. Distinguish the TSP decision problem from minimum-tour optimisation.

NP-hard does not mean every instance is impossible. Small instances, special classes, parameterisation and branch-and-bound may work well. Pseudo-polynomial algorithms depend on numeric magnitude rather than binary input length. Undecidability is a separate issue: no algorithm decides every instance.

Keep the reduction direction straight

To prove a new problem X hard using SAT, transform a SAT instance into an X instance in polynomial time and prove SAT yes iff X yes. This is SAT≤pX. Showing only X≤pSAT does not prove X hard: easy problems can also reduce to SAT.

NP-completeness requires two obligations: membership in NP through polynomial certificate verification, and a reduction from a known NP-hard problem. Calling an optimisation problem NP-complete without naming its decision version is imprecise. Decision versions include a threshold, such as whether a tour of cost≤K exists.

Encoding matters. Unary encoding of numeric n has length n; binary encoding has length about log₂ n. An O(n) loop can therefore be exponential in binary input length. Always identify the input-length measure before assigning a complexity class.

Foundation: Contrast verifying and finding a Hamiltonian certificate. Intermediate: Define a threshold-based decision version of an optimisation problem. Advanced: Present a reduction’s mapping, polynomial construction cost and two-way correctness separately. P≠NP has not been proved; preserve its open status.

A worked trace

Validate a proposed Hamiltonian cycle by checking vertices and edges; finding the certificate differs from verifying it.

Cost and assumptions

Certificate checks can be expected O(V+E) with suitable hashing; this does not bound the cost of finding one.

Executable example

Run with Python 3. Assertions check a small example; they do not constitute a general correctness proof. Some advanced examples are explicitly small models or reference oracles, not full production implementations.

Python 3
def verify_cycle(vertices,edges,cycle):
    return len(cycle)==len(vertices) and set(cycle)==set(vertices) and all((cycle[i],cycle[(i+1)%len(cycle)]) in edges for i in range(len(cycle)))
assert verify_cycle([0,1,2],{(0,1),(1,2),(2,0)},[0,1,2])

TEST YOUR UNDERSTANDING

Your turn

Does NP membership prove that an optimal tour can be found quickly?

Reveal the reasoning

No; it concerns efficient verification of yes certificates for the decision version.

Take it further

  1. Write down the input contract and the invariant before changing the example.
  2. Add empty, boundary, duplicate and adversarial cases where they are relevant. Compare with a simple oracle.
  3. Derive the cost under the stated model; measure several input sizes and explain discrepancies.

Primary reading & lecture notes

Original explanations in this course; these references provide proofs and further reading. Source links may cover the broader module.

Jeff Erickson · Algorithms ↗MIT 6.046J · Design and Analysis ↗
Solve the exercise before moving on.