[英]Get nodes in shortestpath using py2neo
我目前正在使用py2neo
來使用最短路徑進行查詢, neo4j
代碼是:
MATCH p=(c:Ciudad)-[*]-(l:Ciudad)
WHERE c.nombre = '%s' and l.nombre = '%s'
RETURN p as shortestPath, reduce(precio=0, r in relationships(p) | precio+r.precio) AS totalPrecio
ORDER BY totalPrecio ASC LIMIT 1;
當我在網絡上使用它時,它為我提供了節點1-關系-節點2的列表,但是當我使用以下命令執行此操作時:
string= "MATCH p=(c:Ciudad)-[*]-(l:Ciudad)
WHERE c.nombre = '%s' and l.nombre = '%s'
RETURN p as shortestPath, reduce(precio=0, r in relationships(p) | precio+r.precio) AS totalPrecio
ORDER BY totalPrecio ASC LIMIT 1;" %(salida, llegada)
n = graph.run(string)
shortestPath
僅具有關系,而沒有節點。
有人知道如何獲得與網絡相同的結果嗎?
旁白:你的Cypher代碼實際上並沒有產生什么保證是一個單一的“最短路徑”作為價值shortestpath
。 它只是產生所有長度的所有匹配路徑。 為了回答您的問題,我將忽略此問題,並假設您想要的是現有行為。
如果結果不包含返回的路徑中的節點,則可以僅在路徑上使用NODES()函數,如以下代碼段所示:
...
RETURN p as shortestPath, NODES(p) AS nodes, ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.