1. Gyanastra
  2. Algorithms
  3. NP-Completeness: P, NP, NP-Hard, Reductions and the Standard Problems
  4. Why this chapter exists
Free lesson, no account needed 6 min read

Algorithms · NP-Completeness: P, NP, NP-Hard, Reductions and the Standard Problems

Why this chapter exists

Every algorithm you have studied so far — merge sort, Dijkstra, Floyd–Warshall, matrix chain multiplication — was solvable. This chapter is about the other half of computer science: problems for which no polynomial-time algorithm is known, and the theory that tells us why they are all equally hard. GATE asks this topic almost every year, usually as 1–2 mark true/false statements or a 2-mark matching question. The marks are easy if and only if you know the definitions to the letter and the direction of reductions. Get those two things right and you cannot be tricked.


Decision problems first

A decision problem is a problem whose answer is yes or no.

Optimisation versionDecision version
Find the shortest tour in a TSP instanceIs there a tour of length ≤k\le k?
Find a maximum cliqueIs there a clique of size ≥k\ge k?
Find a minimum vertex coverIs there a vertex cover of size ≤k\le k?
Find the maximum value of a knapsackIs there a subset with value ≥V\ge V and weight ≤W\le W?

Why we restrict to decision problems. Complexity classes P, NP, NP-complete are defined only for decision problems, because a class is a set of languages (sets of strings over an alphabet). Every decision problem corresponds to a language LL = set of encodings of "yes" instances. Optimisation problems are handled by their decision versions; if the decision version is hard, the optimisation version is at least as hard.

Exam convention. GATE almost always phrases NP-completeness questions about the decision version. When a question says "TSP is NP-complete", it means the decision version "is there a tour of length ≤k\le k". The optimisation version is NP-hard but not known to be in NP (see §12).


The class P

Definition. PP is the class of decision problems solvable by a deterministic Turing machine in time O(nk)O(n^k) for some constant kk, where nn is the input size.

Informally: problems with a polynomial-time algorithm.

Examples: sorting, shortest path (Dijkstra/Bellman–Ford), MST (Kruskal/Prim), maximum matching in bipartite graphs, 2-SAT, linear programming (Khachiyan's ellipsoid method, Karmarkar's algorithm), primality testing (AKS 2002), Eulerian circuit existence, 2-colouring (bipartite check), string matching, LCS.

Note on primality. For decades primality was not known to be in P; it was known to be in NP and co-NP. In 2002 Agrawal–Kayal–Saxena gave a deterministic polynomial-time algorithm, so primality is now known to be in P. GATE has asked this.


The class NP

Definition (verifier form). NPNP is the class of decision problems for which a "yes" answer can be verified in polynomial time given a certificate (witness). Formally, L∈NPL \in NP iff there is a polynomial-time deterministic verifier VV and a polynomial pp such that

x∈L  ⟺  ∃ y, ∣y∣≤p(∣x∣), V(x,y)=accept.x \in L \iff \exists\, y,\ |y| \le p(|x|),\ V(x,y) = \text{accept}.

Definition (NDTM form). NPNP is the class of decision problems solvable by a non-deterministic Turing machine in polynomial time.

These two definitions are equivalent. The verifier form is the one GATE uses in statements like "NP is the class of problems for which a solution can be verified in polynomial time".

Examples. SAT (certificate: a satisfying assignment), Hamiltonian cycle (certificate: the cycle), clique (certificate: the vertex set), subset sum (certificate: the subset), 3-colouring (certificate: the colouring), composite numbers (certificate: a factor).

Trivially P⊆NPP \subseteq NP, because if you can solve it in polynomial time you can ignore the certificate and just solve it.

Trap. "NP" does not stand for "not polynomial". It stands for Non-deterministic Polynomial. A problem in NP may well be in P. Every problem in P is in NP.


The class co-NP

Definition. co-NP={ L:Lˉ∈NP }co\text{-}NP = \{\,L : \bar{L} \in NP\,\}, where Lˉ\bar L is the complement (swap yes/no).

Equivalently: problems where a "no" answer has a short, polynomial-time-checkable certificate.

Examples. Tautology (is a given Boolean formula true for all assignments? — a "no" certificate is one falsifying assignment), unsatisfiability of a formula (a "no" certificate would be a satisfying assignment — so UNSAT is in co-NP), primality (a "no" certificate is a factor — so primality is in co-NP; it is also in P now).

Facts.

  • P⊆NP∩co-NPP \subseteq NP \cap co\text{-}NP.
  • If P=NPP = NP then NP=co-NPNP = co\text{-}NP.
  • It is not known whether NP=co-NPNP = co\text{-}NP. It is widely believed they differ.
  • If some NP-complete problem were shown to be in co-NP, then NP=co-NPNP = co\text{-}NP.

NP-hard and NP-complete

Definition (NP-hard). A problem XX (not necessarily a decision problem, not necessarily in NP) is NP-hard if every problem in NP is polynomial-time reducible to XX. That is, for every L∈NPL \in NP, L≤pXL \le_p X.

Definition (NP-complete). XX is NP-complete if

  1. X∈NPX \in NP, and
  2. XX is NP-hard.

Both conditions are required. A problem that is NP-hard but not in NP is not NP-complete.

Immediate consequences.

  • If any NP-complete problem is in P, then P=NPP = NP.
  • If any problem in NP is not in P, then no NP-complete problem is in P.
  • If P≠NPP \ne NP, then NP-complete problems are outside P and outside co-NP (assuming NP≠co-NPNP \ne co\text{-}NP).

Pages 1–2 of 2