Path Finding
algo.dijkstra
| Property | Value |
|---|---|
Procedure |
|
Category |
Path Finding |
Complexity |
CPU |
Min Args |
4 |
Max Args |
5 |
Syntax
CALL algo.dijkstra(startNode, endNode, relTypes, weightProperty [, direction])
YIELD path, weight
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
|
Vertex |
Yes |
— |
Source vertex |
|
Vertex |
Yes |
— |
Destination vertex |
|
String |
Yes |
— |
Comma-separated relationship types; pass |
|
String |
Yes |
— |
Edge property name to use as weight |
|
String |
No |
|
Traversal direction: |
Yield Fields
| Field | Type | Description |
|---|---|---|
|
List<RID> |
Ordered list of vertex identities on the shortest path |
|
Double |
Total path weight |
Description
Finds the single shortest weighted path between two vertices using Dijkstra’s algorithm. Non-negative edge weights are required. Internally delegates to the A* implementation with no heuristic.
Use Cases
-
Route planning in transportation networks
-
Cost-optimal navigation in weighted graphs
-
Network latency analysis
Example
MATCH (src:City {name:'Rome'}), (dst:City {name:'Berlin'})
CALL algo.dijkstra(src, dst, 'ROAD', 'distance', 'OUT')
YIELD path, weight
RETURN path, weight
References
See also: reference/graph-algorithms/sql-path-functions.adoc#algo-dijkstra-sql for the SQL function variant, and astar() / dijkstra() for the SQL function equivalents.
algo.astar
| Property | Value |
|---|---|
Procedure |
|
Category |
Path Finding |
Complexity |
CPU |
Min Args |
4 |
Max Args |
6 |
Syntax
CALL algo.astar(startNode, endNode, relTypes, weightProperty [, latProperty, lonProperty])
YIELD path, weight
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
|
Vertex |
Yes |
— |
Source vertex |
|
Vertex |
Yes |
— |
Destination vertex |
|
String |
Yes |
— |
Comma-separated relationship types; pass |
|
String |
Yes |
— |
Edge property name to use as weight |
|
String |
No |
— |
Vertex property name for latitude (enables geographic heuristic) |
|
String |
No |
— |
Vertex property name for longitude (enables geographic heuristic) |
Yield Fields
| Field | Type | Description |
|---|---|---|
|
List<RID> |
Ordered list of vertex identities on the shortest path |
|
Double |
Total path weight |
Description
Finds the shortest weighted path using the A* algorithm. When latProperty and lonProperty are provided, a geographic (Euclidean) heuristic accelerates the search. Without the geographic properties it behaves identically to Dijkstra.
Use Cases
-
Geospatial route optimization
-
Game AI pathfinding with spatial heuristics
-
Weighted graph shortest paths with domain-specific heuristics
Example
MATCH (src:City {name:'Milan'}), (dst:City {name:'Paris'})
CALL algo.astar(src, dst, 'ROAD', 'km', 'lat', 'lon')
YIELD path, weight
RETURN path, weight
References
algo.bellmanford
| Property | Value |
|---|---|
Procedure |
|
Category |
Path Finding |
Complexity |
CPU |
Min Args |
4 |
Max Args |
4 |
Syntax
CALL algo.bellmanford(startNode, endNode, relTypes, weightProperty)
YIELD path, weight, negativeCycle
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
|
Vertex |
Yes |
— |
Source vertex |
|
Vertex |
Yes |
— |
Destination vertex |
|
String |
Yes |
— |
Comma-separated relationship types; pass |
|
String |
Yes |
— |
Edge property name to use as weight (may be negative) |
Yield Fields
| Field | Type | Description |
|---|---|---|
|
List<RID> |
Ordered list of vertex identities on the shortest path |
|
Double |
Total path weight |
|
Boolean |
|
Description
Finds the shortest weighted path between two vertices using the Bellman-Ford algorithm. Unlike Dijkstra, it correctly handles graphs with negative edge weights. It runs V−1 relaxation iterations followed by one extra iteration to detect negative-weight cycles. If a negative cycle is detected, negativeCycle is set to true and the path result is unreliable.
Use Cases
-
Financial transaction networks with negative-cost edges
-
Currency arbitrage detection (negative cycle = profit cycle)
-
Graphs where edge weights can be negative
Example
MATCH (a:Account {id:1}), (b:Account {id:5})
CALL algo.bellmanford(a, b, 'TRANSFER', 'cost')
YIELD path, weight, negativeCycle
RETURN path, weight, negativeCycle
References
See also: reference/graph-algorithms/sql-path-functions.adoc#algo-bellman-ford-sql for the SQL function variant.
algo.allsimplepaths
| Property | Value |
|---|---|
Procedure |
|
Category |
Path Finding |
Complexity |
CPU |
Min Args |
4 |
Max Args |
4 |
Syntax
CALL algo.allsimplepaths(startNode, endNode, relTypes, maxDepth)
YIELD path
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
|
Vertex |
Yes |
— |
Source vertex |
|
Vertex |
Yes |
— |
Destination vertex |
|
String |
Yes |
— |
Comma-separated relationship types; pass |
|
Integer |
Yes |
— |
Maximum path length in hops (minimum 1) |
Yield Fields
| Field | Type | Description |
|---|---|---|
|
List<RID> |
Ordered list of vertex identities for one simple path |
Description
Enumerates all simple (loop-free) paths between two vertices up to a maximum depth, using DFS with backtracking. Edges are traversed in both directions. Each result row contains one complete path. Use maxDepth to prevent combinatorial explosion in dense graphs.
Use Cases
-
Finding all routes between two nodes in a network
-
Dependency analysis between components
-
Attack path enumeration in security graphs
Example
MATCH (s:Service {name:'AuthService'}), (t:Service {name:'Database'})
CALL algo.allsimplepaths(s, t, 'CALLS', 4)
YIELD path
RETURN path
References
algo.kShortestPaths
| Property | Value |
|---|---|
Procedure |
|
Category |
Path Finding |
Complexity |
CPU+RAM |
Min Args |
3 |
Max Args |
5 |
Syntax
CALL algo.kShortestPaths(startNode, endNode, k [, relTypes, weightProperty])
YIELD path, weight, rank
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
|
Vertex |
Yes |
— |
Source vertex |
|
Vertex |
Yes |
— |
Destination vertex |
|
Integer |
Yes |
— |
Number of shortest paths to find |
|
String |
No |
all types |
Comma-separated relationship types |
|
String |
No |
uniform weight |
Edge property name for weights |
Yield Fields
| Field | Type | Description |
|---|---|---|
|
List<RID> |
Ordered list of vertex identities |
|
Double |
Total path weight |
|
Integer |
1-based rank (1 = shortest) |
Description
Finds the k shortest loopless paths between two vertices using Yen’s algorithm. Internally uses Dijkstra on a V×V weight matrix, progressively building candidate paths by spur-node deviation from previous shortest paths. Memory usage scales as O(V²) for the weight matrix.
Use Cases
-
Resilience planning: top-k alternative routes in case of failures
-
Network redundancy analysis
-
Multi-criteria path exploration
Example
MATCH (s:Router {id:'R1'}), (t:Router {id:'R10'})
CALL algo.kShortestPaths(s, t, 3, 'LINK', 'latency')
YIELD path, weight, rank
RETURN rank, path, weight ORDER BY rank ASC
References
algo.apsp
| Property | Value |
|---|---|
Procedure |
|
Category |
Path Finding |
Complexity |
CPU+RAM |
Min Args |
0 |
Max Args |
2 |
Syntax
CALL algo.apsp([weightProperty, relTypes])
YIELD source, target, distance
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
|
String |
No |
uniform weight |
Edge property name for weights |
|
String |
No |
all types |
Comma-separated relationship types |
Yield Fields
| Field | Type | Description |
|---|---|---|
|
RID |
Source vertex identity |
|
RID |
Target vertex identity |
|
Double |
Shortest distance between source and target |
Description
Computes shortest distances between all reachable pairs of distinct vertices using the Floyd-Warshall algorithm. Time complexity is O(V³); memory usage is O(V²). Only reachable pairs (finite distance, source ≠ target) are returned. Suitable for small to medium graphs.
Use Cases
-
Graph diameter and radius computation
-
Network reachability analysis
-
Pre-computing all pairwise distances for downstream analytics
Example
CALL algo.apsp('weight', 'ROAD')
YIELD source, target, distance
RETURN source, target, distance ORDER BY distance DESC LIMIT 10
References
algo.bfs
| Property | Value |
|---|---|
Procedure |
|
Category |
Path Finding |
Complexity |
CPU |
Min Args |
1 |
Max Args |
4 |
Syntax
CALL algo.bfs(startNode [, relTypes, direction, maxDepth])
YIELD node, depth
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
|
Vertex |
Yes |
— |
Source vertex from which the traversal starts |
|
String |
No |
all types |
Comma-separated relationship types to follow |
|
String |
No |
|
Traversal direction: |
|
Integer |
No |
unbounded |
Maximum BFS depth (hop count) from the start node |
Yield Fields
| Field | Type | Description |
|---|---|---|
|
RID |
Vertex identity (the start node is not included) |
|
Integer |
BFS depth from the start node |
Description
Performs a Breadth-First Search traversal from a given start node, visiting all reachable vertices in non-decreasing distance order. Each reachable vertex (excluding the start node itself) is yielded together with its BFS depth. An optional maxDepth bound limits the traversal radius.
Use Cases
-
Shortest hop-count paths from a source
-
Neighbourhood exploration at a given radius
-
Level-order processing of tree or DAG structures
Example
MATCH (start:Person {name:'Alice'})
CALL algo.bfs(start, 'KNOWS', 'BOTH', 3)
YIELD node, depth
RETURN node.name, depth ORDER BY depth
References
algo.dfs
| Property | Value |
|---|---|
Procedure |
|
Category |
Path Finding |
Complexity |
CPU |
Min Args |
1 |
Max Args |
4 |
Syntax
CALL algo.dfs(startNode [, relTypes, direction, maxDepth])
YIELD node, depth
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
|
Vertex |
Yes |
— |
Source vertex from which the traversal starts |
|
String |
No |
all types |
Comma-separated relationship types to follow |
|
String |
No |
|
Traversal direction: |
|
Integer |
No |
unbounded |
Maximum DFS depth from the start node |
Yield Fields
| Field | Type | Description |
|---|---|---|
|
RID |
Vertex identity (the start node is not included) |
|
Integer |
DFS discovery depth from the start node |
Description
Performs an iterative (non-recursive) Depth-First Search from a given start node using an explicit stack, avoiding Java stack-overflow on large graphs. Vertices are yielded in DFS discovery order together with their depth. An optional maxDepth bound limits the traversal.
Use Cases
-
Reachability analysis
-
Dependency resolution and cycle detection
-
Tree/DAG structure enumeration
Example
MATCH (start:Module {name:'core'})
CALL algo.dfs(start, 'DEPENDS_ON', 'OUT')
YIELD node, depth
RETURN node.name, depth
References
algo.dijkstra.singleSource
| Property | Value |
|---|---|
Procedure |
|
Category |
Path Finding |
Complexity |
CPU |
Min Args |
3 |
Max Args |
4 |
Syntax
CALL algo.dijkstra.singleSource(startNode, relTypes, weightProperty [, direction])
YIELD node, cost
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
|
Vertex |
Yes |
— |
Source vertex |
|
String |
Yes |
— |
Comma-separated relationship types; pass |
|
String |
Yes |
— |
Edge property name to use as weight (must be numeric, non-negative) |
|
String |
No |
|
Traversal direction: |
Yield Fields
| Field | Type | Description |
|---|---|---|
|
RID |
Reachable vertex identity (the start node is not included) |
|
Double |
Minimum total weight from the start node to this vertex |
Description
Computes the Single-Source Shortest Path (SSSP) from a given start node to every reachable vertex using Dijkstra’s algorithm with a binary min-heap.
This procedure extends algo.dijkstra to return results for all reachable targets at once instead of just a single source-target pair.
All edge weights must be non-negative; edges with negative weights are silently ignored.
Use Cases
-
Distance maps from a hub in transportation networks
-
Risk propagation from an origin node
-
Identifying the closest facility to every location in a network
Example
MATCH (hub:City {name:'London'})
CALL algo.dijkstra.singleSource(hub, 'ROAD', 'distance', 'OUT')
YIELD node, cost
RETURN node.name, cost ORDER BY cost LIMIT 10
References
See also: algo.dijkstra for the single source-target variant.
algo.longestPath
| Property | Value |
|---|---|
Procedure |
|
Category |
Path Finding |
Complexity |
CPU |
Min Args |
0 |
Max Args |
2 |
Syntax
CALL algo.longestPath([relTypes, weightProperty])
YIELD node, distance, source
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
|
String |
No |
all types |
Comma-separated relationship types to follow |
|
String |
No |
|
Edge property name to use as weight |
Yield Fields
| Field | Type | Description |
|---|---|---|
|
RID |
Vertex identity |
|
Double |
Maximum distance from the furthest source reaching this vertex |
|
RID |
The source vertex that achieves the maximum distance |
Description
Finds the longest path in a Directed Acyclic Graph (DAG) using dynamic programming on topological order (Kahn’s algorithm). If the graph contains a cycle the procedure detects it and returns an empty result set.
When no weightProperty is provided, each hop has weight 1.0 (i.e., the result is the maximum hop count).
Use Cases
-
Critical path analysis in project scheduling (CPM)
-
Longest dependency chain in build systems
-
Pipeline bottleneck identification
Example
CALL algo.longestPath('DEPENDS_ON')
YIELD node, distance, source
RETURN node.name AS task, distance, source.name AS criticalSource
ORDER BY distance DESC LIMIT 5
References
See also: reference/graph-algorithms/structural-analysis.adoc#algo-topological-sort for topological ordering.
algo.msa
| Property | Value |
|---|---|
Procedure |
|
Category |
Path Finding |
Complexity |
CPU |
Min Args |
1 |
Max Args |
3 |
Syntax
CALL algo.msa(rootNode [, relTypes, weightProperty]) YIELD source, target, weight, totalWeight
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
|
Vertex |
— |
Root vertex for the arborescence |
|
String |
all types |
Comma-separated edge type names to traverse |
|
String |
|
Edge property for weights; |
Yield Fields
| Field | Type | Description |
|---|---|---|
|
Vertex |
Source vertex of this MSA edge |
|
Vertex |
Destination vertex of this MSA edge |
|
Float |
Weight of this edge |
|
Float |
Total weight of the arborescence (same on every row) |
Description
Computes the Minimum Spanning Arborescence (directed MST) rooted at a specified vertex using the Chu-Liu/Edmonds algorithm. Unlike the undirected algo.mst, the arborescence is a directed spanning tree where every non-root vertex has exactly one incoming edge and all vertices are reachable from the root along directed paths.
Returns n − 1 rows (one per arborescence edge). Returns an empty result if no spanning arborescence exists (the directed graph is not strongly connected from the root).
Use Cases
-
Optimising directed distribution networks (water, power, logistics)
-
Dependency resolution with minimum total cost
-
Directed network backbone extraction
Example
MATCH (r:City {name:'Rome'})
CALL algo.msa(r, 'ROAD', 'distance')
YIELD source, target, weight, totalWeight
RETURN source.name, target.name, weight ORDER BY weight
References
-
Chu, Y. J. & Liu, T. H. (1965). On the Shortest Arborescence of a Directed Graph. Science Sinica.
algo.steinerTree
| Property | Value |
|---|---|
Procedure |
|
Category |
Path Finding |
Complexity |
CPU+RAM |
Min Args |
1 |
Max Args |
3 |
Syntax
CALL algo.steinerTree(terminalNodes [, relTypes, weightProperty]) YIELD source, target, weight, totalWeight
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
|
List<Vertex> |
— |
List of terminal (required) vertices to connect |
|
String |
all types |
Comma-separated edge type names to traverse (undirected) |
|
String |
|
Edge property for weights; |
Yield Fields
| Field | Type | Description |
|---|---|---|
|
Vertex |
Source vertex of this Steiner-tree edge |
|
Vertex |
Destination vertex of this Steiner-tree edge |
|
Float |
Weight of this edge |
|
Float |
Total Steiner-tree weight (same on every row) |
Description
Finds an approximate minimum-weight connected subgraph that spans all terminal vertices, using the Kou-Markowsky-Berman 2-approximation algorithm (approximation ratio 2(1 − 1/|T|) where |T| is the number of terminals):
-
Run Dijkstra from each terminal to find shortest paths to all other terminals.
-
Build a complete graph on terminals with shortest-path distances.
-
Find the MST of this terminal graph (Kruskal’s).
-
Expand each MST edge back to its actual shortest path in the original graph.
-
Prune non-terminal leaves iteratively.
Edges are traversed undirected. Returns an empty result if terminals are unreachable from each other.
Use Cases
-
Designing minimum-cost subnetworks connecting mandatory waypoints
-
Content distribution network planning
-
Pipeline / cable routing connecting required endpoints
Example
MATCH (a:City {name:'Rome'}), (b:City {name:'Milan'}), (c:City {name:'Naples'})
CALL algo.steinerTree([a, b, c], 'ROAD', 'distance')
YIELD source, target, weight, totalWeight
RETURN source.name, target.name, weight
References
-
Kou, L., Markowsky, G. & Berman, L. (1981). A fast algorithm for Steiner trees. Acta Informatica.