简体   繁体   English

neo4j中通过航路点的2个节点之间的最短路径

[英]shortest path between 2 nodes through waypoints in neo4j

I have a graph in neo4j where a node represents a city and a relationship represents roads connecting the cities. 我在neo4j中有一个图,其中一个节点代表一个城市,一个关系代表连接城市的道路。 The relationships are weighted and have a property called 'distance'. 关系经过加权,并具有称为“距离”的属性。 I want to find the shortest (least weighted path) between two cities A and B. This is easily done using Dijkstra's algorithm. 我想找到两个城市A和B之间的最短路径(最小加权路径)。使用Dijkstra的算法很容易做到这一点。 The tricky part is that I have a set of cities which are to be covered in the path from A to B. In other words, the path from A to B should cover all the waypoints, the order doesn't matter. 棘手的部分是,我有一组从A到B的路径将要覆盖的城市。换句话说,从A到B的路径应覆盖所有路标,顺序无关紧要。 Something like providing a source, destination and list of waypoints and optimize:true in Google API. 诸如提供航点的来源,目的地和列表并进行优化:在Google API中为true。 I have tried this using Cypher with the below query- 我已经在以下查询中使用Cypher进行了尝试-

match(s:City{ID:"A"})
match(f:City{ID:"B"})
match (n:City) where n.name in ['City1','City2','City3','City4']
with collect(n) as wps 
match path=allshortestPaths((s)-[:ROADWAY*..9]->(f))
where ALL ( n in wps WHERE n in nodes(path) ) with path, 
reduce(d=0, rel IN relationships(path)| d+rel.displacement) as             
totaldistance,Extract (n in nodes(path)| n.name) as cities 
return cities, totaldistance, tofloat(totaldistance) as TD order by 
totaldistance

But this didn't work. 但这没有用。 Maybe because the path wasn't passing through all the waypoints. 可能是因为该路径未通过所有路标。 I am also trying to use A* or Dijkstra (using Neo4j Traversal) but not sure how to visit all the waypoints. 我也尝试使用A *或Dijkstra(使用Neo4j Traversal),但不确定如何访问所有航路点。

Thanks in advance!! 提前致谢!!

Try using ANY rather ALL . 尝试使用ANY而不是ALL

From the neo4j documentation 来自neo4j文档

All - Tests whether a predicate holds for all elements of this list. All -测试谓词是否对该列表的所有元素成立。

whereas

Any - Tests whether a predicate holds for at least one element in the list. Any -测试谓词是否对列表中的至少一个元素成立。

match(s:City{ID:"A"})
match(f:City{ID:"B"})
match (n:City) where n.name in ['City1','City2','City3','City4']
with collect(n) as wps 
match path=allshortestPaths((s)-[:ROADWAY*..9]->(f))
where ANY ( n in wps WHERE n in nodes(path) ) with path, 
reduce(d=0, rel IN relationships(path)| d+rel.displacement) as             
totaldistance,Extract (n in nodes(path)| n.name) as cities 
return cities, totaldistance, tofloat(totaldistance) as TD order by 
totaldistance

Edit - We can change this to ensure that all cities are included by combining multiple ANY in the where clause: 编辑-我们可以通过在where子句中组合多个ANY来更改此设置,以确保包括所有城市:

match(s:City{ID:"A"})
match(f:City{ID:"B"})
with collect(n) as wps 
match path=allshortestPaths((s)-[:ROADWAY*..9]->(f))
where ANY ( n in nodes(wps) WHERE n.name = 'City1' ) AND
      ANY ( n in nodes(wps) WHERE n.name = 'City2) 
with path, 
reduce(d=0, rel IN relationships(path)| d+rel.displacement) as             
totaldistance,Extract (n in nodes(path)| n.name) as cities 
return cities, totaldistance, tofloat(totaldistance) as TD order by 
totaldistance

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 neo4j中的图形建模基于日期/时间的最短路径? - Graph Modelling in neo4j for shortest path based on date/time? 存储neo4j的朋友[节点]之间的关系路径并在将来进行更新 - Storing relationship path between friends[nodes] from neo4j and updating it in future 是否有任何预先实现的路由算法可用于使用航路点作为节点来查找 2 个机场之间的最短路径 - Is there any pre implemented routing algorithm that I can use to find the shortest path between 2 airports using waypoints as nodes 在图中经过两个节点子集的两个节点之间找到最短路径 - Finding the shortest path in a graph between 2 nodes that goes through a subset of nodes 图中节点之间的最短路径 - The shortest path between nodes in graph 获取n个节点之间最短路径的子图 - Get subgraph of shortest path between n nodes 如何获得网格上节点之间的最短路径? - How to get shortest path between nodes on a grid? 如何找到节点之间的最短路径? - How to find the shortest path between nodes? 两个Trie节点之间的最短路径 - Shortest Path between two Trie Nodes 找到两个节点(顶点)之间的最短路径 - Find the shortest Path between two nodes (vertices)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM