TactivergeLite

Lite / Independent Engineering Research / 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 · Full interactive version

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.

Predict

Going from 9 cities to 10 multiplies the number of distinct round trips by about…

  1. 1.1×
  2. 2×
  3. 9×
  4. 100×
Guess first, then reveal

Distinct tours are (n − 1)!/2. From 9 to 10 cities that goes from 8!/2 = 20,160 to 9!/2 = 181,440: nine times more. Every extra city multiplies the work again.

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?

What the interactive shows (text version)

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.

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

Watch for: 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.

“P vs. NP and the Computational Complexity Zoo” by hackerdashery (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

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

Intuitive

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.

Technical

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.

Mathematical

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²).

Applied

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 1. Which statement captures membership of NP?

Practice 3. Your greedy scheduler beats random scheduling on every test. What should the report claim?

calls

Practice 5. Which problem has a known polynomial-time algorithm?

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.

Reflect

Name a problem from your own projects that is secretly a search over every combination. What heuristic would you try?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Divide, conquer and count with recurrencesSingle-cycle, multi-cycle and the Iron Law →