Find Shortest Paths with A* Algorithm Â
ShortestPathAStar uses the A* (A-star) algorithm to find optimal shortest paths in weighted graphs. It combines the precision of Dijkstra’s algorithm with heuristic guidance to dramatically improve performance, especially for long-distance pathfinding in spatial graphs.When using the SDKs or curling the endpoint, the query name must match what is defined in the
queries.hx file exactly.How It Works
A* combines two costs:- g(n): Actual cost from start to current node (like Dijkstra)
- h(n): Heuristic estimate from current node to goal
The heuristic must be admissible (never overestimate the true cost) to guarantee finding the optimal path. Common admissibles include straight-line distance for geographic routing.
When to Use A*
A* is ideal when:- Spatial graphs: Nodes have geographic or coordinate-based positions
- Long paths: Target is far from source (A* excels here)
- Goal-directed: You know the general direction to the target
- Performance critical: Need faster results than Dijkstra
- Admissible heuristic available: You have a property that estimates remaining cost
When A* Outperforms Dijkstra
A* can be significantly faster than Dijkstra when:- The heuristic effectively guides search toward the target
- The graph is large and sparse
- The path length is substantial
- Node coordinates or positions are available
Heuristic Requirements
The heuristic property must:- Be stored on each node
- Estimate cost to reach the target
- Never overestimate (admissible)
- Be consistent across the graph
- Straight-line distance: For geographic graphs
- Manhattan distance: For grid-based graphs
- Euclidean distance: For coordinate-based graphs
Example 1: Geographic routing with straight-line distance heuristic
Example 2: Time-optimized routing with traffic-aware weights
Admissible Heuristics
For A* to guarantee optimal results, the heuristic must be admissible:Good Heuristics (Admissible)
- Straight-line distance: Always ≤ actual road distance
- Manhattan distance: For grid-based movement
- Minimum theoretical time: Based on maximum speed limits
Bad Heuristics (Inadmissible)
- Overestimated distances: May miss optimal path
- Random values: No guarantee of optimality
- Negative values: Breaks the algorithm
Using an inadmissible heuristic may cause A* to return suboptimal paths. Always ensure your heuristic never overestimates the true remaining cost.
Performance Comparison
| Scenario | Dijkstra | A* with Good Heuristic |
|---|---|---|
| Small graph (< 100 nodes) | Fast | Similar |
| Large graph (> 10,000 nodes) | Slow | 10-100x faster |
| Short paths | Fast | Similar |
| Long paths | Slow | Much faster |
| No spatial info | Only option | Not applicable |
| Spatial graph | Explores everything | Explores targeted area |
Result Structure
A* returns the same structure as Dijkstra:Best Practices
Pre-calculate Heuristics
For static targets, pre-calculate and store heuristic values:Choose the Right Heuristic
- Geographic graphs: Use haversine or Euclidean distance
- Grid graphs: Use Manhattan distance
- Time-based: Use minimum theoretical time