在 Neo4j 的 Dijkstra 算法中多次使用 属性
Using more than once property in Dijkstra algorithm in Neo4j
在 Cypher 中有什么方法可以使用 Dijkstra 算法来计算具有多个 属性 的最小权重,而不是:
CALL apoc.algo.dijkstra(start, end, 'RELATED_TO>', '1_property')
yield path, weight
做类似的事情:
CALL apoc.algo.dijkstra(start, end, 'RELATED_TO>', '1_property+2_property')
yield path, weight
它对我不起作用。你有什么建议吗?因为我想将路径的长度放入权重计算中,作为对最小权重计算的影响。
对于 apoc,您需要使用一个额外的 属性 来保存总和。
对于图算法中的算法,您可以使用将两个权重相加的图投影。
https://neo4j-contrib.github.io/neo4j-graph-algorithms/#_single_source_shortest_path
您可以在配置部分使用 nodeQuery
和 relationshipQuery
。
CALL algo.shortestPath.stream(start, end, 'weight',
{nodeQuery:'match (n) return id(n) as id',
relationshipQuery:'match (n)-[r]->(m) return id(n) as source, id(m) as target, r.weight1+r.weight2 as weight',
defaultValue:1.0,direction:'OUTGOING'})
YIELD nodeId, cost
你可以看看Memgraph, high-performance, in-memory and transactional graph database. openCypher and Bolt兼容。 (免责声明:我是联合创始人兼首席技术官)。 Memgraph 内置了加权最短路径功能,其中总权重通过用户定义的 lambda 函数计算。基于这个数据集
CREATE (n1 {id: 1}) CREATE (n2 {id: 2}) CREATE (n3 {id: 3}) CREATE (n4 {id: 4})
CREATE (n1)-[:E {weight1: 1, weight2: 1}]->(n2)
CREATE (n1)-[:E {weight1: 2, weight2: 10}]->(n3)
CREATE (n2)-[:E {weight1: 3, weight2: 100}]->(n4)
CREATE (n3)-[:E {weight1: 4, weight2: 1}]->(n4);
Memgraph 的相关查询是
MATCH (a {id: 1})-[
edges *wShortest (e, n | e.weight1 + e.weight2) total_weight
]-(b {id: 4})
RETURN startNode(head(edges)).id +
reduce(acc = "", edge IN edges | acc + " -> " + endNode(edge).id) AS hops,
total_weight;
结果如下。
| hops | total_weight |
|-----------|--------------|
|1 -> 3 -> 4| 17.0 |
在 Cypher 中有什么方法可以使用 Dijkstra 算法来计算具有多个 属性 的最小权重,而不是:
CALL apoc.algo.dijkstra(start, end, 'RELATED_TO>', '1_property')
yield path, weight
做类似的事情:
CALL apoc.algo.dijkstra(start, end, 'RELATED_TO>', '1_property+2_property')
yield path, weight
它对我不起作用。你有什么建议吗?因为我想将路径的长度放入权重计算中,作为对最小权重计算的影响。
对于 apoc,您需要使用一个额外的 属性 来保存总和。
对于图算法中的算法,您可以使用将两个权重相加的图投影。
https://neo4j-contrib.github.io/neo4j-graph-algorithms/#_single_source_shortest_path
您可以在配置部分使用 nodeQuery
和 relationshipQuery
。
CALL algo.shortestPath.stream(start, end, 'weight',
{nodeQuery:'match (n) return id(n) as id',
relationshipQuery:'match (n)-[r]->(m) return id(n) as source, id(m) as target, r.weight1+r.weight2 as weight',
defaultValue:1.0,direction:'OUTGOING'})
YIELD nodeId, cost
你可以看看Memgraph, high-performance, in-memory and transactional graph database. openCypher and Bolt兼容。 (免责声明:我是联合创始人兼首席技术官)。 Memgraph 内置了加权最短路径功能,其中总权重通过用户定义的 lambda 函数计算。基于这个数据集
CREATE (n1 {id: 1}) CREATE (n2 {id: 2}) CREATE (n3 {id: 3}) CREATE (n4 {id: 4})
CREATE (n1)-[:E {weight1: 1, weight2: 1}]->(n2)
CREATE (n1)-[:E {weight1: 2, weight2: 10}]->(n3)
CREATE (n2)-[:E {weight1: 3, weight2: 100}]->(n4)
CREATE (n3)-[:E {weight1: 4, weight2: 1}]->(n4);
Memgraph 的相关查询是
MATCH (a {id: 1})-[
edges *wShortest (e, n | e.weight1 + e.weight2) total_weight
]-(b {id: 4})
RETURN startNode(head(edges)).id +
reduce(acc = "", edge IN edges | acc + " -> " + endNode(edge).id) AS hops,
total_weight;
结果如下。
| hops | total_weight |
|-----------|--------------|
|1 -> 3 -> 4| 17.0 |