Complexity Classes: P, NP, NP-Hard and NP-Complete
Explore the theoretical language used to classify difficult computational decision problems and understand why some problems resist efficient exact solutions.
01
Learning objectives
Distinguish decision problems from optimisation problems.
Explain the informal meaning of P and NP.
Describe NP-hard and NP-complete relationships at a conceptual level.
Explain why verification can be easier than discovering a solution.
02
Lesson notes
Complexity theory studies how computational resources grow as input size increases. A decision problem asks a yes/no question, such as whether a graph contains a route shorter than a specified bound. An optimisation problem asks for the best value, such as the shortest possible route. Complexity classes are defined for formal problem models, not simply for a particular Python function.
P is the class of decision problems that can be solved in polynomial time by a deterministic algorithm. NP is the class whose yes answers have certificates that can be verified in polynomial time. Every problem in P is also in NP, because a solution that can be found efficiently can also be verified efficiently.
An NP-hard problem is at least as hard as every problem in NP under the relevant polynomial-time reductions; it does not have to be in NP itself. An NP-complete problem is both in NP and NP-hard. If any NP-complete problem were shown to have a polynomial-time algorithm, every problem in NP would also have one, implying P = NP.
This theory does not mean every practical instance of an NP-complete problem is impossible. Real inputs may be small, structured or approximated. Complexity results help engineers recognise when an exact solution may scale poorly and consider heuristics, approximation, restrictions or additional domain knowledge.
03
Worked code examples
Worked example
This toy example illustrates checking a supplied certificate. Complexity theory asks how the cost of verification scales for a formally defined problem.