1
votes

The following stack overflow question

How to increase performance of shortest path using Gremlin?

shows how to find the shortest path from a start a single starting vertex with id 687 to an ending vertex with id 1343 and does so efficiently by ensuring no paths are repeated using store, without, and aggregate

g.V(687).store('x').repeat(out().where(without('x')).aggregate('x')).until(hasId(1343)).limit(1).path()

I would like to perform the same query with the same level of efficiency, however I need all the shortest paths from multiple starting vertices with the same label all to the same ending vertex, for example it would look something like this (though this doesn't work)

g.V().hasLabel('label').store('x').repeat(out().where(without('x')).aggregate('x')).until(hasId(1343)).limit(1).path()

I have tried multiple constructs with two repeats in the statement, but am not able to get an independent store('x') for each starting vertex. I am also using AWS Neptune platform, so it restricts the usage of Gremlin where loops/scripts are not allowed. All gremlin queries must start with g. and be composed of commands chained together with .

https://docs.aws.amazon.com/neptune/latest/userguide/access-graph-gremlin-differences.html

1

1 Answers

2
votes

This technique can not be applied to multiple start vertices. However, you can just start from the other side as this is a single known vertex:

g.V(1343).store('x').
  repeat(__.in().where(without('x')).aggregate('x')).
    until(hasLabel('label')).
  path()

If one of the start vertices can be part of the path of another start vertex, then you may not break at a potential start vertex and instead do this:

g.V(1343).store('x').
  repeat(__.in().where(without('x')).aggregate('x')).
    emit(hasLabel('label')).
  path()