Elhanan Schwarts
Elhanan Schwarts

Reputation: 383

Neo4j finding a path between two stations

Assume a train passes between stations.

The data structure connecting two stations is as follows:

(x:Station)<-[:Departure]-(:Train)-[:Arrival]->(y:Station)

I need to find the path between station A and station B. The path does not have to be direct (with one train) but it can pass through other stations

Upvotes: 0

Views: 70

Answers (1)

Tomaž Bratanič
Tomaž Bratanič

Reputation: 6514

With your current graph model you could try:

MATCH (x:Station{id:"x"}),(y:Station{id:"y"})
MATCH p=shortestPath((x)-[*]-(y))
RETURN p

However, you could improve your graph model, because currently, you have to look at undirected relationships, because your relationship direction does not indicate the direction of the path. If you changed it to:

(x:Station)-[:Departure]->(:Train)-[:Arrival]->(y:Station)

For example, you can then search for a directed path, where the query will perform better:

MATCH (x:Station{id:"x"}),(y:Station{id:"y"})
MATCH p=shortestPath((x)-[*]->(y))
RETURN p

Upvotes: 1

Related Questions