Skip to main content
The 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:
Leaving heuristicScale at its default 1.0 while weightProp is not a distance in meters (for example a drive-time weight) makes the heuristic inadmissible, so A* may return a sub-optimal path. Always scale the heuristic to the units of weightProp.
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 its length (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

Here weightProp 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

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.
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).
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.