I have a database with the following structure.
The property node is of the type
create (A:Property {value:"abc"})
How to do a dfs so that it will be able to print all the values in the graph.In the order A->B->E->F->C->G->H->D->I->J
The relationship r is in the downward direction(single direction) with no properties.I have tried this link but looks complex to me.
Is there an easier way to do a simple dfc on an already existing Neo4j database
The link you linked to is very verbose to cover all the different things you can do with Neo4j's powerful Traversal API.
I think all you have to do is this:
Should print
A->B->E->F->C->G->H->D->I->J->
You can make the print statement smarter by not appending the arrow at the last node but I'll leave that up to you
EDIT
After trying the code myself I got a depth first search but the iterator order was out of order. It seemed it arbitrarily picked which child node to walk first. So I got output like
A->D->J->I->C->H->G->B->F->E->
.So you have to sort the returned Paths of the
TraversalDescription
which has asort(Comparator<Path> )
method.To match the traversal you want, I sorted the paths by the node property that gives the node its name, which I called "id". Here's my updated traversal code:
Where PathComparatorByName is a comparator I wrote that sorts Paths based on the nodes traversed in the path lexigraphically sorted by name:
Rerunning it now with the comparator will output: