Finding good heuristic for A search
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Finding an effective heuristic is paramount for the efficiency of A* search, a widely-used algorithm in artificial intelligence for pathfinding and graph traversal. The A* search algorithm relies on heuristics to estimate the cost of the cheapest path from the current node to the goal. A well-chosen heuristic can significantly improve the performance of A* search, making it feasible to solve complex problems efficiently.
A* Search Algorithm Overview
A* search combines features of Dijkstra's Algorithm and Greedy Best-First Search. It finds the least costly path by maintaining a tree of paths originating at the start node and extending those paths one edge at a time until the destination node is reached. The algorithm uses the following formula to determine the node to explore next:
where:
- is the cost of the path from the start node to node .
- is the heuristic estimate of the cost from node to the goal.
Properties of Heuristics
1. Admissibility
A heuristic is said to be admissible if it never overestimates the cost to reach the goal. An admissible heuristic ensures that A* is an optimal search algorithm, meaning it guarantees finding the least cost solution.
where is the true cost from node to the goal.
2. Consistency
A heuristic is consistent (or monotonic) if, for every node and its successor , the estimated cost of reaching the goal from is no greater than the cost of getting to plus the estimated cost from $ n' $ to the goal.
where is the actual cost of moving from $ n $ to $ n' $. Consistent heuristics are also admissible.
Designing Heuristics
1. Domain Knowledge
Incorporating domain-specific knowledge can significantly enhance heuristic effectiveness. For instance, in pathfinding problems on a grid, using the Euclidean distance as a heuristic is beneficial if movement is allowed in any direction.
2. Relaxed Problems
Another method is to solve a relaxed problem, which is the original problem but with fewer restrictions. The solution cost to the relaxed problem provides a lower bound to the original problem, thus a valid heuristic. For instance, in the 8-puzzle, where each tile can be moved across any number of empty spaces, the Manhattan distance (the sum of the horizontal and vertical distances of tiles to their goal positions) is commonly used.
3. Combining Heuristics
Using multiple heuristics can provide better performance. A common approach is to combine them using the maximum function to create a new "dominant" heuristic:
4. Learning Heuristics
Machine learning techniques can be used to approximate heuristics by learning from examples. For instance, neural networks can be trained to predict the cost to the goal based on the current state of the game.
Examples of Heuristics
Here's a table summarizing some common heuristics for well-known problems:
| Problem | Admissible Heuristic | Description |
| 8-puzzle | Manhattan Distance | Sum of horizontal and vertical distances to the goal. |
| 15-puzzle | Linear Conflict + Manhattan | Adds penalty for specific tile arrangements. |
| Pathfinding | Euclidean Distance | Straight line distance to the target on a continuous grid. |
| Robot Path | Grid Distance (Diagonals) | Like Euclidean but allows diagonal moves. |
Evaluating Heuristics
When evaluating heuristics, consider:
- Accuracy: Does it provide a close estimate to the actual cost?
- Computational Efficiency: Is it quick to compute?
- Applicability: Is it suitable for the problem at hand?
- Dominance: If comparing two heuristics, a dominant heuristic is better if it always provides a better estimate.
Conclusion
Finding an effective heuristic is crucial to the performance of the A* search algorithm. Admissible, consistent heuristics ensure that A* remains an optimal and complete search algorithm. By exploiting domain knowledge, solving relaxed problems, or employing learning-based approaches, one can design heuristics that drastically reduce the search space and time, thus making previously intractable problems solvable.
Related reading
- Finding groups of similar strings in a large set of strings
- Finding K-nearest neighbors and its implementation
- Finding the best cosine similarity in a set of vectors
- Finding the correlation matrix
- Finding height in Binary Search Tree
- Finding highest product of three numbers
- Finding items in an universal hash table?
- Finding largest f satisfying a property given f is non-decreasing in its arguments

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.