Sub topic 5
NP: Problems Whose Answers We Can Check Efficiently
This chapter assumes you know the class P, Big-O notation, and the idea of a certificate from the previous chapter. Here we turn the certificate idea into a precise definition of NP.
The Verifier Definition
A decision problem is in NP if a polynomial-time verifier exists for it. A verifier is an algorithm V that takes two inputs: the instance x and a proposed certificate c. Formally, a problem L is in NP when there is a polynomial p and a verifier V running in time polynomial in |x| such that:
- If x is a yes-instance, some certificate c with |c| ≤ p(|x|) makes V(x, c) accept.
- If x is a no-instance, every certificate makes V(x, c) reject.
Both conditions matter. The first is completeness: every true "yes" has a short proof. The second is soundness: no proof, however clever, can fool the verifier into accepting a "no".
Example: Clique
The Clique problem asks: does graph G contain k vertices that are all pairwise adjacent? A certificate is a list of k vertices. Checking it takes O(k²) edge lookups, which is polynomial.
def verify_clique(adj, k, certificate):
"""adj: dict mapping each vertex to a set of neighbors (undirected graph)."""
nodes = set(certificate)
if len(nodes) != k or not nodes <= adj.keys():
return False
return all(v in adj[u] for u in nodes for v in nodes if u != v)
graph = {1: {2, 3}, 2: {1, 3}, 3: {1, 2, 4}, 4: {3}}
print(verify_clique(graph, 3, [1, 2, 3])) # True
print(verify_clique(graph, 3, [2, 3, 4])) # False: 2 and 4 are not adjacentThe verifier never searches. It only checks the certificate it is given. Finding a clique may require trying many subsets, but that cost is not part of the definition.
P Is Contained in NP
Every problem in P is also in NP. If an algorithm A solves L in polynomial time, define V(x, c) to ignore c and return A(x). That verifier is polynomial and accepts exactly the yes-instances. So P ⊆ NP. Whether the containment is strict is the P vs NP question, which comes later in the tutorial.
Common Errors
| Misconception | Correction |
|---|---|
| NP means "not polynomial" | NP stands for *nondeterministic polynomial*. Every problem in P is in NP. |
| NP problems are the hard ones | NP contains easy problems too. Hardness is a separate notion (NP-hard). |
| No-instances also need certificates | Only yes-instances need them. Short proofs of "no" define co-NP. |
| Any certificate size is fine | The certificate must be polynomial in the input size, or verification could hide exponential work. |
Exercise
The Hamiltonian Cycle problem asks whether a graph has a cycle visiting every vertex exactly once. Describe a certificate and a polynomial-time verifier, and give its running time.
Show answer
The certificate is an ordering of all n vertices. The verifier checks that each vertex appears exactly once, that each consecutive pair is joined by an edge, and that the last vertex is joined to the first. With adjacency sets this takes O(n) time, so Hamiltonian Cycle is in NP.
Exercise
Why does the definition require the certificate length to be bounded by a polynomial in |x|?
Show answer
The verifier's running time is measured against the instance size. If certificates could be exponentially long, a "verifier" could read an exponential amount of data and do exponential work while still counting as polynomial in |x| + |c|. The bound keeps verification truly efficient.