-1

A well understood problem in graph theory is to compute the shortest distance between two vertices.

What I want to do is find the shortest distance from a given vertex to the closest vertex which has a certain property-value (the exact vertex that this is, is unknown).

For example, find the shortest distance from V(1) to the closest vertex V(?) where V(?).property(color)==red.

An approach I've used for this before is to iteratively step outwards from a focal vertex, asking each unseen neighbor along the way whether it has that color=red.

I also upper bounded the number of outward steps for some added efficiency, i.e. limiting the search to the k step neighbourhood.

  1. Is there a better way to solve this problem?
  2. How might one code this using Gremlin? (I code primarily in Python and am unsure how to migrate the algorithm across)
Ian
  • 3,605
  • 4
  • 31
  • 66
  • Chances are this is not an under-theorized problem, and just that I havn't Googled well enough. – Ian Jul 11 '19 at 20:15

1 Answers1

1

As you said this is a well known problem in graph theory, and there are many algorithms, like: Dijkstra's, that solved this problem. you can read about them here.

Also you can find many gremlin recipes here, including a simple shortest path algorithm.

By changing the loop stop condition, I believe it will give you the desired result:

g.V(1).repeat(out().simplePath()).until(has('color', 'red')).path().limit(1)
noam621
  • 2,766
  • 1
  • 16
  • 26
  • This is really nice and elegant, thanks! As an extension, is there a way to traverse both outward and inward edges? (i.e. to assume that the graph is undirected when running this query) – Ian Jul 11 '19 at 22:03
  • 1
    Thanks noam I appreciate the help here – Ian Jul 11 '19 at 22:12
  • note also `shortestPath()` step in newer versions of Gremlin: http://tinkerpop.apache.org/docs/current/reference/#shortestpath-step – stephen mallette Jul 11 '19 at 22:30
  • Sorry to bother, but final question: do you know how I might modify the query above for passing *multiple* vertices? For example, `g.V()`, instead of say `g.V(1)`. I could simply take the limit statement out, but then it would compute multiple paths for the same focal vertex. Ideally I'd want to limit to 1, but *for each* vertex in `g.V()`. – Ian Jul 12 '19 at 04:36