TExcelTAD

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 P⊆NP\mathrm{P} \subseteq \mathrm{NP}, and states carefully the open question of whether the two are equal.

Certificates and verifiers

A verification algorithm AA takes two inputs: an instance xx and a certificate yy (also called a witness or proof). AA verifies xx if there is some yy with A(x,y)=1A(x, y) = 1. The language verified by AA is

L={x:there exists y with A(x,y)=1}.L = \{ x : \text{there exists } y \text{ with } A(x, y) = 1 \}.

Definition. A language LL is in NP if there are a polynomial-time verification algorithm AA and a constant cc such that

x∈L  ⟺  there exists y with ∣y∣=O(∣x∣c) and A(x,y)=1.x \in L \iff \text{there exists } y \text{ with } \lvert y \rvert = O(\lvert x \rvert^c) \text{ and } A(x, y) = 1 .

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 LL in polynomial time.

For HAM-CYCLE, the certificate is a list of the vertices in cycle order, of length O(Vlg⁡V)O(V \lg V) bits, and the check takes O(V2)O(V^2) time with an adjacency matrix:

python · try it yourself
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: 2p(n)2^{p(n)} bit strings of length p(n)p(n), or (V−1)!/2(V-1)!/2 distinct cycles in a graph.

python · try it yourself
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 9!=362 8809! = 362\,880 orders. With 20 vertices the same search would face 19!≈1.2⋅101719! \approx 1.2 \cdot 10^{17} orders.

P is contained in NP

If L∈PL \in \mathrm{P}, a verifier can ignore the certificate and decide xx itself in polynomial time. So every language in P is in NP:

P⊆NP.\mathrm{P} \subseteq \mathrm{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 P=NP\mathrm{P} = \mathrm{NP}? 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 P≠NP\mathrm{P} \ne \mathrm{NP}, 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
P⊆NP\mathrm{P} \subseteq \mathrm{NP} proved (above)
P=NP\mathrm{P} = \mathrm{NP} or P≠NP\mathrm{P} \ne \mathrm{NP} open
an NP-complete problem in P would prove P=NP\mathrm{P} = \mathrm{NP} (next lessons)
an NP problem provably not in P would prove P≠NP\mathrm{P} \ne \mathrm{NP}

co-NP

The complement of a language LL is L‾={0,1}∗∖L\overline{L} = \{0,1\}^* \setminus L. 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 P⊆NP∩co-NP\mathrm{P} \subseteq \mathrm{NP} \cap \mathrm{co\text{-}NP}. Whether NP=co-NP\mathrm{NP} = \mathrm{co\text{-}NP} is also open; if P=NP\mathrm{P} = \mathrm{NP} then NP=co-NP\mathrm{NP} = \mathrm{co\text{-}NP}, but not necessarily the other way round. Factoring (as a decision problem: "does NN have a factor below kk?") lies in NP∩co-NP\mathrm{NP} \cap \mathrm{co\text{-}NP}, since the full prime factorization certifies both answers; that is one reason it is not believed to be NP-complete.

python · try it yourself
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?

  1. They must be unique and sorted
  2. They must have polynomial length, and a polynomial-time algorithm must check them
  3. They must be found in polynomial time
  4. They must be binary strings of length exactly nn

Why is every language in P also in NP?

  1. Because NP contains all languages
  2. Because a verifier can ignore the certificate and decide the instance itself in polynomial time
  3. Because P problems have no certificates
  4. It is not known

What is the status of the question whether P=NP\mathrm{P} = \mathrm{NP}?

  1. Proved false in 1971
  2. Proved true by AKS
  3. Open: no proof either way is known
  4. Shown to be independent of mathematics

Which problem is in co-NP, but not known to be in NP?

  1. HAM-CYCLE
  2. COMPOSITE
  3. NON-HAM-CYCLE: the graph has no Hamiltonian cycle
  4. SHORTEST-PATH