Sub topic 8
NP as Problems with Checkable Certificates
This chapter assumes you know what a decision problem is, can read polynomial running times in Big-O notation, and recognize Independent Set and Vertex Cover from earlier chapters. NP is the class of problems where a correct "yes" answer can be checked quickly. Later chapters on NP-hardness need this class as their target.
The verifier definition
A decision problem L is in NP if there is a deterministic algorithm V, called a verifier, and a polynomial p with these properties:
- V takes two inputs: an instance x and a candidate certificate c (also called a witness).
- V runs in time polynomial in |x|.
- If x is a yes-instance, then some certificate c with |c| ≤ p(|x|) makes V(x, c) accept.
- If x is a no-instance, then V(x, c) rejects for every certificate c.
The two sides are not symmetric. A yes-instance needs just one certificate that convinces the verifier. A no-instance must hold up against every possible certificate. NP says nothing about how to find the certificate.
A worked verifier: Independent Set
An instance is a graph G and an integer k. The certificate is a set of vertices. The verifier checks the set's size and confirms that no edge has both endpoints inside it.
def verify_independent_set(vertices, edges, k, cert):
s = set(cert)
if not s <= set(vertices):
return False
if len(s) < k:
return False
return all(not (u in s and v in s) for u, v in edges)
vertices = [1, 2, 3, 4]
edges = [(1, 2), (2, 3), (3, 4)]
print(verify_independent_set(vertices, edges, 2, [1, 3])) # True
print(verify_independent_set(vertices, edges, 2, [1, 2])) # False: edge (1, 2)The certificate has at most |V| vertices, and the check makes one pass over the edges, so both conditions hold.
Verification versus solving
Every problem in NP can be solved by trying every certificate up to length p(|x|). That takes exponential time:
from itertools import combinations
def brute_force_independent_set(vertices, edges, k):
for cert in combinations(vertices, k):
if verify_independent_set(vertices, edges, k, cert):
return True
return False
print(brute_force_independent_set([1, 2, 3, 4], [(1, 2), (2, 3), (3, 4)], 2)) # TrueWhether this search can always be replaced by a polynomial-time algorithm is the P versus NP question. P ⊆ NP holds because a polynomial-time solver can serve as its own verifier and ignore the certificate.
Common errors
| Mistake | Correction |
|---|---|
| "NP means not polynomial." | NP stands for *nondeterministic polynomial time*. Every problem in P is also in NP. |
| Using a certificate whose size grows exponentially | The certificate must have polynomial length, or the definition means nothing. |
| Assuming no-instances have short proofs | That property defines **coNP**. Nobody knows whether NP = coNP. |
| Treating "in NP" as meaning "hard" | Membership in NP is an upper bound. Hardness comes from reductions, covered in the next chapter. |
Exercise
Show that Vertex Cover (given G and k, does G have a vertex cover of size at most k?) is in NP. Name the certificate, bound its size, and describe the verifier.
Show answer
The certificate is a set C of vertices. Its size is at most |V|, which is polynomial. The verifier checks that C is a subset of V, that |C| ≤ k, and that every edge (u, v) has u in C or v in C. That takes O(|V| + |E|) time with a hash set. Yes-instances have a valid cover as a certificate. For a no-instance, every set of size at most k misses some edge, so the verifier rejects every certificate.
Exercise
Is "Does graph G have NO independent set of size k?" obviously in NP? Explain.
Show answer
It is not obviously in NP. A yes-answer here claims that no such set exists. The direct way to check that claim is to examine all C(|V|, k) subsets, and no short certificate for it is known. The problem is the complement of Independent Set, so it lies in coNP.