A* Search Algorithm
A* is an informed search algorithm that uses both:
- The cost to reach a node so far, and
- A heuristic estimate of the cost to reach the goal
to guide the search efficiently toward the goal.
Core Idea
A* evaluates nodes using the function:
f(n) = g(n) + h(n)
Where:
g(n)= cost from the start node tonh(n)= heuristic estimate of the cheapest cost fromnto a goalf(n)= estimated total cost of a solution path throughn
The node with the lowest f(n) value is expanded first.
Algorithm Outline
- Initialize the frontier with the start node.
- Set
g(start) = 0and computef(start). - Loop until the frontier is empty:
- Select the node with the smallest
f(n) - If it satisfies the goal test, return the solution
- Expand the node and generate successors
- Update
g,h, andffor each successor - Insert or update successors in the frontier
- Select the node with the smallest
Data Structures
- Frontier (Open Set): Priority queue ordered by
f(n) - Explored Set (Closed Set): Tracks expanded nodes to avoid re-expansion
Heuristic Function h(n)
A heuristic estimates how close a node is to the goal.
Admissible Heuristic
- Never overestimates the true cost to the goal
- Formally:
h(n) ⤠h*(n)for alln
Guarantee:
If h is admissible, A* is optimal.
Consistent (Monotonic) Heuristic
-
Satisfies the triangle inequality:
h(n) ⤠c(n, nā) + h(nā)
-
Ensures that
f(n)values are non-decreasing along a path
Guarantee:
If h is consistent, A* never needs to re-expand a node.
If h is consistent, A* is admissible.
Properties of A*
| Property | Result |
|---|---|
| Complete | Yes (if step costs ℠ε > 0) |
| Optimal | Yes (with admissible heuristic) |
| Time Complexity | Exponential in worst case |
| Space Complexity | Exponential |
Relationship to Other Search Algorithms
- If
h(n) = 0ā A* becomes Uniform Cost Search - If
g(n) = 0ā A* becomes Greedy Best-First Search - Better heuristics ā fewer expanded nodes
Practical Considerations
Pros:
- Very efficient with a good heuristic
- Guarantees optimal solutions
- Widely used (pathfinding, planning, games)
Cons:
- High memory usage
- Performance heavily depends on heuristic quality
Common Heuristic Examples
- Manhattan Distance (grid-based movement)
- Euclidean Distance (continuous space)
- Misplaced Tiles (8-puzzle)
- Manhattan Distance Sum (sliding puzzles)
Key Takeaways
- A* balances actual cost and estimated future cost
- Optimality depends entirely on the heuristic
- Trade-off: speed vs memory
- One of the most important search algorithms in AI
Key Notation
g(n)ā path cost so farh(n)ā heuristic estimate to goalf(n)ā estimated total costh*(n)ā true cost to goal