P vs NP

The Million Dollar Question

CS305 -- Formal Language Theory & Complexity

P =?= NP SOLVING (find answer) VERIFYING (check answer) $1,000,000 · Unsolved since 1971 ?

Use arrow keys or buttons to navigate

1 / 23

The Big Picture

We've studied what's computable. Now: among computable problems, which are EFFICIENTLY solvable?

The Computability/Complexity Landscape ======================================== All Problems (Languages) +----------------------------------------------------------+ | Undecidable (Halting Problem, etc.) | | "No algorithm can solve these -- ever." | | | | +----------------------------------------------------+ | | | Decidable (Computable) | | | | "An algorithm EXISTS, but how fast is it?" | | | | | | | | +----------------------------------------------+ | | | | | Efficiently Solvable? | | | | | | THIS is the P vs NP question! | | | | | +----------------------------------------------+ | | | +----------------------------------------------------+ | +----------------------------------------------------------+

Key Shift in Perspective

In the first half of the course, we asked: "Can this problem be solved at all?"
Now we ask: "Can it be solved FAST ENOUGH to be practical?"

Analogy: Finding Your Keys

Computability: "Your keys exist somewhere in the universe."
Complexity: "Can you find them before lunch?"

2 / 23

Measuring Efficiency

Time complexity as a function of input size n

Polynomial Time (Tractable)

  • O(n) -- Linear
  • O(n log n) -- Linearithmic
  • O(n^2) -- Quadratic
  • O(n^3) -- Cubic
  • O(n^k) -- Polynomial for fixed k

These grow manageably.

Exponential Time (Intractable)

  • O(2^n) -- Exponential
  • O(n!) -- Factorial
  • O(n^n) -- Superexponential

These EXPLODE.

Growth Rate Racing Chart

n = 1

Why This Matters

At n=100, an O(n^2) algorithm takes 10,000 steps (milliseconds on modern hardware).
An O(2^n) algorithm takes 10^30 steps -- that's more than the age of the universe in nanoseconds.
No amount of faster hardware will save you from exponential growth.

3 / 23

The Class P

Problems solvable in polynomial time by a deterministic Turing Machine

P = { L | there exists a deterministic TM M and polynomial p(n) such that M decides L in time O(p(n)) }

What "P" Means Intuitively

  • Problems we can actually solve in practice
  • Algorithms that scale to large inputs
  • The time to solve grows at most as some fixed power of input size
  • "Efficiently solvable" = "in P"

Key Idea

P captures the notion of feasible computation. If a problem is in P, we can build practical software to solve it.

P Algorithm Demos

Click Sort to watch merge sort in action

Analogy

P problems are like recipes where the cooking time is proportional to the number of guests. Double the guests? Maybe quadruple the cooking time. That's manageable.

4 / 23

Examples of P Problems

A tour of problems we CAN solve efficiently

IN P ✓

NOT KNOWN TO BE IN P

Click a problem card, then click a zone to classify it

Note

Being "in P" doesn't mean it's trivially fast -- O(n^100) is technically polynomial but impractical. In practice, most P algorithms we use are O(n), O(n log n), or low-degree polynomials.

5 / 23

The Class NP

Problems where solutions can be VERIFIED quickly

NP = { L | there exists a polynomial-time VERIFIER V such that: x in L <==> there exists certificate c, |c| <= poly(|x|), where V(x, c) accepts }

The Two Key Parts

  • Certificate (witness): A proposed solution or "proof" that the answer is YES
  • Verifier: An algorithm that CHECKS the certificate in polynomial time

NP stands for Nondeterministic Polynomial time, NOT "Non-Polynomial"!

Common Misconception

NP does NOT mean "not solvable in polynomial time." Many NP problems ARE in P! NP means verifiable in polynomial time.

Interactive: Subset Sum Verifier

Set = {3, 7, 1, 8, 4}   Target = 12. Click numbers to select them.

6 / 23

NP as Nondeterministic Polynomial Time

The equivalent "lucky guessing" definition

NTM Branching Tree Explorer

The "Lucky Guesser" Interpretation

Think of it as a two-phase machine:

Phase 1: GUESS

Nondeterministically "guess" a certificate (candidate solution). Magically picks the right one if it exists.

Phase 2: VERIFY

Deterministically check the guess in polynomial time. This is the verifier.

Analogy: A Magic Oracle

Imagine you have a friend with perfect intuition. They always guess the right answer, but you still need to double-check their work. If the checking step is fast (polynomial), the problem is in NP.

Why These Definitions Are Equivalent

Certificate-based: "A short proof exists and can be checked quickly."
NTM-based: "A nondeterministic machine can find and verify in poly time."
Each nondeterministic branch corresponds to a different candidate certificate.

7 / 23

Examples of NP Problems

Problems where we can verify but (probably) can't efficiently solve

Click Verify to check the certificate

The Pattern

For every NP problem: finding the answer seems to require searching an exponential space, but checking a given answer is fast. That asymmetry is the heart of P vs NP.

8 / 23

P is a Subset of NP

Every problem in P is also in NP -- but is the reverse true?

Why P ⊆ NP

If you can solve a problem in polynomial time, you can certainly verify a solution in polynomial time.

Step through to see why P ⊆ NP

The Million Dollar Question

We KNOW: P NP We DON'T KNOW: Does P = NP ? (everything verifiable is also solvable) Or P NP ? (some problems are harder to solve than to verify)

The Stakes

This is one of the Clay Millennium Prize Problems. Solve it (either direction) and win $1,000,000.

It has been open since Stephen Cook formalized it in 1971 -- over 50 years of the brightest minds failing to resolve it.

9 / 23

The P vs NP Question -- Visualized

Two possible worlds — click to explore each scenario

Why Most Experts Believe P ≠ NP

Decades of effort by brilliant researchers have failed to find polynomial algorithms for NP-complete problems. It would be astonishing if all that effort missed something. But nobody can PROVE it either way!

10 / 23

Polynomial-Time Reductions

The tool for comparing problem difficulty

Core Idea

Problem A reduces to problem B (written A ≤_P B) if we can transform any instance of A into an instance of B in polynomial time, such that solving B gives us the answer to A.

Polynomial-Time Reduction: A ≤_P B Instance of A Instance of B Solution to B +-----------+ poly +-----------+ solve +-----------+ | | -------> | | -------> | | | "Is x | time | "Is f(x) | B's | YES/NO | | in A?" | f(x) | in B?" | algo | | +-----------+ +-----------+ +-----------+ | | | | +---------- Same answer! ----------------------+ If B is easy (in P), then A is easy too! If A is hard, then B must be hard too!

Analogy: Language Translation

If you can translate French to English quickly, and you have an English dictionary, then you can effectively look up French words. Reducing French-lookup to English-lookup.

Direction Matters!

A ≤_P B means "A is no harder than B" (or "B is at least as hard as A").
If you show a hard problem reduces TO your problem, your problem must be hard too!

11 / 23

NP-Hardness

Problems that are "at least as hard as anything in NP"

Definition: A problem H is NP-HARD if: For EVERY problem L in NP: L ≤_P H "Every NP problem can be reduced to H in polynomial time"

What This Means

  • H is a universal problem -- if you could solve H efficiently, you could solve EVERYTHING in NP efficiently
  • H is "at least as hard" as the hardest problems in NP
  • If ANY NP-hard problem is in P, then P = NP
Every NP problem reduces to H: SAT --------\ TSP ---------\ Clique -------+----> H Coloring ----/ SubsetSum --/ ...every NP problem

NP-Hard Does NOT Mean "in NP"

NP-hard problems can be harder than NP! They might not even be decidable.

Example: The Halting Problem is NP-hard (every NP problem reduces to it) but it's not in NP -- it's not even decidable!

Analogy: The Master Lock

Think of an NP-hard problem as a "master lock." If you can pick this one lock, every other lock (NP problem) opens automatically. It's at least as tough as every other lock in the building.

12 / 23

NP-Completeness

The hardest problems IN NP — click regions and problems to explore

Definition

A problem is NP-Complete if it is:

1. In NP (solutions can be verified in polynomial time), AND

2. NP-Hard (every NP problem reduces to it)

Click a problem badge or region label to learn more.

Why NP-Complete Problems Are Special

If you find a polynomial algorithm for ANY NP-complete problem, then P = NP (all NP problems become easy). Conversely, if you prove ANY NP-complete problem has no poly-time algorithm, then P ≠ NP. They're the "gatekeepers" of the P vs NP question.

13 / 23

Cook's Theorem (Cook-Levin, 1971)

The theorem that launched complexity theory

Theorem

SAT is NP-complete.

Boolean Satisfiability was the FIRST problem ever proven NP-complete. Every other NP-completeness proof builds on this foundation.

Proof Idea (Sketch)

  1. SAT is in NP: Given a truth assignment (certificate), we can evaluate the formula in polynomial time.
  2. SAT is NP-Hard: Take ANY problem L in NP with its verifier V. We can encode the entire computation of V(x,c) as a Boolean formula φ such that:
    • φ is satisfiable ⇔ V accepts some certificate for x
    • φ has polynomial size (since V runs in poly time)
Any NP verifier V: Input x, Certificate c | V's computation (poly steps) | Accept/Reject ENCODE as Boolean formula: - Variables for each tape cell at each time step - Clauses enforcing TM rules - Clauses enforcing acceptance

Why This Is Profound

Cook showed that the general act of computation can be captured by Boolean logic. Every polynomial-time verification can be "compiled" into a SAT instance.

Analogy

It's like discovering that every recipe in every cookbook can be translated into one universal recipe format. SAT is that universal format -- it can express ANY NP computation.

Historical Note

Stephen Cook (Toronto) proved this in 1971. Leonid Levin independently proved a similar result in the Soviet Union. Cook received the Turing Award in 1982 for this work.

14 / 23

The SAT Problem -- Interactive Solver

Boolean Satisfiability: toggle variables and watch clauses evaluate live

CNF Formula

φ = (x1 ∨ ¬x2 ∨ x3)
(¬x1 ∨ x2)
(¬x1 ∨ ¬x3)
(x2 ∨ x3)

Toggle Variables

x1
x2
x3

All 8 Assignments

x1x2x3C1C2C3C4φ

The Difficulty

With 3 variables, we checked 8 rows. With n variables, there are 2n possible assignments. At n=300, that's more than the atoms in the universe.

3-SAT

When every clause has exactly 3 literals, it's called 3-SAT. This restricted version is STILL NP-complete! (2-SAT, however, is in P.)

15 / 23

Proving NP-Completeness

Click edges for reduction details, nodes for connections, or watch the chain cascade

The Recipe for Proving Problem X is NP-Complete

1. Show X is in NP (give a polynomial-time verifier).
2. Pick a known NP-complete problem Y and show Y ≤_P X (reduce Y to X in polynomial time).

Click any edge (arrow) or node to explore the reduction web.

Analogy: A Chain of Dominoes

Cook knocked over the first domino (SAT). Karp knocked over 21 more in 1972. Now thousands of NP-complete problems are known. Each new one just needs ONE reduction from an existing NP-complete problem.

16 / 23

Classic NP-Complete Problems

The "greatest hits" -- problems you'll see everywhere

3-SAT

SAT where each clause has exactly 3 literals.
(x1 OR ~x2 OR x3) AND (~x1 OR x4 OR x2)
The "workhorse" for reductions.

Clique

Find k vertices all connected to each other.
"Is there a friend group of size 5 where everyone knows everyone?"

Independent Set

Find k vertices with NO edges between them.
"Can you seat 5 people at a dinner where no two are enemies?"

Vertex Cover

Find k vertices that touch every edge.
"Place k security cameras to watch every hallway."

Hamiltonian Cycle

Visit every vertex exactly once and return to start.
"Can the mail carrier visit every house on one loop?"

TSP (Decision Version)

Is there a tour of all cities with distance ≤ k?
4 cities: "Can I visit A, B, C, D and return in ≤ 20 miles?"

Subset Sum

Given S = {3, 7, 1, 8, 4, 12}, target = 15.
Is there a subset summing to 15? Yes: {3, 8, 4}.

Graph k-Coloring (k ≥ 3)

Color vertices with k colors, no adjacent vertices same color.
"Color a map with 3 colors so no bordering countries match."

They're All Connected

If you solve ANY ONE of these in polynomial time, you've solved ALL of them (and won $1M). They all reduce to each other. They stand or fall together.

17 / 23

Dealing with NP-Completeness

Your problem is NP-complete. Now what?

Don't despair! NP-completeness is a worst-case statement. In practice, there are many strategies:

1. Approximation Algorithms

Don't find the OPTIMAL solution -- find one that's provably CLOSE to optimal.

Example: For Vertex Cover, a simple greedy algorithm always finds a cover at most 2x the optimal size. Good enough for many applications!

2. Heuristics & Metaheuristics

Algorithms that work well in practice without guarantees: simulated annealing, genetic algorithms, local search.

Example: TSP tours for millions of cities are routinely found near-optimally using heuristics.

3. Special Cases / Fixed Parameters

Your specific inputs might have structure that makes the problem easier.

Example: 2-SAT is in P! Graph coloring on trees is in P! TSP on Euclidean distances has good approximations.

4. Randomized Algorithms

Allow random coin flips. Sometimes randomness helps!

Example: Random assignment satisfies at least 7/8 of clauses in MAX-3-SAT on average.

5. Exponential but Practical

Modern SAT solvers handle formulas with millions of variables using clever techniques (DPLL, CDCL, unit propagation). Worst case is exponential, but typical cases are fast.

6. Restrict Input Size

If n is small enough, even O(2^n) is fine. Subset Sum with 30 elements? 2^30 ≈ 10^9 -- a computer handles that in seconds.

Analogy

NP-completeness says "you can't always find the exit in a giant maze quickly." But YOUR maze might have helpful signs, be smaller than you think, or you might accept finding an exit that's close enough to the shortest one.

18 / 23

Beyond NP -- The Complexity Hierarchy

NP is just one level in a vast tower of complexity

The Complexity Tower (each level contains all below it) +================================+ || UNDECIDABLE || || (Halting Problem, etc.) || +================================+ | +--------------------------------+ | EXPSPACE | | (exponential space) | +--------------------------------+ | +--------------------------------+ | EXPTIME | | (exponential time) | | Chess, Checkers (generalized) | +--------------------------------+ | +--------------------------------+ | PSPACE | | (polynomial space) | | QBF, Generalized Geography | | PSPACE = NPSPACE (Savitch's) | +--------------------------------+ | +---+---+------------------------+ | coNP | NP | | | SAT, Clique, TSP | +---+---+---+--------------------+ | | +---+-------+--------------------+ | P | | Sorting, Shortest Path, GCD | +--------------------------------+ | +--------------------------------+ | L (LOGSPACE) | | Reachability in undirected | | graphs | +--------------------------------+

Key Classes Beyond NP

coNP

Complements of NP problems. "Can you verify that something is NOT the case?"

Example: "Is this formula UNSATISFIABLE?" (complement of SAT). Easy to prove satisfiable (give an assignment), hard to prove unsatisfiable.

PSPACE

Problems solvable with polynomial SPACE (but possibly exponential time).

Example: Quantified Boolean Formulas (QBF): "For all x, there exists y, such that φ(x,y) is true." Like a two-player game -- much harder than SAT!

EXPTIME

Problems requiring exponential time. Generalized chess and checkers are EXPTIME-complete. We KNOW P ≠ EXPTIME!

What We Know vs. Don't Know

P ⊆ NP ⊆ PSPACE ⊆ EXPTIME -- all inclusions are known.
But we only know P ≠ EXPTIME for certain. Whether P ≠ NP, NP ≠ PSPACE, etc. are all open questions!

19 / 23

What If P = NP?

The "mathematical apocalypse" scenario

If someone proved P = NP and gave us actual algorithms, the consequences would be staggering:

Cryptography BREAKS

Modern encryption (RSA, AES, etc.) relies on problems being HARD to solve (factoring, discrete log). If P = NP, an attacker could break any cipher, forge any digital signature, and decrypt any message. Online banking, HTTPS, blockchain -- all gone.

Optimization Becomes Trivial

Scheduling, logistics, resource allocation, protein folding, chip design -- all solvable optimally in polynomial time. Companies would save billions. Supply chains perfected overnight.

AI / Machine Learning Leaps

Many ML problems (optimal neural network training, feature selection) are NP-hard. If P = NP, we could find provably optimal models efficiently.

Mathematical Proofs Become Automated

Finding proofs is in NP (verify by checking each step). If P = NP, computers could find short proofs of any provable theorem automatically. Mathematics itself would be transformed.

Creative Tasks Become Algorithmic

Writing a symphony "as good as Beethoven's" (if quality can be verified) becomes a computation. Generating optimal code, designing drugs, composing music -- all become algorithmic.

Scott Aaronson's famous quote: "If P = NP, then the world would be a profoundly different place than we usually assume it to be. Everyone who could appreciate a symphony would be Mozart; everyone who could follow a step-by-step argument would be Gauss; everyone who could recognize a good investment strategy would be Warren Buffett."

The Catch

Even if P = NP, the polynomial might be impractically large (e.g., O(n^1000000)). A "yes" answer doesn't guarantee practical algorithms -- but historically, polynomial algorithms are eventually improved.

20 / 23

What If P ≠ NP? (The Likely Answer)

The world as we know it -- some problems are inherently hard

The "Expected" Reality

Most computer scientists believe P ≠ NP. A 2019 poll showed 88% of complexity theorists expect P ≠ NP.

Cryptography is Safe

The hardness of certain problems guarantees that encryption, digital signatures, and secure communication work as intended. Your online banking is safe because breaking the encryption is (probably) an inherently hard problem.

Fundamental Asymmetry Exists

There really IS a deep difference between creating and checking. Composing a symphony is harder than appreciating one. Writing a proof is harder than verifying one. This asymmetry is built into the fabric of mathematics.

The Creativity Gap is Real

Some tasks require genuine insight that cannot be shortcut by brute computation. Human (and AI) creativity retains its value.

What Proving P ≠ NP Would Mean

A proof of P ≠ NP would: 1. WIN the $1,000,000 Clay Prize 2. CONFIRM that NP-complete problems have no efficient exact algorithms 3. VALIDATE 50+ years of complexity theory built on this assumption 4. REQUIRE fundamentally new math (current techniques are known to be insufficient -- this is called "barrier results")

Why Is It So Hard to Prove?

We'd need to show that no polynomial-time algorithm exists for SAT -- ruling out ALL possible algorithms, including ones nobody has thought of yet. Known proof techniques (diagonalization, relativization, natural proofs) have been shown to be insufficient for this task.

Razborov and Rudich (1997) showed that a large class of "natural" proof strategies cannot resolve P vs NP. We need genuinely new mathematical ideas.

Analogy

Proving P ≠ NP is like proving that no shortcut exists through a maze -- not just that YOU can't find one, but that nobody ever could. You have to rule out every conceivable path.

21 / 23

Summary & Cheat Sheet

Everything you need to know about P vs NP on one slide

COMPLEXITY CHEAT SHEET ====================== P: Solvable in poly time (deterministic TM) Examples: sorting, shortest path, primality, matching NP: Verifiable in poly time (given a certificate) Examples: SAT, TSP, Clique, Subset Sum, Graph Coloring P ⊆ NP: Always true. If you can solve it fast, you can verify it fast. NP-Hard: At least as hard as every NP problem. Every NP problem reduces to it. NP-Complete: NP-Hard AND in NP. The "hardest" problems that are still in NP. Examples: SAT, 3-SAT, Clique, Vertex Cover, TSP, Hamiltonian Cycle P =? NP: OPEN. $1M prize. Most believe P ≠ NP.

How to Prove X is NP-Complete

1. Show X is in NP (describe a verifier).
2. Reduce a known NP-complete problem to X.
That's it! The "known problem" is usually 3-SAT.

RELATIONSHIP MAP (if P ≠ NP) +---------------------------------+ | NP | | +-------+ +----------------+ | | | P | | NP-Complete | | | +-------+ +----------------+ | +---------------------------------+ KEY REDUCTIONS: SAT -> 3-SAT -> Clique -> Vertex Cover -> Subset Sum -> 3-Coloring -> Ham. Cycle -> TSP
IF P = NP: - Crypto breaks, optimization trivial, proofs automated - The world transforms IF P ≠ NP (likely): - Some problems inherently hard - Crypto is safe - Creativity has value - Use approximation, heuristics, special cases in practice

The Big Takeaway

P vs NP is about whether finding solutions is fundamentally harder than checking solutions. It's arguably the most important open question in all of computer science and mathematics. We believe the answer is yes (P ≠ NP), but proving it remains one of humanity's greatest intellectual challenges.

22 / 23

Challenge Quiz

Test your understanding of P vs NP concepts

Q1: Which is NOT in NP?

Q2: If 3-Coloring is solved in O(n5), what follows?

Q3: A ≤P B means:

Q4: Which statement is TRUE?

23 / 23