The short answer
In computer science, a heuristic is a method that aims to find a good-enough solution quickly, rather than a provably best one. It works from a rule of thumb, an estimate or a shortcut. Heuristics are used when an exact method would take too long, or when the problem is too large to search completely. The price is that a heuristic usually cannot promise that its answer is the best one.
Why programs use heuristics
Many problems have an exact solution that can be found in principle. The difficulty is time. Take the traveling salesman problem: find the shortest route that visits every city on a list and returns to the start. With a few cities, you can try every order. With dozens, the number of orders is so large that checking them all is impractical, and no fast exact method is known that works for every case. Problems like this are part of the topic covered in undecidable problems and unreasonable time algorithms.
A heuristic offers a practical alternative. A delivery planner could simply drive to the nearest stop it has not visited yet. That takes moments, and the route is usually reasonable, though it is not guaranteed to be the shortest.
How heuristics differ from exact algorithms
An exact algorithm gives the correct or optimal answer for every valid input. A heuristic gives up some of that certainty in exchange for speed, lower memory use or simpler code. Depending on the method, it may give up:
- Optimality: the answer may not be the best possible one.
- Completeness: the method may fail to find an answer even when one exists.
- Accuracy or precision: the answer may be an approximation.
The exact alternative to a heuristic is an exhaustive search, sometimes called brute force, which tries every option. For more on that approach, see brute force algorithm examples.
The main kinds of heuristics
Greedy methods make the choice that looks best at each step and never reconsider it. They are fast and simple, but an early choice can rule out a better overall result.
Local search starts with a complete solution and makes small changes that improve it. It can get stuck at a local optimum: an answer that no small change can improve, even though a better one exists elsewhere.
Metaheuristics are general strategies that guide a search so it can avoid getting stuck. Simulated annealing sometimes accepts a worse move, and the chance of doing so shrinks over time. Genetic algorithms keep a population of candidate solutions and combine and vary them.
Heuristic functions are estimates that guide a search. In A* search, the estimate is the guessed remaining cost to reach the goal.
Examples
Making change with few coins
Suppose you need to give 6 units of change using coins worth 1, 3 and 4. The greedy rule takes the largest coin that still fits: 4, then 1, then 1. That is three coins. Two coins of 3 would do it. For ordinary coin sets, such as US coins, the greedy rule does give the fewest coins, but there is no general guarantee that it will.
Finding a shortest path
A* search finds a shortest path on a map by using a heuristic estimate of the distance remaining to the goal. If that estimate never overestimates the true remaining distance, the estimate is called admissible, and A* still returns a shortest path. On a grid where you can move only up, down, left or right, the Manhattan distance is one such estimate. So a heuristic can make a search faster without giving up the correct answer.
Visiting stops in order
The nearest-neighbor method for routes always moves to the closest stop not yet visited. It runs quickly, but the total route can be noticeably longer than the best possible one.
Where heuristics appear
- Routing and logistics: planning delivery or service routes, where exact optimization is too slow for the number of stops.
- Scheduling: assigning jobs or staff to time slots when the number of combinations is large.
- Game-playing programs: a chess program cannot search every line to the end of the game, so it scores positions with a heuristic evaluation where its search stops.
- Caching: the least recently used rule removes the item that has gone longest without use, on the guess that recent use predicts future use. The best possible rule would need to know future requests, which a cache cannot know.
- Security software: antivirus programs use heuristic scanning to flag suspicious behavior that matches no known signature.
The trade-offs
Heuristics are useful, but they bring risks. The result may be worse than the best possible one, and the size of the gap may be hard to measure. A heuristic tuned for one kind of input can perform poorly on another. The no free lunch theorems, published by David Wolpert and William Macready in the 1990s, show that when averaged over all possible problems, no search method does better than any other. In practice, a heuristic’s value depends on whether it suits the problems you actually have. For a closer look at when the trade is worth it, see are heuristics good or bad.
Key takeaways
- A heuristic finds a good-enough answer quickly, without a guarantee that it is the best.
- Heuristics are used when exact methods are too slow, which is common for large or hard problems.
- The main kinds are greedy methods, local search, metaheuristics and heuristic functions.
- Examples include change-making, A* search, nearest-neighbor routes and chess evaluation.
- A heuristic’s value depends on the problems it is applied to, so test it on realistic inputs.