简体   繁体   English

如何在 GTFS 数据上使用 Cypher 查询(neo4j)找到传输连接中的最短路径?

[英]How find shortest path in transport connection by using Cypher query (neo4j) over GTFS data?

I am new in neo4j, I created a graph following this steps , based on a data model from GTFS.我是 Neo4j 的新手,我根据 GTFS 的数据模型按照以下步骤创建了一个图表。 I would like to find all the shortest indirect routes in the graph (with transfers).我想在图中找到所有最短的间接路线(带转移)。 Data model of graph database contains 4 entities: Route, Trip, Stop, Stoptime.图数据库的数据模型包含 4 个实体:Route、Trip、Stop、Stoptime。 Here is a screenshot of db.scheme() .这是db.scheme()的屏幕截图。

Based on query which wrote Bruggen, I modified it for my use:根据编写 Bruggen 的查询,我对其进行了修改以供我使用:

MATCH 
(from:Stop {code:'VBR'})--(st_from:Stoptime),
(to:Stop {code:'VIR'})--(st_to:Stoptime),
p1=((st_from)-[:PRECEDES*]->(st_midway_arr:Stoptime)),
(st_midway_arr)--(midway:Stop),
(midway)--(st_midway_dep:Stoptime),
p2=((st_midway_dep)-[:PRECEDES*]->(st_to))
WHERE
st_from.departure_time > '00:00'
AND st_from.departure_time < '23:00'
AND st_midway_arr.arrival_time > st_from.departure_time
AND st_midway_dep.departure_time > st_midway_arr.arrival_time
AND st_to.arrival_time > st_midway_dep.departure_time
RETURN
from,st_from,to,st_to,p1,p2,midway
order by (st_to.arrival_time_int-st_from.departure_time_int) ASC
limit 1;

This query is not using the shortest path, and it takes in average 30s to find a path, but the output of the query is good .此查询未使用最短路径,平均需要 30 秒才能找到路径,但查询的输出是好的

So I tried to write another query, with method allshortestpaths, it really fast (0,3s).所以我尝试编写另一个查询,使用 allshortestpaths 方法,它非常快(0,3s)。 But it returns me also trips which run in a different direction (VIR -> VBR)... another problem is the timing od that connection.但它也返回了我在不同方向运行的旅行(VIR - > VBR)......另一个问题是连接的时间。

Could you help me, how to access to the transfer node (Station) when I am using allshortestpath method?你能帮我,当我使用allshortestpath方法时如何访问传输节点(Station)? I want to write a condition for timing and stop_sequence to be sure that's the right direction.我想为计时和 stop_sequence 编写一个条件,以确保这是正确的方向。

match (from:Stop {code:'VBR'}),(to:Stop {code:'VIR'})
with from,to
match p = allshortestpaths((from)-[*]-(to))
where NONE (x in relationships(p) where type(x)="OPERATES")
return p
limit 10;
match (from:Stop {code:'VBR'}),(to:Stop {code:'VIR'})
with from,to
match p = allshortestpaths((from)-[*]->(to)) // here you needed you give the direction to make sure paths are from 'VBR' to 'VIR'
where NONE (x in relationships(p) where type(x)="OPERATES")
return p
limit 10;

Next , if you want to see the nodes in the path , then you can use the nodes(p)接下来,如果你想看到路径中的节点,那么你可以使用nodes(p)

match (from:Stop {code:'VBR'}),(to:Stop {code:'VIR'})
with from,to
match p = allshortestpaths((from)-[*]->(to)) 
where NONE (x in relationships(p) where type(x)="OPERATES")
AND ALL(node in nodes WHERE node = from OR node = to OR YOUR CONDTION ON TRANSFER NODE)
limit 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM