School/✦ Research Engineer/Week 19
When exact answers are out of reach
A delivery van must visit 20 shops. Trying every route would take a fast computer about two years. How do real route planners answer in a second?
About 45 min · 3 goals · Checked by automated tests
1.Why should I care?
Scheduling, routing, chip placement, packing and many planning problems are NP-hard. Engineers still solve them every day, by giving up a guarantee of optimality in exchange for speed, and by measuring how much they gave up.
- ◇ Explain the difference between polynomial-time (tractable) and exponential-time problems.
- ◇ Describe P, NP and NP-hard informally, using the idea that checking can be easier than finding.
- ◇ Justify using a heuristic or approximation algorithm and measure its gap from the optimum on small cases.
2.Predict
Going from 9 cities to 10 multiplies the number of distinct round trips by about…
Guess first. Being wrong here is how the surprise works.
3.Discover
Start at 4 cities and add one at a time up to 9, watching 'tours tried'. On several random maps, compare the greedy nearest-neighbour route with the exact best. How far off is greedy at its worst?
Loading the interactive…
Can't use the interactive? Read a description instead
The explorer draws 4 to 9 random cities with two routes: the exact best tour (found by trying all (n−1)!/2 tours) and the greedy nearest-neighbour tour. A table reports tours tried, both lengths and the percentage gap; at 20 cities brute force would need about 6×10¹⁶ tours.
4.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
Which statement captures membership of NP?
5.Watch
→ Listen for the difference between finding a solution and checking one: that gap is the whole P versus NP question.
A brisk tour of what P and NP mean, why checking an answer can be easy when finding it seems hard, and why it matters to real engineering.
Length: see YouTube · captions on YouTube · source
Nothing loads from YouTube until you press play.
The video is optional: everything you need is also in the lesson text and the interactive.
6.Understand
Tractable means polynomial
A problem is usually called tractable if some algorithm solves it in O(n^k) time for a fixed k. Sorting, shortest paths (Dijkstra) and linear systems are tractable. Problems whose best known algorithms take 2ⁿ or n! time become impossible to solve exactly beyond a few dozen items, whatever the hardware.
P, NP and NP-hard, informally
P: problems solvable in polynomial time. NP: problems where a proposed answer can be checked in polynomial time (is this route shorter than 500 km? add up its legs). NP-hard: at least as hard as every NP problem; a fast algorithm for one would give fast algorithms for all. Nobody has proved P ≠ NP, but no one has found a polynomial algorithm for an NP-hard problem either, so engineers plan as if none exists.
Heuristics and approximations
A heuristic (nearest neighbour, greedy packing, simulated annealing) is fast but has no promise about quality. An approximation algorithm comes with a proof: for travelling-salesperson maps that obey the triangle inequality, Christofides' algorithm is never worse than 1.5× optimal. Either way, measure the gap on cases small enough to solve exactly, as the explorer does.
Explain it another way
Checking that a jigsaw is finished takes a glance. Finishing it takes an afternoon. Many engineering problems look like jigsaws: easy to check, hard to find.
Decision problems are classified by resource bounds. NP-complete problems (SAT, Hamiltonian cycle, subset sum, decision-TSP) are in NP and NP-hard; they reduce to each other in polynomial time. Optimisation versions are NP-hard. Practical tools: branch-and-bound, integer programming solvers, local search and problem-specific heuristics.
Exact TSP by brute force costs (n − 1)!/2 tour evaluations, each Θ(n). By Stirling, n! ≈ √(2πn)(n/e)ⁿ, which outgrows any cⁿ. Held–Karp dynamic programming does better, Θ(n²·2ⁿ), but is still exponential. Nearest neighbour is Θ(n²).
Chip design tools place millions of standard cells and route their wires: NP-hard problems at enormous scale. They use heuristics such as simulated annealing and analytic placement, and accept good rather than optimal layouts.
Worked example 1
How many distinct round trips through 12 cities, ignoring direction?
(12 − 1)!/2 = 39,916,800/2 = 19,958,400. At a million tours a second that is 20 s; at 15 cities it is 43.6 billion tours, about half a day.
Worked example 2
A greedy route is 112 km and the exact best is 100 km. Report the heuristic's quality.
Gap = (112 − 100)/100 = 12%, ratio 1.12. One map is one data point: report the worst and average gap over many maps before trusting it.
Common mix-ups
- NP means 'not polynomial'.
✓ It means 'nondeterministic polynomial': solutions are checkable in polynomial time. P is inside NP. - If a problem is NP-hard, engineers cannot solve it.
✓ They solve large instances well with heuristics, approximations and solvers, and small ones exactly.
7.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2
How many distinct round trips (direction ignored) through 8 cities?
Practice 3
Your greedy scheduler beats random scheduling on every test. What should the report claim?
Practice 4 · a fresh version every time
How many times is work() called when n = 270? Code: for i in 0 … n−1: for j in i+1 … n−1: work()
Practice 5
Which problem has a known polynomial-time algorithm?
8.Apply it
In the explorer, record the greedy-to-optimal ratio on 20 random maps of 8 cities. Report the mean, the worst case and a histogram. Then propose one improvement to nearest neighbour (for example, 2-opt: reverse a segment if it shortens the tour) and predict whether it narrows the gap.
9.Reflect
Concepts: Algorithms, searching and complexity · Path planning and search · Optimisation and gradient descent