Neo4j Cypher Bus Route Selection -


i new cypher, know whether following case possible in neo4j cypher:

bus route example

when want query buses should taken station 1 station 4, output should (which consists of least number of interchange):

  1. station 1 -> route 1 -> station 3 -> route 3 -> station 4
  2. station 1 -> route 2 -> station 3 -> route 3 -> station 4

but not every possible combinations:

  1. station 1 -> route 1 -> station 2 -> route 1 -> station 3 -> route 3 -> station 4
  2. station 1 -> route 1 -> station 2 -> route 2 -> station 3 -> route 3 -> station 4
  3. station 1 -> route 2 -> station 2 -> route 1 -> station 3 -> route 3 -> station 4
  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; 

http://console.neo4j.org/r/koe6fo


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -