Get all paths in a graph from a node, but only ones which terminate

497 Views Asked by At

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?

1

There are 1 best solutions below

5
On

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