Skip to content
ComputerCS
Start learning

Algorithms and Problem Solving · Lesson 3.3.3

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.

candidates = ["A", "B", "C", "D"]
valid_certificate = "B"
print("Certificate accepted:", valid_certificate in candidates)
Output
Certificate accepted: True
04

Key terminology

P

Decision problems solvable in deterministic polynomial time.

NP

Decision problems whose yes certificates can be verified in polynomial time.

NP-hard

Problems at least as hard as all problems in NP under suitable reductions.

NP-complete

Problems that are both in NP and NP-hard.

05

Common misconceptions

Common mistake

NP means “not polynomial”.

Correct understanding

NP means nondeterministic polynomial time; it is defined through efficient certificate verification.

Common mistake

Every NP-hard problem must itself belong to NP.

Correct understanding

NP-hard problems need not be in NP; NP-complete problems satisfy both conditions.

06

Check your understanding

1. Which relationship is known to hold?

2. An NP-complete problem is:

3. Why can a certificate be useful?

NERDLYY · FEEDBACK

Help shape Nerdlyy

Your feedback goes directly to the team. We read every submission.

No reply needed — you can send feedback without sharing contact details.