docjosh
docjosh

Reputation: 165

AQL Path from Node to Leaf

I am new to Arango and I am trying to understand the 'right' way to write some queries. I read (https://www.arangodb.com/docs/stable/graphs-traversals-using-traversal-objects.html) and (http://jsteemann.github.io/blog/2015/01/28/using-custom-visitors-in-aql-graph-traversals/), since they always popped up when searching for what I am trying to do. In particular, I have a graph where a given node has a single path (via a certain 'type' of edge) from that node to a leaf. Something like x -a-> y -a-> z. Where a is the edge type, and x,y,z are the nodes. These paths can be of arbitrary length. I would like to write an AQL query that returns the single 'longest' path from the starting node to the leaf node. I find that I always get every sub-path and would then have to do some post-processing. The traversal objects looked like they supplied a solution to this issue, but it seems they are now deprecated. Is there a correct way to do this in AQL? Is there a document that shows how to do what steemann does in his article, but only using AQL? Is there some great AQL documentation on graph queries other than what is on the arangodb site (all of which I have already read, including the graph presentation and the udemy course)? If not, I would be happy to write something to share with the community, but I am not sure yet how to do this myself, so I'd need some pointers to material that can get me started. Long, short, I'd just like to know how to run my query to find the path from node to leaf. However, I'd be happy to contribute once I see how things should be done without traversal-objects. Thank you for your help

Upvotes: 1

Views: 496

Answers (2)

Galdor
Galdor

Reputation: 1985

works for me:

LET longest = LAST (
  FOR vertex, edge, path IN 1..100
  OUTBOUND x
  a
  RETURN path
)

Upvotes: 0

CodeManX
CodeManX

Reputation: 11915

Taking a traversal in OUTBOUND direction as example, you can do a second traversal with depth = 1 to check if you reached a leaf node (no more incoming edges). Based on this information, the “short” paths can be filtered out.

Note that a second condition is required: it is possible that the last node in a traversal is not a leaf node if the maximum traversal depth is exceeded. Thus, you need to also let paths through, which contain as many edges as hops you do in the traversal (here: 5).

LET maxDepth = 5
FOR v, e, p IN 1..maxDepth OUTBOUND "verts/s" edges
  LET next = (
    FOR vv, ee IN OUTBOUND v edges
      //FILTER ee != e // needed if traversing edges in ANY direction
      LIMIT 1 // optimization, no need to check for more than a single neighbor
      RETURN true // using a constant here, which is not actually used
  )
  FILTER LENGTH(next) == 0 || LENGTH(p.edges) == maxDepth
  RETURN CONCAT_SEPARATOR(" -> ", p.vertices[*]._key)

Cross-post from https://groups.google.com/g/arangodb/c/VAo_i_1UHbo/m/ByIUTqIfBAAJ

Upvotes: 1

Related Questions