Sub topic 8
NP as Problems with Checkable Certificates
The earlier chapters treated reductions as tools for moving algorithms and hardness between problems. Before we can use them to classify problems as "hard," we need a precise class of problems to work inside. That class is NP. This chapter defines NP through certificates and verifiers, works through several examples, and covers the misreadings that cause trouble later in NP-completeness proofs.
This chapter assumes you know decision problems over encoded instances, polynomial running time in terms of input length, and propositional formulas in conjunctive normal form (CNF).
The core idea: solutions that are easy to check
Many hard problems have the same shape. Finding a solution seems to need a search over exponentially many candidates, but once someone hands you a candidate, checking it is quick.
- Independent Set: given a graph G and integer k, is there a set of k pairwise non-adjacent vertices? Finding one may need a search over many subsets. Checking a proposed set takes one pass over the edges.
- SAT: given a CNF formula, is there a satisfying assignment? Trying all 2^n assignments is slow. Checking one assignment takes one pass over the clauses.
- Hamiltonian Cycle: does a graph have a cycle that visits every vertex exactly once? Checking a proposed vertex ordering is easy.
The proposed solution is called a certificate (or witness). The checking procedure is called a verifier.
The formal definition
A decision problem (language) L is in NP if there exist a polynomial p and a deterministic algorithm V, the verifier, such that:
- V takes two inputs, an instance x and a candidate certificate c, and runs in time polynomial in |x|.
- Completeness: if x ∈ L, then some certificate c with |c| ≤ p(|x|) makes V(x, c) accept.
- Soundness: if x ∉ L, then V(x, c) rejects for every c.
Both conditions matter. Completeness says yes-instances have a short proof. Soundness says no-instance can fool the verifier, whatever certificate an adversary supplies.
The name comes from an equivalent definition. NP is the class of languages decided in polynomial time by a nondeterministic Turing machine, which may "guess" at each step and accepts if any branch of guesses accepts. The guessed bits play the role of the certificate. A polynomial-time branch can only guess polynomially many bits, and that is why the certificate length is bounded. The verifier definition is usually easier to work with, so we use it from here on.
Writing verifiers
Here are verifiers for Independent Set and SAT. Each one checks the certificate's format as well as its content, because soundness has to hold for arbitrary certificates, including malformed ones.
def verify_independent_set(vertices, edges, k, cert):
# Instance: (vertices, edges, k). Certificate: a collection of vertices.
s = set(cert)
if len(s) < k:
return False # too small
if not s <= set(vertices):
return False # mentions vertices not in G
for u, v in edges: # O(|E|) check
if u in s and v in s:
return False
return True
def verify_sat(clauses, num_vars, cert):
# Instance: CNF clauses as tuples of nonzero ints (DIMACS-style).
# Certificate: dict mapping variable index -> bool.
if set(cert) != set(range(1, num_vars + 1)):
return False # must assign every variable
for clause in clauses:
if not any(cert[abs(l)] == (l > 0) for l in clause):
return False # clause unsatisfied
return True
vertices = [1, 2, 3, 4]
edges = [(1, 2), (2, 3), (3, 4)]
assert verify_independent_set(vertices, edges, 2, [1, 3])
assert not verify_independent_set(vertices, edges, 2, [1, 2])
assert not verify_independent_set(vertices, edges, 2, [1, 9])
clauses = [(1, -2), (2, 3), (-1, -3)]
assert verify_sat(clauses, 3, {1: True, 2: True, 3: False})
assert not verify_sat(clauses, 3, {1: True, 2: False, 3: False})
print("all verifier checks passed")Both verifiers run in time linear in the instance size, and both certificates are no longer than the instance. That is typical. For most natural problems the certificate is simply "the thing you are asked whether it exists."
P is inside NP
Every problem in P is in NP. If a polynomial-time algorithm D decides L, define V(x, c) = D(x) and ignore the certificate, using the empty string as c. Completeness and soundness follow at once. So P ⊆ NP. Whether the inclusion is strict is the P versus NP question, and it is still open.
Membership is the easy half of NP-completeness
The next chapter defines NP-completeness, which has two parts: membership in NP and NP-hardness. Membership is shown with a verifier and needs no reduction. In practice you state three things:
| Step | What to state | Example (Hamiltonian Cycle) |
|---|---|---|
| Certificate | What c is and why its length is polynomial | An ordering of the n vertices, O(n log n) bits |
| Verifier | The checks V performs | Each vertex appears exactly once, and consecutive vertices (with wraparound) are adjacent |
| Correctness | Completeness and soundness | A cycle gives a passing ordering, and a passing ordering is a cycle |
The asymmetry: yes is short, no may not be
The definition treats yes and no differently. A satisfiable formula has a short proof, the assignment. For an unsatisfiable formula, no short proof of unsatisfiability is known in general. The class of languages whose complements are in NP is called coNP. UNSAT and TAUTOLOGY are in coNP, and nobody knows whether they are in NP.
This matters for reductions. A many-one reduction maps yes to yes and no to no, so if A reduces to B and B is in NP, then A is in NP too. Mapping SAT to UNSAT by negating the answer is not a many-one reduction. It is a Turing-style move, covered in a later chapter, and it does not preserve membership in NP.
Common errors
"NP means not polynomial"
NP stands for nondeterministic polynomial time. NP includes every problem in P, including trivial ones. Calling a problem "in NP" says it is easy to verify. It does not say the problem is hard.
Forgetting the length bound
A certificate must be polynomial in |x|. For a problem like "does this program halt within 2^n steps?", the full execution trace is exponentially long, so handing it over as a certificate does not put the problem in NP.
Encoding numbers carelessly
Instance size counts bits. In Subset Sum, a target T is written in about log T bits. A certificate that lists a subset of the input numbers is polynomial. A verifier that loops T times is not polynomial, because T is exponential in its bit length. The same issue explains why the O(nT) dynamic program for Subset Sum does not put it in P.
Leaving the problem as an optimization problem
"Find the largest independent set" is not a decision problem, so it is not in NP as stated. Use the decision version, "is there one of size at least k?" Note also that "is the maximum independent set exactly k?" is probably not in NP. Proving that no larger set exists is a coNP-style claim.
Exercises
Exercise
Show that Vertex Cover (given G and k, is there a set C of at most k vertices touching every edge?) is in NP. Give the certificate, the verifier, and argue completeness and soundness.
Show answer
The certificate is a list of at most k vertices, which takes O(|V| log |V|) bits. The verifier rejects if the list has more than k distinct vertices or names a vertex not in G, then checks that every edge (u, v) has u or v in C, in O(|V| + |E|) time. Completeness: if a cover of size at most k exists, listing it passes every check. Soundness: if the verifier accepts, the listed set has at most k vertices and touches every edge, so it is a cover and the instance is a yes-instance.
Exercise
COMPOSITE asks whether a binary integer N is composite. A student says its verifier tries every d from 2 to N-1 and checks whether d divides N. Why is this not a valid NP verifier, and what is a correct one?
Show answer
That procedure ignores the certificate and takes about N steps, which is exponential in the input length of roughly log N bits. A correct verifier takes a certificate d, checks that 1 < d < N, and checks that d divides N. Division of log N-bit numbers takes polynomial time, and d has at most log N bits.
Exercise
Is UNSAT (the CNF formula has no satisfying assignment) known to be in NP? Explain why listing every assignment with a falsified clause for each does not work as a certificate.
Show answer
UNSAT is not known to be in NP. It is in coNP. The proposed certificate has 2^n entries, which is exponential in the formula's size, so it breaks the polynomial length bound. No polynomial-size certificate for unsatisfiability is known in general.