Shortest path of weighted and directed graph in Neo4j

43 Views Asked by At

I created a map of city in Neo4j using OpenStreetMap and OpenAddresses data. I want to find the shortest path from one address to another. The problem is that, when I'm using function apoc.algo.aStarConfig,I'm getting a path without consideration about direction of a relationship.

MATCH (a:Address)-[:NEAREST_INTERSECTION]->(source:Intersection)
WHERE a.full_address CONTAINS "410 E 5TH AVE SAN MATEO, CA"
MATCH (poi:Address)-[:NEAREST_INTERSECTION]->(dest:Intersection) 
WHERE poi.full_address CONTAINS "39 GRAND BLVD SAN MATEO, CA"
CALL apoc.algo.aStarConfig(source, dest, "ROAD_SEGMENT", 
  {pointPropName: "location", weight: "length"}) 
YIELD weight, path
RETURN *

Result of the query

So my question is if there is a possibility to find the shortest path from one path to another not consider upstream relathionships?

1

There are 1 best solutions below

0
Finbar Good On

If you want to only find paths where the relationships are in one direction e.g. outbound, you can either prepend < to the relationship type for inbound, or append > for outbound.

To only follow outbound ROAD_SEGMENT relationships, your query would become:

MATCH (a:Address)-[:NEAREST_INTERSECTION]->(source:Intersection)
WHERE a.full_address CONTAINS "410 E 5TH AVE SAN MATEO, CA"
MATCH (poi:Address)-[:NEAREST_INTERSECTION]->(dest:Intersection) 
WHERE poi.full_address CONTAINS "39 GRAND BLVD SAN MATEO, CA"
CALL apoc.algo.aStarConfig(source, dest, "ROAD_SEGMENT>", 
  {pointPropName: "location", weight: "length"}) 
YIELD weight, path
RETURN *