Creating a level attribute for speed

My graph is a simple tree structure that has a root node (inode:0) and then has various branches off the tree. Some of these are 1000 levels deep. My graph shows "CALLS" as well as "RUNS" but we only need to worry about "RUNS" ( Calls drop off much faster)

I have been using the number parameter on the match [*3] but when we run larger programs, it gets slower and slower. Not to mention that the UI folks get a bit annoying when you start at a node in level 3 and need to see 3-5.

I have written a python to a) get the longest chain. b) work up from the bottom to the top and assign that a level.

But that seems terribly clumsy. Fortunately, the graph is relatively static in terms of structure. Attributes change but not structure.

Is there a clever way to do this within Cypher ? Assign each node a level based on the distance from 0 ?

Some followup information as requested.

  1. we really only need it for "RUNS" so yes, I can safely ignore "CALLS"
  2. The largest is about 1000. Rare that it gets over 800 but it does happen.
  3. Speed for setting the level isn't a key. Getting it back is.

Thanks - Any help would be good.

Any thoughts ? Thanks

Assign each node a level based on the distance from 0 ?

Sounds good to me, if the distance to the root is pretty static.
Do the work once on insertion instead of on every MATCH.

I can't do it on insertion. They come from different sources so literally as each file processes it only knows its parent and its child. So doing it up front is not practical. My current approach sets them all to the max, and then does a count down **200, **199, **198 etc etc which is wickedly in efficient.

My alternative was to go path by path and update the nodes in succession but that doesn't work well either.

I'm looking for a more neo4j ish approach. Thanks

I see. But are the insertions completely arbitrary (meaning the node and its parent node possibly float around in isolation), or is the node a new node connects to already connected to the root node (through others)?
In which case a new node could inherit the level of its parent node and increment it.

Hi Bill,

Interesting problem I have a question. How do you decide a level associated with a node? This is a bit ambiguous to me as two nodes can either be at same level or different levels, how do you decide which node should be placed at what level ? Thanks in advance.

We have one “tree” that is balanced ( one parent to one child ) fairly well. The “RUNS” relationship is very structured. Other relationships are less balanced and that’s ok - this is the only one we need.
We are following the direction -> always



So one node can only have a maximum of two children connected by :RUNS relationship?

Sorry - I said that badly

It can have lots of Children but one parent. And siblings are unconnected

Hi Bill,

This link by @mark.needham can help you in solving the problem.
Link: Neo4j: Cypher - Creating a time tree down to the day | Mark Needham


Thanks - I don’t see the relevance and that’s quite possibly because the design is so different. Can you help me understand how they connect ? Thanks