Search code examples
gremlinamazon-neptune

How do I return in Gremlin only paths which ends with vertices that other paths didn't end with yet?


Let's assume I have the following graph:

A->B, B->D
A->C, C->D
A->E

I want to return all paths which start from A, but don't include multiple paths which end with the same vertex. So in case, the result will be:
A->B->D (or A->C->D) and A->E

Since the graph is very big and there can be a lot of paths, if there is an efficient way the engine can prune the paths without collecting all of them first, it is preferable.


Solution

  • Depending upon how cyclic the data in the graph is, this can be an expensive query. I am assuming each eventual target is a leaf node. In such cases, the basic query might end up something like this:

    g.V('A').
      repeat(out().simplePath()).
      until(not(out())).
      where(without('x')).store('x').
      path()