P, NP, NP-complete, NP-hard. Computational tractability.
Multi-session curriculum - substantial prior knowledge and complex material. Use mastery gates and deliberate practice.
Some problems feel “easy” because we can solve them quickly for large inputs. Others feel “hard” because every known approach blows up. Complexity classes are the vocabulary for saying, precisely, which side a problem seems to live on—and for proving that two very different-looking problems are equally hard by building reductions between them.
Class P = decision problems solvable in polynomial time. NP = decision problems whose “yes” answers have certificates verifiable in polynomial time (equivalently: solvable by a nondeterministic polynomial-time machine). NP-hard = at least as hard as every problem in NP under polynomial-time reductions. NP-complete = both NP-hard and in NP. Reductions () are the main tool: if and is easy, then is easy; if is hard and , then is hard.
When you analyze an algorithm, you can say it runs in time, or time, etc. That’s useful—but it’s attached to a particular algorithm. Complexity theory asks a different question:
Is the problem itself efficiently solvable?
A complexity class is a set of problems grouped by the resources needed by the best possible algorithms (time, space, randomness, etc.). In this lesson we focus on time and on four names you’ll see constantly:
Complexity classes are usually defined for decision problems: the answer is YES or NO.
Examples:
Why decision problems?
To talk about time, we need a notion of input length, typically denoted .
This matters: an algorithm that is polynomial in the value of may be exponential in the bit-length of .
In practice, “efficient” is nuanced. But complexity theory uses a robust proxy:
Polynomial time is considered tractable.
Why? Because polynomials compose well, are stable under reasonable machine models, and scale far better than exponentials.
A mental comparison (not a proof): if , then might be heavy but imaginable; is completely impossible.
Below is the conceptual picture we’re building toward (interactive in the tech tree canvas):
We do not know whether .
This diagram isn’t decoration—it encodes the definitions. In later sections, we’ll use it as a correctness checklist when doing reductions.
Definition (P). A decision problem is in P if there exists a deterministic algorithm that decides in time polynomial in the input size.
Formally: if there exists an algorithm and a polynomial such that for every input , halts in at most steps and outputs YES iff .
Intuition: problems in P are the ones we can solve efficiently.
Examples commonly in P:
Definition (NP) via certificates. A decision problem is in NP if there exists:
such that:
In logic form:
with running in polytime and polynomially bounded.
Intuition: NP problems are those where a proposed solution can be checked quickly.
Examples:
It’s tempting to say NP means “hard.” That’s not the definition.
NP contains problems that might be easy or hard; it’s about the existence of short proofs of “YES.”
So we know:
Whether the containment is strict is the famous open question.
Another equivalent definition: NP is the class of problems solvable by a nondeterministic Turing machine in polynomial time.
Interpretation:
This equivalence is conceptually helpful, but in reductions we usually work with the certificate viewpoint.
Many NP problems are naturally search problems (“find an assignment,” “find a tour”). Complexity classes are defined for decision versions.
For SAT:
In many settings (including SAT under standard assumptions), decision and search are polynomial-time interreducible, but that’s an extra theorem. For this node, focus on decision formulations.
In the canvas, imagine SAT as a card with:
The point is to make NP feel mechanical: you can literally plug in a candidate and check it.
Suppose you’re given a new problem and you suspect it’s hard. Proving “no polynomial algorithm exists” is beyond current techniques for most natural problems.
Instead, complexity theory uses a powerful relative statement:
If were easy, then a known-hard problem would also be easy.
That implication is established by a polynomial-time reduction.
We write:
to mean: there exists a polynomial-time computable function that maps instances of to instances of such that:
This “iff” is the invariant you must protect.
A reduction is not magic. It’s a program with a contract:
In the canvas, this should be a clickable pipeline:
with a live displayed invariant: “YES() ⇔ YES().”
If and , then .
Reason: to decide on input :
Polynomial + polynomial = polynomial.
This is why we say:
Now we can define hardness precisely.
Definition (NP-hard). A problem is NP-hard if for every problem , we have .
Interpretation: is at least as hard as every NP problem.
Definition (NP-complete). A problem is NP-complete if:
Interpretation: NP-complete problems are the “hardest problems in NP.”
A concrete reminder:
To prove a new decision problem is NP-complete:
Notice the direction: reduce from a known-hard problem to your target problem.
A common mistake is to reverse it. If you show , that only says is no harder than .
If , then every NP problem (including NP-complete ones) has a polynomial-time algorithm. The reduction machinery still works, but the interpretation of “hard” changes.
If , then NP-complete problems are not in P.
We don’t know which world we live in.
Once you can classify problems as P, NP, NP-complete, or NP-hard, you gain a practical workflow:
Complexity classes become a decision-making tool.
We’ll do a full reduction with the reduction pipeline mindset.
3SAT
CLIQUE
A set is a clique if for all distinct , .
A satisfying assignment for 3SAT picks, for each clause, at least one literal that is made true. That resembles selecting one “compatible” choice per clause.
A clique enforces pairwise compatibility: every chosen literal must be consistent with every other chosen literal.
So we’ll build a graph where:
Then a clique of size corresponds to picking one non-conflicting literal from each clause—exactly what we need for satisfiability.
Input: with clauses .
Construct a graph as follows:
Set .
Output: .
This is clearly polynomial-time to build: literal-pair checks, each check constant-time with a suitable encoding.
We must prove:
We’ll do both directions carefully.
##### (⇒) If ϕ is satisfiable, then G has a clique of size m
Assume is satisfiable. Then there exists an assignment that makes every clause true.
For each clause , pick one literal ℓᵢ in that is true under this assignment.
Now consider the set of vertices:
We claim is a clique of size .
Take any two distinct vertices and with .
So ℓᵢ and ℓⱼ are not contradictory, therefore condition (2) holds and the edge exists.
Thus every pair in is connected: is a clique. And , so has a clique of size .
##### (⇐) If G has a clique of size m, then ϕ is satisfiable
Assume has a clique with .
Key observation: edges only connect vertices from different clauses. That means:
Since has size and there are clauses, must contain exactly one vertex from each clause.
So we can write:
Because is a clique, every pair is connected by an edge, so no two literals among are contradictory.
Now we build an assignment:
This assignment is well-defined: we never choose both and among the because that would create a contradiction, and contradictory literals are not connected by an edge—so they cannot both appear in the clique.
Finally, does this assignment satisfy ?
So is satisfiable.
We have proven the invariant, so 3SAT ≤ₚ CLIQUE.
In the containment visualization:
This is what “reduction machinery” looks like when you run it end-to-end.
For each decision problem, identify whether it is in P, in NP (via a certificate), or both.
1) PATH: Given a graph G and vertices s,t, is there a path from s to t?
2) CLIQUE: Given G and k, is there a clique of size ≥ k?
3) SAT: Given formula φ, is it satisfiable?
1) PATH
2) CLIQUE
3) SAT
Insight: NP membership is often straightforward: propose the natural ‘witness’ object and show you can check it quickly. The hard part is usually NP-hardness, which comes from reductions.
Assume hypothetically there exists a polynomial-time algorithm CliqueSolve(G,k) that decides CLIQUE. Use the reduction 3SAT ≤ₚ CLIQUE to build a polynomial-time algorithm for 3SAT.
Goal: build SatSolve(φ) that decides whether a 3CNF formula φ is satisfiable.
1) Reduction step (transform)
2) Solve step
3) Correctness argument
4) Runtime
Conclusion: If CLIQUE ∈ P, then 3SAT ∈ P. Since 3SAT is NP-complete, this would imply P = NP.
Insight: Reductions are like ‘plug adapters’: they let you route instances of A through a solver for B. The direction A ≤ₚ B matters because it determines which way algorithms and hardness transfer.
Reduce the 3SAT instance φ = (x ∨ y ∨ z) ∧ (¬x ∨ y ∨ ¬z) ∧ (x ∨ ¬y ∨ w) to a CLIQUE instance (G,k), and exhibit a clique if φ is satisfiable.
1) Identify clauses
C₁ = (x ∨ y ∨ z)
C₂ = (¬x ∨ y ∨ ¬z)
C₃ = (x ∨ ¬y ∨ w)
So m = 3, hence k = 3.
2) Create vertices (i,ℓ)
Clause 1: (1,x), (1,y), (1,z)
Clause 2: (2,¬x), (2,y), (2,¬z)
Clause 3: (3,x), (3,¬y), (3,w)
Total 9 vertices.
3) Add edges between different clauses unless contradictory
4) Find a satisfying assignment (one example)
Try x=true, y=true, z=false, w=false.
So φ is satisfiable.
5) Map that to a clique of size 3
Pick one true literal per clause:
6) Check pairwise compatibility (edges)
So these 3 vertices form a 3-clique, hence (G,k) is a YES instance.
Insight: The reduction doesn’t just claim existence—it constructs a graph whose cliques literally encode consistent choices of literals across clauses.
Complexity classes classify problems by the best possible resource bounds, not a particular algorithm’s runtime.
P = decision problems solvable in polynomial time; NP = decision problems whose YES instances have polynomial-size certificates verifiable in polynomial time.
We know P ⊆ NP; whether P = NP is open.
A polynomial-time many-one reduction A ≤ₚ B is a polytime transform f such that x ∈ A ⇔ f(x) ∈ B.
If A ≤ₚ B and B ∈ P, then A ∈ P (algorithms transfer along reductions).
NP-hard means every NP problem reduces to it; NP-complete means NP-hard and also in NP.
To prove NP-completeness: (1) show the problem is in NP (certificate + verifier), (2) reduce from a known NP-complete problem to it.
Reversing reduction direction: proving B ≤ₚ A does not show B is NP-hard if A is known hard; you usually need A ≤ₚ B.
Forgetting the ‘iff’ invariant in reductions: you must prove both YES ⇒ YES and NO ⇒ NO (equivalently YES ⇔ YES).
Confusing NP with ‘not polynomial’: NP is about verifiability, and it includes all of P.
Mixing optimization and decision versions without care: NP is defined over decision problems; optimization problems are often handled via decision variants or NP-hardness statements.
Show that P ⊆ NP using the certificate/verifier definition of NP.
Hint: Given a polytime decider for a problem L, design a verifier that ignores (or uses a trivial) certificate.
Let L ∈ P. Then there exists a deterministic polynomial-time decider A(x) for L.
Define verifier V(x,c) as: run A(x) and output its answer; ignore c (or require c to be empty).
V runs in polynomial time, and the certificate length is 0 (which is polynomial). Hence L ∈ NP. Therefore P ⊆ NP.
Prove CLIQUE ∈ NP by explicitly describing a certificate and a polynomial-time verifier, including a runtime bound in terms of |V| and k.
Hint: Certificate can be a list of k vertices. The verifier checks every pair is an edge.
Certificate: a list S = (v₁,…,v_k) of k vertices.
Verifier V(G,k,S):
1) Check that all v_i are valid vertices and distinct.
2) For every pair (v_i,v_j) with i<j, check whether (v_i,v_j) ∈ E.
3) Accept iff all checks pass.
Runtime: step (2) performs C(k,2)=k(k−1)/2 edge checks. With an adjacency matrix, each check is O(1), so total O(k²). Even with adjacency lists, you can preprocess or hash adjacency for expected O(1) checks; in any standard encoding the total is polynomial in the input size. Therefore CLIQUE ∈ NP.
Let A ≤ₚ B and B ≤ₚ C. Prove that A ≤ₚ C (transitivity).
Hint: Compose the two reduction functions and argue polynomial time is preserved.
Assume A ≤ₚ B via f and B ≤ₚ C via g.
Define h(x) = g(f(x)).
Correctness:
Thus x ∈ A ⇔ h(x) ∈ C.
Runtime:
Therefore h is polynomial-time computable, and A ≤ₚ C.
Suggested parallel review nodes (if available in your tech tree):