Skip to main content

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:
  1. g(n): Actual cost from start to current node (like Dijkstra)
  2. h(n): Heuristic estimate from current node to goal
The algorithm prioritizes nodes with the lowest f(n) = g(n) + h(n), guiding search toward the target.
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
In these scenarios, A* can be 10-100x faster by avoiding exploration of irrelevant areas.

Heuristic Requirements

The heuristic property must:
  1. Be stored on each node
  2. Estimate cost to reach the target
  3. Never overestimate (admissible)
  4. Be consistent across the graph
Common heuristics:
  • 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

Here’s how to run the query using the SDKs or curl

Example 2: Time-optimized routing with traffic-aware weights

Here’s how to run the query using the SDKs or curl

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

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

Verify Admissibility

Test that your heuristic never overestimates:

ShortestPathDijkstras

Learn about Dijkstra’s algorithm (A* without heuristic)

Custom Weights

Property contexts for weight calculations

Weight Expressions

Advanced mathematical weight expressions

Overview

Compare all shortest path algorithms