Skip to content

Instantly share code, notes, and snippets.

@pierredewilde
Created August 19, 2011 08:19
Show Gist options
  • Save pierredewilde/1156323 to your computer and use it in GitHub Desktop.
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
\,,,/
(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]]
@pierredewilde
Copy link
Author

[[id:B]] syntax is not supported anymore. Updated to .filter{it.id==B}.

@pierredewilde
Copy link
Author

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