Mapping all paths between nodes

134 views
Skip to first unread message

Jack

unread,
Mar 31, 2012, 7:16:22 PM3/31/12
to gremli...@googlegroups.com
Hello,
I am trying to map all paths between two nodes in a directed graph so I'll know how many paths are there in lenght 1, 2, 3...N etc.
The final result should look like: ['1':3, '2':7, '3':12] where the key is the path's lenght and the value is the amount of paths between A and B in that length.
I found this https://github.com/tinkerpop/gremlin/wiki/Finding-All-Paths-Between-2-Vertices (btw, it only works when using path*S* at the end) and tried saving the result to a list or map without any success.
This line:
g.v(A).out.loop(1){it.loops<=N && !(it.object.id in [A,B])}.filter{it.id==B}.paths[0]
does return the first path, but it wont save it to a list variable, and running a ".size()" returns a non related integer (e.g., 5 instead 2)
Any suggestions?
Thank you
Jack

Jack

unread,
Apr 1, 2012, 2:17:19 PM4/1/12
to gremli...@googlegroups.com
Found a solution, not sure it is optimal but after the ".paths" command I have added a ".toList()" on which I iterate using ".each" and populate a map based on each path's ".size()"

Marko Rodriguez

unread,
Apr 1, 2012, 2:42:52 PM4/1/12
to gremli...@googlegroups.com

Hi,

Gremlin 1.6-SNAPSHOT uses 'path' instead of 'paths'.

The way I would solve your problem is:

g.v(1)...path.transform{it.size()}.groupCount.cap.next()

HTH,
Marko.

http://markorodriguez.com

Jack

unread,
Apr 1, 2012, 3:26:09 PM4/1/12
to gremli...@googlegroups.com
Much more elegant, thanks!
 
By the way is 1.6-SNAPSHOT available for download as a compiled version?
 
Jack

Marko Rodriguez

unread,
Apr 2, 2012, 12:52:39 AM4/2/12
to gremli...@googlegroups.com
Hi,

Much more elegant, thanks!

Cool.

 By the way is 1.6-SNAPSHOT available for download as a compiled version?


Not for download like a stable release, but you can find the respective jars in the Sontatype Maven SNAPSHOT repository:


If you use Maven, simply add the dependency:

<dependency>
   <groupId>com.tinkerpop.gremlin</groupId>
   <artifactId>gremlin-groovy</artifactId>
   <version>1.6-SNAPSHOT</version>
</dependency>

        <repository>
            <id>sonatype-nexus-releases</id>
            <name>Sonatype Nexus Snapshots</name>
        </repository>

Enjoy!,
Marko.

Reply all
Reply to author
Forward
0 new messages