0
votes

I have a tree with about 300 nodes. For an arbitrary node, I need to draw a subtree from that node to the root (including all possible paths).

For example if I have this tree (edited):

  a
  |
-----
| | | 
b c d
| |
---
 |
 e
 |
 f

and the e node is selected, I need to draw:

 a
 |
---
| |  
b c
| |
---
 |
 e

I am using this Cypher query:

start n=node({nodeId}) optional match n-[r:DEPENDS*]->p return n,r,p

Although it works, depending on the depth of the searched node, it is very very slow (more than 10 seconds).

¿How can I achieve this efficiently?

1
How deep is your tree? Which Neo4j version do you use?Michael Hunger
I'm using Neo4j 2.1.0 and the worst case is a depth of about 20 levelsIsidroGH

1 Answers

0
votes

Your query will compute all paths, while you are only interested in the one path too the root. So get the root and the node and the shortest-path in beween.

MATCH path=shortestPath((root)<-[:DEPENDS*]-(n))
WHERE id(root) = {rootId} and id(n) = {nodeId}
RETURN path