Verification and the Class NP
Finding a Hamiltonian cycle in a graph, a cycle that visits every vertex exactly once, seems to require trying a huge number of orderings. But if someone hands you a candidate cycle, checking it is easy: walk along it, confirm every vertex appears once and every consecutive pair is an edge. Many hard-looking problems share this asymmetry, and the class NP is defined by it: problems whose yes-answers have short proofs that can be checked quickly. This lesson defines NP through verification algorithms, shows why , and states carefully the open question of whether the two are equal.
Certificates and verifiers
A verification algorithm takes two inputs: an instance and a certificate (also called a witness or proof). verifies if there is some with . The language verified by is
Definition. A language is in NP if there are a polynomial-time verification algorithm and a constant such that
Both conditions matter: the certificate must be short (polynomial in the input) and the check fast (polynomial in the input). Correctness runs both ways: every yes-instance has some certificate that passes, and no certificate passes for a no-instance. The name stands for nondeterministic polynomial time: a machine allowed to guess the certificate could decide in polynomial time.
For HAM-CYCLE, the certificate is a list of the vertices in cycle order, of length bits, and the check takes time with an adjacency matrix:
def verify_ham_cycle(n, edges, cycle):
"""polynomial-time verifier: is `cycle` a Hamiltonian cycle of the graph?"""
E = {frozenset(e) for e in edges}
if sorted(cycle) != list(range(n)): # every vertex exactly once
return False
return all(frozenset((cycle[i], cycle[(i + 1) % n])) in E for i in range(n))
n = 6
edges = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 0), (0, 3), (1, 4)]
for cert in [[0, 1, 2, 3, 4, 5], [0, 3, 2, 1, 4, 5], [0, 1, 4, 3, 2, 5], [0, 2, 4, 1, 3, 5], [0, 1, 2, 3, 4]]:
print(cert, "->", verify_ham_cycle(n, edges, cert))[0, 1, 2, 3, 4, 5] -> True
[0, 3, 2, 1, 4, 5] -> True
[0, 1, 4, 3, 2, 5] -> False
[0, 2, 4, 1, 3, 5] -> False
[0, 1, 2, 3, 4] -> False
The verifier says nothing about how to find a good certificate. The only general method is to try them all, and the number of candidate certificates is exponential: bit strings of length , or distinct cycles in a graph.
from itertools import permutations
def verify_ham_cycle(n, E, cycle):
return all(frozenset((cycle[i], cycle[(i + 1) % n])) in E for i in range(n))
def search(n, edges):
E = {frozenset(e) for e in edges}
checked = 0
for rest in permutations(range(1, n)): # fix vertex 0 first
checked += 1
if verify_ham_cycle(n, E, (0,) + rest):
return (0,) + rest, checked
return None, checked
graphs = {
"ladder, 8 vertices": [(0, 1), (1, 2), (2, 3), (4, 5), (5, 6), (6, 7), (0, 4), (1, 5), (2, 6), (3, 7)],
"Petersen graph, 10 vertices": [(0, 1), (1, 2), (2, 3), (3, 4), (4, 0), (0, 5), (1, 6), (2, 7), (3, 8),
(4, 9), (5, 7), (7, 9), (9, 6), (6, 8), (8, 5)],
}
for name, edges in graphs.items():
n = 1 + max(max(e) for e in edges)
cycle, checked = search(n, edges)
print(f"{name}: cycle {cycle}, {checked} orders checked")ladder, 8 vertices: cycle (0, 1, 2, 3, 7, 6, 5, 4), 24 orders checked
Petersen graph, 10 vertices: cycle None, 362880 orders checked
The ladder is lucky: a cycle turns up after 24 orders. The Petersen graph has no Hamiltonian cycle, and the search can only say so after checking all orders. With 20 vertices the same search would face orders.
P is contained in NP
If , a verifier can ignore the certificate and decide itself in polynomial time. So every language in P is in NP:
Many problems are in NP for the reason above, a short certificate: HAM-CYCLE (the cycle), SUBSET-SUM (the subset), CLIQUE (the vertices of the clique), 3-COLORING (the coloring), COMPOSITE (a nontrivial factor, checked by one division), SAT (a satisfying assignment). Some NP problems turned out to be in P after a long search: PRIMES has short certificates (Pratt, 1975) and was eventually shown to be in P (Agrawal, Kayal and Saxena, 2002), and linear programming is in P by the ellipsoid method.
The P versus NP question
Is ? In words: if every yes-answer of a problem can be checked quickly, can it also be found quickly? This is open. It was posed formally by Cook in 1971 (with Levin independently, and anticipated in a 1956 letter of Gödel), it is one of the seven Clay Millennium Prize Problems, and it has resisted every attempt. Most researchers believe , for three kinds of reasons: decades of failure to find polynomial algorithms for thousands of NP-complete problems, the intuition that creating a proof is harder than checking one, and the consequences of equality (most of modern cryptography would break). But belief is not proof, and some known proof techniques (relativization, natural proofs, algebrization) are known not to be strong enough to settle it.
The precise status, as of today:
| Statement | Status |
|---|---|
| proved (above) | |
| or | open |
| an NP-complete problem in P | would prove (next lessons) |
| an NP problem provably not in P | would prove |
co-NP
The complement of a language is . The class co-NP contains the languages whose complement is in NP: those whose no-answers have short certificates. NON-HAM-CYCLE ("this graph has no Hamiltonian cycle") is in co-NP, but no short proof that a graph is not Hamiltonian is known, so it is not known to be in NP. P is closed under complement (flip the answer), so . Whether is also open; if then , but not necessarily the other way round. Factoring (as a decision problem: "does have a factor below ?") lies in , since the full prime factorization certifies both answers; that is one reason it is not believed to be NP-complete.
def verify_has_small_factor(N, k, cert):
"""yes-certificate: a factor d with 1 < d < k"""
return 1 < cert < k and N % cert == 0
def verify_no_small_factor(N, k, primes):
"""no-certificate: the prime factorization; every prime factor must be >= k"""
prod = 1
for p in primes:
if p < 2 or any(p % d == 0 for d in range(2, int(p ** 0.5) + 1)): # stands for a primality check
return False
prod *= p
return prod == N and all(p >= k for p in primes)
N = 1022117 # = 1009 * 1013
print("factor below 1010? yes, certificate 1009:", verify_has_small_factor(N, 1010, 1009))
print("factor below 1000? no, certificate [1009, 1013]:", verify_no_small_factor(N, 1000, [1009, 1013]))factor below 1010? yes, certificate 1009: True
factor below 1000? no, certificate [1009, 1013]: True
(The primality check inside the no-verifier uses trial division for brevity; a real verifier uses a polynomial-time primality test.)
Test yourself
Which two requirements does the definition of NP place on certificates?
- They must be unique and sorted
- They must have polynomial length, and a polynomial-time algorithm must check them
- They must be found in polynomial time
- They must be binary strings of length exactly
Why is every language in P also in NP?
- Because NP contains all languages
- Because a verifier can ignore the certificate and decide the instance itself in polynomial time
- Because P problems have no certificates
- It is not known
What is the status of the question whether ?
- Proved false in 1971
- Proved true by AKS
- Open: no proof either way is known
- Shown to be independent of mathematics
Which problem is in co-NP, but not known to be in NP?
- HAM-CYCLE
- COMPOSITE
- NON-HAM-CYCLE: the graph has no Hamiltonian cycle
- SHORTEST-PATH