P vs NP

Understand the biggest open problem in computer science. Check a route instantly, watch brute force explode, explore the P, NP, NP-complete and NP-hard map, and see a SAT puzzle turn into a Clique.

?

1Finding is the hard part

To find a short round trip you may have to try a huge number of routes. Every extra town multiplies the work.

2Checking is quick

Hand someone one finished route and they can add up its legs in a few steps and say yes or no.

PNP=?

3The big question

Problems that are quick to solve (P) sit inside problems that are quick to check (NP). Is that inner circle the whole circle?

The whole idea

Finding an answer vs checking one

A delivery driver must visit 8 towns and come home, with a budget on the total distance. This is the Travelling Salesman problem, and we'll use it all page. Build a route by hand and the check is instant. Then ask the computer to find the best route and watch how much work that is.

ABCDEFGH

Click the towns in the order you want to visit them. The trip returns to your first town at the end.

The question: is there a round trip through all 8 towns of length 299 or less?

Budget
299
Your route
–
Towns visited
0 / 8

Build a route. The check runs as you click: one step per leg and one per town.

2,520 possible routes with 8 towns

What “fast” means

Polynomial vs explosive growth

Computer scientists call an algorithm fast when its steps grow like n², n³ and so on (polynomial). It's slow when it doubles with every extra item (2ⁿ) or tries every ordering (n!). Drag the slider and watch the gap open up.

10
  • n² (n × n)polynomial: fast100 steps

    On a computer doing a billion steps a second: instant

  • 2ⁿ (2 × 2 × … × 2)grows explosively1,024 steps

    On a computer doing a billion steps a second: instant

  • n! (n × (n−1) × … × 1)grows explosively3,628,800 steps

    On a computer doing a billion steps a second: a blink

Bars use a log scale (each tick is ×10 more), otherwise only the biggest one would be visible.

Back to the delivery route: with 10 towns there are 181,440 different round trips. Checking one takes about 20 steps. Trying them all takes instant.

The map

P, NP, NP-complete and NP-hard

Step through the picture. Each step adds one idea and one new word.

NP-hardHalting problem(can't even be solvedby any algorithm)NPanswers can be checked fastPSortingShortest pathMultiplyingIn-between?factoring?graph iso.?NP-completeRoute ≤ budgetSATCliqueSudokuSubset sumNP-complete problems become solvable fast

Step 1 of 5

NP: answers you can check fast

Everything inside the big circle has one thing in common: if someone hands you an answer, you can check it quickly. Is this route within budget? Does this Sudoku grid follow the rules? That's all NP means. It does not mean "not polynomial", and it doesn't say finding the answer is hard.

Why NP-complete is special

One problem wearing a different costume

A reduction rewrites one problem as another so that the answers match. Here a logic puzzle (SAT) becomes a "find a group of friendly nodes" problem (Clique). That's how Karp showed all those problems were NP-complete: if you can solve the target quickly, you can solve the original quickly too.

Pick a puzzle

Puzzle (SAT)

Can you set each of x, y, z to true or false so that every bracket has at least one true item?

  • (x ∨ y ∨ ¬z)
  • (¬x ∨ y ∨ z)
  • (x ∨ ¬y ∨ z)
The rewrite: make one node for every item in a bracket (colour = which bracket). Join two nodes with a line if they're in different brackets and don't contradict (x and ¬x can't both be true). Now a group of 3 nodes that are all joined to each other (a clique) picks one true item from every bracket: exactly a solution.
xy¬z¬xyzx¬yz

Checking a proposed group is quick (are all 3 nodes joined?). Finding one means searching. That's the NP pattern again, and why this rewrite proves Clique is as hard as SAT.

Watch out

Common mix-ups

These are the ones that trip people up most.

“NP means not polynomial”

It stands for nondeterministic polynomial time. NP is the problems whose answers can be checked quickly, and that includes every problem in P.

“Every NP problem is hard”

Sorting is in NP (and in P). NP only promises that checking is quick. Hardness belongs to the NP-complete corner.

“Hard in theory means hopeless in practice”

NP-complete is a worst-case label. Route planners and SAT solvers routinely handle huge real inputs, or settle for a good-enough answer such as the nearest-town shortcut.

“The shortest route is in NP”

Only the yes/no question “is there a route within budget?” is. “Find the shortest route” is NP-hard but its answer can't be checked fast: you'd have to know no shorter one exists.

“Quantum computers solve NP”

Not as far as anyone knows. They speed up some problems (Shor's algorithm factors numbers), but no quantum algorithm is known to solve NP-complete problems quickly.

“Most experts think P = NP”

The opposite: in Gasarch's 2019 poll, 88% of respondents expected P ≠ NP. A belief isn't a proof, which is why it's still an open prize problem.

Keep going

Resources

Hand-picked places to go deeper. They're all free to read or watch, and they open in a new tab.