Reputation: 157
I'm still at my earlier stages with graphs and gremlin.
Is it possible to randomly select graph vertices in Gremlin?
Consider the following pipeline that gets the cars owned by a user's friend:
u.out('Friend')[0..9].out('Drives').map()
But this code is only executed against the first 10 friends every time. How can I make the selection random efficiently?
Thank you for your time :)
Upvotes: 3
Views: 1845
Reputation: 46216
In Gremlin 2.x you could use random step as in:
g.v(1).out.random()
or in 3.x random
has become coin:
as in:
g.V(1).out.coin()
in 3.x, you might also look at sample and order(shuffle) steps in 3.x.
Upvotes: 6
Reputation: 1552
You could use shuffle
:
g = TinkerGraphFactory.createTinkerGraph()
gremlin> g.v(1).out.shuffle[0]
==>v[3]
gremlin> g.v(1).out.shuffle[0]
==>v[2]
gremlin> g.v(1).out.shuffle[0]
==>v[3]
gremlin> g.v(1).out.shuffle[0]
==>v[4]
This solution isn't very efficient though since all neighbours of v(1)
need to be fetched.
This might help as well: Random Walk on Bipartite Graph with Gremlin
Upvotes: 1