Sub topic 4
Checking an Answer Versus Finding One
This chapter assumes you can read Python, know Big-O notation, and have met decision problems and polynomial time in the earlier chapters. Here we separate two tasks that are easy to confuse: finding a solution and checking a proposed one.
Certificates
A certificate is a piece of evidence that a yes-answer is correct. For Subset Sum, the question is "does some subset of these numbers add up to the target?" A certificate is the subset itself, given as a list of indices. If someone gives you the indices, you add up those numbers and compare the total with the target.
A verifier is an algorithm that takes an instance and a candidate certificate and either accepts or rejects. A useful verifier has two properties:
- Completeness: for every yes-instance, at least one certificate makes the verifier accept.
- Soundness: for every no-instance, no certificate makes it accept.
The verifier must also be efficient. It has to run in time polynomial in the size of the instance, and the certificate itself must have polynomial length.
A Polynomial-Time 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
print(verify_subset_sum([3, 34, 4, 12, 5, 2], 9, [0, 2, 5])) # True: 3 + 4 + 2
print(verify_subset_sum([3, 34, 4, 12, 5, 2], 9, [1])) # FalseThe duplicate check, the bounds check, and the sum each take O(n) steps. A verifier must defend itself against bad certificates. Without the duplicate check, the certificate [0, 0, 0] would "prove" that 9 is reachable using the single number 3 three times.
Finding Is Another Matter
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]This search calls the verifier's logic, in effect, on up to 2^n candidates. With 60 numbers that is about 10^18 subsets. Each check is cheap, but there are too many of them to try.
Common Errors
| Mistake | Why it is wrong |
|---|---|
| "Easy to check means easy to solve." | Nobody knows whether this holds in general. That open question is P vs NP. |
| Letting the certificate be exponentially long | Then the verifier could not even read it in polynomial time. |
| Expecting certificates for no-answers | The definition only asks for yes-certificates. Proving that no subset works is a different problem. |
| Skipping validation of the certificate | A verifier that can be fooled is not sound. |
Exercise
For Hamiltonian Cycle (does graph G have a cycle that visits every vertex exactly once?), describe a certificate and what a polynomial-time verifier must check.
Show answer
The certificate is an ordering of the vertices, v1, ..., vn. The verifier checks that the list has length n and contains each vertex exactly once. It also checks that every consecutive pair (vi, vi+1) is an edge, and that (vn, v1) is an edge. Using a set and an adjacency set, this takes O(n) time after O(V + E) preprocessing.
Exercise
Why does verifysubsetsum reject the certificate [0, 0, 0] for numbers [3, 34] and target 9?
Show answer
The duplicate check fails because the set has size 1 but the list has length 3. This keeps the verifier sound, since each number may be used at most once.