簡體   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