algo.AStar procedure finds the shortest path between a source and a target node using the A* search algorithm.
Like algo.SPpaths, it minimizes a numeric edge property (weightProp), but it is guided by a geographic heuristic — the straight-line (great-circle) distance from each node to the target. On spatial graphs such as road networks this lets A* explore far fewer nodes than a plain Dijkstra search, while still returning an optimal path.
Every node must carry latitude and longitude properties; A* uses them to compute the haversine distance to the target.
Syntax
Parameters
Returns
The heuristicScale parameter
The A* heuristic is the haversine distance in meters between a node and the target. For the search to return an optimal path, the heuristic must never overestimate the remaining cost — it must be a lower bound on the true remaining weightProp (this is the admissibility requirement of A*).
When weightProp is not a distance in meters, the raw meters heuristic is on the wrong scale and can dwarf the actual edge weights, causing A* to behave greedily and return a sub-optimal path. heuristicScale multiplies the heuristic to bring it into weightProp units. Set it to a lower bound on the weight accrued per meter of straight-line progress:
heuristicScale must be a non-negative number. A smaller value is always
safe (the search stays optimal but explores more nodes); a value larger than
the true minimum weight-per-meter trades optimality for speed.Examples
Consider a small road network where each junction stores its coordinates and each road stores itslength (meters) and time (seconds):
Example: shortest route by distance
length is in meters, so the default heuristicScale of 1.0 is correct:
Expected Result:
Example: fastest route by travel time
HereweightProp is time in seconds, so the heuristic must be scaled by 1 / max_speed. Assuming a maximum speed of 30 m/s, use heuristicScale: 0.0333:
Expected Result:
Frequently Asked Questions
When should I use algo.AStar vs algo.SPpaths?
When should I use algo.AStar vs algo.SPpaths?
Use algo.AStar for point-to-point queries on spatial graphs where nodes have coordinates (road networks, maps): the geographic heuristic prunes the search and is usually faster than Dijkstra. Use algo.SPpaths when nodes have no coordinates, or when you need cost constraints (
costProp/maxCost) or all shortest paths.Why is my A* path not the shortest one?
Why is my A* path not the shortest one?
Almost always because
heuristicScale doesn’t match the units of weightProp. The heuristic is in meters; if weightProp is a travel time (or any non-meter unit) and the scale is left at the default 1.0, the heuristic overestimates the remaining cost and the search returns a sub-optimal path. Set heuristicScale to a lower bound on the weight per meter (see the heuristicScale section).What happens if a node is missing its latitude or longitude?
What happens if a node is missing its latitude or longitude?
A* needs coordinates on every node it visits to compute the heuristic. Ensure
latitudeProperty and longitudeProperty are present and numeric on all nodes reachable during the search.