AStar Module
A* (A-Star) search algorithm for optimal pathfinding with heuristic guidance.
A* is an informed search algorithm that finds the shortest path from a start node to a goal node using a heuristic function to guide exploration. It combines the completeness of Dijkstra's algorithm with the efficiency of greedy best-first search.
Algorithm
Algorithm |
Function |
Complexity |
Best For |
|---|---|---|---|
A* Search |
aStar |
O((V + E) log V) |
Pathfinding with good heuristics |
Implicit A* |
implicitAStar |
O((V + E) log V) |
Large/infinite graphs generated on-demand |
Key Concepts
-
Evaluation Function: f(n) = g(n) + h(n)
- g(n): Actual cost from start to node n
- h(n): Heuristic estimate from n to goal
- f(n): Estimated total cost through n
- Admissible Heuristic: h(n) ≤ actual cost (never overestimates)
- Consistent Heuristic: h(n) ≤ cost(n→n') + h(n') (triangle inequality)
When to Use A*
Use A* when: - You have a specific goal node (not single-source to all) - You can provide a good heuristic estimate - The heuristic is admissible (underestimates)
Use Dijkstra when: - No good heuristic available (h(n) = 0 reduces A* to Dijkstra) - You need shortest paths to all nodes from a source
Heuristic Examples
Domain |
Heuristic |
Admissible? |
|---|---|---|
Grid (4-way) |
Manhattan distance |
Yes |
Grid (8-way) |
Chebyshev distance |
Yes |
Geospatial |
Haversine/great-circle |
Yes |
Road networks |
Precomputed landmarks |
Yes |
Use Cases
- Video games: NPC pathfinding on game maps
- GPS navigation: Route planning with distance estimates
- Robotics: Motion planning with obstacle avoidance
- Puzzle solving: Sliding puzzles, mazes, labyrinths
Functions and values
| Function or value |
Description
|
Full Usage:
implicitAStar zero add compare heuristic successors isGoal start
Parameters:
'cost
add : 'cost -> 'cost -> 'cost
compare : 'cost -> 'cost -> int
heuristic : 'state -> 'cost
successors : 'state -> ('state * 'cost) list
isGoal : 'state -> bool
start : 'state
Returns: 'cost option
|
Finds the shortest path in an implicit graph using A* search with a heuristic.
|
Full Usage:
implicitAStarBy zero add compare heuristic successors keyFn isGoal start
Parameters:
'cost
add : 'cost -> 'cost -> 'cost
compare : 'cost -> 'cost -> int
heuristic : 'state -> 'cost
successors : 'state -> ('state * 'cost) list
keyFn : 'state -> 'key
isGoal : 'state -> bool
start : 'state
Returns: 'cost option
|
Like
|