Chapter 20
P and NP
The toy's every question was settled by trying everything: 4,096 programs on four inputs. That works because the toy is finite. Complexity theory is about formal languages with inputs of every length, where trying everything grows with the input, and it sorts them by how fast the work grows. This chapter gives the two classes the rest of the book leans on, P and NP, at the level of their definitions.
Problems, formal languages and algorithms §
People say "the traveling salesman problem is in NP", and this chapter makes that kind of sentence precise. Under it sit three layers:
- A problem, as people say it: "is this formula satisfiable?"
- A formal language, the set of strings that encode the problem's yes-instances. SAT is the set of DIMACS files, the format of “The SAT solver”, that encode a satisfiable formula.
- An algorithm, a machine or program that decides the set. rocsat is one algorithm that decides SAT, and a different thing from SAT.
So "in P" is a property of the formal language: some machine decides it in polynomial time. The primes show it. For centuries every known primality test was slow in the number of digits, and in 2002 Manindra Agrawal, Neeraj Kayal and Nitin Saxena proved that PRIMES is in P. The set stayed the same, and the known machines improved. Trial division looks cheap at about √n steps, but n in binary has about log₂ n digits, so √n is exponential in the input's length; “Nibble, sized” writes a count in unary for this reason.
A question about two things is one string too. (P, x) names the single string that encodes the pair: the first part's bits, then the second's. For Nibble, (P, x) is P's 2k bits followed by x's n bits.
Time as a function of length §
An input is a string, and its length n is the measure of its size. A machine's running time is the most steps it takes on any input of length n, a function of n. A running time is polynomial when it is at most c·n^k for some fixed c and k: n, n², n³ and so on. The contrast is exponential time, such as 2^n, the cost of trying every assignment of n bits:
| n | n² | 2^n |
|---|---|---|
| 10 | 100 | 1,024 |
| 20 | 400 | 1,048,576 |
| 50 | 2,500 | 1,125,899,906,842,624 |
Polynomial is the dividing line because it is robust: a polynomial-time machine that calls another as a step is still polynomial, and every reasonable model of a machine simulates the others with a polynomial slowdown.
P, deciding fast §
P is the class of formal languages that some machine decides in polynomial time: on every input, it halts within c·n^k steps with its verdict, accept or reject. Whether a list of numbers is sorted is in P: one pass compares each pair of neighbours. So is whether a number written in binary is even, and, by the 2002 algorithm, whether it is prime.
NP, checking fast §
NP is the class of formal languages whose yes answers can be checked fast. A formal language L is in NP when there is a machine V, the verifier, that runs in polynomial time on a pair (x, c), with two properties:
- Every yes has evidence. If x is in L, some string c, a certificate, makes V(x, c) accept, and c is short: its length is at most polynomial in the length of x.
- Every non-member resists all evidence. If x is outside L, V(x, c) rejects for every c whatsoever.
The certificate is the answer's evidence, and the verifier is a skeptic that only checks. Finding the certificate is someone else's job.
Two examples:
- SAT, the satisfiable Boolean formulas. The certificate is an assignment, and the verifier evaluates the formula at it:
rocsat check, in “The SAT solver”, is such a verifier. - Composite numbers. The certificate is a factor d with 1 < d < x, and the verifier divides.
In each, checking is quick and finding looks hard: the obvious way to find a certificate tries them all, and there are exponentially many.
The second property matters as much as the first. A verifier that accepted every certificate would make every formal language look like a member. “The SAT solver”'s unhappy paths hold rocsat's checker to it on small cases: a false assignment is refused, and the clause it breaks is named.
What is known §
Every formal language in P is in NP: its verifier ignores the certificate and decides x directly. Whether every formal language in NP is in P, whether finding is always about as easy as checking, is the P versus NP question, open since the early 1970s and one of the Clay Mathematics Institute's Millennium Prize Problems.
The definition is lopsided. A certificate shows membership, and NP asks for evidence of membership alone. For an unsatisfiable formula, short evidence of its unsatisfiability is, as far as anyone knows, out of reach in general. The DRAT proofs of “The solver's work, in full” are evidence of exactly that kind, and they are short here because the toy is small; for some formulas, every proof of the kind a solver like rocsat writes is exponentially long.
The N stands for nondeterministic: NP was first defined by machines that guess, and “P, NP and Cook-Levin” takes up that view, the guess being the certificate.