Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 1: The Complexity Classes — P, NP, NP-Hard, NP-Complete

Lesson 3 of 15 in the free Complexity Theory notes on Siksha Sarovar, written by Rohit Jangra.

2.1 Class P — solvable fast

P = the class of decision problems (languages) decidable by a deterministic algorithm in polynomial time, i.e. time O(nᵏ) for some constant k.

Members: searching, sorting-based decisions, shortest path (Dijkstra/Bellman–Ford), minimum spanning tree, maximum matching, linear programming, 2-SAT, 2-colourability, and — since the 2002 AKS algorithm — primality testing.

P is robust: it is unchanged whether you compute on a Turing machine, a RAM, or in any reasonable programming language (they simulate each other with polynomial overhead), and it is closed under composition — a polynomial algorithm calling polynomial subroutines stays polynomial.

2.2 Class NP — verifiable fast

Two equivalent definitions (be able to state both!):

Verifier definition. L ∈ NP iff there is a polynomial-time verifier V and polynomial p such that: x ∈ L ⟺ ∃ certificate c, |c| ≤ p(|x|), with V(x, c) = ACCEPT.

Machine definition. NP = problems decidable by a non-deterministic Turing machine in polynomial time (Unit 2 makes this precise).

NP = Nondeterministic Polynomial — not "non-polynomial"! Every problem in P is in NP (P ⊆ NP).

The intuition: NP problems may be hard to solve, but a claimed solution is easy to check:

ProblemCertificatePolynomial check
SAT — is this formula satisfiable?a truth assignmentevaluate the formula
CLIQUE — has G a clique of size k?the k verticescheck all pairs adjacent
HAMCYCLEthe vertex orderingcheck consecutive edges exist
SUBSET-SUMthe subsetadd it up
3-COLOURINGthe colouringcheck every edge bi-chromatic

(co-NP, for contrast, has short certificates for NO answers — e.g. "this formula is unsatisfiable" has no obvious short proof; whether NP = co-NP is also open.)

2.3 NP-hard and NP-complete

Using polynomial-time reductions ≤ₚ (next lesson) we define the "hardest" problems:

NP-hard: L is NP-hard if every problem in NP reduces to L (A ≤ₚ L for all A ∈ NP). L itself need not be in NP. NP-complete: L is NP-complete if (1) L ∈ NP and (2) L is NP-hard.

NP-complete problems are the summit of NP: solve one of them in polynomial time and all of NP collapses into P.

2.4 The four classes side by side

ClassSolvable in poly time?Verifiable in poly time?Example
Pshortest path
NPunknownSAT, CLIQUE
NP-completeunknown (= P iff P=NP)SAT (and thousands more)
NP-hardunknown or provably nonot necessarilyTSP optimisation, halting problem

Common trap clarified:

  • NP-hard but (probably) not in NP: the optimisation version of TSP ("find the shortest tour" — not a decision problem), and the halting problem (undecidable, hence certainly not in NP, yet NP-hard).
  • "NP-complete" is a precise term; "NP-hard" is the one to use for "at least as hard as NP" when membership in NP is unclear.

2.5 Why thousands of problems turned out NP-complete

After Cook (1971) proved SAT NP-complete, Karp (1972) reduced SAT to 21 famous problems — covering scheduling, packing, routing, graph colouring. Today the catalogue runs to thousands across biology (protein folding), economics (market equilibria), games (Sudoku, Minesweeper generalisations) and operations research. One theorem, an entire scientific landscape: that is why the Cook–Levin theorem (Unit 3) is the keystone of the course.

2.6 Three small proofs you must be able to write

Claim 1: P ⊆ NP. Let L ∈ P via algorithm A. Define the verifier V(x, c) = A(x) — it simply ignores the certificate. V runs in polynomial time, and x ∈ L ⟺ V accepts (with, say, the empty certificate). Hence L ∈ NP. ∎ (One paragraph, full marks; ignoring the certificate is the entire trick.)

Claim 2: NP ⊆ EXP. Let L ∈ NP with verifier V and certificate length bound p(n). On input x, enumerate all 2^p(|x|) candidate certificates and run V on each; accept iff some run accepts. Time 2^p(n) · poly(n) = 2^poly(n). ∎ So NP sits between P and EXP — and since P ≠ EXP is proved (time hierarchy theorem), at least one of P ≠ NP, NP ≠ EXP must hold. Embarrassingly, we do not know which.

Claim 3 (the lever of the whole theory). If any single NP-complete problem B is in P, then P = NP. Proof: take any L ∈ NP. Since B is NP-hard, L ≤ₚ B via some polynomial f. Decide L by computing f(x) and running B's polynomial algorithm on it: a polynomial composed with a polynomial is polynomial. ∎ Contrapositive: if P ≠ NP, no NP-complete problem has a polynomial algorithm.

2.7 A worked verifier argument (the reusable template)

Show COMPOSITE = { ⟨n⟩ : n is composite } ∈ NP.

  • Certificate: a non-trivial factor d with 1 < d < n.
  • Size bound: |d| ≤ |n| in bits — polynomial. ✓
  • Verifier: check 1 < d < n and that d divides n (one long division — polynomial in the bit-length). ✓
  • Iff: n is composite ⟺ such a d exists ⟺ some certificate makes the verifier accept. ∎

Use exactly this four-line shape (certificate, size bound, polynomial check, iff) for every "show X ∈ NP" question — it is the first half of every NP-completeness proof in Unit 3.

2.8 Ladner's theorem — between P and the summit

Ladner (1975): if P ≠ NP, there exist NP-intermediate problems — in NP, not in P, and not NP-complete.

So (assuming P ≠ NP) NP is not just "easy + complete": an infinite ladder of intermediate difficulties exists. The natural candidate inhabitants are integer factoring and graph isomorphism — both in NP, neither known to be in P, and both believed not NP-complete (e.g. if factoring's decision version were NP-complete, NP = co-NP would follow, which is not believed).

2.9 Frequently confused pairs (exam-trap table)

PairThe distinction
NP vs "non-polynomial"NP = nondeterministic polynomial; P ⊆ NP, so NP contains easy problems too
NP-hard vs NP-completeNP-complete = NP-hard and in NP; NP-hard alone may lie outside NP (halting problem)
Undecidable vs intractableundecidable = no algorithm at all, ever; NP-hard = (probably) no fast algorithm
Problem vs instanceCLIQUE is a problem; "this G with k = 4" is an instance — hardness is a property of problems
Decision vs optimisation TSPdecision ("cost ≤ B?") is NP-complete; optimisation is NP-hard and not known to be in NP

Exam pointer

The five-mark classic "Define P, NP, NP-hard, NP-complete with examples and a diagram" is answered by: the four boxed definitions (§2.1–2.3), one example each, the containment diagram of §2.3, plus the sentence "if any NP-complete problem is in P then P = NP" (Claim 3). If asked to compare the classes, reproduce §2.4's table.

Self-check

  1. State the verifier definition of NP naming all three quantified objects (verifier, polynomial bound, certificate).
  2. Why is the halting problem NP-hard but not NP-complete?
  3. Prove P ⊆ NP in three lines.
  4. If SAT ∈ P were proved tomorrow, what exactly happens to CLIQUE? Trace it through the definitions.
  5. Name two suspected NP-intermediate problems and explain why one of them matters to cryptography.