BFS and DFS

Choose the order a walk reaches records in.

Syntax

Write BFS or DFS after the edge collection.

FOR v IN 1..3 OUTBOUND "gid://namespace/resource/id" name_edge BFS
FOR v IN 1..3 OUTBOUND "gid://namespace/resource/id" name_edge DFS

Parameters and arguments

  • BFS

    Nearest first, one level at a time. This is the default order

    Required: No
  • DFS

    One branch to its end before the next

    Required: No

Examples

A breadth-first walk. The content store holds no edges, so the answer shows the shape with no results.

Return type and behaviour

BFS returns the records nearest to the start first, ordered by depth and then by record. DFS follows one branch to its end, taking the records in order, before it moves to the next. Results are capped at 10,000 rows, so with a cap the two orders can return different sets.

Errors and exceptions
ErrorWhat happenedHow to recover
HTQL-1003The traversal is not written in HTQL's grammarCheck the range, the direction, the start and the edge collection
HTQL-2012The edge collection's name is not in lower snake caseWrite a name such as placed_by_edge
Placement and context

The order keyword follows the edge collection and comes before PRUNE.

Best practices
  • Use BFS when you want the nearest records first.
  • Use DFS to follow a chain to its end.
  • Leave the keyword out when you want the default, BFS.