Search code examples
gremlintinkerpop3janusgraph

How can I cycle all the vertex that are connected?


In JanusGraph I have 4 nodes.
A(name=alice) -> B
B -> C
C -> D
D -> A

I want to get all the cycles from node A.

g.V().has('name', 'alice').both().both().cyclicPath().path().by('name')

I can get A->B->A and A->D->A. But I can't get A->B->C->D->A. I want to get all the path from vertex A to Vertex A. How can I get it using groovy language?


Solution

  • You are only traversing away from "alice" twice with both().both() therefore your path can only be length 3 which would include the "alice" vertex and the two vertices at each step away from "alice" (i.e. both() is one step and the second both() is the next step). If you want to get do "D" you have add yet another both() so that Gremlin can traverse another set of edges and I would think yet another to get back to "alice" from "D".

    Or, you could simply use repeat():

    g.V().has('name', 'alice').
      repeat(both()).emit().times(4).
      cyclicPath().
      path().by('name')
    

    Note that I controlled the loop with times(4) to define how many steps away from "alice" I wanted to traverse both(). You could also use until() to control that for a more dynamic way to control loop termination - see repeat() for more information.