UltipaDocs
Products
Solutions
Resources
Company
Start Free Trial
UltipaDocs
Start Free Trial
  • Introduction
  • Running Algorithms
    • Degree Centrality
    • Closeness Centrality
    • Harmonic Centrality
    • Eccentricity Centrality
    • Betweenness Centrality
    • Bridges
    • Articulation Points
    • Eigenvector Centrality
    • Katz Centrality
    • CELF
    • PageRank
    • ArticleRank
    • TextRank
    • HITS
    • SybilRank
    • Jaccard Similarity
    • Overlap Similarity
    • Cosine Similarity
    • Pearson Correlation Coefficient
    • Euclidean Distance
    • KNN
    • Vector Similarity
    • Bipartite Graph
    • HyperANF
    • Weakly Connected Components (WCC)
    • Strongly Connected Components (SCC)
    • k-Edge Connected Components
    • Local Clustering Coefficient
    • Triangle Count
    • Clique Count
    • k-Core
    • k-Truss
    • p-Cohesion
    • Induced Subgraph
    • Topological Sort
    • Breadth-First Search (BFS)
    • Depth-First Search (DFS)
    • Dijkstra's Shortest Path
    • A* Shortest Path
    • Yen's K-Shortest Paths
    • Shortest Path (BFS)
    • Delta-Stepping SSSP
    • Shortest Path Faster Algorithm (SPFA)
    • All-Pairs Shortest Path (APSP)
    • Minimum Spanning Tree (MST)
    • K-Spanning Tree
    • Steiner Tree
    • Prize-Collecting Steiner Tree (PCST)
    • Minimum Cost Flow
    • Maximum Flow
    • K-Hop Fast
    • Longest Path (DAG)
    • Random Walk
    • Adamic-Adar Index
    • Common Neighbors
    • Preferential Attachment
    • Resource Allocation
    • Total Neighbors
    • Same Community
    • Louvain
    • Leiden
    • Modularity Optimization
    • Label Propagation
    • HANP
    • SLPA
    • k-Means
    • HDBSCAN
    • K-1 Coloring
    • Modularity
    • Conductance
    • Max k-Cut
      • Node2Vec
      • Struc2Vec
      • LINE
      • Fast Random Projection
      • Summary of Graph Embedding
      • Gradient Descent
      • Backpropagation
      • Skip-gram
      • Skip-gram Optimization
  1. Docs
  2. /
  3. Graph Algorithms
  4. /
  5. Pathfinding

A* Shortest Path

Overview

The A* (A-star) algorithm finds the shortest path between a source node and a target node, similar to Dijkstra's algorithm, but uses a heuristic function to guide the search toward the target more efficiently.

Concepts

A* Algorithm

A* extends Dijkstra's algorithm by adding a degree-based heuristic estimate. At each step, instead of expanding the node with the smallest known distance dist(n), A* expands the node with the smallest f(n) = dist(n) + h(n), where:

  • dist(n) is the actual distance from the source to node n
  • h(n) is the heuristic estimate of remaining cost from node n to the target based on node n's degree: h(n) = 1 / (1 + degree(n))

Nodes with higher degree get lower heuristic values, making them appear more promising — the intuition being that well-connected nodes are more likely to lead toward the target. The heuristic allows A* to explore fewer nodes than Dijkstra while still finding the optimal path.

Find the shortest path from A to G following outgoing edges in this graph:

First, compute the heuristic h(n) for each node based on out-degree. For the target node, h(n) = 0. For nodes with no outgoing edges, h(n) = 1.

ABCDEFG
Out-degree2302110
h(n)0.3330.2510.3330.50.50

Visit nodes step-by-step in the graph from the source A. In each step, update dist and f for the outgoing neighbors of the visited node, and pick the next unvisited node with the lowest f:

Step 1: Visit A, update B and F

Nodedist(n)h(n)f(n)VisitedPicked
A00.3330.333Yes
B10.251.25NoYes
F10.51.5NoYes

Step 2: Visit B, update C, D, and F (remains unchanged)

Nodedist(n)h(n)f(n)VisitedPicked
A00.3330.333Yes
B10.251.25Yes
F10.51.5NoYes
C213NoNo
D20.3332.333NoNo

Step 3: Visit F, update E

Nodedist(n)h(n)f(n)VisitedPicked
A00.3330.333Yes
B10.251.25Yes
F10.51.5Yes
C213NoNo
D20.3332.333NoYes
E20.52.5NoNo

Step 4: Visit D, update E (remains unchanged) and F (visited)

Nodedist(n)h(n)f(n)VisitedPicked
A00.3330.333Yes
B10.251.25Yes
F10.51.5Yes
D20.3332.333Yes
C213NoNo
E20.52.5NoYes

Step 5: Visit E, it reaches the target node G

Nodedist(n)h(n)f(n)VisitedPicked
A00.3330.333Yes
B10.251.25Yes
F10.51.5Yes
D20.3332.333Yes
E20.52.5Yes
C213NoNo
G303NoYes

The path found is A->F->E->G, distance = 3.

Example Graph

GQL
INSERT (A:default {_id: "A"}), (B:default {_id: "B"}),
       (C:default {_id: "C"}), (D:default {_id: "D"}),
       (E:default {_id: "E"}), (F:default {_id: "F"}),
       (G:default {_id: "G"}),
       (A)-[:default {value: 2}]->(B), (A)-[:default {value: 4}]->(F),
       (B)-[:default {value: 3}]->(C), (B)-[:default {value: 3}]->(D),
       (B)-[:default {value: 6}]->(F), (D)-[:default {value: 2}]->(E),
       (D)-[:default {value: 2}]->(F), (E)-[:default {value: 3}]->(G),
       (F)-[:default {value: 1}]->(E)

Parameters

NameTypeDefaultDescription
startNodeSTRING/Required. Source node _id.
endNodeSTRING/Required. Target node _id.
weightSTRING/Edge property to use as weight. If unset, all edges have unit weight.

Run Mode

Returns:

ColumnTypeDescription
nodeIdSTRINGNode identifier (_id) in the path
distanceFLOATDistance from source to this node
pathLISTFull shortest path as list of node _ids
GQL
CALL algo.astar({
  startNode: "A",
  endNode: "G",
  weight: "value"
}) YIELD nodeId, distance, path

Stream Mode

Returns the same columns as run mode, streamed for memory efficiency.

GQL
CALL algo.astar.stream({
  startNode: "A",
  endNode: "G",
  weight: "value"
}) YIELD nodeId, distance
RETURN nodeId, distance

Stats Mode

Returns:

ColumnTypeDescription
nodeCountINTNumber of nodes in the shortest path
totalDistanceFLOATTotal distance of the shortest path
GQL
CALL algo.astar.stats({
  startNode: "A",
  endNode: "G",
  weight: "value"
}) YIELD nodeCount, totalDistance