Article
Good enough, fast: heuristics
A delivery driver must visit 25 houses and come back to the depot, using as little fuel as possible. Which order should she choose? You would think a fast computer could find the best order at once. It can't. This article shows why, and what people do instead.
A problem and its instances
A problem is a general description of a task. An instance of the problem is the same task with real input filled in. An algorithm solves the problem, so it must work for every instance.
From a problem to an answer
Problem
A general task: find the shortest route.
add the inputInstance
The task with real input: these 4 towns and these road lengths.
run on itAlgorithm
Steps that work for any instance.
Answer
One route, for this instance only.
| Problem | One instance of it |
|---|---|
| Is a number even? | Is 18 even? |
| Sort a list. | Sort [5, 2, 9]. |
| Find the shortest route through some towns. | Find the shortest route through towns A, B, C and D, with these road lengths. |
Two kinds of problem
A decision problem has a yes-or-no answer. An optimization problem asks for the best answer out of many possible answers (the shortest, the cheapest, the largest).
| Decision problem (yes or no) | Optimization problem (the best one) |
|---|---|
| Is 17 a prime number? | What is the largest number in the list? |
| Is there a route through all four towns that is shorter than 22 km? | What is the shortest route through all four towns? |
Often one situation gives you both kinds. If you can solve the optimization problem, the decision problem is easy. Find the shortest route, then compare its length with 22.
Check your understanding
Which of these is an optimization problem?
When checking everything takes too long
Take the driver's problem: visit every town once, come back to the start, and make the trip as short as possible. The exact method is to try every route and keep the shortest. With n towns there are (n − 1) × (n − 2) × … × 1 routes. That is n − 1 factorial, and factorial time is unreasonable.
| Towns | Routes to check | Time at 1 billion routes per second |
|---|---|---|
| 5 | 24 | instant |
| 10 | 362,880 | less than a millisecond |
| 15 | 87,178,291,200 | about 87 seconds |
| 20 | about 1.2 × 10^17 | about 4 years |
| 25 | about 6.2 × 10^23 | about 20 million years |
Five more towns turn seconds into years. A faster computer does not help much, because the number of routes grows far faster than the speed of any computer. No one has found an algorithm that finds the exact shortest route in reasonable time for every instance. Some problems are like this.
A heuristic: good enough, fast
A heuristic is an approach that finds a good solution quickly. It is not guaranteed to be the best one. People use a heuristic when the algorithm that always finds the best solution is impractical (it would take far too long).
Here is a heuristic for the driver, the nearest-city rule: start at the depot. Always drive to the nearest town you have not visited yet. When all towns are visited, go home. For 25 towns this needs only about 25 × 25 comparisons. That is a reasonable time.
Now try it on four towns. These are road lengths in km. The picture is not to scale, so use the numbers.
| A | B | C | D | |
|---|---|---|---|---|
| A | 10 | 4 | 3 | |
| B | 10 | 6 | 7 | |
| C | 4 | 6 | 6 | |
| D | 3 | 7 | 6 |
Start at A. The nearest town is D (3 km). From D, the nearest unvisited town is C (6 km, because B is 7). Then only B is left (6 km). Then go home to A (10 km).
The nearest-city route, 25 km
Is 25 km the best? Check every route. There are only three different routes (a route and its reverse have the same length):
| Route | Length | Total |
|---|---|---|
| A, B, C, D, A | 10 + 6 + 6 + 3 | 25 km |
| A, B, D, C, A | 10 + 7 + 6 + 4 | 27 km |
| A, C, B, D, A | 4 + 6 + 7 + 3 | 20 km |
The best route is 20 km, so the heuristic's answer is 5 km too long. Run it and try your own routes:
Check your understanding
Which statement about a heuristic is true?
Common mistakes
- "A heuristic is a mistake in the program." No. It is a planned trade: a slightly worse answer in return for a fast one.
- "A heuristic always gives a bad answer." Often it gives a very good one, and sometimes the best one. It just has no guarantee.
- "A heuristic finds the exact best answer, only faster." If it did, it would not be a heuristic.
- "A faster computer solves the problem." Factorial growth beats any computer.
- Mixing up the two kinds. "Is there a route under 22 km?" is a decision problem (yes or no). "What is the shortest route?" is an optimization problem.
- Mixing up problem and instance. "Sort a list" is a problem. "Sort
[5, 2, 9]" is an instance.
On the exam
You may be asked to tell a decision problem from an optimization problem, to say why a heuristic is used (the best solution would take an unreasonable amount of time), or to say what a heuristic does not promise (the best answer). Read the question: if it asks for "the best", "shortest" or "largest", it is optimization.
Key words
Problem: a general description of a task that an algorithm could solve.
Instance: a problem together with a particular input.
Decision problem: a problem with a yes-or-no answer.
Optimization problem: a problem that asks for the best answer out of many.
Heuristic: an approach that finds a good solution quickly, but does not guarantee the best one.