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 Breadth-First Search (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):


Run the following statements on an empty graph to define its structure and insert data:
INSERT (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);
To load the entire graph to the HDC server hdc-server-1 as my_hdc_graph:
CREATE HDC GRAPH my_hdc_graph ON "hdc-server-1" OPTIONS { nodes: {"*": ["*"]}, edges: {"*": ["*"]}, direction: "undirected", load_id: true, update: "static" }
Algorithm name: traverse
Name | Type | Spec | Default | Optional | Description |
|---|---|---|---|---|---|
ids | _id | / | / | No | Specifies the node to start traversal by its _id. |
uuids | _uuid | / | / | No | Specifies the node to start traversal by its _uuid. |
direction | String | in, out | / | Yes | Specifies to traverse through only incoming edges (in) or outgoing edges (out). |
traverse_type | String | bfs | bfs | Yes | To traverse the graph in the BFS fashion, keep it as bfs. |
return_id_uuid | String | uuid, id, both | uuid | Yes | Includes _uuid, _id, or both to represent nodes in the results. |
algo(traverse).params({ projection: "my_hdc_graph", return_id_uuid: "id", ids: ['A'], direction: 'out', traverse_type: 'bfs' }).write({ file: { filename: "visited_nodes" } })
Result:
File: visited_nodesnode,parent D,A F,E B,A A,A E,B C,F