1
votes

I'm trying to traverse all the nodes of a graph starting from a given root node. While traversing I want to "fix up" the edge from the previous vertex to the current vertex, so that the edge goes from the previous vertex to the current one (It may be the other way around, but the end graph should be a tree with the root vertex at the root).

The problem seems to be that the edges being traversed aren't all strictly out or in edges.

I'm trying to create a breadth first traversal (should I not do breadth first?), but since there are both in and out edges the query I started with looks like this

g.v('rootVertex').as('x').both.gather.scatter.loop('x'){true}{true}

This seems to be creating an infinite loop due to the ".both" (I think).

Is there a way to do this in gremlin that doesn't create an infinite loop and allows me to fix / verify the edge from the previous node?

Also, could I get a simple illustrative example of the "gather.scatter" part of the query? I'm having trouble visualizing what it exactly is that that step is achieving.

1

1 Answers

3
votes

Add a simplePath step to prevent the traversal from cycling between two nodes:

g.v('rootVertex').as('x').both().simplePath().loop('x'){true}{true}