简体   繁体   中英

How to do an efficient random walk in arangodb?

I am working on a graph database on ArangoDB and I'm trying to have paths through a random walk. My purpose is from a start vertex V, I get for instance 4 random paths with a specified depth.

As far as from now, I've found the following code that works :

FOR vertex, edge, path IN 4..4 ANY 'Vertex/417438' edge_collection OPTIONS {bfs: TRUE, uniqueVertices : True, uniqueEdges : True}
    SORT RAND()
    LIMIT 3
    FILTER  IS_SAME_COLLECTION('Vertex', vertex)
    RETURN path

This indeed gives me 3 paths with a depth of 4, but it takes quite a long time because of the SORT RAND() at the beginning. I guess it first sorts randomly all the possible solution and then returns the solution.

Do you think, there's a way to have random solutions that costs less time?

Thanks for your time and for your futures answers

I've just discussed with someone from the dev Team of Arango. Getting a random node in AQL is not possible for the moment. It's in there roadmap however.

I found a way to do a random Walk on ArangoDB though.

Let's take v , the starting vertex.

  1. Query the number of neighbors N of v with this command :

     FOR clip, edge, path IN ANY '${vertex}' hasClipped OPTIONS {bfs: TRUE, uniqueVertices : True, uniqueEdges : True} COLLECT WITH COUNT INTO len RETURN len 
  2. Then choose a random number rd between 0 and N and execute the following request :

     FOR obj IN ANY '${vertex}' hasClipped OPTIONS {bfs : true, uniqueVertices : 'path'} LIMIT ${rd}, 1 RETURN obj 

    This request will return a random neighbor among the existing ones.

  3. Iterate to get a random walk with the depth you want.

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