### 9.4.3. The Single Source Shortest Path algorithm

This section describes the Single Source Shortest Path algorithm in the Neo4j Labs 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 was developed by the Neo4j Labs team and is not officially supported.

This section includes:

#### 9.4.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.

#### 9.4.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.

#### 9.4.3.4. Single Source Shortest Path algorithm sample

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'})

##### 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.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'})

Table 9.68. 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.

#### 9.4.3.5. Cypher projection

If node 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'})

#### 9.4.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'})

Table 9.69. 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. The property contains the cost it takes to get from the start node to the specific node.

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 9.70. Results
Name Type Description

nodeCount

int

The number of nodes considered

totalCost

float

The sum of all weights along the path

int

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 9.71. 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 9.72. Results
Name Type Description

nodeId

int

Node ID

cost

int

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

#### 9.4.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'

#### 9.4.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.