Search code examples
neo4jcypherneo4j-apoc

Noe4j: height of a root node


I'm using Neo4j 3.5 version

I want to figure out the height of a node 'p' in the relationship (p:Person)-[r:CHILD*0..]->(c:Person).

Height of a node is defined as the distance between the root and the leaf node which is at the maximum distance from the root.

I want to figure it out for the > 100 000 nodes using apoc procedures. Could anyone please suggest how should I solve this problem.


Solution

  • I have figured it out:

    MATCH k = (p:Person)-[r:CHILD*0..]->(c:Person) 
    where p.name = "Krishna"
    with p, collect(length(k)) as score
    unwind score as e
    return p.code, max(e) as score
    

    Here,

    k is the length of the each path

    collect(length(k)) makes a list of lengths of paths to child nodes against each root

    unwind will be used to traverse each element of the list

    And then using max function we can figure out the farthest child node distance