apoc.algo.aStarProcedure
Syntax |
|
||
Description |
Runs the A* search algorithm to find the optimal path between two |
||
Input arguments |
Name |
Type |
Description |
|
|
The node to start the search from. |
|
|
|
The node to end the search on. |
|
|
|
The relationship types to restrict the algorithm to. Relationship types are represented using APOC’s rel-direction-pattern syntax; |
|
|
|
The name of the property to use as the weight. |
|
|
|
The name of the property to use as the latitude. |
|
|
|
The name of the property to use as the longitude. |
|
Return arguments |
Name |
Type |
Description |
|
|
The path result. |
|
|
|
The weight of the given path. |
|
Example
Given this dataset:
CREATE (amsterdam:City {name: 'Amsterdam', latitude: 52.37, longitude: 4.90}),
(brussels:City {name: 'Brussels', latitude: 50.85, longitude: 4.35}),
(paris:City {name: 'Paris', latitude: 48.85, longitude: 2.35}),
(amsterdam)-[:ROAD {distance: 210000}]->(brussels),
(brussels)-[:ROAD {distance: 265000}]->(paris),
(amsterdam)-[:ROAD {distance: 500000}]->(paris)
The following query finds the lowest-cost path from Amsterdam to Paris.
The latitude and longitude properties are used as the geographic heuristic, and distance (in metres) as the relationship cost.
The algorithm prefers the route via Brussels (475,000 m) over the direct connection (500,000 m) as it is cheaper:
MATCH (start:City {name: 'Amsterdam'}), (end:City {name: 'Paris'})
CALL apoc.algo.aStar(start, end, 'ROAD>', 'distance', 'latitude', 'longitude')
YIELD path, weight
RETURN [n IN nodes(path) | n.name] AS route, weight
| route | weight |
|---|---|
["Amsterdam", "Brussels", "Paris"] |
475000.0 |