115

What is the difference between a heuristic and an algorithm?

3

12 Answers 12

115

An algorithm is the description of an automated solution to a problem. What the algorithm does is precisely defined. The solution could or could not be the best possible one but you know from the start what kind of result you will get. You implement the algorithm using some programming language to get (a part of) a program.

Now, some problems are hard and you may not be able to get an acceptable solution in an acceptable time. In such cases you often can get a not too bad solution much faster, by applying some arbitrary choices (educated guesses): that's a heuristic.

A heuristic is still a kind of an algorithm, but one that will not explore all possible states of the problem, or will begin by exploring the most likely ones.

Typical examples are from games. When writing a chess game program you could imagine trying every possible move at some depth level and applying some evaluation function to the board. A heuristic would exclude full branches that begin with obviously bad moves.

In some cases you're not searching for the best solution, but for any solution fitting some constraint. A good heuristic would help to find a solution in a short time, but may also fail to find any if the only solutions are in the states it chose not to try.

4
  • 3
    Another common use for heuristics is in virus detection, where you might not be sure a virus is there, but you can look for specific key attributes of a virus.
    – TWA
    Mar 17, 2010 at 15:59
  • Heah thats true and for cracking programms Mar 17, 2010 at 16:06
  • 1
    @kriss, So.. a heuristic is a kind of algorithm.
    – Pacerier
    Jun 2, 2016 at 22:30
  • 1
    @Pacerier: yes. It's an algorithm helping to navigate in the solution space of a particular problem. You can also see it as a strategy to modify an algorithm to make it practical (a meta-algorithm). It's still an algorithm, all methods are, and a Heuristic is definitely a method.
    – kriss
    Jun 3, 2016 at 9:30
35
  • An algorithm is typically deterministic and proven to yield an optimal result
  • A heuristic has no proof of correctness, often involves random elements, and may not yield optimal results.

Many problems for which no efficient algorithm to find an optimal solution is known have heuristic approaches that yield near-optimal results very quickly.

There are some overlaps: "genetic algorithms" is an accepted term, but strictly speaking, those are heuristics, not algorithms.

2
  • 3
    I would not say that an algorithm is proven to yield an optimal result: it depends on the algorithm with respect to which problem.
    – nbro
    Dec 31, 2016 at 20:33
  • 1
    Yielding an optimal result is not the essential quality of algorithms, it is preciseness i.e. the exact result whereas heuristic provides you with approximate results. Mar 18, 2017 at 11:05
22

Heuristic, in a nutshell is an "Educated guess". Wikipedia explains it nicely. At the end, a "general acceptance" method is taken as an optimal solution to the specified problem.

Heuristic is an adjective for experience-based techniques that help in problem solving, learning and discovery. A heuristic method is used to rapidly come to a solution that is hoped to be close to the best possible answer, or 'optimal solution'. Heuristics are "rules of thumb", educated guesses, intuitive judgments or simply common sense. A heuristic is a general way of solving a problem. Heuristics as a noun is another name for heuristic methods.

In more precise terms, heuristics stand for strategies using readily accessible, though loosely applicable, information to control problem solving in human beings and machines.

While an algorithm is a method containing finite set of instructions used to solving a problem. The method has been proven mathematically or scientifically to work for the problem. There are formal methods and proofs.

Heuristic algorithm is an algorithm that is able to produce an acceptable solution to a problem in many practical scenarios, in the fashion of a general heuristic, but for which there is no formal proof of its correctness.

9

An algorithm is a self-contained step-by-step set of operations to be performed 4, typically interpreted as a finite sequence of (computer or human) instructions to determine a solution to a problem such as: is there a path from A to B, or what is the smallest path between A and B. In the latter case, you could also be satisfied with a 'reasonably close' alternative solution.

There are certain categories of algorithms, of which the heuristic algorithm is one. Depending on the (proven) properties of the algorithm in this case, it falls into one of these three categories (note 1):

  • Exact: the solution is proven to be an optimal (or exact solution) to the input problem
  • Approximation: the deviation of the solution value is proven to be never further away from the optimal value than some pre-defined bound (for example, never more than 50% larger than the optimal value)
  • Heuristic: the algorithm has not been proven to be optimal, nor within a pre-defined bound of the optimal solution

Notice that an approximation algorithm is also a heuristic, but with the stronger property that there is a proven bound to the solution (value) it outputs.

For some problems, noone has ever found an 'efficient' algorithm to compute the optimal solutions (note 2). One of those problems is the well-known Traveling Salesman Problem. Christophides' algorithm for the Traveling Salesman Problem, for example, used to be called a heuristic, as it was not proven that it was within 50% of the optimal solution. Since it has been proven, however, Christophides' algorithm is more accurately referred to as an approximation algorithm.

Due to restrictions on what computers can do, it is not always possible to efficiently find the best solution possible. If there is enough structure in a problem, there may be an efficient way to traverse the solution space, even though the solution space is huge (i.e. in the shortest path problem).

Heuristics are typically applied to improve the running time of algorithms, by adding 'expert information' or 'educated guesses' to guide the search direction. In practice, a heuristic may also be a sub-routine for an optimal algorithm, to determine where to look first.

(note 1): Additionally, algorithms are characterised by whether they include random or non-deterministic elements. An algorithm that always executes the same way and produces the same answer, is called deterministic.

(note 2): This is called the P vs NP problem, and problems that are classified as NP-complete and NP-hard are unlikely to have an 'efficient' algorithm. Note; as @Kriss mentioned in the comments, there are even 'worse' types of problems, which may need exponential time or space to compute.

There are several answers that answer part of the question. I deemed them less complete and not accurate enough, and decided not to edit the accepted answer made by @Kriss

5
  • I believe your definition of the word algorithm is too restrictive. Does the use of the word sequence implies non-parallell ? Parallell algorithms are fine and even usual nowaday. What about solving a problem using a neural network ? Or a constraint propagation tool ? Algorithms ? Meta-algorithms ?
    – kriss
    Apr 19, 2016 at 22:05
  • The reader get the feeling NP problems are the worse there is. That's untrue. There are truly hard problems needing truly bad algorithms like exponential ones or worse. NP are special because if we have a solution it is easy and fast to check it, while it is very hard to find it if we don't already have it. It's easy to check that we have correct instructions to get out of a labyrinth, it's much harder to find the exit. Thus NP are both easy and hard if we could try all possible solutions at the same time (non deterministically) solving it would be very simple... but we can't.
    – kriss
    Apr 19, 2016 at 22:15
  • Thanks for the feedback! I've updated the phrasing slightly, and approached it differently. In my view, constraint propagation is a technique to approach something, but is not yet an algorithm that describes how to step-wise come to the solution described in constraint propagation. You are ofcourse correct about the classes of expspace and 'worse', I've added a note on that too. BTW: please write NP-Complete and/or NP-Hard fully, as the subset of NP also contains 'efficiently' solvable problems, which are not (conjectured to be) the same class.
    – Joost
    Apr 20, 2016 at 7:21
  • Of course you are right I should have written NP-Complete. My bad.
    – kriss
    Apr 20, 2016 at 9:03
  • It's way better than what one of my colleagues names it: NP-ness (which sounds just awful and kinda gross...)
    – Joost
    Apr 21, 2016 at 11:21
6

Actually I don't think that there is a lot in common between them. Some algorithm use heuristics in their logic (often to make fewer calculations or get faster results). Usually heuristics are used in the so called greedy algorithms.

Heuristics is some "knowledge" that we assume is good to use in order to get the best choice in our algorithm (when a choice should be taken). For example ... a heuristics in chess could be (always take the opponents' queen if you can, since you know this is the stronger figure). Heuristics do not guarantee you that will lead you to the correct answer, but (if the assumptions is correct) often get answer which are close to the best in much shorter time.

0
5

An Algorithm is a clearly defined set of instructions to solve a problem, Heuristics involve utilising an approach of learning and discovery to reach a solution.

So, if you know how to solve a problem then use an algorithm. If you need to develop a solution then it's heuristics.

1
  • This should be the accepted answer. Should be stressed that both the programmer and the (complex) algorithm can use the heuristics approach.
    – G M
    Apr 27, 2022 at 7:12
5

Heuristics are algorithms, so in that sense there is none, however, heuristics take a 'guess' approach to problem solving, yielding a 'good enough' answer, rather than finding a 'best possible' solution.

A good example is where you have a very hard (read NP-complete) problem you want a solution for but don't have the time to arrive to it, so have to use a good enough solution based on a heuristic algorithm, such as finding a solution to a travelling salesman problem using a genetic algorithm.

4

Algorithm is a sequence of some operations that given an input computes something (a function) and outputs a result.

Algorithm may yield an exact or approximate values.

It also may compute a random value that is with high probability close to the exact value.

A heuristic algorithm uses some insight on input values and computes not exact value (but may be close to optimal). In some special cases, heuristic can find exact solution.

2

A heuristic is usually an optimization or a strategy that usually provides a good enough answer, but not always and rarely the best answer. For example, if you were to solve the traveling salesman problem with brute force, discarding a partial solution once its cost exceeds that of the current best solution is a heuristic: sometimes it helps, other times it doesn't, and it definitely doesn't improve the theoretical (big-oh notation) run time of the algorithm

2

I think Heuristic is more of a constraint used in Learning Based Model in Artificial Intelligent since the future solution states are difficult to predict.

But then my doubt after reading above answers is "How would Heuristic can be successfully applied using Stochastic Optimization Techniques? or can they function as full fledged algorithms when used with Stochastic Optimization?"

http://en.wikipedia.org/wiki/Stochastic_optimization

1
  • oops!! spelling mistake it should be "Artificial Intelligence"
    – A_tanA
    Jan 26, 2011 at 18:06
2

One of the best explanations I have read comes from the great book Code Complete, which I now quote:

A heuristic is a technique that helps you look for an answer. Its results are subject to chance because a heuristic tells you only how to look, not what to find. It doesn’t tell you how to get directly from point A to point B; it might not even know where point A and point B are. In effect, a heuristic is an algorithm in a clown suit. It’s less predict- able, it’s more fun, and it comes without a 30-day, money-back guarantee.

Here is an algorithm for driving to someone’s house: Take Highway 167 south to Puy-allup. Take the South Hill Mall exit and drive 4.5 miles up the hill. Turn right at the light by the grocery store, and then take the first left. Turn into the driveway of the large tan house on the left, at 714 North Cedar.

Here’s a heuristic for getting to someone’s house: Find the last letter we mailed you. Drive to the town in the return address. When you get to town, ask someone where our house is. Everyone knows us—someone will be glad to help you. If you can’t find anyone, call us from a public phone, and we’ll come get you.

The difference between an algorithm and a heuristic is subtle, and the two terms over-lap somewhat. For the purposes of this book, the main difference between the two is the level of indirection from the solution. An algorithm gives you the instructions directly. A heuristic tells you how to discover the instructions for yourself, or at least where to look for them.

1
  • Stating that there exists a difference between an algorithm and a heuristic is like stating that there is a difference between a bird and a chicken. Heuristics are a type of algorithm.
    – Joost
    Jan 21, 2016 at 8:08
0

They find a solution suboptimally without any guarantee as to the quality of solution found, it is obvious that it makes sense to the development of heuristics only polynomial. The application of these methods is suitable to solve real world problems or large problems so awkward from the computational point of view that for them there is not even an algorithm capable of finding an approximate solution in polynomial time.

Not the answer you're looking for? Browse other questions tagged or ask your own question.