Neo4j Cypher Bus Route Selection -
i new cypher, know whether following case possible in neo4j cypher:

when want query buses should taken station 1 station 4, output should (which consists of least number of interchange):
- station 1 -> route 1 -> station 3 -> route 3 -> station 4
- station 1 -> route 2 -> station 3 -> route 3 -> station 4
but not every possible combinations:
- station 1 -> route 1 -> station 2 -> route 1 -> station 3 -> route 3 -> station 4
- station 1 -> route 1 -> station 2 -> route 2 -> station 3 -> route 3 -> station 4
- station 1 -> route 2 -> station 2 -> route 1 -> station 3 -> route 3 -> station 4
- station 1 -> route 2 -> station 2 -> route 2 -> station 3 -> route 3 -> station 4
thanks!
this difficult without conditional expressions (case/when in 2.0). close got in few minutes of trying. you'd have pull out start nodes resulting relationship collection.
start st1=node:node_auto_index(name="station1"), st4=node:node_auto_index(name="station4") match p=st1-[r*]->st4 reduce(acc=[], route in rels(p): case when length(acc) > 0 , last(extract(a in acc: a.name)) = route.name acc else acc + route end) reducedroutes return reducedroutes, length(reducedroutes) len order len;
Comments
Post a Comment