Unit 4: Algorithms · Solving hard problems

Article

Good enough, fast: heuristics

8 min read

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

  1. Problem

    A general task: find the shortest route.

    add the input
  2. Instance

    The task with real input: these 4 towns and these road lengths.

    run on it
  3. Algorithm

    Steps that work for any instance.

  4. Answer

    One route, for this instance only.

One problem has endless instances. An algorithm must handle all of them.
ProblemOne 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.

TownsRoutes to checkTime at 1 billion routes per second
524instant
10362,880less than a millisecond
1587,178,291,200about 87 seconds
20about 1.2 × 10^17about 4 years
25about 6.2 × 10^23about 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.

ABCD
A1043
B1067
C466
D376

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

A (start)BCD
3 + 6 + 6 + 10 = 25 km. The first greedy choices left a long trip home.

Is 25 km the best? Check every route. There are only three different routes (a route and its reverse have the same length):

RouteLengthTotal
A, B, C, D, A10 + 6 + 6 + 325 km
A, B, D, C, A10 + 7 + 6 + 427 km
A, C, B, D, A4 + 6 + 7 + 320 km

The best route is 20 km, so the heuristic's answer is 5 km too long. Run it and try your own routes:

Runs in your browser

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.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.