![](/img/trans.png)
[英]Shortest path between two nodes vs shortest path from one node to all other nodes
[英]How to get a path from one node to another including all other nodes and relationships involved in between
如評論中所述,在Cypher中,您不能使用針對某些關系使用不同方向的有向可變長度關系。
但是, APOC程序剛剛添加了根據關系序列進行擴展的功能。 您可以嘗試一下:
MATCH (start:station), (end:station)
WHERE start.name='NBW' AND end.name='THT'
CALL apoc.path.expandConfig(start, {terminatorNodes:[end], limit:1,
relationshipFilter:'has_platform>, can_board>, goto>, can_alight>, <has_platform'}) YIELD path
RETURN path
我添加了一個限制,以便僅返回到終端站的第一個(也是最短的)路徑。 不建議取消限制,因為這將繼續在擴展中重復從站到站的關系,直到找到找到到達終端站的所有可能方式,這可能會掛起查詢。
編輯
關於新模型的更改,上述方法不起作用的原因是關系序列中不能包含可變長度序列。 您有2個goto>
關系可以遍歷,但是序列中僅指定了一個。
這是一種不使用序列的替代方法,只是將允許的關系列入白名單。 spanningTree()
過程使用NODE_GLOBAL唯一性,因此到找到的每個節點將只有一條唯一路徑(路徑不會回溯或重新訪問以前訪問的節點)。
MATCH (start:station), (end:station)
WHERE start.name='NBW' AND end.name='RD'
CALL apoc.path.spanningTree(start, {terminatorNodes:[end], limit:1,
relationshipFilter:'has_platform>|can_board>|goto>|can_alight>|<has_platform'}) YIELD path
RETURN path
您的查詢是針對-->
,並非兩個工作站之間的所有關系都朝相同的方向運行。 如果刪除關系方向,您將得到結果。
然后,一旦獲得結果,我認為類似的事情可能會使您指出正確的方向,以便在工作后從結果路徑中提取特定細節。
本質上,我假設您感興趣的一切都在返回的路徑中,您只需要過濾掉返回的不同部分即可。
正如@InverseFalcon指出的那樣,此查詢應限制在較大的圖中,否則可能會輕易消失。
MATCH p = (a:Station)-[r:Goto|can_board|can_alight|has_platfrom*0..]-(c:Station)
WHERE (a.name='NBW')
AND c.name='THT'
RETURN filter( n in nodes(p) WHERE 'Platform' in labels(n)) AS Platforms
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.