Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Neo4j 在树中,使用密码计算每个节点到根的距离_Neo4j_Tree_Cypher - Fatal编程技术网

Neo4j 在树中,使用密码计算每个节点到根的距离

Neo4j 在树中,使用密码计算每个节点到根的距离,neo4j,tree,cypher,Neo4j,Tree,Cypher,给一棵这样的树 a / \ b c / \ d e | f 我想编写一个返回我的密码查询: startPoint: f, endPoint: a, path: [{f,3},{d,2},{b,1},{a,0}] startPoint: d, endPoint: a, path: [{d,2},{b,1},{a,0}] startPoint: e, endPoint: a, path: [{e,2},{b,1},{a,0}] startPoint: b,

给一棵这样的树

     a
    / \
   b   c
  / \
 d   e
 |
 f
我想编写一个返回我的密码查询:

startPoint: f, endPoint: a, path: [{f,3},{d,2},{b,1},{a,0}]
startPoint: d, endPoint: a, path: [{d,2},{b,1},{a,0}]
startPoint: e, endPoint: a, path: [{e,2},{b,1},{a,0}]
startPoint: b, endPoint: a, path: [{b,1},{a,0}]
startPoint: c, endPoint: a, path: [{c,1},{a,0}]

(以startPoint的任何特定顺序)

假设您的关系在树中是“向下”的,并且每个节点都有一个
id
属性,那么这应该可以工作:

MATCH p=(a)<-[*]-(b {id:'a'})
WITH a, b, NODES(p) AS pts, LENGTH(p) AS n
RETURN
  a.id AS startPoint,
  b.id AS endPoint,
  REDUCE(s = [], i IN RANGE(0, n) | s + {id: (pts[i]).id, depth: n-i}) AS path;

请详细说明您在做“想做”的事情时面临的问题,并访问以了解如何更清楚地表达您的问题。。
+-------------------------------------------------------------------------------------------+
| startPoint | endPoint | path                                                              |
+-------------------------------------------------------------------------------------------+
| "b"        | "a"      | [{id=b, depth=1},{id=a, depth=0}]                                 |
| "d"        | "a"      | [{id=d, depth=2},{id=b, depth=1},{id=a, depth=0}]                 |
| "f"        | "a"      | [{id=f, depth=3},{id=d, depth=2},{id=b, depth=1},{id=a, depth=0}] |
| "e"        | "a"      | [{id=e, depth=2},{id=b, depth=1},{id=a, depth=0}]                 |
| "c"        | "a"      | [{id=c, depth=1},{id=a, depth=0}]                                 |
+-------------------------------------------------------------------------------------------+