Oct 27 2011

Algorithm vs. Heuristic

Category: Software Engineeringtuxie @ 09:47

According to Wikipedia:


In mathematics and computer science, an algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function. Algorithms are used for calculation, data processing, and automated reasoning. In simple words an algorithm is a step-by-step procedure for calculations.

Starting from an initial state and initial input (perhaps empty), the instructions describe a computation that, when executed, will proceed through a finite number of well-defined successive states, eventually producing “output” and terminating at a final ending state.


Heuristic refers to experience-based techniques for problem solving, learning, and discovery. Heuristic methods are used to speed up the process of finding a satisfactory solution, where an exhaustive search is impractical. Examples of this method include using a “rule of thumb”, an educated guess, an intuitive judgment, or common sense.

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

Both are ways of reaching from point A to point B, but the algorithm is predictable, that is, the steps are predefined and do not change. On the other hand the heuristic is a higher level technique that does not output a result, as the algorithm does, but instead guides you in finding the solution. The final result varies because the heuristic is only a guide on how to look, not on what to find.

From the book Code Complete: A Practical Handbook of Software Construction:

Here is an algorithm for driving to someone’s house: Take highway 167 south to Puyallup. 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 is 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.

This kind of metaphors can be applied at many levels of software development, from architecture to GUI, relieving us from the need of knowing the internals of the system and therefore easing the understanding of the big picture.