Cypher query shortest path

岁酱吖の 提交于 2020-01-01 22:00:14

问题


I build a graphe this way: the nodes represents: busStops, and the relationship represent the bus line linking bus stops each others.

The relationship type correspond to the time needed to go from a node two another one.

When I'm querying the graph (thanks to cypher) to get the shortestPath between two which are maybe not linked, the result is the one where the number of relations used is the smallest.

I would to change that in order that the shortest path corresponds to the path where the addition of all relationship types used between two nodes(which correspond to the time) is the smallest?


回答1:


first, you are doing it wrong. don't use a unique relationship type for each time. use one relationship type and then put a property "time" on all relations.

second, to calculate the addition you can use this cypher formula:

START from=node({busStopId1}), to=node({busStopId2})
MATCH p=from-[:LINE*]-to  //asterix * means any distance
RETURN p,reduce(total = 0, r in relationships(p): total + r.time) as tt
ORDER by tt asc;


来源:https://stackoverflow.com/questions/14893127/cypher-query-shortest-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!