apoc.algo.dijkstra

Procedure APOC Core

apoc.algo.dijkstra(startNode, endNode, 'KNOWS|<WORKS_WITH|IS_MANAGER_OF>', 'distance', defaultValue, numberOfWantedResults) YIELD path, weight - run dijkstra with relationship property name as cost function

Signature

apoc.algo.dijkstra(startNode :: NODE?, endNode :: NODE?, relationshipTypesAndDirections :: STRING?, weightPropertyName :: STRING?, defaultWeight = NaN :: FLOAT?, numberOfWantedPaths = 1 :: INTEGER?) :: (path :: PATH?, weight :: FLOAT?)

Input parameters

Name Type Default

startNode

NODE?

null

endNode

NODE?

null

relationshipTypesAndDirections

STRING?

null

weightPropertyName

STRING?

null

defaultWeight

FLOAT?

NaN

numberOfWantedPaths

INTEGER?

1

Output parameters

Name Type

path

PATH?

weight

FLOAT?

Usage Examples

The examples in this section are based on the following sample graph:

CREATE
  (a:Loc{name:'A'}), (b:Loc{name:'B'}),
  (c:Loc{name:'C'}), (d:Loc{name:'D'}),
  (a)-[:ROAD {d:100}]->(d),
  (a)-[:RAIL {d:5}]->(d), (a)-[:ROAD {d:10}]->(b),
  (b)-[:ROAD {d:20}]->(c),
  (c)-[:ROAD {d:30}]->(d),
  (a)-[:ROAD {d:20}]->(c);

We can find the shortest path from A to D, by running the following query:

MATCH (from:Loc{name:'A'}), (to:Loc{name:'D'})
CALL apoc.algo.dijkstra(from, to, 'ROAD>', 'd')
yield path, weight
RETURN path, weight;
Table 1. Results
path weight

(:Loc {name: "A"})-[:ROAD {d: 20}]→(:Loc {name: "C"})-[:ROAD {d: 30}]→(:Loc {name: "D"})

50.0