Variable-Length Paths

Variable-length path patterns match paths of varying depth in a single query.

Syntax

MATCH (a)-[*min..max]->(b)
  • *1..3 — match paths of length 1 to 3
  • *2 — match paths of exactly length 2
  • *2.. — match paths of length 2 or more
  • * — match paths of any length (at least 1)

A type, or several separated by |, restricts every hop: -[:KNOWS|LIKES*1..2]->.

Each path is one result. A path uses each relationship at most once, but may pass through a node more than once, including the one it started from. A node reached by two different paths is therefore returned twice, which is why the examples below use DISTINCT where they want each node once.

The relationships of a path also count toward the rest of its MATCH: no other relationship of the same MATCH, single-hop or variable-length, can use one of them again. See Relationship Uniqueness.

Examples

Fixed Range

// Friends of friends (exactly 2 hops)
MATCH (a:Person {name: "Alice"})-[:KNOWS*2]->(b:Person)
RETURN b.name

Bounded Range

// Reachable within 1 to 3 hops
MATCH (a:Person {name: "Alice"})-[:KNOWS*1..3]->(b:Person)
RETURN DISTINCT b.name

Open-Ended

// All reachable nodes (2+ hops)
MATCH (a:Person {name: "Alice"})-[*2..]->(b)
RETURN b

Any Length

// All nodes reachable at any depth
MATCH (a:Person {name: "Alice"})-[*]->(b)
RETURN DISTINCT b

The relationships of the path

A variable on a variable-length relationship is the list of the relationships the path went through, in the order it walked them; zero hops give an empty list:

MATCH (a:Person {name: "Alice"})-[r:KNOWS*1..3]->(b:Person)
WHERE all(k IN r WHERE k.since >= 2020)
RETURN b.name, size(r) AS hops, [k IN r | k.since] AS years

The variable is a list, not a relationship: DELETE r and SET r.x = 1 are errors, while r[0], size(r), comprehensions and quantifiers work on it.

An inline property map holds for every relationship of the path: -[:KNOWS*1..3 {since: 2020}]-> matches paths whose relationships all have since = 2020. Paths are walked first and filtered after, so a selective map on an unbounded pattern still costs as much as the walk without it.

If the far end is a node that is already bound, only paths that end at it are returned.

Shortest Paths

shortestPath finds one shortest path between two nodes, and allShortestPaths every path of that shortest length:

MATCH (a:Person {name: "Alice"}), (b:Person {name: "Dan"})
MATCH p = shortestPath((a)-[:KNOWS*]-(b))
RETURN length(p), [n IN nodes(p) | n.name] AS route

The pattern inside is one relationship between two nodes, of any direction and types, with an optional upper bound (*..6) and a lower bound of 0 or 1. The ends can be bound earlier or matched by the pattern itself, with labels and properties; a WHERE on the ends filters them. For each pair of ends the search runs breadth-first from both ends at once and stops at the first length at which they meet, so it reads the neighbourhoods of the two ends, never every path. Pairs that are not connected within the bound give no row, or null after OPTIONAL MATCH. A relationship variable is the list of the path's relationships. From a node to itself the path has no relationships when the lower bound is 0, and there is none otherwise.

Not supported, each with an error naming it: a property map on the relationship, a pattern of more than one relationship, a lower bound above 1, shortestPath outside MATCH (in CREATE, MERGE, EXISTS { } or as an expression), and a WHERE right after the MATCH that reads the path or its relationships, since the search would not look for a longer path that passes. The relationships of a shortest path are not kept apart from the other relationships of its MATCH.

Each pair of ends is searched on its own, so ends that match many nodes multiply the work: give both ends labels and properties, or bind them first. On a graph of 100,000 nodes and 1,000,000 random directed relationships (ReleaseFast, macOS), a search between two random nodes, about five hops apart, took a median of 0.34 ms one way and 0.16 ms undirected; allShortestPaths, 0.19 ms for about five paths per pair.

Performance

Paths are expanded depth-first, one path at a time. There is no hop limit: an unbounded * or *n.. ends because a path cannot use more relationships than exist. The number of paths, though, can grow exponentially with the size of a densely connected graph, so give unbounded patterns over large, well-connected graphs an upper bound.

  • Short paths (1-3 hops): microseconds
  • The cost grows with the number of paths, not the number of reachable nodes
  • DISTINCT removes the duplicate rows that several paths to one node produce

On a graph of 10K nodes with 5 outgoing edges each, MATCH (a:Person {uid: $uid})-[:KNOWS*1..5]->(b) RETURN count(b) enumerates about 3,900 paths in 0.47 ms (0.60 ms at 100K nodes), about 0.12 to 0.15 us per path. See Benchmarks for how this was measured.