A* search
heuristic development
pathfinding algorithms
artificial intelligence
algorithm optimization

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.

Practice ML system design

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:

f(n)=g(n)+h(n)f(n) = g(n) + h(n)

where:

  • g(n)g(n) is the cost of the path from the start node to node nn.
  • h(n)h(n) is the heuristic estimate of the cost from node nn 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.

h(n)h\*(n)h(n) \leq h^\*(n)

where h(n)h^*(n) is the true cost from node nn to the goal.

2. Consistency

A heuristic is consistent (or monotonic) if, for every node nn and its successor nn', the estimated cost of reaching the goal from nn is no greater than the cost of getting to nn' plus the estimated cost from $ n' $ to the goal.

h(n)c(n,n)+h(n)h(n) \leq c(n, n') + h(n')

where c(n,n)c(n, n') 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:

h(n)=max(h_1(n),h_2(n),,h_k(n))h(n) = \max(h\_1(n), h\_2(n), \ldots, h\_k(n))

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:

ProblemAdmissible HeuristicDescription
8-puzzleManhattan DistanceSum of horizontal and vertical distances to the goal.
15-puzzleLinear Conflict + ManhattanAdds penalty for specific tile arrangements.
PathfindingEuclidean DistanceStraight line distance to the target on a continuous grid.
Robot PathGrid 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice ML system design

All Rights Reserved.