When the perfect answer would take longer than the universe has, you take a good answer now. Build a route by hand, run a real heuristic on it, and see exactly what you gave up for the speed.
No reading — this page is the reading. Runestone doesn’t cover Unit 4 (search, efficiency, limits, simulation), so everything you need is right here.
Programs, Algorithms region. Yesterday: some problems take unreasonable time to solve exactly. Today: what real software does about it — because your phone answers those problems every day, in seconds.
Honest answer to the second question: probably close. Not guaranteed.
When the exact answer takes unreasonable time, you can trade the guarantee for speed. A heuristic is an approach that finds a good-enough answer quickly, without promising it’s the best. The answer it gives is an approximate solution.
A classic heuristic for the route problem is nearest neighbor: from wherever you are, go to the closest city you haven’t visited yet. When none are left, go home.
route = [start] while len(unvisited) > 0: i = closest_index(route[-1], unvisited) route.append(unvisited[i]) unvisited.pop(i) route.append(start) # go home
route ← [start] REPEAT UNTIL (LENGTH(unvisited) = 0) { i ← closestIndex(route[LENGTH(route)], unvisited) APPEND(route, unvisited[i]) REMOVE(unvisited, i) } APPEND(route, start)
Hover or tap a line to light its twin. closest_index is 3.9’s min
algorithm over distances — nothing new. Note the last-element flip on line 3: Python’s
route[-1] is AP’s route[LENGTH(route)].
Start at A. Click cities to build your own round trip, then run nearest neighbor and compare. Last, reveal the true best route — found by checking all 120 orderings.
Five questions. The key idea every time: what is being traded for what?
A company gets 5,000 applications for 10 jobs. Nobody reads them all. Software ranks them with a heuristic — keywords, school names, gaps in work history — and a human reads the top 100. Fast. Good enough, on average.
But “good enough on average” means some strong candidates get filtered out, and the heuristic decides which ones. A route that’s 12% too long costs gas. A résumé screen that’s 12% wrong costs people jobs. Unit 8 comes back to this.