【问题标题】:Can't find the lightest path in Neo4j with cypher使用密码在 Neo4j 中找不到最轻的路径
【发布时间】:2019-03-30 02:28:34
【问题描述】:

我试图在下图中找到从节点 ae 的最轻路径:

结果应该是 13:

a -> b:  1
b -> c:  3
c -> d:  4
d -> e:  5 (take lighter edge)
----------
        13

我尝试了几个示例(例如https://neo4j.com/docs/graph-algorithms/current/algorithms/shortest-path/),但找不到正确的查询。

MATCH (start:LocationNode{name:'a'}), (end:LocationNode{name:'e'})
CALL algo.shortestPath(start, end, 'weight',{write:true,writeProperty:'sssp'})
YIELD writeMillis,loadMillis,nodeCount, totalCost
RETURN writeMillis,loadMillis,nodeCount,totalCost

结果

╒═════════════╤════════════╤═══════════╤═══════════╕
│"writeMillis"│"loadMillis"│"nodeCount"│"totalCost"│
╞═════════════╪════════════╪═══════════╪═══════════╡
│3            │3           │5          │12.0       │
└─────────────┴────────────┴───────────┴───────────┘

MATCH (start:LocationNode{name:'a'}), (end:LocationNode{name:'e'})
CALL algo.shortestPath(start, end, 'weight',{
nodeQuery:'MATCH(n:LocationNode) RETURN id(n) as id',
relationshipQuery:'MATCH(n:LocationNode)-[r:CONNECTED_TO]->(m:LocationNode) RETURN id(n) as source, id(m) as target, r.weight as weight',
graph:'cypher'})
YIELD writeMillis,loadMillis,nodeCount, totalCost
RETURN writeMillis,loadMillis,nodeCount,totalCost

结果

╒═════════════╤════════════╤═══════════╤═══════════╕
│"writeMillis"│"loadMillis"│"nodeCount"│"totalCost"│
╞═════════════╪════════════╪═══════════╪═══════════╡
│3            │19          │4          │14.0       │
└─────────────┴────────────┴───────────┴───────────┘

像下面这样的其他查询甚至不返回任何内容:

MATCH p=(LocationNode{name:'a'})-[:CONNECTED_TO*]->(LocationNode{name:'e'})
RETURN p as shortestPath,
REDUCE(weight=0, r in relationships(p) | weight+r.weight) AS totalDistance

我希望看到一个返回“13”作为解决方案的查询,并理想地显示选择的路径,如下所示:

我怎样才能做到这一点?

非常感谢。

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

    这个查询:

    MATCH p=(a:LocationNode{name:'a'})-[:CONNECTED_TO*]->(e:LocationNode{name:'e'})
    WITH p, REDUCE(s=0, r IN RELATIONSHIPS(p) | s + r.weight) AS totalWeight
    RETURN p, totalWeight
    ORDER BY totalWeight
    LIMIT 1
    

    返回这个结果:

    ╒══════════════════════════════════════════════════════════════════════╤═════════════╕
    │"p"                                                                   │"totalWeight"│
    ╞══════════════════════════════════════════════════════════════════════╪═════════════╡
    │[{"name":"a"},{"weight":1},{"name":"b"},{"name":"b"},{"weight":3},{"na│13           │
    │me":"c"},{"name":"c"},{"weight":4},{"name":"d"},{"name":"d"},{"weight"│             │
    │:5},{"name":"e"}]                                                     │             │
    └──────────────────────────────────────────────────────────────────────┴─────────────┘
    

    在 neo4j 浏览器中,如果您禁用 Connect result nodes 选项(在浏览器设置窗格的底部,您可以通过单击左侧面板中的齿轮图标来显示),那么可视化将是:

    【讨论】:

    • 非常感谢!您是否知道该查询中使用了哪种算法? Dijkstra、A*、...?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    相关资源
    最近更新 更多