How can we find how many levels of a particular hierarchy exists?

What I want to do

Get the max depth of hierarchy of a Company nodes.

What I did

I have nodes Company, and then it is connected down to other Company (children of it) by “subsidiary” edge.

for eg:
A->B->D->E
|
→ C
I need to get that max depth is 3.

Hey @aditya_shelar. I’m not quite grokking what you’re trying to do. Can you share a schema and some queries that you’ve tried?

It depends on how you want to use this info.
For example if it is in a client logic a query like

{
  list(func: uid(0x6241ae)) @recurse @normalize{
    name:name
    subsidiary 
  }
}

will return (in my case I have A->B->C)

{
  "data": {
    "list": [
      {
        "name": [
          "A",
          "B",
          "C"
        ]
      }
    ]
  },

With that, data["list"][0]["name"].length corresponds to the depth of the traversal.

More details needed to see if variables and math function could help you too…

1 Like

Thanks for the reply! This did the job for me with some addition.

In the list object I got multiple objects data is like tree structure, so did groupby and calculated the max value.

schema:

<project>: uid @reverse .
<id>: string @index(hash) .
<name>: string .
<subsidiary>: [uid] @reverse .

type <Company> {
     id
    project
    name
    subsidiary
}
{
   var(func: uid(0x6241ae)) @recurse{
    a as uid
    name:name
    subsidiary 
  }
  var(func:uid(a)) @groupby(subsidiary) {
      c as count(uid)
  }
  res(){
      maxlevels :max(val(c))
  }
}

Let me know if you have any other optimal way to do this.