简体   繁体   English

用密码找不到Neo4j中最轻的路径

[英]Can't find the lightest path in Neo4j with cypher

I'm trying to find the lightest path from node a to e in the following graph: 我试图在下图中找到从节点ae的最轻路径:

在此处输入图片说明

The result should be 13: 结果应为13:

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

I tried several examples, (eg https://neo4j.com/docs/graph-algorithms/current/algorithms/shortest-path/ ) but can't find the right query. 我尝试了几个示例(例如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

results in 结果是

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

and

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

results in 结果是

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

Other queries like the following don't even return anything: 其他类似以下查询甚至都不返回任何内容:

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

I would like to see a query that returns '13' as solution and ideally displays the chosen path like this: 我想看到一个返回“ 13”作为解决方案的查询,理想情况下将显示所选的路径,如下所示:

在此处输入图片说明

How can I achieve this? 我该如何实现?

Thank you very much. 非常感谢你。

This query: 该查询:

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

Returns this result: 返回此结果:

╒══════════════════════════════════════════════════════════════════════╤═════════════╕
│"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"}]                                                     │             │
└──────────────────────────────────────────────────────────────────────┴─────────────┘

In the neo4j Browser, if you disable the Connect result nodes option (at the bottom of the Browser Settings pane, which you can display by clicking the gear icon in the left panel), then the visualization will be: 在neo4j浏览器中,如果禁用“ 连接结果节点”选项(位于“浏览器设置”窗格的底部,可通过单击左侧面板中的齿轮图标来显示该选项),则可视化效果为:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM