Created
August 19, 2011 08:19
-
-
Save pierredewilde/1156323 to your computer and use it in GitHub Desktop.
Finding all paths between 2 nodes in a directed and undirected graph using Gremlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\,,,/ | |
(o o) | |
-----oOOo-(_)-oOOo----- | |
gremlin> g = TinkerGraphFactory.createTinkerGraph() | |
==>tinkergraph[vertices:6 edges:6] | |
gremlin> A = '1'; B = '5'; N = 3 | |
==>3 | |
gremlin> g.v(A).out.loop(1){it.loops<=N && !(it.object.id in [A,B])}.has('id',B).path | |
==>[v[1], v[4], v[5]] | |
gremlin> g.v(A).both.loop(1){it.loops<=N && !(it.object.id in [A,B])}.has('id',B).path | |
==>[v[1], v[4], v[5]] | |
==>[v[1], v[3], v[4], v[5]] | |
gremlin> | |
gremlin> A = '5'; B = '1'; N = 3 | |
==>3 | |
gremlin> g.v(A).out.loop(1){it.loops<=N && !(it.object.id in [A,B])}.has('id',B).path | |
gremlin> g.v(A).both.loop(1){it.loops<=N && !(it.object.id in [A,B])}.has('id',B).path | |
==>[v[5], v[4], v[1]] | |
==>[v[5], v[4], v[3], v[1]] | |
gremlin> | |
gremlin> A = '1'; B = '6'; N = 3 | |
==>3 | |
gremlin> g.v(A).out.loop(1){it.loops<=N && !(it.object.id in [A,B])}.has('id',B).path | |
gremlin> g.v(A).both.loop(1){it.loops<=N && !(it.object.id in [A,B])}.has('id',B).path | |
==>[v[1], v[3], v[6]] | |
==>[v[1], v[4], v[3], v[6]] |
Never mind my previous comment about the filter not working correctly. It does now that I translated correctly.
Thanks again for the extremely helpful posting.
You're welcome. Happy to see how stigmergy(*) is helpful.
Pierre
[[id:B]] syntax is not supported anymore. Updated to .filter{it.id==B}.
Replace .paths with .path
Replace .filter{it.id==B} by .has('id',B)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks so much for providing this solution, which I found in another post linked to this page. In that post, you say:
If [[id:B]] is too cryptic, replace it with .filter{it.id==B}
I tried to replace it, since I don't understand exactly what [[id:B]] is doing, but the filter does not seem to return any paths and it runs nearly forever. Could you elaborate on this?
Thanks.
http://groups.google.com/group/gremlin-users/browse_thread/thread/714f173835798262/8b81908e21380b1a?show_docid=8b81908e21380b1a