P, NP, NP-hard, and NP-complete: Definitions and Relationships
In computational complexity theory, , , , and classify decision problems by how difficult they are to solve and/or verify. The key relationships are defined using polynomial time bounds and polynomial-time reductions.4
At a high level:
- keywordP are efficiently solvable.
- keywordNP are efficiently verifiable (equivalently, solvable by nondeterministic polynomial-time machines).
- keywordNP-hard are hardest (or beyond) the NP frontier; they may or may not lie in .
- keywordNP-complete are the hardest problems within : they are in and NP-hard.
Footnotes
-
P (complexity) - Wikipedia - Defines P as deterministic polynomial-time decision problems. ↩
-
Polynomial-time reduction - Wikipedia - Defines polynomial-time many-one (Karp) reductions and their role in completeness. ↩
-
NP-hardness - Wikipedia - Defines NP-hard problems as those to which every NP problem reduces in polynomial time; notes NP-hard need not be in NP. ↩ ↩2
-
NP-completeness - Wikipedia - Defines NP-complete as problems in NP that are NP-hard; states relationship to NP and NP-hard. ↩ ↩2
-
NP (complexity) - Wikipedia - Defines NP via nondeterministic polynomial time and certificate verification. ↩
P and NP - Intro
Decision problems and polynomial-time reductions (the “glue”)
A decision problem asks a yes/no question for each input instance. Complexity class statements here are for decision problems.
To compare difficulty, we use keywordpolynomial-time many-one (Karp) reductions: means we can convert inputs for into inputs for in polynomial time so that answers are preserved.2
This lets definitions like “ is NP-hard” mean: if we could solve fast, we could solve every problem in fast too (via reduction).
Footnotes
-
Polynomial-time reduction - Wikipedia - Defines polynomial-time many-one (Karp) reductions and their role in completeness. ↩ ↩2
-
NP-hardness - Wikipedia - Defines NP-hard problems as those to which every NP problem reduces in polynomial time; notes NP-hard need not be in NP. ↩ ↩2
1) Class
Definition. is the class of decision problems solvable in deterministic polynomial time.
Formally: a language/problem is in if there exists a deterministic algorithm that decides it using time (polynomial in input length).
Implication: If a problem is in , then both finding and verifying solutions are efficient (because solving is efficient).
Key terms (for this block): keyworddecision problem, keyworddeterministic, keywordpolynomial time, keywordtime complexity.
Footnotes
-
P (complexity) - Wikipedia - Defines P as deterministic polynomial-time decision problems. ↩
2) Class
There are two equivalent standard characterizations:
-
Nondeterministic view: is the class of decision problems decidable by a nondeterministic Turing machine in polynomial time.2
-
Verifier/certificate view: is also the class of decision problems for which “yes” instances have polynomial-size certificates that can be verified in polynomial time by a deterministic verifier.2
That these are equivalent is standard (e.g., through correspondence between nondeterministic choices and certificates).
Relationship with . Every problem in is also in because if you can solve it in polynomial time, you can also verify a claimed solution in polynomial time.2
So: .2
Key terms (for this block): keywordcertificate, keywordverifier, keywordnondeterminism, keywordverification.
Footnotes
-
P (complexity) - Wikipedia - Notes equivalently that P is contained in NP (verifiability). ↩ ↩2 ↩3 ↩4
-
NP (complexity) - Wikipedia - Defines NP via nondeterministic polynomial time and certificate verification. ↩ ↩2 ↩3
-
P (complexity) - Wikipedia - Defines P as deterministic polynomial-time decision problems. ↩ ↩2
3)
Definition. A problem (usually decision; can be more general) is keywordNP-hard if every problem in reduces to in polynomial time.
Crucially:
- does not have to be in . It might not have efficiently checkable certificates (or might even be undecidable in broader settings), but it is still “at least as hard as” all of under reductions.
Why reductions matter. If and , then a polynomial-time solver for would imply a polynomial-time solver for (by composing the reduction with the solver).2
Key terms (for this block): keywordhardness, keywordcomposition, keyworddifficulty order, keywordoutside-NP.
Footnotes
-
NP-hardness - Wikipedia - Defines NP-hard problems as those to which every NP problem reduces in polynomial time; notes NP-hard need not be in NP. ↩ ↩2 ↩3
-
Polynomial-time reduction - Wikipedia - Defines polynomial-time many-one (Karp) reductions and their role in completeness. ↩
4)
Definition. A problem is keywordNP-complete if:
So -complete problems are the “hardest” problems within : they are efficiently verifiable, but every other problem in reduces to them in polynomial time.
Cook–Levin theorem (existence of NP-complete problems). A standard result states that Boolean satisfiability (, and variants like ) is NP-complete; equivalently, every problem in reduces to satisfiability in polynomial time.
Footnotes
-
NP-completeness - Wikipedia - Defines NP-complete as problems in NP that are NP-hard; states relationship to NP and NP-hard. ↩ ↩2
-
Cook–Levin theorem - Wikipedia - States SAT is NP-complete and that any problem in NP reduces to it in polynomial time. ↩
How to use reductions to prove a problem is NP-hard / NP-complete
- 1Step 1
If you want NP-hardness, you need reductions from every (often done by reducing from a single known NP-complete ). NP-completeness requires membership in plus NP-hardness.
Footnotes
-
NP-completeness - Wikipedia - Defines NP-complete as problems in NP that are NP-hard; states relationship to NP and NP-hard. ↩
-
- 2Step 2
Give a function that maps instances of to instances of in polynomial time while preserving yes/no answers (many-one/Karp reduction).
Footnotes
-
Polynomial-time reduction - Wikipedia - Defines polynomial-time many-one (Karp) reductions and their role in completeness. ↩
-
- 3Step 3
Show: . This establishes that solving solves .
Footnotes
-
Polynomial-time reduction - Wikipedia - Defines polynomial-time many-one (Karp) reductions and their role in completeness. ↩
-
- 4Step 4
Because is NP-complete, all problems in reduce to , hence (by transitivity of reductions) to as well.2
Footnotes
-
Polynomial-time reduction - Wikipedia - Defines polynomial-time many-one (Karp) reductions and their role in completeness. ↩
-
NP-hardness - Wikipedia - Defines NP-hard problems as those to which every NP problem reduces in polynomial time; notes NP-hard need not be in NP. ↩
-
- 5Step 5
Provide a polynomial-time verifier or a nondeterministic polynomial-time algorithm for .2
Footnotes
-
NP-completeness - Wikipedia - Defines NP-complete as problems in NP that are NP-hard; states relationship to NP and NP-hard. ↩
-
NP (complexity) - Wikipedia - Defines NP via nondeterministic polynomial time and certificate verification. ↩
-
5) Relationship between , , , and
The core inclusions are:
- .2
- (by definition: NP-complete problems are in ).
- (again by definition: NP-complete problems are NP-hard).
- is not necessarily a subset of (NP-hard problems may lie outside ).2
So we can write the clean set-theoretic relationship:
This is exactly the “in and NP-hard” definition.
The big open question: vs
- If , then every problem in becomes tractable, and many consequences follow.
- If there exists an NP-complete problem in , then (because NP-complete problems are NP-hard, so polynomial-time solvability would collapse the whole NP hierarchy to ).
This is why NP-completeness is central: NP-complete problems are the “borderline” of what might be solvable efficiently.
Footnotes
-
P (complexity) - Wikipedia - Defines P as deterministic polynomial-time decision problems. ↩
-
P (complexity) - Wikipedia - Notes equivalently that P is contained in NP (verifiability). ↩
-
NP-completeness - Wikipedia - Defines NP-complete as problems in NP that are NP-hard; states relationship to NP and NP-hard. ↩ ↩2 ↩3 ↩4 ↩5
-
NP-hardness - Wikipedia - Defines NP-hard problems as those to which every NP problem reduces in polynomial time; notes NP-hard need not be in NP. ↩
Conceptual roadmap
Efficient solving ($P$)
Step ADeterministic polynomial-time algorithms decide the problem."
Footnotes
-
P (complexity) - Wikipedia - Defines P as deterministic polynomial-time decision problems. ↩
Efficient verifying ($NP$)
Step BYes-instances have polynomial-size certificates verifiable in polynomial time (equivalently, nondeterministic polynomial time)."
Footnotes
-
NP (complexity) - Wikipedia - Defines NP via nondeterministic polynomial time and certificate verification. ↩
Hardness boundary ($NP$-hard)
Step CEvery problem reduces to it in polynomial time; it may be outside ."
Footnotes
-
NP-hardness - Wikipedia - Defines NP-hard problems as those to which every NP problem reduces in polynomial time; notes NP-hard need not be in NP. ↩
Hardest inside $NP$ ($NP$-complete)
Step DIn and NP-hard; reductions characterize complete difficulty within ."
Footnotes
-
NP-completeness - Wikipedia - Defines NP-complete as problems in NP that are NP-hard; states relationship to NP and NP-hard. ↩
Where each class sits conceptually
Higher bars mean the class is defined to be harder (via reductions) and/or supports efficient verification.
Pro Tip: Use the “intersection” viewpoint
A problem is NP-complete exactly when it is both in NP and NP-hard. So once you prove NP-hardness via reductions, you only need to show polynomial-time verifiability to finish NP-completeness.
Footnotes
-
NP-completeness - Wikipedia - Defines NP-complete as problems in NP that are NP-hard; states relationship to NP and NP-hard. ↩
Warning: NP-hard does not mean “in NP”
NP-hard problems might not have polynomial-time verifiable certificates; they are “hard” by reduction, not by membership in NP.
Footnotes
-
NP-hardness - Wikipedia - Defines NP-hard problems as those to which every NP problem reduces in polynomial time; notes NP-hard need not be in NP. ↩
Common confusions
Knowledge Check
Which statement is correct about the relationship between the classes?
Explore Related Topics
Dynamic Programming and Greedy Algorithms: Comparative Analysis, Failure Cases, and Core DP Properties
The article contrasts greedy algorithms with dynamic programming, shows when greedy fails and DP is necessary, and explains DP’s core properties of optimal substructure and overlapping subproblems.
- Greedy makes irrevocable local choices and works only with the property, while DP stores and reuses subproblem results to guarantee optimality.
- Counterexamples such as coin change ( for amount 6) and knapsack illustrate failures of greedy and the need for DP recurrences like and .
- DP relies on to form recurrences and on overlapping subproblems to justify caching.
- Design steps: recognize structure, detect repetition, formulate recurrence, compute once (memoization/tabulation), optionally reconstruct solution.
- Rule of thumb: use greedy if local choices can be proved globally safe; otherwise apply DP.
Iterated Power Set Cardinality: Computing $n(P(P(P(\Phi))))$