【问题标题】:Using more than once property in Dijkstra algorithm in Neo4j在 Neo4j 的 Dijkstra 算法中使用多次属性
【发布时间】:2018-05-06 11:03:56
【问题描述】:

在 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

这对我不起作用。你有什么建议吗?因为我想将路径的长度作为对最小权重计算的影响来计算权重。

【问题讨论】:

    标签: neo4j cypher dijkstra


    【解决方案1】:

    您可以查看Memgraph,高性能、内存和事务图形数据库。 openCypherBolt 兼容。 (免责声明:我是联合创始人兼首席技术官)。 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         |
    

    【讨论】:

      【解决方案2】:

      对于 apoc,您需要使用一个额外的属性来保存总和。

      对于图形算法中的一个,您可以使用将两个权重相加的图形投影。

      https://neo4j-contrib.github.io/neo4j-graph-algorithms/#_single_source_shortest_path

      您可以在配置部分使用nodeQueryrelationshipQuery

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-05
        • 1970-01-01
        • 1970-01-01
        • 2014-01-20
        • 1970-01-01
        • 1970-01-01
        • 2021-12-24
        相关资源
        最近更新 更多