neo4j finding all paths that meets a certain criteria

て烟熏妆下的殇ゞ 提交于 2020-01-10 05:24:46

问题


I am trying to model a graph to solve some connection time problem. So for example I have the following graph

    F1,F2     F3     F4

ST_B--------->ST2----->ST3------>ST_E

   F5,F6      F7       F8

ST_B-------->ST4---->ST5------>ST_E

    F9

ST_B-------->ST_E

I model ST_B, ST2,ST3,ST4,ST5, ST_E as station (node). and F1-F9 as flt node. And each flt node has a departure time and arrival time. And the relationship is connect. Also in this case, let's assume F2 arrival time is 30mins less than F3 departure time, and F6 is 30mins less than F7. (means the connection is not valid) So the valid route from ST_B to ST_E should be F1-F3-F4, F5-F7-F8 and F9. I have try to use cypher to solve this problem without success. (may be I am modeling it wrong).


回答1:


I have added labels for the nodes to distinguish flights and stations. All flights F1-F9 are labeled with :Flight, all stations ST_B, ST_E and ST_2-ST_5 are labeled with :Station.

Match path=stb:Station-[:Connect*]->ste:Station
Where stb.name='ST_B' and ste.name='ST_E'
With filter(x in nodes(path) where x:Flight ) as flts
Where all ( i in Range(0,length(flts)-2) Where flts[i].arrvTime < flts[i+1].dptrTime)
Return extract(flt in flts | flt.name)
  1. The "Match" and "Where" clause retrieve all paths from ST_B to ST_E;
  2. The "With" clause retrieve all flight nodes on the paths and pass them to the next "Where" clause.
  3. The next "Where" clause checks the arrival time of each flight and the departure time of the connected flight to return only the valid flight combinations.
  4. The final "Return" clause returns the names of flights that form a valid route.


来源:https://stackoverflow.com/questions/18923027/neo4j-finding-all-paths-that-meets-a-certain-criteria

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