4.6.23 · D4Theory of Computation

Exercises — NP — non-deterministic polynomial, verifier definition

3,278 words15 min readBack to topic

This page is a self-test ladder for NP and the verifier definition. Every exercise states a clean problem, then hides a complete worked solution inside a collapsible callout — try first, then reveal.

The rungs climb like this:

  • L1 Recognition — can you name the pieces of the definition?
  • L2 Application — can you use the recipe (certificate / length / verifier)?
  • L3 Analysis — can you see why a piece is needed and what breaks without it?
  • L4 Synthesis — can you combine NP with its neighbours (co-NP, P (complexity class), NP-Completeness)?
  • L5 Mastery — can you invent certificates and reason at the frontier (P vs NP Problem)?

Level 1 — Recognition

Exercise 1.1

Expand the letters NP. In one sentence, say what NP is about (solving or checking?).

Recall Solution

NP = Nondeterministic Polynomial time. It is about checking: a problem is in NP when a proposed solution can be verified quickly, even if finding one from scratch might be slow. It is not "non-polynomial."

Exercise 1.2

Match each name to its role in the verifier definition: (a) (b) (c) (d) . Choices: the polynomial length bound · the input instance · the certificate/witness · the deterministic poly-time checker.

Recall Solution
  • ::: the input instance
  • ::: the certificate / witness
  • ::: the deterministic poly-time checker (verifier)
  • ::: the polynomial length bound, giving

Exercise 1.3

True or false, with one-line reasons: (a) Every problem in P is in NP. (b) NP means a problem is definitely hard. (c) A nondeterministic machine is the same as a random (coin-flipping) machine.

Recall Solution
  • (a) True. A solver is a verifier — run it and ignore the certificate (). So .
  • (b) False. NP = Nondeterministic Polynomial. It contains the easy class P; it is about fast verification, not about being unsolvable.
  • (c) False. Nondeterminism accepts if some branch accepts (a logical OR over guesses). Randomness weighs branches by probability. Different models.

Level 2 — Application

For each problem: give the certificate, argue its length is polynomial, and describe a poly-time verifier. This is the standard three-part recipe.

Exercise 2.1 — CLIQUE

CLIQUE: given a graph on vertices and an integer , does contain a set of vertices all pairwise connected (a "clique")? Show .

Recall Solution
  • Certificate : the list of vertex labels claimed to form the clique.
  • Length: at most labels, each bits, so — polynomial in the input size. ✔ (Short)
  • Verifier : check that all vertices are distinct, and that for every pair among them the edge is present. There are pairs, each looked up in constant time — polynomial. ✔ (Fast)
  • Yes-only: if a -clique exists, listing its vertices makes accept; if none exists, no list of pairwise-adjacent vertices can be produced, so no certificate works. ✔

Figure — CLIQUE certificate (below). The graph has 6 vertices . The three coral (red) vertices are the certificate handed to the verifier for . The mint (green) thick edges are the three pairs that must confirm are all present; the thin lavender edges are the rest of the graph, which ignores. Because all required edges exist, accepts this certificate.

Figure — NP — non-deterministic polynomial, verifier definition
Figure s01: The certificate for CLIQUE is the vertex set itself (coral); the verifier only checks that every pair inside it is joined by an edge (mint).

Exercise 2.2 — SUBSET-SUM

SUBSET-SUM: given integers and a target , is there a subset that sums to exactly ? Show it is in NP.

Recall Solution
  • Certificate: the subset itself — encode it as a bit-string where bit is iff is chosen.
  • Length: exactly bits. Polynomial. ✔
  • Verifier: add up the chosen numbers and compare to . Adding numbers is polynomial in the input size (the numbers are part of the input, so their bit-lengths are already counted). ✔
  • Yes-only: a valid subset makes the sum equal and accepts; if no subset hits , every fails. ✔

Exercise 2.3 — GRAPH 3-COLORING

3-COLOR: can the vertices of be painted with 3 colors so that no edge joins two same-colored vertices? Show it is in NP.

Recall Solution
  • Certificate: an assignment of one color from to each of the vertices.
  • Length: symbols, each needing bits, so bits. Polynomial. ✔
  • Verifier: scan every edge and check . With edges this is . ✔
  • Yes-only: a proper coloring passes all edge checks; if the graph is not 3-colorable, some edge fails under every coloring. ✔

Level 3 — Analysis

Now we probe why each clause of the definition is load-bearing.

Exercise 3.1 — Kill the length bound

Suppose we dropped the requirement and allowed certificates of any length. Show that then every decidable language would be "in NP." What clause of the definition just collapsed?

Recall Solution

Take any decidable language decided by some algorithm (possibly very slow). Let the certificate be the entire accepting computation trace of on — the full sequence of machine configurations. A verifier can check "each step follows the transition rules and the last step accepts" in time linear in the trace length.

With no length bound the trace is allowed to be astronomically long (exponential, or worse), and 's "poly time" is measured against the certificate too — so trivially runs in time polynomial in while itself is unbounded. Every decidable language sneaks in.

The clause that collapsed: Short. Bounding is precisely what stops "check a whole giant computation" from qualifying. Without it, NP would equal the class of all decidable languages.

Exercise 3.2 — Kill the fast verifier

Keep the short certificate, but allow to run in exponential time. Which famous class do we now capture, and why does that make the definition useless for its purpose?

Recall Solution

If certificates stay polynomially short but may run in exponential time, then can — with an empty certificate — simply solve the problem by brute force. Any problem in EXP (exponential time, defined at the top of this page) now "qualifies," because the exponential verifier just re-solves it and ignores .

That defeats the entire point: NP was meant to isolate "checking is easy," and an exponential checker is not easy checking — it is solving in disguise. The clause that collapsed: Fast. Polynomial-time verification is what makes "verify" genuinely cheaper than "solve" (unless P = NP).

Exercise 3.3 — Why one-sided?

The definition demands a certificate only for yes instances. Suppose you also demanded, symmetrically, a short certificate of "no" for every no instance. Which class does that symmetric requirement define, and how does it relate to NP?

Recall Solution

Requiring a short, poly-checkable certificate for no instances is the definition of co-NP: iff its complement . Demanding certificates for both answers means the language is in .

Whether is open and widely believed false. Example asymmetry: for SAT a "yes" is proved by exhibiting a satisfying assignment, but no one knows a short certificate that a formula is unsatisfiable (that would put SAT in co-NP, hence NP = co-NP). This one-sidedness is the whole reason NP and co-NP look different.


Level 4 — Synthesis

Combine NP with its neighbours.

Exercise 4.1 — P inside NP, precisely

Give a fully explicit verifier that witnesses for a specific problem: PARITY ("does the input bit-string have an even number of 1s?"). Name the certificate, its length, and .

Recall Solution

PARITY is in P — count the 1s in a single linear scan. To place it in NP explicitly:

  • Certificate: the empty string, . Length trivially. ✔
  • Verifier : ignore , scan , count 1s, accept iff the count is even. This is — polynomial. ✔
  • Yes-only: accepts exactly when genuinely has even parity; the (empty) certificate exists for every yes instance and its acceptance depends only on . ✔

This is the general pattern: any solver becomes a "lazy verifier" that discards the hint. Hence .

Exercise 4.2 — NP inside EXP, with the count

Show by brute-forcing certificates. If a verifier accepts certificates of length up to and each check costs steps, write the total worst-case running time as an expression, and confirm it is at most exponential.

Recall Solution

Enumerate all binary strings of length . The number of such strings is Each is fed to at cost . Total time This is dominated by the factor, which is — exactly the shape of EXP (defined at the top of the page). So the brute-force verifier decides in exponential time, giving . ✔ (Accept iff any enumerated certificate makes accept — a giant OR, echoing the nondeterministic machine.)

Exercise 4.3 — Reduction plumbing

You are told SAT is NP-complete (by the Cook–Levin Theorem) and you have a polynomial-time reduction from SAT to CLIQUE. What fact about CLIQUE does this reduction alone establish, and which single membership assumption must you additionally prove to upgrade "CLIQUE is NP-hard" to "CLIQUE is NP-complete"?

Recall Solution

A poly-time reduction means: any NP problem, first reduced to SAT (Cook–Levin), then to CLIQUE, ends up as a CLIQUE instance. So the reduction alone establishes exactly one thing:

  1. CLIQUE is NP-hard — at least as hard as everything in NP.

But NP-hardness says nothing about whether CLIQUE is itself in NP — an NP-hard problem can lie far outside NP (some are even undecidable). To upgrade to NP-complete you must additionally supply the missing half: 2. the membership assumption — proved by exhibiting a certificate (the vertex set) and a poly-time verifier, exactly as in Exercise 2.1.

NP-complete = NP-hard AND in NP. The reduction gives the hardness; the certificate gives the membership. The single fact you must isolate and prove is . (Separately, whether these NP-complete problems fall into P is the genuinely open P vs NP question.)


Level 5 — Mastery

Exercise 5.1 — Invent a subtle certificate

Recall the parent note claims COMPOSITE NP via a factor. Now the hard sibling: give a short, poly-time-checkable certificate that a number is PRIME, thereby showing . (Hint: use a primitive root / Pratt certificate idea — you need a witness whose order modulo is exactly .)

Recall Solution

A number is prime iff there exists a generator of the multiplicative group under multiplication mod — i.e. has multiplicative order exactly . That happens iff:

  1. , and
  2. for every prime factor of , (so no smaller exponent works, forcing the order to be the full ).

Certificate (Pratt certificate): the generator , the complete list of prime factors of , and — recursively — a Pratt primality certificate for each factor (so a verifier can trust that each really is prime rather than taking it on faith). The recursion bottoms out at small primes like , which are certified directly.

  • Length: each recursive level shrinks the number by a factor of at least (the factors multiply to ), so the recursion has depth and the total certificate is bits — polynomial in . ✔ (Short)
  • Verifier: perform the two modular exponentiations in conditions (1) and (2) — fast modular exponentiation runs in time polynomial in — verify that the listed actually multiply to , and recurse into each sub-certificate. The total work sums to a polynomial in . ✔ (Fast)
  • Yes-only: if is prime a generator exists (a standard theorem: the group is cyclic for prime ), so the certificate exists; if is composite, no element has order , so conditions (1)–(2) fail for every candidate and no valid certificate can be built. ✔

Worked micro-example (): take . Then has prime factors . Check (1): . Check (2): , and . Both sub-conditions pass, so has order exactly , certifying that is prime.

Thus — and since COMPOSITE NP too, PRIMES lies in . (Historically this was the strong hint that PRIMES is "not too hard," later confirmed by PRIMES P.)

Exercise 5.2 — What P = NP would actually mean

State crisply what a proof of would imply about certificates. Then answer: would it mean certificates become useless, or that finding them becomes as easy as checking them?

Recall Solution

would mean: for every problem where a solution can be checked in polynomial time, a solution can also be found in polynomial time. The certificate would not become useless — rather, the search for it would become as cheap as the check. The "hard-to-solve / easy-to-check" asymmetry that defines the practical interest of NP would vanish: SAT, CLIQUE, HAM-CYCLE, 3-COLOR would all get efficient solvers.

Because that would break most modern cryptography (which relies on the gap), and because decades of effort found no such algorithm, the consensus expectation is — but it remains the central open open problem.

Exercise 5.3 — Guessing = writing down the guess

Explain, in the language of the Nondeterministic Turing Machine, why the certificate in the verifier definition is literally the same object as a nondeterministic guess. Then state the running-time equivalence that makes both views define the same class.

Recall Solution

A nondeterministic machine at each step may branch into several choices; it accepts if some path of choices leads to accept. Freeze one accepting path and write down, in order, every choice it made — that string of choices is a certificate . Feeding back into a deterministic machine that "replays the choices instead of guessing them" is exactly the verifier .

Conversely, given a verifier and bound , build a nondeterministic machine that first guesses a string of length (branching on each bit), then runs deterministically; it accepts iff some guess is a valid certificate.

Running-time equivalence: a poly-time nondeterministic machine ⇔ a poly-time deterministic verifier with a polynomially bounded certificate. The guess length is bounded by the machine's running time (poly), matching ; the deterministic replay is poly. Same time budget on both sides ⇒ same class, NP.


Recall One-line self-check before you leave

For any language, could you (a) name a certificate, (b) bound its length by a polynomial, and (c) describe a poly-time check? If yes to all three, it is in NP — Short, Fast, Yes-only.