簡體   English   中英

如何在 GTFS 數據上使用 Cypher 查詢(neo4j)找到傳輸連接中的最短路徑?

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

我是 Neo4j 的新手,我根據 GTFS 的數據模型按照以下步驟創建了一個圖表。 我想在圖中找到所有最短的間接路線(帶轉移)。 圖數據庫的數據模型包含 4 個實體:Route、Trip、Stop、Stoptime。 這是db.scheme()的屏幕截圖。

根據編寫 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;

此查詢未使用最短路徑,平均需要 30 秒才能找到路徑,但查詢的輸出是好的

所以我嘗試編寫另一個查詢,使用 allshortestpaths 方法,它非常快(0,3s)。 但它也返回了我在不同方向運行的旅行(VIR - > VBR)......另一個問題是連接的時間。

你能幫我,當我使用allshortestpath方法時如何訪問傳輸節點(Station)? 我想為計時和 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;

接下來,如果你想看到路徑中的節點,那么你可以使用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