Skip to content
Graph Intelligence Hub
Menu

Algorithm

A* search

activeNeeds verification

Shortest-path search guided by a heuristic estimate of remaining distance; with an admissible heuristic it finds optimal paths while expanding fewer nodes than Dijkstra.

Definition

Intuition

Dijkstra that prefers nodes which look closer to the goal, e.g. using straight-line distance on a map.

Complexity

TimeDepends on the heuristic; worst case as Dijkstra
SpaceO(V)

Python

import networkx as nx
h = lambda u, v: euclid(pos[u], pos[v])
path = nx.astar_path(G, 'A', 'F', heuristic=h, weight='weight')

Cypher (Neo4j GDS)

CALL gds.shortestPath.astar.stream('roads', {
  sourceNode: s, targetNode: t,
  latitudeProperty: 'lat', longitudeProperty: 'lon',
  relationshipWeightProperty: 'distance'})
YIELD totalCost, nodeIds RETURN totalCost, nodeIds

Procedure names and options vary between GDS versions; check the GDS docs for your version.

An interactive step-by-step visualizer for this algorithm is planned for phase 3.

Profile

Time complexityDepends on the heuristic; worst case as Dijkstra
Space complexityO(V)

Relationships

Prerequisites
Related