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]] |
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
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.