João Cota
João Cota

Reputation: 117

Neo4j max depth and path

The simple question is: How to calculate the maximum depth value and the maximum depth path from a node using neo4j (2.3.11) cypher?

Explanation of my scenario

I have node label called person and relationship types called supervised.

Think in a scenario: a node 1 can supervise 2, 2 can supervise 3 and 4, 4 can supervise 5 and so on.

So 1 need of:

  1. I want to make a cypher, that receive the id, e.g. 1(personID), as parameter, and return the path [1, 2, 4, 5 ....]

  2. And another cypher that returns the path count.


I tried that cypher (neo4j 2.3.11) for the second case, without success:

MATCH (person:Person)-[SUPERVISED*]->()
WITH person, LENGTH(SUPERVISED) AS depth
where person.personID = 'la3486551918237738'
RETURN person, depth;

Based in this solution

Upvotes: 0

Views: 1640

Answers (1)

cybersam
cybersam

Reputation: 66989

You can get the max depth and the list of IDs in a single query:

MATCH path=(person:Person)-[:supervised*]->(x)
WHERE person.personID = 'la3486551918237738' AND NOT EXISTS((x)-[:supervised]->())
WITH person, path
ORDER BY LENGTH(path) DESC
LIMIT 1
RETURN person, [n IN NODES(path) | n.personID] AS ids, LENGTH(path) AS depth

Notes:

  • I assume that supervised is the required relationship type. Note that a colon must precede the relationship type when used in a Cypher pattern, as in [:supervised*].
  • The NOT EXISTS((x)-[:supervised]->()) test requires that the paths found end with a leaf node.
  • ORDER BY LENGTH(path) DESC LIMIT 1 picks the longest path.

[UPDATE]

I found the following problems in your sample data linked to in the comments:

  1. None of the Relations.csv file's studentIDs appear as a personID in Nodes.csv. (Since you indicated that this is just an oversight in the sample, I just fixed this in my own copy of the files.)
  2. The first LOAD CSV query contains an unneeded OPTIONAL MATCH.
  3. The second LOAD CSV query is requires the csvFile.institution property, but the Relations.csv file does not contain an institution header (it has a university_name header instead). So, replacing csvFile.institution with csvFile.university_name fixes the second query.

Upvotes: 2

Related Questions