使用py2n获取最短路径中的节点

2024-05-05 22:03:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用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;

当我在web上使用它时,它会给出节点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只有关系,没有节点

有人知道如何得到与网络相同的结果吗


Tags: andinreducereturn节点asmatchwhere
1条回答
网友
1楼 · 发布于 2024-05-05 22:03:33

旁白:您的Cypher代码实际上并没有生成保证为单个“最短路径”的值shortestpath。它只是生成任意长度的所有匹配路径。为了回答你的问题,我将忽略这个问题,并假设现有的行为是你想要的

如果结果中不包含返回路径中的节点,则可以在路径上使用NODES()函数,如以下代码段所示:

...
RETURN p as shortestPath, NODES(p) AS nodes, ...

相关问题 更多 >