6.3. The Single Source Shortest Path algorithm

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.

This section includes:

6.3.1. History and explanation

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.

6.3.2. Use-cases - when to use the Single Source Shortest Path algorithm

6.3.3. Constraints - when not to use the Single Source Shortest Path algorithm

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.

6.3.4. Single Source Shortest Path algorithm sample

sssp

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);

6.3.4.1. Delta stepping algorithm

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.getNodeById(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

Table 6.13. Results
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.

6.3.5. Cypher projection

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 1.3.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

6.3.6. Syntax

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

Table 6.14. Parameters
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

Table 6.15. Results
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

Table 6.16. Parameters
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

Table 6.17. Results
Name Type Description

nodeId

int

Node ID

cost

int

The cost it takes to get from start node to specific node

6.3.7. Graph type support

The shortest path algorithms support the following graph types:

  • ✓ directed, unweighted:

    • direction: 'OUTGOING' or INCOMING, weightProperty: null
  • ✓ directed, weighted

    • direction: 'OUTGOING' or INCOMING, weightProperty: 'cost'
  • ✓ undirected, unweighted

    • direction: 'BOTH', weightProperty: null
  • ✓ undirected, weighted

    • direction: 'BOTH', weightProperty: 'cost'

6.3.8. Implementations

algo.shortestPath.deltaStepping

  • Specify start node, find the shortest paths to all other nodes.
  • Parallel non-negative single source shortest path algorithm for weighted graphs.
  • It can be tweaked using the delta-parameter which controls the grade of concurrency.
  • If initialized with an non-existing weight-property, it will treat the graph as unweighted.