我怎样才能获得最短路径密码查询?

pan*_*ing 3 neo4j cypher

我在这里用cypher语句创建了一个Neo4j数据库:https://gist.github.com/neoecos/8748091

我想知道:如何获得:1.无传输路径(按传输顺序)2.最短路径(按路径长度排序)3.最佳路径(较少传输和最短路径)

请给出相应的查询.您认为这是创建总线查询系统的最佳方式吗?非常感谢.

Bri*_*ood 6

最短路径非常简单:

MATCH path=shortestPath((station_44:STATION {id:44})-[*0..10]-(station_46:STATION {id:46}))
RETURN path
Run Code Online (Sandbox Code Playgroud)

对于计数转移,您可以执行以下操作:

MATCH path=allShortestPaths((station_44:STATION {id:44})-[rels*0..10]-(station_46:STATION {id:46}))
RETURN length(path) AS stop_count, length(FILTER(index IN RANGE(1, length(rels)-1) WHERE (rels[index]).bus <> (rels[index - 1]).bus)) AS transfer_count
Run Code Online (Sandbox Code Playgroud)

一旦你有了这两个变量,你就可以计算/排序.例如:

MATCH path=(station_44:STATION {id:44})-[rels*0..4]-(station_46:STATION {id:46})
WITH length(path) AS stop_count, length(FILTER(index IN RANGE(1, length(rels)-1) WHERE (rels[index]).bus <> (rels[index - 1]).bus)) AS transfer_count
RETURN stop_count, transfer_count
ORDER BY (stop_count * 0.5) + (transfer_count * 2.0) DESC
Run Code Online (Sandbox Code Playgroud)

在这里,我删除了allShortestPaths呼叫,以便您获得不同长度的路径.在ORDER BY对两个指标权重的用途.不幸的是,至少在我的数据库中,如果超过四个路径长度,它开始变得非常慢.如果在您的情况下有意义,您可以通过在路径中引入方向箭头来改进它.