Graph traversal is a search technique used to systematically visit and explore all the nodes in a graph. Its primary goal is to reveal and examine the structure and connections of the graph. There are two common strategies for graph traversal:
The BFS algorithm explores a graph level by level and proceeds as follows:
The following example demonstrates BFS traversal starting from node A, assuming neighbors are visited in alphabetical order (A–Z):


GQLINSERT (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]->(B), (A)-[:default]->(D), (B)-[:default]->(E), (C)-[:default]->(A), (E)-[:default]->(F), (F)-[:default]->(C), (G)-[:default]->(D)
| Name | Type | Default | Description |
|---|---|---|---|
startNode | STRING | / | Required. Starting node _id. |
maxDepth | INT | -1 | Maximum depth to traverse (-1 = unlimited). |
direction | STRING | out | Edge direction: in, out, or both. |
Returns:
| Column | Type | Description |
|---|---|---|
nodeId | STRING | Node identifier (_id) |
depth | INT | Depth from start node |
parent | STRING | Parent node in BFS tree |
GQLCALL algo.bfs({ startNode: "A" }) YIELD nodeId, depth, parent
Result:
| nodeId | depth | parent |
|---|---|---|
| A | 0 | |
| D | 1 | A |
| B | 1 | A |
| E | 2 | B |
| F | 3 | E |
| C | 4 | F |
Returns the same columns as run mode, streamed for memory efficiency.
GQLCALL algo.bfs.stream({ startNode: "A", maxDepth: 2 }) YIELD nodeId, depth RETURN nodeId, depth
Result:
| nodeId | depth |
|---|---|
| A | 0 |
| D | 1 |
| B | 1 |
| E | 2 |
Returns:
| Column | Type | Description |
|---|---|---|
nodeCount | INT | Total number of nodes visited |
maxDepth | INT | Maximum depth reached from start node |
GQLCALL algo.bfs.stats({ startNode: "A" }) YIELD nodeCount, maxDepth
Result:
| nodeCount | maxDepth |
|---|---|
| 6 | 4 |