我已经创建了节点和关系。我需要从第一个节点遍历到最后一个节点。我希望输出显示为 印度->泰米尔纳德邦(Tamil Nadu)->钦奈(Chennai)->坎奇普兰(Kanchipuram)->旺达卢(Vandalur)。
您知道如何显示此路径吗?
以下用于创建节点和关系的查询
CREATE (in:place {name:"India", continent:"Asia", Language:"English"}),
(tn:place {name:"Tamil Nadu", continent:"Asia", Language:"Tamil"}),
(ap:place {name:"Andra Pradesh", continent:"Asia", Language:"Telugu"}),
(ch:place {name:"Chennai", continent:"Asia", Language:"Tamil"}),
(co:place {name:"Coimbatore", continent:"Asia", Language:"Tamil"}),
(ka:place {name:"Kanchipuram", continent:"Asia", Language:"Tamil"}),
(th:place {name:"Thiruvallur", continent:"Asia", Language:"Tamil"}),
(va:place {name:"Vandalur", continent:"Asia", Language:"Tamil"}),
(pa:place {name:"Padapai", continent:"Asia", Language:"Tamil"}),
(in)- [:parent] ->(tn),
(in)- [:parent] ->(ap),
(tn)- [:parent] ->(ch),
(tn)- [:parent] ->(co),
(ch)- [:parent] ->(ka),
(ch)- [:parent] ->(th),
(ka)- [:parent] ->(va),
(ka)- [:parent] ->(pa)
答案 0 :(得分:0)
Neo4j提供了shortest path
算法,其他图形数据库可能有所不同(请参见documentation)。
MATCH (start:place { name: "India" }), (end:place { name: "Vandalur" }), p = shortestPath((start)-[:parent*]-(end)) RETURN p ;
如果您拥有干净的树结构,则还可以从目标节点开始,找到所有传入的parent
关系,这些关系最终将到达根节点,如下所示:
MATCH (start:place { name: "Vandalur" })<-[:parent*]-(root:place)
RETURN start, root;
答案 1 :(得分:0)
MATCH (a:place)--(b:place)--(c:place)--(d:place)--(e:place)
RETURN a,b,c,d,e;