Value of Heuristics

This article is written as a response to a technical interview question I wrote: What is the reasoning behind using a heuristic element in algorithms? What are the drawbacks of using heuristics and when should they be used?

Austin Abraham
2 min readMay 7, 2021

Heuristics, as a general term, are where the code is designed to find a more optimal solution to a problem. Optimal could be defined as a more accurate solution, or as a quicker solution that is still fairly accurate. For example, in program 1, where we had to find the optimal way to maximize space and profit of paintings on a given wall, my heuristic function divided the price by the total area of the painting. Other heuristic functions could be made, such as taking the price of a painting and only dividing by one dimension for example. Both of these are valid heuristics. In this sense, a heuristic is generally seen as a more optimal way of producing code.

The other form of heuristic is where using a heuristic is a question to the optimal goal. In a pathfinding example, like I discussed on my midterm with A* (A star), a heuristic takes on a different meaning. The heuristic in this case acts as a pointer in the right direction, which is used in other algorithms like A* referred to as greedy algorithms. In A*, a programmer implements Dijkstra, and then adds a heuristic function on top of it. An example could be a measure of exact distance on a plane. Adding this allows the programmer to estimate how close they are to arriving to the final pathfinding destination and add that calculation to the Dijkstra priority queue. This allows A* to run far faster than Dijkstra, but adding the heuristic has the downside of it may conclude the route before the optimal route is found due to adding the heuristic. This is why pathfinding heuristics should be used in cases where speed is more important than correct answer.

Purely heuristic algorithms are important and help build a basis for code, but as heuristics mix with other elements of coding, like in A*, it comes a matter of if the runtime or correct solution is more important in deciding which to implement.

Further Reading

Included below is links to further reading. In addition to two articles provided from the internet discussing heuristics, I also included a third link which directly links to my midterm post that discusses A*.

https://data-flair.training/blogs/heuristic-search-ai/

https://medium.com/smucs/a-search-algorithm-e22721d38b87

--

--