简体   繁体   中英

How to select random vertices in Gremlin

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 :)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM