2
votes

I'm very new to Gremlin.

I have a Pregel (GraphX) algorithm that is doing the following: given a list of vertices, finds all the network neighborhoods up to level N (adjacent vertices, and vertices adjacent to them, repeating N times. so that the number of edges between the furthest vertex is no more than N)

In Pregel this is basically just doing a shortest paths traversal starting at the source vertices, but stopping after N iterations.

How using Gremlin (tinkerpop3) can i achieve the same behavior?

Example use cases are - in a social networks, find all friends, friends of friends and friends of friends of friends of an individual, etc...

It sounds like a very common use case but I couldn't find the syntax to do it.

1

1 Answers

1
votes

A possible solution:

g.V(1, 2, 3).repeat(
  bothE().dedup().store('edges').bothV().dedup().store('vertices')
).times(2).cap('vertices', 'edges')

This returns an object with a key vertices containing an array of vertices and a key edges containing an array of edges.

The returns vertices and edges are the neighborhood of vertices with ids (1, 2, 3) up to distance 2.