Aller au contenu principal
Data structures, algorithms, and the core CS foundations — plus an optional advanced track for expert topics.

Core Computer Science

Data structures, algorithms, and the core CS foundations — plus an optional advanced track for expert topics.

Computational Complexity: P vs NP

Computational complexity theory asks a deceptively simple question: which problems can be solved efficiently, and which are intrinsically hard? Its central object of study is the P vs NP question — whether every problem whose answer can be checked quickly can also be solved quickly.

The Class P

P is the set of decision problems solvable in polynomial time — O(n^k) for some constant k. Sorting, searching, shortest paths, and matching are all in P. “Polynomial” is the theory’s stand-in for “feasible”, because polynomials compose and remain polynomial: any efficient subprocedure can be used inside any other.

The Class NP

NP (nondeterministic polynomial) is the set of decision problems whose yes-instances can be verified in polynomial time given a certificate (a proposed solution). The traveling-salesman question “is there a tour cheaper than B?” is in NP: if someone hands you a tour, you can check its cost quickly. Crucially, NP does not mean “not polynomial” — it means “nondeterministically polynomial”. Every problem in P is automatically in NP.

Reductions

To compare problems, we use polynomial-time reductions: a reduction A ≤ B means “solve A by solving B, with only polynomial extra work”. If A reduces to B, then B is at least as hard as A. Reductions chain together the difficulty landscape: show one problem reduces to another and you’ve shown a whole class of problems collapses together.

A Worked Reduction: Vertex Cover → Independent Set

The reduction web only becomes concrete when you write one down. Vertex Cover asks: does there exist a set of ≤ k vertices touching every edge? Independent Set asks: does there exist a set of ≥ k vertices with no edges between them? On the same graph, the two are complements:

  • A vertex cover touches every edge.
  • An edge is “uncovered” exactly when both endpoints are left out of the cover.

So a set S is an independent set if and only if V \ S is a vertex cover. This is an immediate, linear-time reduction in both directions:

Vertex Cover (G, k)  ≤  Independent Set (G, n − k)   via complement
Independent Set (G, k) ≤  Vertex Cover (G, n − k)     via complement

Why this is a valid reduction: an oracle that solves Independent Set on (G, n − k) answers “yes” exactly when (G, k) has a vertex cover, and the transformation (complement the set) takes polynomial time. This proves the two problems are equally hard — which is why the practice of reductions is so powerful: one hardness result, applied through a chain of such complement/transformation arguments, propagates across the whole web of NP-complete problems.

A Second Example: SAT → 3-SAT

SAT asks whether a Boolean formula has a satisfying assignment. 3-SAT restricts each clause to exactly three literals. The reduction takes each clause and rewrites it to a chain of 3-literal clauses using fresh variables:

(x₁ ∨ x₂)                 → (x₁ ∨ x₂ ∨ y) ∧ (x₁ ∨ x₂ ∨ ¬y)
(x₁ ∨ x₂ ∨ x₃ ∨ x₄)      → (x₁ ∨ x₂ ∨ y) ∧ (¬y ∨ x₃ ∨ x₄)

The y variables are fresh per clause, so the transformation is linear in the input size. If the original clause is satisfiable, so is the chain (pick y appropriately); if the chain is satisfiable, the original clause must be too. This is the canonical “shape” of every reduction: a polynomial-time transformation plus a “yes iff yes” equivalence proof.

NP-Hard and NP-Complete

  • NP-hard: a problem is NP-hard if every problem in NP reduces to it. NP-hard problems are at least as hard as anything in NP (they need not even be in NP — optimization versions often aren’t).
  • NP-complete: a problem that is both in NP and NP-hard. NP-complete problems are the hardest problems in NP — and they are all equivalently hard.

The classic NP-complete problems form a reduction web: SAT (Boolean satisfiability), 3-SAT, clique, vertex cover, Hamiltonian cycle, traveling salesman, knapsack, and graph coloring all reduce to each other.

The Complexity Landscape

It helps to picture the classes as nested regions:

        ┌─────────────────────────────────────────┐
        │              All problems               │
        │   ┌─────────────────────────────────┐   │
        │   │             NP-hard             │   │
        │   │   ┌─────────────────────────┐   │   │
        │   │   │        NP              │   │   │
        │   │   │   ┌───────────────┐    │   │   │
        │   │   │   │      P        │    │   │   │
        │   │   │   └───────────────┘    │   │   │
        │   │   │    NP-complete =       │   │   │
        │   │   │    NP ∩ NP-hard        │   │   │
        │   │   └─────────────────────────┘   │   │
        │   └─────────────────────────────────┘   │
        └─────────────────────────────────────────┘

Reading the diagram: P ⊆ NP (everything solvable quickly is verifiable quickly). NP-complete sits inside NP and NP-hard — the hardest problems in NP. NP-hard extends outside NP entirely: NP-hard problems (like the optimization version of TSP) need not even be checkable quickly. If P = NP, the two inner circles collapse into one; if P ≠ NP, the NP-complete problems form a hard shell around P.

A Field Guide to NP-Complete Problems

Every NP-complete problem has an easy certificate — the “yes-instance evidence” that makes it verifiable in polynomial time:

ProblemQuestion (“yes” instance)Certificate
SATCan the formula be satisfied?A satisfying assignment
3-SATCan each 3-literal clause be satisfied?A satisfying assignment
CliqueIs there a clique of size ≥ k?The k vertices (all pairwise adjacent)
Vertex coverA set of ≤ k vertices touching all edges?The k vertices
Independent setA set of ≥ k vertices with no edges between?The k vertices
Hamiltonian cycleA cycle visiting each vertex once?The cyclic ordering of vertices
TSP (decision)A tour cheaper than B?The tour and its cost
Knapsack (decision)A subset of items with value ≥ V and weight ≤ W?The chosen subset
Graph coloringA coloring with ≤ k colors?The color of each vertex
Subset sumA subset summing exactly to T?The subset

The pattern to internalize: in every case, the certificate is short and checkable in polynomial time — that is exactly what makes the problem a member of NP. Finding the certificate, however, is what resists polynomial time.

The Open Question

The big question: is P = NP? If P = NP, every quickly-checkable problem has a quick solution — an outcome most theorists consider unlikely (a proof would collapse cryptography and most optimization theory). If P ≠ NP, then the NP-complete problems have no polynomial-time algorithms at all, and the best we can do is heuristic, approximate, or exponential algorithms. The problem is one of the Clay Mathematics Institute’s Millennium Prize Problems.

Why It Matters Practically

Even though the question is open, complexity theory guides daily engineering:

  • Recognize hardness: a reduction to an NP-complete problem is a signal to stop hunting for an exact polynomial algorithm.
  • Choose the right approach: for NP-hard inputs, reach for approximation algorithms, heuristics (greedy, simulated annealing), branch-and-bound with pruning, or parameterized algorithms.
  • Verify certificates: cryptographic systems rely on the asymmetry that generating a solution (e.g., factoring) is believed hard while verifying one is easy.

Practice Trajectory

  1. Classify familiar algorithms into P (polynomial) — sorting, shortest paths, MST.
  2. Classify classic problems into NP — TSP, knapsack, graph coloring, 3-SAT.
  3. Practice reductions: show vertex cover reduces to/from clique via complement.
  4. Confirm SAT is the “root” NP-complete problem (Cook–Levin theorem).
  5. Discuss why a proof of P = NP would break modern public-key cryptography.

When It’s the Right Tool

SituationTakeaway
Problem reduces to SAT/knapsack/TSPLikely NP-hard — stop seeking exact polynomial algorithms
Need a solution anywayUse heuristics, approximation, or exponential-with-pruning
Certificate is easy to checkThe problem is at least in NP — a starting point for analysis
Crypto relies on factoring/discrete logThe asymmetry between solving and verifying is the whole point