0
votes

I was trying to run a gremlin query to find k distance vertices from a given vertex v and omit directly connected vertices to v.

I am using Gremlin 3.2.6.

So, something like this, for a k-distance(friend of friend) isn't working properly

g.V(v).both().as(“x”).repeat(both()).times(k).where(neq("x")).dedup()

The above should omit the vertices in "x" but it is not. My graph is directed and there may be edges in both directions between a pair of vertices.

Also, how do I make this general for given distances less than some k using loops(having a hard time with it) and is there some way to print the distance along with the vertex list. Thanks.

1

1 Answers

3
votes

x should actually be an aggregation of all adjacent vertices, not just a reference to the adjacent vertex on the current path.

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V(1).both().aggregate("x").
           repeat(both().dedup()).
             times(5).
             emit().
           where(without("x"))
==>v[1]
==>v[6]
==>v[5]

And if you also want to exclude the start vertex, just add it to the collection:

gremlin> g.V(1).store("x").
           both().aggregate("x").
           repeat(both().dedup()).
             times(5).
             emit().
           where(without("x"))
==>v[6]
==>v[5]