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}] |
+-------------------------------------------------------------------------------------------+