Neo4j 最短路径
Neo4j shortestPath
我有一个包含两种关系的图表:
- 类型
T1
表示单向街道
- 类型
T2
表示 双向 街道。
根据 neo4j 文档的建议,我想避免为两条路创建两个关系,因为它会减慢 shortestPath 函数的速度。
但是有没有办法遍历T1
方向和T2
双向在shortestPath
功能?
请注意,由于 T1
关系,我无法使用无向搜索!
使用 cypher shortespath
函数你不能 做到。
所以如果你想使用这个功能,你必须为T2
创建两个关系。
但是当我看到 street
和 shortestpath
时,我假设您正在执行一些路由算法,因此 shortespath 函数可能不是最优化的 为了那个原因。
如果你有一个复杂的路由算法,你应该看看 graph traversal API 或者 graph algo into APOC (https://neo4j-contrib.github.io/neo4j-apoc-procedures/#_graph_algorithms_work_in_progress) 与 AStar
或 dijkstraWithDefaultWeight
过程。
更新
这是您使用来自 APOC 的 apoc.algo.dijkstraWithDefaultWeight
算法查找的查询示例:
MATCH (from:Way { id: $idFrom }),
(to:Way { id: $idTo })
WITH from, to
CALL apoc.algo.dijkstraWithDefaultWeight(from, to, 'T1>|T2', 'distance', 1) YIELD path, weight
RETURN path, weight
我有一个包含两种关系的图表:
- 类型
T1
表示单向街道 - 类型
T2
表示 双向 街道。
根据 neo4j 文档的建议,我想避免为两条路创建两个关系,因为它会减慢 shortestPath 函数的速度。
但是有没有办法遍历T1
方向和T2
双向在shortestPath
功能?
请注意,由于 T1
关系,我无法使用无向搜索!
使用 cypher shortespath
函数你不能 做到。
所以如果你想使用这个功能,你必须为T2
创建两个关系。
但是当我看到 street
和 shortestpath
时,我假设您正在执行一些路由算法,因此 shortespath 函数可能不是最优化的 为了那个原因。
如果你有一个复杂的路由算法,你应该看看 graph traversal API 或者 graph algo into APOC (https://neo4j-contrib.github.io/neo4j-apoc-procedures/#_graph_algorithms_work_in_progress) 与 AStar
或 dijkstraWithDefaultWeight
过程。
更新
这是您使用来自 APOC 的 apoc.algo.dijkstraWithDefaultWeight
算法查找的查询示例:
MATCH (from:Way { id: $idFrom }),
(to:Way { id: $idTo })
WITH from, to
CALL apoc.algo.dijkstraWithDefaultWeight(from, to, 'T1>|T2', 'distance', 1) YIELD path, weight
RETURN path, weight