Sub topic 4
Checking an Answer Versus Finding One
This chapter assumes you can read Python, use Big-O notation, and know what a decision problem and a polynomial-time algorithm are from the earlier chapters. You should also know the basic graph terms: vertex, edge, and adjacency list.
Many problems are lopsided. Finding an answer seems to need a huge search, but if someone hands you an answer, you can check it quickly. This chapter makes that idea precise with two terms, certificates and verifiers. They form the base for the definition of NP in the next chapter.
A Concrete Asymmetry: Subset Sum
In Subset Sum, you get a list of integers and a target. The question is whether some subset of the numbers adds up exactly to the target.
Take numbers = [3, 34, 4, 12, 5, 2] and target = 9. The answer is yes, because 4 + 5 = 9. Once someone points to those two numbers, you can confirm the answer with one addition. Finding them is a different job. With n numbers there are 2^n subsets, and no known algorithm avoids exponential worst-case time as n and the bit length of the numbers both grow.
Finding: brute force
from itertools import combinations
def subset_sum_brute_force(numbers, target):
n = len(numbers)
for r in range(n + 1):
for combo in combinations(range(n), r):
if sum(numbers[i] for i in combo) == target:
return list(combo)
return None
print(subset_sum_brute_force([3, 34, 4, 12, 5, 2], 9)) # [2, 4]
print(subset_sum_brute_force([3, 34, 4, 12, 5, 2], 100)) # NoneOn a yes-instance this function may stop early. On a no-instance it must try all 2^n subsets before it can return None. At n = 60 that is about 10^18 subsets, which is far beyond any practical budget.
Checking: a verifier
def verify_subset_sum(numbers, target, certificate):
"""certificate: a list of distinct indices into numbers."""
if len(set(certificate)) != len(certificate):
return False
if any(i < 0 or i >= len(numbers) for i in certificate):
return False
return sum(numbers[i] for i in certificate) == target
nums = [3, 34, 4, 12, 5, 2]
print(verify_subset_sum(nums, 9, [2, 4])) # True
print(verify_subset_sum(nums, 9, [0, 0, 0])) # False: repeated index
print(verify_subset_sum(nums, 9, [0, 5])) # False: 3 + 2 = 5
print(verify_subset_sum(nums, 9, [7])) # False: index out of rangeThe verifier runs in time roughly linear in the input size: one pass to look for duplicates, one range check, and one sum. Each addition costs time proportional to the bit length of the numbers, so the total is still polynomial.
Certificates and Verifiers, Defined
A certificate (also called a witness) is a piece of evidence that a given instance is a yes-instance. A verifier for a decision problem is an algorithm V that takes an instance x and a candidate certificate c and either accepts or rejects. A verifier is correct when it has two properties:
- Completeness: if x is a yes-instance, then some certificate c makes V(x, c) accept.
- Soundness: if x is a no-instance, then every certificate c makes V(x, c) reject.
A verifier is polynomial-time when it runs in time polynomial in the length of x. Because of that time bound, it can only read a polynomially long certificate, so useful certificates are short compared with the instance.
More Examples
The same pattern shows up in many problems. Here is a verifier for 3-Coloring, which asks whether you can color a graph's vertices with three colors so that no edge joins two vertices of the same color.
def verify_three_coloring(adj, coloring):
"""adj: dict vertex -> list of neighbors (undirected).
coloring: dict vertex -> color in {0, 1, 2}."""
if set(coloring) != set(adj):
return False
if any(c not in (0, 1, 2) for c in coloring.values()):
return False
for u in adj:
for v in adj[u]:
if coloring[u] == coloring[v]:
return False
return True
triangle = {"a": ["b", "c"], "b": ["a", "c"], "c": ["a", "b"]}
print(verify_three_coloring(triangle, {"a": 0, "b": 1, "c": 2})) # True
print(verify_three_coloring(triangle, {"a": 0, "b": 0, "c": 2})) # FalseThis check runs in O(V + E) time. The best known algorithms for finding a 3-coloring take exponential time.
| Problem | Certificate | Verification cost | Best known general search |
|---|---|---|---|
| Subset Sum | List of distinct indices | O(n) additions | Exponential in input size |
| 3-Coloring | A color for each vertex | O(V + E) | Exponential in V |
| SAT | A true/false value for each variable | Linear in formula size | Exponential in variable count |
| Hamiltonian Cycle | Ordering of all vertices | O(V + E) with an edge set | Exponential in V |
| Composite (is N not prime?) | A factor d with 1 < d < N | One division | Polynomial (AKS); factoring itself has no known polynomial algorithm |
The last row shows that a fast verifier says nothing about whether fast solving is impossible. Compositeness has an easy certificate, and in 2002 it also turned out to have a polynomial-time decision algorithm. Whether some problem has easy certificates but no fast decision algorithm is the P vs NP question, covered later in the tutorial.
Edge Cases and Common Errors
Certificates for no-instances
A certificate proves "yes." For a no-instance of Subset Sum, nobody knows a short proof that no subset hits the target. The definition only asks verifiers to confirm yes-answers. That one-sided rule matters, and the co-NP chapter covers the reverse case.
Certificate length must be polynomial
A list of all 2^n subsets with their sums would technically "prove" a no-answer, but it is exponentially long. A polynomial-time verifier cannot even read it, so it does not count.
Verifier time is measured in the instance size
Count the bits needed to write down the input, not the numeric values in it. A verifier that loops target times is not polynomial, because target can be exponential in its own bit length. The pseudo-polynomial chapter returns to this point.
Verifying is not the same as solving with a hint
The verifier must never trust the certificate. It checks everything itself: format, ranges, distinctness, and the actual property. Think of the certificate as coming from an adversary.
Search versus decision
A verifier checks a solution, but the decision problem only asks whether one exists. For many problems, a fast decision algorithm can also find a solution through self-reduction. A later chapter covers this.
Exercise
Write a verifier for Vertex Cover: given a graph as an edge list, an integer k, and a certificate (a list of vertices), accept exactly when the certificate is a vertex cover of size at most k. What makes it sound?
Show answer
def verifyvertexcover(edges, k, cert): s = set(cert); return len(s) <= k and all(u in s or v in s for u, v in edges). It is sound because it computes the size from the set of distinct vertices, so duplicates cannot fake a small cover, and it checks every edge, so it never accepts a non-cover. It runs in O(|cert| + E) time.
Exercise
A student proposes this Subset Sum "verifier": return sum(numbers[i] for i in certificate) == target. Give an instance and certificate where it accepts a no-instance.
Show answer
Take numbers = [3] and target = 6. No subset sums to 6, so this is a no-instance. The certificate [0, 0] makes the student's function compute 3 + 3 = 6 and accept, so it is not sound. Rejecting repeated indices fixes it.
Exercise
Why can't "the full table of all 2^n subset sums" serve as a certificate that a Subset Sum instance is a no-instance, in the sense of this chapter?
Show answer
The table has exponential length in n. A polynomial-time verifier can read only polynomially many bits, so it cannot check the table. Certificates must have length polynomial in the instance size.