The classes

ClassDefinition
Psolvable in polynomial time
NPa solution can be verified in polynomial time
NP-hardat least as hard as every problem in NP (via polynomial reduction)
NP-completein NP and NP-hard
co-NPthe complement is in NP
PSPACEsolvable in polynomial space
pcounting the solutions

, and whether is the central open question. See Complexity Theory.

Reductions

(” reduces to ”) means: a polynomial-time transformation maps instances of to instances of with the same yes/no answer.

If is NP-hard and , then is NP-hard.

Note the direction — to prove is hard, reduce a known hard problem to , not the other way round. Reversing this is the classic error.

The reduction map

                      SAT  (Cook-Levin: the first NP-complete problem)
                       |
                     3-SAT
        /         /    |     \            \
  3-COLORING  CLIQUE  SUBSET-SUM   HAMILTONIAN-CYCLE   ...
      |          |         |               |
  planar 3-col  IND-SET  PARTITION        TSP
                  |         |
             VERTEX-COVER  KNAPSACK

Karp’s 21 problems (1972) established the core of this map. To show a new problem is NP-hard, reduce from the closest one on it.

The problems to recognise

ProblemStatus
SAT, 3-SATNP-complete
2-SATP — see 2-SAT
Clique, independent set, vertex coverNP-complete
Vertex cover / independent set on bipartite graphsP (König)
Graph 3-colouringNP-complete
2-colouring (bipartiteness)P
Hamiltonian path/cycleNP-complete
Eulerian path/circuitP
TSPNP-hard
Subset sum, partition, knapsackNP-complete (weakly — pseudo-poly DP exists)
Bin packing, makespanNP-hard
Set cover, dominating setNP-complete
Steiner treeNP-hard
Shortest path, MST, max flow, matchingP
Longest path (general graph)NP-hard
Longest path in a DAGP
Integer programmingNP-hard
Linear programmingP
Graph isomorphismquasipolynomial; not known to be either
Factoringin NP ∩ co-NP; not known to be NP-complete

The pairs are the useful part: each row where a tiny change flips the difficulty is a pattern worth memorising. Hamiltonian vs Eulerian, 2-SAT vs 3-SAT, 2-colouring vs 3-colouring, DAG longest path vs general longest path, LP vs ILP.

Weak vs strong NP-hardness

Weakly NP-hardStrongly NP-hard
Pseudo-polynomial algorithmexistsdoes not (unless P=NP)
FPTASpossibleimpossible
Examplessubset sum, knapsack, partitionTSP, bin packing, 3-partition, clique

Knapsack’s DP is polynomial in the value of but exponential in its number of bits — that is exactly what “weakly NP-hard” means, and it is why an FPTAS exists for knapsack but not for TSP.

What to do when you recognise NP-hardness

Stop looking for a polynomial exact algorithm. Then check the constraints — they tell you the intended approach:

ConstraintApproach
bitmask DP
meet in the middle
, sparse or structuredbranch and bound
Small parameter FPT algorithm
Bounded values ()pseudo-polynomial DP
The graph is a treetree DP — most NP-hard graph problems are easy on trees
The graph is bipartitematching/flow
The graph is planarseparator-based, or a specialised algorithm
The graph has small treewidthDP over a tree decomposition
Scored/marathon problemheuristics

“Restricted to trees” is the most common escape hatch in contests. Independent set, dominating set, vertex cover, colouring and Steiner tree are all NP-hard in general and all linear-time on trees.

Beyond NP

ClassTypical problem
#P-completecounting perfect matchings (the permanent), counting SAT solutions
PSPACE-completegeneralised games (Go, chess on ), QBF
EXPTIME-completesome two-player games with full generality
Undecidablehalting problem, Post correspondence

Counting is often strictly harder than deciding: finding a perfect matching is polynomial, counting them is p-complete. If a problem asks for a count where the decision version is already interesting, be alert.

See also: Complexity Theory · Parameterized Complexity · Approximation Algorithms