Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 71

Defining NP: TMs and Verifiers

Recommended resources

Hello! Welcome back to our exploration of computational complexity.

In the last lesson, we defined the class P, which captures the set of decision problems we consider "efficiently solvable"—those that can be decided by a deterministic Turing Machine in polynomial time. We saw examples like the PATH problem, which are comfortably in P.

Today, we address a different, much larger, and more mysterious class of problems: NP. You've likely heard of these problems in your AI and algorithm work—problems like satisfiability, the traveling salesperson, or scheduling, where finding a solution seems to require a brute-force search, but checking a given solution is easy. This lesson will formalize that intuition. Our goal is to define the complexity class NP using two powerful and equivalent perspectives: one involving a magical "guessing" machine and another based on efficient verification.

1. The "Lucky Guesser": Nondeterministic Polynomial Time

Let's start by revisiting a concept from earlier in the course: the Nondeterministic Turing Machine (NTM). An NTM is a TM that can have multiple possible next moves at any point. It accepts an input if any of its possible computation paths leads to an accept state.

You can think of an NTM as a "lucky guesser." At every choice point, it magically makes the best possible guess that will lead to an acceptance, if such a path exists.

16. Complexity: P, NP, NP-completeness, Reductions

To build our intuition, let's watch a clip from an MIT lecture by Prof. Erik Demaine. He introduces the class NP and explains this 'lucky model' of computation.

Watch from 01:21 to 03:25. Focus on how NP is defined in terms of a 'non-deterministic model' and how this relates to decision problems.

This leads us to our first formal definition of NP. It mirrors the definition of P, but uses NTMs instead of deterministic TMs.

Definition: The Complexity Class NP (NTM version)
The complexity class NP is the set of all languages that can be decided by a Nondeterministic Turing Machine (NTM) in polynomial time.

Formally, we can express this as:

Here, is the set of languages decidable by an NTM where every computational branch halts in steps.

Nondeterministic Turing Machine Model
This diagram illustrates the conceptual model of an NTM. The 'Guessing Module' represents the nondeterministic ability to choose a path, while the 'Finite State Control' then performs deterministic computation based on that guess.

Example: The 3-SAT Problem

A classic problem in NP is 3-Satisfiability (3-SAT).

  • Input: A Boolean formula in 3-Conjunctive Normal Form (3-CNF), which is an AND of clauses, where each clause is an OR of exactly three variables or their negations. For example: .
  • Question: Is there an assignment of true/false values to the variables that makes the entire formula true?

A deterministic algorithm would need to try all possible assignments for variables, which is exponential. But an NTM can solve this in polynomial time.

16. Complexity: P, NP, NP-completeness, Reductions

Let's return to the MIT lecture to see how an NTM solves 3-SAT efficiently.

Watch the segment from 05:30 to 08:30. Pay close attention to the two-stage process: a nondeterministic 'guessing' phase and a deterministic 'checking' phase.

As the video explained, the NTM algorithm for 3-SAT is:

  1. Guessing (Nondeterministic): For each of the variables , nondeterministically guess a truth value (true or false). This takes nondeterministic steps.
  2. Checking (Deterministic): Substitute the guessed assignment into the formula and evaluate it. This takes time proportional to the formula's length, which is polynomial.

If there exists a satisfying assignment, one of the NTM's computation paths will guess it, the formula will evaluate to true, and the machine will accept. Since the entire process runs in polynomial time, 3-SAT is in NP.


2. The "Easy to Check" View: Polynomial-Time Verifiers

The NTM definition is powerful but can feel abstract. A more intuitive and practical way to think about NP is through the lens of verification.

Think about 3-SAT again. While finding a satisfying assignment is hard, if I give you a potential solution (e.g., "try )", you can check if it works very quickly—just plug in the values and evaluate.

This proposed solution is called a certificate or a witness. The algorithm that checks the certificate is called a verifier.

Verifiers and Certificates

This next video from Easy Theory provides a clear, focused explanation of verifiers and certificates using the CLIQUE problem as an example.

Watch from the beginning to 08:52. This covers: An intro to verifiers. The k-clique problem as a motivating example. The formal definition of a verifier and certificate. How a certificate makes verifying k-clique easy.

Let's formalize what we saw in the video.

Definition: Verifier
A verifier for a language is an algorithm such that:

In other words, a string is in the language if and only if there is a certificate that the verifier can use to confirm it.

This leads to our second, equivalent definition of NP.

Definition: The Complexity Class NP (Verifier version)
The complexity class NP is the class of languages that have a polynomial-time verifier.

A verifier is "polynomial-time" if it runs in time polynomial in the length of the original input, . An important consequence is that the certificate must also have a polynomial length, otherwise the verifier wouldn't even have time to read it.


3. The Equivalence of the Two Definitions

These two definitions—one based on nondeterministic machines and the other on deterministic verifiers—are equivalent. This is a fundamental theorem in complexity theory. A language is in NP if and only if it is decided by a polynomial-time NTM, and if and only if it has a polynomial-time verifier.

Let's briefly examine why this is true.

Time Complexity

These lecture notes from the University of Massachusetts Amherst provide a formal definition of NP via verifiers and a concise proof sketch of the equivalence. This connects the two concepts we've just learned.

Please read the sections titled 'Polynomial Verifiers' and 'The Class NP'. Focus on the formal definition of a verifier and the theorem establishing the equivalence between the NTM and verifier definitions of NP. The two short proof sketches under the theorem are the key part.

Let's summarize the logic from the notes.

Proof Sketch: Poly-time Verifier Poly-time NTM

If a language has a polynomial-time verifier , we can build a polynomial-time NTM for it:

  1. On input , nondeterministically guesses a certificate of polynomial length.
  2. then runs the deterministic verifier on the pair .
  3. If accepts, accepts.

The guessing part is the "N" in NTM, and the verification part is the "P" (polynomial time).

Proof Sketch: Poly-time NTM Poly-time Verifier

If a language is decided by a polynomial-time NTM , we can build a polynomial-time verifier for it:

  1. The certificate will be a string describing an accepting computation path of on input . Since is poly-time, this path has a polynomial length.
  2. The verifier , on input , deterministically simulates the single computation path of on that is described by .
  3. If this path ends in an accept state, accepts.

This shows that the two definitions are just different ways of looking at the same fundamental property.

Test your understanding!

The BINPACK problem is defined as follows: Given a set of items with different sizes, a number of bins , and a bin capacity , can the items be placed into the bins such that no bin's total item size exceeds ?

Based on what you've learned, how would you argue that BINPACK is in NP using both definitions?

  1. NTM approach: How would a polynomial-time NTM decide BINPACK?
  2. Verifier approach: What would be the certificate, and what would the polynomial-time verifier do?
Show answer

This exact problem is explored in the tutorial sheet (resource LINK), and here's the reasoning:

  1. NTM Approach: A polynomial-time NTM would:

    • Guess (Nondeterministic): Nondeterministically assign each item in the set to one of the bins. This creates a partition . This takes polynomial time.
    • Check (Deterministic): For each of the bins, calculate the sum of the sizes of the items inside it. Check if this sum is less than or equal to the capacity . This also takes polynomial time.
    • If all bins satisfy the capacity constraint, the NTM accepts.
  2. Verifier Approach:

    • Certificate (c): The certificate would be a specific partition of the items into bins. For example, it could be a list of lists, where each inner list represents the items in one bin.
    • Verifier (V): The verifier takes the original input and the certificate . It performs two polynomial-time checks:
      1. It verifies that is a valid partition of (i.e., every item from is in exactly one bin).
      2. It calculates the sum of item sizes for each bin in the partition and confirms that no sum exceeds .
    • If both checks pass, the verifier accepts.

Since we can define both a poly-time NTM and a poly-time verifier, BINPACK is in NP.


Conclusion

Today we formally defined the immensely important complexity class NP. It captures a vast array of problems that we encounter constantly in computer science, from optimization to AI to logistics.

Key Takeaways:

  • The class NP stands for Nondeterministic Polynomial time. It does not mean "non-polynomial".
  • There are two equivalent, fundamental ways to define NP:
    1. The NTM Definition: NP is the class of decision problems solvable in polynomial time by a Nondeterministic Turing Machine (the "lucky guesser").
    2. The Verifier Definition: NP is the class of decision problems where a "yes" answer has a certificate that can be checked for correctness in polynomial time by a deterministic algorithm (the "easy to check" view).
  • The equivalence of these definitions is a cornerstone of complexity theory, linking the abstract power of nondeterminism to the practical concept of efficient verification.

Preview of the Next Lesson:
We've now defined two major classes: P (efficiently solvable) and NP (efficiently verifiable). It's clear that P is a subset of NP (if you can solve a problem efficiently, you can certainly verify a solution efficiently—just solve it again and see if you get the same answer!). The biggest open question in theoretical computer science is whether P = NP. In our next lesson, we will explore this question and introduce the concept of NP-completeness—a set of the "hardest" problems in NP that hold the key to this monumental question.