shortestPath / allShortestPaths

Finds shortest path(s) between two nodes.

Category: query-language

Syntax

shortestPath((<a>)-[*..max]->(<b>))
allShortestPaths((<a>)-[*..max]->(<b>))

Description

## Overview shortestPath and allShortestPaths are path-finding functions that locate the shortest route(s) between two nodes in the graph. shortestPath returns a single shortest path (arbitrary choice if multiple exist), while allShortestPaths returns all paths that tie for the minimum length. Both functions are used inside MATCH pattern assignments. In DeltaForge, shortest path computation uses BFS on the CSR graph structure. The path result includes all intermediate nodes and relationships, which can be decomposed using the nodes() and relationships() functions. For weighted shortest paths, use the CALL algo.shortestPath procedure instead, which supports edge weight parameters. ## Behavior - shortestPath finds one path with the minimum number of hops between the two endpoint nodes. If no path exists, the MATCH produces no results (or NULLs if used with OPTIONAL MATCH). - allShortestPaths returns all paths that share the minimum hop count. The result may contain multiple rows if several equally short paths exist. - The variable-length pattern inside the function specifies the maximum traversal depth. `*` without bounds uses the engine's default maximum depth. `*..10` caps traversal at 10 hops. - The path result (bound via `p = shortestPath(...)`) supports nodes(p), relationships(p), and length(p) functions for decomposition. - Direction constraints (`->`, `<-`, `-`) are respected during path search. Undirected search traverses edges in both directions. ## Limitations - shortestPath and allShortestPaths compute unweighted shortest paths (hop count). For weighted shortest paths considering edge weights, use CALL algo.shortestPath with the weighted parameter. - Path finding on disconnected graphs returns no result when no path exists between the endpoints. There is no built-in indication of disconnectedness; the MATCH simply produces zero rows. - The maximum depth for unbounded paths is internally capped. Specify explicit bounds for predictable behavior on large graphs.

Examples

-- Find the shortest path between two named nodes
USE my_zone.my_schema.my_graph
MATCH p = shortestPath((a:Employee {name: 'Dr. Chen'})-[*]-(b:Employee {name: 'Dr. Rivera'}))
RETURN p, length(p) AS hops;
-- All shortest paths between two nodes
USE my_zone.my_schema.my_graph
MATCH p = allShortestPaths((a:Employee {id: 1})-[*]-(b:Employee {id: 33}))
RETURN p, length(p) AS hops;
-- Shortest path with bounded depth
USE my_zone.my_schema.my_graph
MATCH p = shortestPath((a)-[*..10]->(b))
WHERE a.id = 1 AND b.id = 42
RETURN length(p) AS distance;
-- Use shortest path with FOREACH to mark path nodes
USE my_zone.my_schema.my_graph
MATCH p = shortestPath((a:Employee {id: 1})-[*]-(b:Employee {id: 33}))
FOREACH (n IN nodes(p) | SET n.on_path = true);
-- Shortest path with direction constraint
USE my_zone.my_schema.my_graph
MATCH p = shortestPath((a:Employee {name: 'Dr. Chen'})-[*]->(b:Employee {name: 'Dr. Rivera'}))
RETURN nodes(p) AS path_nodes, length(p) AS hops;

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →