This section describes the Single Source Shortest Path algorithm in the Neo4j Graph Algorithms library.
The Single Source Shortest Path (SSSP) algorithm calculates the shortest (weighted) path from a node to all other nodes in the graph.
The Single Source Shortest Path algorithm is experimental and not officially supported. |
This section includes:
SSSP came into prominence at the same time as the shortest path algorithm and Dijkstra’s algorithm can act as an implementation for both problems.
We implement a delta-stepping algorithm that has been shown to outperform Dijkstra’s.
Delta stepping does not support negative weights. The algorithm assumes that adding a relationship to a path can never make a path shorter - an invariant that would be violated with negative weights.
The following will create a sample graph:
MERGE (a:Loc {name:'A'})
MERGE (b:Loc {name:'B'})
MERGE (c:Loc {name:'C'})
MERGE (d:Loc {name:'D'})
MERGE (e:Loc {name:'E'})
MERGE (f:Loc {name:'F'})
MERGE (a)-[:ROAD {cost:50}]->(b)
MERGE (a)-[:ROAD {cost:50}]->(c)
MERGE (a)-[:ROAD {cost:100}]->(d)
MERGE (b)-[:ROAD {cost:40}]->(d)
MERGE (c)-[:ROAD {cost:40}]->(d)
MERGE (c)-[:ROAD {cost:80}]->(e)
MERGE (d)-[:ROAD {cost:30}]->(e)
MERGE (d)-[:ROAD {cost:80}]->(f)
MERGE (e)-[:ROAD {cost:40}]->(f);
The following will run the algorithm and stream results:
MATCH (n:Loc {name:'A'})
CALL algo.shortestPath.deltaStepping.stream(n, 'cost', 3.0)
YIELD nodeId, distance
RETURN algo.asNode(nodeId).name AS destination, distance
The following will run the algorithm and write back results:
MATCH (n:Loc {name:'A'})
CALL algo.shortestPath.deltaStepping(n, 'cost', 3.0, {defaultValue:1.0, write:true, writeProperty:'sssp'})
YIELD nodeCount, loadDuration, evalDuration, writeDuration
RETURN nodeCount, loadDuration, evalDuration, writeDuration
Name | Cost |
---|---|
A |
0 |
B |
50 |
C |
50 |
D |
90 |
E |
120 |
F |
160 |
The above table shows the cost of going from A to each of the other nodes, including itself at a cost of 0.
If label and relationship-type are not selective enough to describe your subgraph to run the algorithm on, you can use Cypher statements to load or project subsets of your graph. This can also be used to run algorithms on a virtual graph. You can learn more in the Section 2.2, “Cypher projection” section of the manual.
Set graph:'cypher'
in the config:
MATCH (start:Loc{name:'A'}), (end:Loc{name:'F'})
CALL algo.shortestPath(start, end, 'cost',{
nodeQuery:'MATCH(n:Loc) WHERE not n.name = "c" RETURN id(n) as id',
relationshipQuery:'MATCH(n:Loc)-[r:ROAD]->(m:Loc) RETURN id(n) as source, id(m) as target, r.cost as weight',
graph:'cypher'})
YIELD writeMillis,loadMillis,nodeCount, totalCost
RETURN writeMillis,loadMillis,nodeCount,totalCost
The following will run the algorithm and write back results:
CALL algo.shortestPath.deltaStepping(startNode:Node, weightProperty:String, delta:Float,
{defaultValue:1.0, write:true, writeProperty:'sssp'})
YIELD nodeCount, loadDuration, evalDuration, writeDuration
RETURN nodeCount, loadDuration, evalDuration, writeDuration
Name | Type | Default | Optional | Description |
---|---|---|---|---|
startNode |
node |
null |
no |
The start node |
weightProperty |
string |
null |
yes |
The property name that contains weight. If null, treats the graph as unweighted. Must be numeric. |
delta |
float |
null |
yes |
The grade of concurrency to use. |
write |
boolean |
true |
yes |
Specifies if the result should be written back as a node property |
writeProperty |
string |
'sssp' |
yes |
The property name written back to the node sequence of the node in the path |
nodeQuery |
string |
null |
yes |
The label to load from the graph. If null, load all nodes |
relationshipQuery |
string |
null |
yes |
The relationship-type to load from the graph. If null, load all nodes |
direction |
string |
outgoing |
yes |
The relationship direction to load from the graph. If 'both', treats the relationships as undirected |
Name | Type | Description |
---|---|---|
nodeCount |
int |
The number of nodes considered |
totalCost |
float |
The sum of all weights along the path |
loadMillis |
int |
Milliseconds for loading data |
evalMillis |
int |
Milliseconds for running the algorithm |
writeMillis |
int |
Milliseconds for writing result data back |
The following will run the algorithm and stream results:
CALL algo.shortestPath.deltaStepping.stream(startNode:Node, weightProperty:String, delta: Float,
{nodeQuery:'labelName', relationshipQuery:'relationshipName', defaultValue:1.0, direction:'OUTGOING'})
YIELD nodeId, cost
Name | Type | Default | Optional | Description |
---|---|---|---|---|
startNode |
node |
null |
no |
The start node |
weightProperty |
string |
null |
yes |
The property name that contains weight. If null, treats the graph as unweighted. Must be numeric. |
delta |
float |
null |
yes |
The grade of concurrency to use. |
nodeQuery |
string |
null |
yes |
The label to load from the graph. If null, load all nodes |
relationshipQuery |
string |
null |
yes |
The relationship-type to load from the graph. If null, load all nodes |
defaultValue |
float |
null |
yes |
The default value of the weight in case it is missing or invalid |
direction |
string |
outgoing |
yes |
The relationship direction to load from the graph. If 'both', treats the relationships as undirected |
Name | Type | Description |
---|---|---|
nodeId |
int |
Node ID |
cost |
int |
The cost it takes to get from start node to specific node |
The shortest path algorithms support the following graph types:
✓ directed, unweighted:
✓ directed, weighted
✓ undirected, unweighted
✓ undirected, weighted
algo.shortestPath.deltaStepping