AP® Computer Science Principles · Big Idea 3 · Topic 3.17

Heuristics and Good-Enough Answers

A heuristic is a rule-of-thumb strategy that finds a good solution quickly without guaranteeing the best one. It trades a little accuracy for a lot of speed on problems too big to solve exhaustively.

Some problems, like finding the shortest route through many cities, have so many possible orderings that checking every single one would take longer than anyone could wait, even with a fast computer. A heuristic sidesteps this by using a simple, consistent rule to build a solution directly, instead of comparing every possibility. The nearest neighbor heuristic is one example: start at a city, then repeatedly travel to the closest unvisited city, until all cities are visited and you return home. It never looks ahead or reconsiders, it just keeps picking the nearest option at each step.

This produces a route fast, but not always the shortest one. Picking the closest city right now can lock you into a bad position later, forcing a long jump near the end that a smarter global plan would have avoided. Still, the result is usually close to optimal, and for many real situations close enough is exactly what you need, especially when the alternative is waiting an unreasonable amount of time for a guaranteed best answer.

This topic connects directly to the idea of undecidable and intractable problems elsewhere in the unit: heuristics are the practical response to problems where an efficient exact algorithm may not exist. Rather than abandoning the problem, programmers accept a provably reasonable approximation. Choosing a heuristic becomes a tradeoff between speed, simplicity, and how much error is tolerable, which matters a lot more when a small percentage off is costly than when it barely matters.

Key terms

heuristic
A rule-of-thumb strategy that finds a good, fast solution without guaranteeing the absolute best one.
nearest neighbor heuristic
A route-building rule that always travels next to the closest unvisited location.
optimal solution
The single best possible answer to a problem, such as the truly shortest route.
intractable problem
A problem whose exact solution takes an impractically long time to compute as its size grows.

Watch it explained

  1. How it worksHow does a nearest neighbor heuristic build a decent route without checking them all?
  2. Worked exampleTrace the nearest neighbor heuristic over six cities and compare it with the true best route.
  3. Worked exampleFind the total distance of the nearest neighbor route through four cities set at the corners of a 3 mile by 4 mile rectangle.
  4. Common mistakeWhy isn't a heuristic answer just a guess?
  5. Going furtherWhat changes about the choice of heuristic when being 10 percent off is unacceptable?

Check yourself

Your turn1 of 2
Find the total distance of a nearest-neighbor route through four cities set at the corners of a 14 mile by 13 mile rectangle, traveling along all four sides to return to the start.

End of topic.