繁体   English   中英

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

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

我试图在下图中找到从节点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”作为解决方案的查询,理想情况下将显示所选的路径,如下所示:

在此处输入图片说明

我该如何实现?

非常感谢你。

该查询:

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浏览器中,如果禁用“ 连接结果节点”选项(位于“浏览器设置”窗格的底部,可通过单击左侧面板中的齿轮图标来显示该选项),则可视化效果为:

暂无
暂无

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

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