如何将多条子路径组合成一条路径
发布于 16 天前 作者 commissarster 94 次浏览 来自 问答

原始数据如下:

MERGE (a:Loc {name:'A'})
MERGE (b:Loc {name:'B'})
MERGE (c:Loc {name:'C'})
MERGE (d:Loc {name:'D'})
MERGE (e:Loc {name:'E'})
MERGE (f:Loc {name:'F'})

MERGE (a)-[:ROAD {cost:50}]->(b)
MERGE (a)-[:ROAD {cost:50}]->(c)
MERGE (a)-[:ROAD {cost:100}]->(d)
MERGE (b)-[:ROAD {cost:40}]->(d)
MERGE (c)-[:ROAD {cost:40}]->(d)
MERGE (c)-[:ROAD {cost:80}]->(e)
MERGE (d)-[:ROAD {cost:30}]->(e)
MERGE (d)-[:ROAD {cost:80}]->(f)
MERGE (e)-[:ROAD {cost:40}]->(f);

我使用k-shortest算法找到两点之间的最短路径

 MATCH (start:Loc{name:'A'}), (end:Loc{name:'F'})
CALL algo.kShortestPaths.stream(start, end, 1, 'null' ,{nodeQuery:"Loc",relationshipQuery:"ROAD"})
YIELD index, nodeIds, costs
with algo.getNodesById(nodeIds) as nodes
UNWIND range(0, length(nodes)-2) as idx
WITH nodes[idx] as from, nodes[idx + 1] as to 
MATCH p=(from)-[:ROAD]-(to)
return p

但这里返回的p是多段路径,我希望能返回一条路径 。虽然我使用algo.kShortestPaths.stream的path:true可以返回一条路径,但那条路径返回的关系是虚拟关系,并不是我想要的。所以,我想问一下,要如何将多段路径组合成一条路径?

回到顶部