3

I have written the following query which returns all paths possible from the specified node.

g.V(<some_id>).repeat(bothE().bothV().simplePath()).emit().dedup().path()

If we had a simple graph of 3 nodes in this structure:

A -- edge_1 -> B -- edge_2 -> C

this query would return two paths:

A, edge_1, B
A, edge_1, B, edge_2, C

However, I only want to return all paths that terminate (i.e. there is no further nodes to traverse), which in this example would only return

A, edge_1, B, edge_2, C

Is this possible?

KOB
  • 4,084
  • 9
  • 44
  • 88

1 Answers1

1

You can try to do the repeat without the emit step and use until to stop at the end of the path.

g.V().hasLabel('A').repeat(bothE().bothV().
    simplePath()).
  until(bothE().simplePath().
    count().is(eq(0))).dedup().path()

example: https://gremlify.com/jqr8y7p24wb

noam621
  • 2,766
  • 1
  • 16
  • 26
  • There are actually some instances where I am not getting the results I want, such as when there are loops in the graph. Here is one such example: https://gremlify.com/ccocxr1uau. Also to add to my original post, I want all nodes and all edges to be returned in the paths, but no one path returned should be a "sub-path" of another. Any ideas how to correct this? – KOB Sep 09 '20 at 10:04
  • @KOB in this case of a circle with different edges if you change the `bothE` to `both` you will get the routes https://gremlify.com/tja7crvmg4b – noam621 Sep 09 '20 at 11:26
  • Ok it works, but I don't understand why the second path doesn't terminate at node 4, but instead continues to both nodes 3 and 2. Can you explian? Ideally I would want the second path just to have 1, D, 4 (i.e. returning the edge, D, that the first path didn't include. The first path already incldues 2, B, 3, C, so I don't need these returned in the second. – KOB Sep 09 '20 at 12:42
  • @KOB Because the two are distinguished by one edge from 1 to another, so no one is a subpath of the other, you need to consider if you really need to travel both directions and not going only on the out edges. – noam621 Sep 09 '20 at 12:56
  • 1
    Ok I see. I don't need to travel in both directions. In short, all I need is the minimal amount of minimal length paths that collectively traverse all nodes and edges in the graph, if that makes sense? I am open to any other suggestions on queries that would achieve this – KOB Sep 09 '20 at 13:46