Neo4J APOC A* 和 Dijkstra 条件

Neo4J APOC A* and Dijkstra Conditions

我目前正在使用内置的 dijkstra 来使用条件查找 Neo4J 路径(见下文)。

CYPHER

PROFILE MATCH path = shortestpath((s:Node {UID: 73946372})-[Road*]->(e:Node {UID: 2383529534}))
WHERE ALL(x in relationships(path) WHERE x.LG_6 > 0 || x.C60 > 100)
WITH path, reduce(s = 0, r IN relationships(path) | s + r.Length) AS dist
RETURN path, dist LIMIT 1

但是,我想知道是否可以对 APOC 做同样的事情,并具有诸如 x.LG_6 > 2000 AND x.C60> 0 之类的条件。下面的密码是我目前使用的密码但是他们没有条件。

APOC A*

PROFILE MATCH (s:Node {UID: 73946372}),(e:Node {UID: 2383529534})
CALL apoc.algo.aStar(s, e, 'Road', 'Length','Latitude','Longitude') YIELD path, weight
RETURN path, weight LIMIT 1

APOC Dijkstra

PROFILE MATCH (s:Node {UID: 73946372}),(e:Node {UID: 2383529534})
CALL apoc.algo.dijkstra(s, e, 'Road', 'Length') YIELD path AS path, weight AS weight
RETURN path, weight LIMIT 1
  1. Cypher 函数 shortestPath 不使用关系权重 属性 来计算最短路径——它只使用路径的长度(即,路径中的关系)。

    因此,您的第一个查询所输入的 dist return 具有误导性,因为它不是 shortestPath 函数用来确定 path 结果的内容。这也是为什么您的第一个查询的结果可能与 使用权重 属性.

  2. 的另一个算法的结果不匹配的原因
  3. Graph Algorithms plugin contains path-finding algorithms that support (via Cypher projection)你想要什么,就可以用一个权重属性。这些方法的 stream 变体 return 结果路径中每个节点的本地 ID,以及路径中该点的总权重。目前该插件仅支持neo4j 3.5.14及以下版本

例如:

一个*

MATCH (s:Node {UID: 73946372}), (e:Node {UID: 2383529534})
CALL algo.shortestPath.astar.stream(s, e, null, 'Latitude', 'Longitude', {
  nodeQuery: 'MATCH (p:Node) RETURN id(p) as id',
  relationshipQuery: 'MATCH (a:Node)-[x:Road]->(b:Node) WHERE x.LG_6 > 0 || x.C60 > 100 RETURN id(a) AS source, id(b) AS target, x.Length AS weight',
  graph: 'cypher'
}) YIELD nodeId, cost
RETURN nodeId,cost

迪杰斯特拉

MATCH (s:Node {UID: 73946372}), (e:Node {UID: 2383529534})
CALL algo.shortestPath.stream(s, e, null, {
  nodeQuery: 'MATCH (p:Node) RETURN id(p) as id',      
  relationshipQuery: 'MATCH (a:Node)-[x:Road]->(b:Node) WHERE x.LG_6 > 0 || x.C60 > 100 RETURN id(a) AS source, id(b) AS target, x.Length AS weight',
  graph: 'cypher'
}) YIELD nodeId, cost
RETURN nodeId, cost