Database Reference
In-Depth Information
Listing 8.1. Walking the entire graph depth-first using the Neo4j Traversal API
First, you need to initialize a traversal description, just like you did when you used the
Neo4jTraversal APIin chapter 4
.Thenyouconfigure thetraversal tofollowonlyout-
going relationships of type CHILD . Next, you set the traversal ordering to depth-first.
To do so, you use the dedicated method depthFirst() as part of the TraversalDe-
scription builder
.
Oncethedescriptioniscomplete,youstartthetraversalbyspecifyingthestartingnode
, and return all nodes you visit during the traversal . Remember, the return type of the
traversal result is a lazy Java Iterable instance. Just specifying the starting node and
calling nodes() on the traverser won't visit any nodes at that point. You need to start
iterating through the results for the traverser to start moving through the graph, which is
exactly what you do here . During iteration, you can process the visited nodes as you
wish,dependingonyoursoftwarerequirements.Inthisexample,yousimplyprintthenode
property "id" to the console.
The resulting output will prove that you've indeed used depth-first algorithm rules when
walking the graph:
1 -> 2 -> 5 -> 6 -> 3 -> 7 -> 8 -> 4 -> 9 ->
Note
Depth-first is the default branch-ordering strategy in Neo4j. If you don't specify the tra-
versal ordering when building your traversal description, depth-first will be used.
Search WWH ::




Custom Search