Traversal and Sampling

algo.randomWalk

Property Value

Procedure

algo.randomWalk

Category

Traversal / Sampling

Complexity

CPU

Min Args

2

Max Args

5

Syntax

CALL algo.randomWalk(startNode, steps [, relTypes, direction, seed])
YIELD path, steps

Parameters

Parameter Type Required Default Description

startNode

Vertex

Yes

—

Starting vertex

steps

Integer

Yes

—

Maximum number of steps to walk

relTypes

String

No

all types

Comma-separated relationship types

direction

String

No

"BOTH"

Traversal direction: "IN", "OUT", or "BOTH"

seed

Long

No

current time

Random seed for reproducibility

Yield Fields

Field Type Description

path

Path

The walk: the nodes visited and the relationships stepped over (see reference/graph-algorithms/path-finding.adoc#graph-algo-path-value)

steps

Integer

Actual number of steps taken (may be less than requested if dead-end reached)

Description

Performs a single random walk starting from the given vertex. At each step, one neighbor is chosen uniformly at random from available edges matching the direction and type constraints. The walk stops when the requested number of steps is reached or a dead-end (no valid neighbors) is encountered. Since v26.12.1 the walk reads the neighbours of the vertices it steps on and nothing else, with or without a Graph Analytical View, so its cost follows the number of steps rather than the size of the database (issue #12176).

Use Cases

  • Graph embedding feature generation (node2vec-style)

  • Sampling large graphs for approximate analysis

  • Stochastic exploration of graph neighborhoods

Example

MATCH (start:Page {id:'home'})
CALL algo.randomWalk(start, 20, 'LINKS_TO', 'OUT', 42)
YIELD path, steps
RETURN path, steps

References