Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

cypher - neo4j finding all paths that meets a certain criteria

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...