gremlintinkerpoptinkerpop-blueprint

Gremlin > recursively find nodes connected by an edge type


Just working with the TinkerGraph, and attempting to recursively find nodes connected by a specific edge label (in this case created).

  1. Is there a way I can recursively(/loop) traverse nodes? In the example below, I want to loop until there are no more matching edges (instead of the hardcoded 3 value).
  2. Is there anyway to find and group connected Vertices, given a graph?

Extra kudos for deduplicating nodes, and handling node loops.

Dependencies

compile("com.thinkaurelius.titan:titan-berkeleyje:0.5.4")
compile('com.tinkerpop:gremlin-groovy:2.6.0')

Code (manually recurse 3 times :( )

Gremlin.load()
def g = TinkerGraphFactory.createTinkerGraph()
println g.v(5).as('x')
    .both('created')
    .dedup
    .loop(2){it.loops <= 3}
    .path
    .toList().flatten() as Set // groovy code to flatten & dedup

Gives me: (correct)

[v[5], v[4], v[3], v[1], v[6]]

Thanks!


Solution

  • You don't need any Groovy code, it can be done by only using Gremlin:

    gremlin> g.v(5).as('x').both('created').dedup()
    gremlin>     .loop('x') {true} {true}.dedup()
    ==>v[4]
    ==>v[3]
    ==>v[5]
    ==>v[6]
    ==>v[1]