Neo4j - apoc.algo.dijkstra - 从a到b的最短路径,但是通过c,d,e等

时间:2017-06-06 13:07:46

标签: neo4j cypher dijkstra

您好我正在尝试使用neo4j进行路由,但我正在努力适应apoc.algo.dijkstra。我不只是想从A到B,而是通过C从A到B,同时考虑到链接上的权重。

A-B查询是......

MATCH  (startNode:road_node {id:'59030214550942348742a27d'}), (endNode:road_node {id:'59030215550942348742a610'})
call apoc.algo.dijkstra(startNode, endNode, 'ROADLINK', 'min_travel_time') YIELD path, weight
return path, weight/60

我尝试了一些方法来包含一个via c但是无法让它工作......例如

MATCH  (startNode:road_node {id:'59030214550942348742a27d'}), (endNode:road_node {id:'59030215550942348742a610'})
call apoc.algo.dijkstra(startNode, endNode, 'ROADLINK', 'min_travel_time') 
with path, weight
MATCH (startNode)-[*]-(via:road_node {id:'59030215550942348742a666'})-[*]-(endNode)
return path, weight

关于如何通过C从A路由到B路由的任何想法或建议,同时考虑对链路进行加权计数,将非常有帮助。

1 个答案:

答案 0 :(得分:1)

您可以调用Dijkstra算法两次,在第一次调用中使用via作为结束节点,然后在第二次调用中作为起始节点。结果将有2个子路径和2个子权重(除以60)。

MATCH (start:road_node {id:'59030214550942348742a27d'}), (end:road_node {id:'59030215550942348742a610'}), (via:road_node {id:'59030215550942348742a666'})
CALL apoc.algo.dijkstra(start, via, 'ROADLINK', 'min_travel_time') YIELD path, weight
WITH end, via, path AS p1, weight/60 AS w1
CALL apoc.algo.dijkstra(via, end, 'ROADLINK', 'min_travel_time') YIELD path, weight
return p1, path AS p2, w1, weight/60 AS w2;