
Easy and Hard Problems
Some problems are solvable but take longer than the universe has. P, the problems solvable in polynomial time, from sorting to shortest routes to primality; NP, the problems whose answers are easy to check, like Clay's dormitory of 400 students, big Sudokus, and the travelling salesman; NP-completeness, from Cook's 1971 paper and Karp's 21 problems; why P against NP decides the fate of internet encryption and carries a million-dollar prize; and what people actually do with hard problems: a proved-optimal walk through 81,998 Korean bars, Christofides' guarantee, SAT solvers, and the one inside your Linux package manager. Ends with a brute-force tour in Python hitting the wall.
- 12 min
- 8 steps
- 8 questions
- 7 hands-on
- Lesson 24 of 24 in How Computers Work
- Drafted by Opus 5.5
In this lesson
- Easy problems
- Easy to check
- The hardest problems in NP
- Why it matters
- What people do instead
- Try it on Linux
- The question in its own words
- Beat brute force
Picking up where you left off.
Easy problems
The halting problem has no algorithm at all. Most problems people care about do have one; the question is how long it takes. Complexity theorists call a problem feasible if some algorithm solves it in a number of steps bounded by a polynomial in the size of the input, n², n³, or the like: the Cobham–Edmonds thesis 1. Those problems form the class P 2.
You have met several. Sorting takes about n log n comparisons (Sorting); binary search, about log n (Searching and Big-O). Finding the route with the fewest steps between two places in a graph takes breadth-first search, from Trees and Graphs, O(n + m) 1 2 3. Deciding whether a number is prime joined P only in 2004, with the AKS test of Agrawal, Kayal, and Saxena 1. Against those, an algorithm that tries every subset of n things takes 2ⁿ steps, and one that tries every order takes n!:
| n | n³ | 2ⁿ | n! |
|---|---|---|---|
| 10 | 1,000 | 1,024 | 3,628,800 |
| 20 | 8,000 | 1,048,576 | 2.4 × 10¹⁸ |
| 50 | 125,000 | 1.1 × 10¹⁵ | 3.0 × 10⁶⁴ |
At a billion steps a second, 50³ is over in a blink, 2⁵⁰ takes about 13 days, and 50! takes about 10⁴⁸ years. Polynomial against exponential is the line between feasible and not.
Easy to check
Some problems have answers that are easy to check but seem hard to find. The Clay Mathematics Institute’s example: 400 students apply for 100 dormitory places, and the dean supplies a list of pairs who must not both be chosen. Given a proposed list of 100, checking it against the dean’s pairs is quick. Finding one from scratch means searching among more ways to choose 100 of 400 than there are atoms in the known universe 4.
That is the class NP: problems whose yes-answers can be checked in polynomial time, given the answer 2. Sudoku is one. A filled grid is checked in moments, but deciding whether a large n² × n² Sudoku has any solution at all is as hard as anything in NP, proved by Takayuki Yato and Takahiro Seta in 2003 5. So is the travelling salesman in its yes-or-no form: is there a round trip through these cities shorter than a given length? Checking a proposed tour is just adding up its legs 1. Finding the best one by brute force means trying (n − 1)! orders.
Every problem in P is in NP: if you can find an answer quickly, you can check one quickly. Whether the reverse holds is the open question.
Work it out
This exercise needs JavaScript.
7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5,040. Each extra city multiplies the count by the number of cities already there.
The hardest problems in NP
In May 1971 Stephen Cook, at the University of Toronto, showed that every problem a nondeterministic machine solves in polynomial time can be reduced to one problem in logic: deciding whether a formula of propositional logic is a tautology, the mirror image of satisfiability 6. “Reduced” had a precise meaning: if tautologies could be recognized instantly, by an oracle, then the other problem could be solved in polynomial time 6. A second problem, finding one graph inside another, turned out equally hard. “It is fruitless,” he wrote, “to search for a polynomial decision procedure for the subgraph problem, since success would bring polynomial decision procedures to many other apparently intractable problems” 6.

The next year Richard Karp showed 20 more problems in the same position, among them classic problems of covering, packing, routing, assignment, and sequencing: either all of them have polynomial algorithms or none does 7 2. Karp also named the classes P and NP. Leonid Levin, writing in Russian, reached the same idea independently 2 4.
Such problems are NP-complete: they are in NP, and every problem in NP reduces to them, so a fast algorithm for any one would give fast algorithms for all 1. Satisfiability, the travelling salesman, and integer programming are all NP-complete. They turn up wherever many requirements must hold at once: scheduling the sessions of a conference, laying out a circuit board, planning deliveries 1.
Some problems sit in between. Factoring and telling whether two graphs are the same are in NP but not known to be either in P or NP-complete 2.
Sort
This exercise needs JavaScript.
The first three have polynomial-time algorithms, primality only since 2004. The rest are NP-complete: a fast method for any one of them would give fast methods for all of NP.
Match up
This exercise needs JavaScript.
Cook’s paper started it, Karp showed how far it reached and named P and NP, Levin reached it independently, and Clay put P against NP among the Millennium Problems in 2000.
Fill the blanks
This exercise needs JavaScript.
Every problem in P is in NP, since finding an answer quickly means checking one quickly. NP-complete problems are the hardest in NP: a fast method for one gives fast methods for all.
Why it matters
Nobody knows whether P = NP. The Clay Mathematics Institute made it one of seven Millennium Prize Problems, announced in Paris on 24 May 2000, each worth $1 million 8.
The security of the internet, including most financial transactions, rests on assumptions such as the difficulty of factoring large numbers; RSA encryption falls if factoring is easy. If P = NP, those assumptions are all false: in Cook’s example, an algorithm solving 3-SAT in n² steps could factor 200-digit numbers in a few minutes 2. The logic doesn’t run backwards, though. Factoring isn’t believed to be NP-complete, so even a proof that P ≠ NP wouldn’t prove factoring hard 2.
A practical P = NP would have a bright side: since formal proofs can be checked quickly, a computer could find a proof of any theorem that has a reasonably short one. Cook suggests the same of designing aircraft wings, building physical theories, and composing music, wherever a good result can be recognized efficiently 2. Meanwhile the best proven bound for 3-SAT is about 1.5ⁿ steps 2.
Quick check
Internet security rests on assumptions like the hardness of factoring, which a fast 3-SAT algorithm would break. Sorting is already easy, and the halting problem lies outside NP altogether.
What people do instead
NP-complete describes the worst case. Real instances are often much kinder, and people solve them anyway.
Exact answers on real maps. Newspapers have claimed a laptop would need 1,000 years for the best route between 22 cities, which is true only of brute force. Between December 2024 and March 2025, William Cook’s team at Waterloo and Keld Helsgaun at Roskilde University in Denmark found the shortest walking tour through 81,998 bars in South Korea, 178 days of walking, and proved that no order of stops saves a single second. They paired Helsgaun’s LKH program, which finds very good tours, with the Concorde program, whose linear-programming bounds prove there is nothing better 9.
Guarantees. In 1976 Nicos Christofides gave a method that runs in about n³ steps and always returns a tour less than 1.5 times the shortest, whenever the distances obey the triangle inequality, so that no detour is shorter than going direct 10.
SAT solvers. Cook’s paper already mentioned the Davis–Putnam procedure for satisfiability 6. Modern solvers settle real mathematics: in 2016 one showed that the numbers 1 to 7,824 can be split into two groups with no Pythagorean triple a² + b² = c² inside either group, but 1 to 7,825 cannot. It took 800 processor cores about two days and left a proof of almost 200 terabytes 11.
Your package manager. Whether a package can be installed alongside everything it depends on, and nothing it conflicts with, is a satisfiability question, and in general NP-complete 12 13. A 2006 study turned the whole Debian archive, 34,701 packages and almost 200,000 relationships, into formulas and found 123 packages that could never be installed; its checker covered the archive in 43 seconds 12. The libsolv library, which handles deb packages among others, calls itself “a package dependency solver using a satisfiability algorithm” 14. apt and Debian Packages showed the solving from the outside.
Select all that apply
This exercise needs JavaScript.
Approximation, solvers that exploit the structure of real instances, and heuristics all work in practice. Brute force grows faster than any hardware, and the halting problem is a limit, not a tool.
Find the mistake
This exercise needs JavaScript.
1971 is the year of Cook’s paper; Yato and Seta’s Sudoku proof came in 2003. The Debian checker ran through 34,701 packages in 43 seconds.
Try it on Linux
Brute force on random cities, timed. Save as tsp.py:
# tsp.py: find the shortest round trip by trying every order. Watch one more city multiply the time.
import itertools, math, random, time
random.seed(1)
cities = [(random.random(), random.random()) for _ in range(12)]
def length(tour):
return sum(math.dist(cities[a], cities[b]) for a, b in zip(tour, tour[1:] + tour[:1]))
for n in range(7, 12):
start = time.perf_counter()
rest = range(1, n) # fix city 0 as the start; try every order of the others
best = min(((0,) + p for p in itertools.permutations(rest)), key=length)
print(f"{n:>2} cities {math.factorial(n - 1):>10,} tours best {length(best):.3f} "
f"{time.perf_counter() - start:8.2f} s")
me@linuxbox:~$ python3 tsp.py
7 cities 720 tours best 3.154 0.00 s
8 cities 5,040 tours best 3.186 0.00 s
9 cities 40,320 tours best 3.295 0.04 s
10 cities 362,880 tours best 3.448 0.37 s
11 cities 3,628,800 tours best 3.487 3.89 s
Each city multiplies the time by about ten here, and the factor keeps growing. Twelve cities would take about 43 seconds, fifteen about a day, twenty some 4,000 years. The doubling test from Searching and Big-O has nothing to double: this is worse than 2ⁿ.
Work it out
This exercise needs JavaScript.
3.89 × 11 ≈ 43 seconds. Fifteen cities would take about a day, and twenty thousands of years.
The question in its own words
- Cook’s 1971 paper is eight pages, scanned on his University of Toronto page; the discussion on page 154 needs no notation 6.
- Cook’s official problem description for the Clay Institute explains P, NP, and the cryptography stakes in twelve pages 2.
- The Waterloo TSP pages map the Korean bar crawl stop by stop, city by city, with an interactive map and an account of the computation 9.
Beat brute force
Add a nearest-neighbour tour to tsp.py: start at city 0, always walk to the closest unvisited city, then return. Compare its length with the brute-force best for 7 to 11 cities and note the worst gap. Then run it on all 12 cities, and on 1,000 random ones, where brute force could never go. Finally, run apt-get -s install for a large package such as gimp and read how many other packages the solver pulls in to satisfy it.
Lesson complete
Nice work.
Teach it back
In two sentences, explain Easy and Hard Problems to a friend who missed it.
Related lessons
Among the starsSources for this lesson
- 1Computational Complexity Theory. Stanford Encyclopedia of Philosophy. 2016. verifiedSubstantive revision July 20, 2016; read November 15, 2026. PRIMES was shown in 2004 to have a polynomial-time decision algorithm (the AKS test of Agrawal, Kayal and Saxena). The Cobham-Edmonds thesis identifies feasible computation with polynomial time. SAT is NP-complete (Cook and Levin, independently), which opened the door to showing many others NP-complete by reduction; TSP (is there a tour within budget b?) and INTEGER PROGRAMMING are NP-complete; such problems arise in scheduling, circuit design, logistics and planning; many NP-complete optimization problems have polynomial-time approximation algorithms.
- 2The P versus NP Problem (official problem description). Clay Mathematics Institute. verifiedRead November 15, 2026. P is the class of decision problems solvable within a number of steps bounded by a polynomial in the input length; NP those accepted by a nondeterministic algorithm in polynomial time. In 1971 Cook showed satisfiability, 3-SAT, and subgraph isomorphism NP-complete; a year later Karp used this to show 20 other natural problems NP-complete (21 in his paper) and introduced the notation P and NP; Levin, independently, defined 'universal search problems' with six examples. Graph isomorphism is an NP problem not known to be in P or NP-complete; composite numbers are now known to be in P, but factoring is thought unlikely to be in P and unlikely to be NP-complete; an efficient factoring algorithm would break RSA. If P = NP the complexity assumptions behind internet security are false; a 3-SAT algorithm running in n^2 steps could factor 200-digit numbers in minutes; it would also let computers find formal proofs of any theorem with a reasonably short proof. The best proven bound for 3-SAT is about 1.5^n.
- 3Pat Morin. Open Data Structures (in pseudocode), Edition 0.1G beta. opendatastructures.org (paperback by AU Press). verifiedFree textbook under a Creative Commons Attribution license. Used: ch. 1, the need for efficiency (a million searches of a million items is 10^12 inspections, over 16 minutes at a billion operations a second), interfaces versus implementations, the Queue (FIFO), Stack (LIFO), and Deque; ch. 2, array-based lists (constant-time get and set, add and remove at i costing O(n - i) for the shifting, growing by allocating an array of twice the size and copying, amortized O(1) growth); ch. 3, singly and doubly linked lists (nodes with references; get(i) walks from the head; insert or delete next to a known node in constant time); ch. 5, hash tables with chaining (an array of lists, hash(x) choosing the list, n kept at most the table length so lists average one item, doubling and reinserting when full) and hash codes (equal objects must have equal hash codes, unequal ones should rarely collide); ch. 6, binary trees (root, parent, child, leaf, depth, height) and the binary search tree property, O(n) worst case when unbalanced; ch. 7, random binary search trees, with search paths of length at most about 2 ln n; ch. 12, graphs (vertices, directed edges, paths, cycles; adjacency matrix with O(n^2) space versus adjacency lists with O(n + m); breadth-first search with a queue and a seen array, O(n + m), visiting vertices in order of distance and giving shortest paths; depth-first search, breadth-first search with a stack instead of a queue, used to detect cycles); ch. 14, B-trees, whose nodes have between B and 2B children and fit in one external-memory block, the primary data structure in file systems including ext4, NTFS, and HFS+, every major database, and cloud key-value stores.
Also cited in Measuring Real Programs, Searching and Big-O, Sorting and 3 more.
- 4P vs NP. Clay Mathematics Institute. verifiedRead November 15, 2026. Example: housing 100 of 400 students with a Dean's list of incompatible pairs; a proposed choice is easy to check, but the number of ways to choose 100 from 400 exceeds the number of atoms in the known universe, so brute force is hopeless; no one has proved such problems really are that hard. Stephen Cook and Leonid Levin formulated the P versus NP problem independently in 1971.
- 5A Survey of NP-Complete Puzzles. ICGA Journal 31(1): 13-34. 2008. verifiedRead November 15, 2026 in the authors' PDF. Surveys 24 single-player puzzles proved NP-complete. Sudoku (Number Place) is played on an n^2 x n^2 grid divided into n x n blocks, each row, column and block to hold 1 to n^2 once; deciding whether a Sudoku has any solution was shown NP-complete by Yato and Seta (2003), as was completing partly filled Latin squares (Colbourn 1984).
- 6The Complexity of Theorem-Proving Procedures. Proceedings of the Third Annual ACM Symposium on Theory of Computing (STOC '71), pp. 151-158. 1971. verifiedRead November 15, 2026 in the scan on Cook's University of Toronto page (pp. 151-154 read). Any recognition problem solved in polynomial time by a nondeterministic Turing machine can be 'reduced' to deciding whether a propositional formula is a tautology (Theorem 1, DNF tautologies); the subgraph problem (is one graph isomorphic to a subgraph of another) has the same polynomial degree (Theorem 2); graph isomorphism and primes are also discussed. P. 154: it is 'fruitless to search for a polynomial decision procedure for the subgraph problem, since success would bring polynomial decision procedures to many other apparently intractable problems'; proving tautologies not in polynomial time 'would be a major breakthrough in complexity theory'; mentions the Davis-Putnam procedure for satisfiability. Cited at: pp. 151–152; p. 151; p. 154.
- 7Reducibility Among Combinatorial Problems. In R. E. Miller and J. W. Thatcher (eds.), Complexity of Computer Computations, Plenum Press, pp. 85-103. 1972. verifiedAbstract read on the publisher's page November 15, 2026 (full text not read): a problem is satisfactorily solved when an algorithm terminates within a number of steps bounded by a polynomial in the input length; Karp shows that a large number of classic unsolved problems of covering, matching, packing, routing, assignment and sequencing are equivalent: either each has a polynomial-bounded algorithm or none does.
- 8The Millennium Prize Problems. Clay Mathematics Institute. verifiedRead November 15, 2026. The prizes were announced in Paris on May 24, 2000, at the Collège de France; the board designated a $7 million fund, $1 million for the solution of each of the seven problems.
- 9Shortest-possible walking tour to 81,998 bars in South Korea. University of Waterloo (Traveling Salesman Problem pages). 2025. verifiedRead November 15, 2026. A TSP through 81,998 bars, with 3,361,795,003 walking times from OSRM, solved with a tour and a proof that no shorter one exists: 15,386,177 seconds, about 178 days; the largest road-map TSP solved to provable optimality, beating the 57,912-stop Netherlands tour of February 2021; computed December 2024 to March 2025 at Roskilde University (Keld Helsgaun, Denmark) and the University of Waterloo, combining the LKH heuristic code with the Concorde cutting-plane code (linear programming). Quotes press claims such as a laptop needing 1,000 years for 22 cities (Washington Post, 2018), which assume checking every tour; the Korean instance has about 2 followed by 367,308 zeros tours.
- 10Worst-Case Analysis of a New Heuristic for the Travelling Salesman Problem. Operations Research Forum 3, article 20 (2022); technical report of 1976. 2022. verifiedOpen access (CC BY 4.0), read November 15, 2026. Written as a technical report during a 1976 sabbatical at Carnegie Mellon, never formally published until this edition. An O(n^3) heuristic for TSPs whose costs obey the triangle inequality, using a shortest spanning tree and a minimum-cost perfect matching, returns a tour strictly less than 3/2 times the optimum, against the previous best ratio of 2.
- 11Solving and Verifying the boolean Pythagorean Triples problem via Cube-and-Conquer. arXiv:1605.00723 (SAT 2016). 2016. verifiedRead November 15, 2026. Can the natural numbers be split into two parts with no Pythagorean triple a^2 + b^2 = c^2 in either part? Theorem 1: {1, ..., 7824} can be, {1, ..., 7825} cannot. Solved with the Cube-and-Conquer SAT method on a cluster of 800 cores in about 2 days; the DRAT proof is almost 200 terabytes, with a 68-gigabyte compressed certificate; Ronald Graham had offered a prize.
- 12Managing the Complexity of Large Free and Open Source Package-Based Software Distributions. ASE '06, 21st IEEE/ACM International Conference on Automated Software Engineering, pp. 199-208. 2006. verifiedRead November 15, 2026 in the HAL copy. Whether a package can be installed is encoded as boolean satisfiability: each package version is a variable, dependencies and conflicts become formulas, and the package is installable if and only if the formula is satisfiable; SAT solving took under 1 second on the hardest installability problems. The Debian pool snapshot had 34,701 packages and almost 200,000 relationships; 123 packages could not be installed, 12 of them because dependencies forced a conflict; the standalone debcheck tool checked the whole pool in 43 seconds.
- 13Towards better tools for the analysis and quality assurance of FOSS distributions (seminar slides). Mancoosi project, Université Paris Diderot. 2011. verifiedApril 26, 2011, read November 15, 2026. Package p is installable if p and the dependency and conflict formulas are satisfiable together; 'A result from EDOS [ASE 2006]: Installability of packages (measured in the number of packages) is NP-complete.'
- 14libsolv README. openSUSE (GitHub). verifiedRead November 15, 2026. 'libsolv, a free package dependency solver using a satisfiability algorithm'; resolves package dependencies using satisfiability; supports rpm, deb, Arch Linux, Haiku and apk packages.