# Delta-Stepping Single-Source Shortest Path

## Glossary

Directed

Directed trait. The algorithm is well-defined on a directed graph.

Directed

Directed trait. The algorithm ignores the direction of the graph.

Directed

Directed trait. The algorithm does not run on a directed graph.

Undirected

Undirected trait. The algorithm is well-defined on an undirected graph.

Undirected

Undirected trait. The algorithm ignores the undirectedness of the graph.

Heterogeneous nodes

Heterogeneous nodes fully supported. The algorithm has the ability to distinguish between nodes of different types.

Heterogeneous nodes

Heterogeneous nodes allowed. The algorithm treats all selected nodes similarly regardless of their label.

Heterogeneous relationships

Heterogeneous relationships fully supported. The algorithm has the ability to distinguish between relationships of different types.

Heterogeneous relationships

Heterogeneous relationships allowed. The algorithm treats all selected relationships similarly regardless of their type.

Weighted relationships

Weighted trait. The algorithm supports a relationship property to be used as weight, specified via the relationshipWeightProperty configuration parameter.

Weighted relationships

Weighted trait. The algorithm treats each relationship as equally important, discarding the value of any relationship weight.

## Introduction

The Delta-Stepping Shortest Path algorithm computes all shortest paths between a source node and all reachable nodes in the graph. The algorithm supports weighted graphs with positive relationship weights. To compute the shortest path between a source and a single target node, Dijkstra Source-Target can be used.

In contrast to Dijkstra Single-Source, the Delta-Stepping algorithm is a distance correcting algorithm. This property allows it to traverse the graph in parallel. The algorithm is guaranteed to always find the shortest path between a source node and a target node. However, if multiple shortest paths exist between two nodes, the algorithm is not guaranteed to return the same path in each computation.

The GDS implementation is based on [1] and incorporates the bucket fusion optimization discussed in [2]. The algorithm implementation is executed using multiple threads which can be defined in the procedure configuration.

For more information on this algorithm, see:

## Syntax

This section covers the syntax used to execute the Delta-Stepping algorithm in each of its execution modes. We are describing the named graph variant of the syntax. To learn more about general syntax variants, see Syntax overview.

Delta-Stepping syntax per mode
Run Delta-Stepping in stream mode on a named graph.
``````CALL gds.allShortestPaths.delta.stream(
graphName: String,
configuration: Map
)
YIELD
index: Integer,
sourceNode: Integer,
targetNode: Integer,
totalCost: Float,
nodeIds: List of Integer,
costs: List of Float,
path: Path``````
Table 1. Parameters
Name Type Default Optional Description

graphName

String

`n/a`

no

The name of a graph stored in the catalog.

configuration

Map

`{}`

yes

Configuration for algorithm-specifics and/or graph filtering.

Table 2. Configuration
Name Type Default Optional Description

nodeLabels

List of String

`['*']`

yes

Filter the named graph using the given node labels. Nodes with any of the given labels will be included.

relationshipTypes

List of String

`['*']`

yes

Filter the named graph using the given relationship types. Relationships with any of the given types will be included.

concurrency

Integer

`4`

yes

The number of concurrent threads used for running the algorithm.

jobId

String

`Generated internally`

yes

An ID that can be provided to more easily track the algorithm’s progress.

logProgress

Boolean

`true`

yes

If disabled the progress percentage will not be logged.

sourceNode

Integer

`n/a`

no

The Neo4j source node or node id.

delta

Float

`2.0`

yes

The bucket width for grouping nodes with the same tentative distance to the source node.

relationshipWeightProperty

String

`null`

yes

Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted.

Table 3. Results
Name Type Description

index

Integer

0-based index of the found path.

sourceNode

Integer

Source node of the path.

targetNode

Integer

Target node of the path.

totalCost

Float

Total cost from source to target.

nodeIds

List of Integer

Node ids on the path in traversal order.

costs

List of Float

Accumulated costs for each node on the path.

path

Path

The path represented as Cypher entity.

The mutate mode creates new relationships in the projected graph. Each relationship represents a path from the source node to the target node. The total cost of a path is stored via the `totalCost` relationship property.

Run Delta-Stepping in mutate mode on a named graph.
``````CALL gds.allShortestPaths.delta.mutate(
graphName: String,
configuration: Map
)
YIELD
relationshipsWritten: Integer,
preProcessingMillis: Integer,
computeMillis: Integer,
postProcessingMillis: Integer,
mutateMillis: Integer,
configuration: Map``````
Table 4. Parameters
Name Type Default Optional Description

graphName

String

`n/a`

no

The name of a graph stored in the catalog.

configuration

Map

`{}`

yes

Configuration for algorithm-specifics and/or graph filtering.

Table 5. Configuration
Name Type Default Optional Description

mutateRelationshipType

String

`n/a`

no

The relationship type used for the new relationships written to the projected graph.

nodeLabels

List of String

`['*']`

yes

Filter the named graph using the given node labels.

relationshipTypes

List of String

`['*']`

yes

Filter the named graph using the given relationship types.

concurrency

Integer

`4`

yes

The number of concurrent threads used for running the algorithm.

jobId

String

`Generated internally`

yes

An ID that can be provided to more easily track the algorithm’s progress.

sourceNode

Integer

`n/a`

no

The Neo4j source node or node id.

delta

Float

`2.0`

yes

The bucket width for grouping nodes with the same tentative distance to the source node.

relationshipWeightProperty

String

`null`

yes

Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted.

Table 6. Results
Name Type Description

preProcessingMillis

Integer

Milliseconds for preprocessing the graph.

computeMillis

Integer

Milliseconds for running the algorithm.

postProcessingMillis

Integer

Unused.

mutateMillis

Integer

Milliseconds for adding relationships to the projected graph.

relationshipsWritten

Integer

The number of relationships that were added.

configuration

Map

The configuration used for running the algorithm.

The write mode creates new relationships in the Neo4j database. Each relationship represents a path from the source node to the target node. Additional path information is stored using relationship properties. By default, the write mode stores a `totalCost` property. Optionally, one can also store `nodeIds` and `costs` of intermediate nodes on the path.

Run Delta-Stepping in write mode on a named graph.
``````CALL gds.allShortestPaths.delta.write(
graphName: String,
configuration: Map
)
YIELD
relationshipsWritten: Integer,
preProcessingMillis: Integer,
computeMillis: Integer,
postProcessingMillis: Integer,
writeMillis: Integer,
configuration: Map``````
Table 7. Parameters
Name Type Default Optional Description

graphName

String

`n/a`

no

The name of a graph stored in the catalog.

configuration

Map

`{}`

yes

Configuration for algorithm-specifics and/or graph filtering.

Table 8. Configuration
Name Type Default Optional Description

nodeLabels

List of String

`['*']`

yes

Filter the named graph using the given node labels. Nodes with any of the given labels will be included.

relationshipTypes

List of String

`['*']`

yes

Filter the named graph using the given relationship types. Relationships with any of the given types will be included.

concurrency

Integer

`4`

yes

The number of concurrent threads used for running the algorithm.

jobId

String

`Generated internally`

yes

An ID that can be provided to more easily track the algorithm’s progress.

logProgress

Boolean

`true`

yes

If disabled the progress percentage will not be logged.

writeConcurrency

Integer

`value of 'concurrency'`

yes

The number of concurrent threads used for writing the result to Neo4j.

writeRelationshipType

String

`n/a`

no

The relationship type used to persist the computed relationships in the Neo4j database.

sourceNode

Integer

`n/a`

no

The Neo4j source node or node id.

delta

Float

`2.0`

yes

The bucket width for grouping nodes with the same tentative distance to the source node.

relationshipWeightProperty

String

`null`

yes

Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted.

writeNodeIds

Boolean

`false`

yes

If true, the written relationship has a nodeIds list property.

writeCosts

Boolean

`false`

yes

If true, the written relationship has a costs list property.

Table 9. Results
Name Type Description

preProcessingMillis

Integer

Milliseconds for preprocessing the graph.

computeMillis

Integer

Milliseconds for running the algorithm.

postProcessingMillis

Integer

Unused.

writeMillis

Integer

Milliseconds for writing relationships to Neo4j.

relationshipsWritten

Integer

The number of relationships that were written.

configuration

Map

The configuration used for running the algorithm.

Run Delta-Stepping in stats mode on a named graph.
``````CALL gds.allShortestPaths.delta.stats(
graphName: String,
configuration: Map
)
YIELD
preProcessingMillis: Integer,
computeMillis: Integer,
postProcessingMillis: Integer,
configuration: Map``````
Table 10. Parameters
Name Type Default Optional Description

graphName

String

`n/a`

no

The name of a graph stored in the catalog.

configuration

Map

`{}`

yes

Configuration for algorithm-specifics and/or graph filtering.

Table 11. Configuration
Name Type Default Optional Description

nodeLabels

List of String

`['*']`

yes

Filter the named graph using the given node labels. Nodes with any of the given labels will be included.

relationshipTypes

List of String

`['*']`

yes

Filter the named graph using the given relationship types. Relationships with any of the given types will be included.

concurrency

Integer

`4`

yes

The number of concurrent threads used for running the algorithm.

jobId

String

`Generated internally`

yes

An ID that can be provided to more easily track the algorithm’s progress.

logProgress

Boolean

`true`

yes

If disabled the progress percentage will not be logged.

sourceNode

Integer

`n/a`

no

The Neo4j source node or node id.

delta

Float

`2.0`

yes

The bucket width for grouping nodes with the same tentative distance to the source node.

relationshipWeightProperty

String

`null`

yes

Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted.

Table 12. Results
Name Type Description

preProcessingMillis

Integer

Milliseconds for preprocessing the graph.

computeMillis

Integer

Milliseconds for running the algorithm.

postProcessingMillis

Integer

Unused.

configuration

Map

The configuration used for running the algorithm.

### Delta

The `delta` parameter defines a range which is used to group nodes with the same tentative distance to the start node. The ranges are also called buckets. In each iteration of the algorithm, the non-empty bucket with the smallest tentative distance is processed in parallel. The `delta` parameter is the main tuning knob for the algorithm and controls the workload that can be processed in parallel. Generally, for power-law graphs, where many nodes can be reached within a few hops, a small delta (e.g. `2`) is recommended. For high-diameter graphs, e.g. transport networks, a high delta value (e.g. `10000`) is recommended. Note, that the value might vary depending on the graph topology and the value range of relationship properties.

## Examples

 All the examples below should be run in an empty database. The examples use native projections as the norm, although Cypher projections can be used as well.

In this section we will show examples of running the Delta-Stepping algorithm on a concrete graph. The intention is to illustrate what the results look like and to provide a guide in how to make use of the algorithm in a real setting. We will do this on a small transport network graph of a handful nodes connected in a particular pattern. The example graph looks like this:

The following Cypher statement will create the example graph in the Neo4j database:
``````CREATE (a:Location {name: 'A'}),
(b:Location {name: 'B'}),
(c:Location {name: 'C'}),
(d:Location {name: 'D'}),
(e:Location {name: 'E'}),
(f:Location {name: 'F'}),
(a)-[:ROAD {cost: 50}]->(b),
(a)-[:ROAD {cost: 50}]->(c),
(a)-[:ROAD {cost: 100}]->(d),
(b)-[:ROAD {cost: 40}]->(d),
(c)-[:ROAD {cost: 40}]->(d),
(c)-[:ROAD {cost: 80}]->(e),
(d)-[:ROAD {cost: 30}]->(e),
(d)-[:ROAD {cost: 80}]->(f),
(e)-[:ROAD {cost: 40}]->(f);``````

This graph builds a transportation network with roads between locations. Like in the real world, the roads in the graph have different lengths. These lengths are represented by the `cost` relationship property.

The following statement will project a graph using a native projection and store it in the graph catalog under the name 'myGraph'.
``````CALL gds.graph.project(
'myGraph',
'Location',
'ROAD',
{
relationshipProperties: 'cost'
}
)``````

In the following example we will demonstrate the use of the Delta-Stepping Shortest Path algorithm using this graph.

### Memory Estimation

First off, we will estimate the cost of running the algorithm using the `estimate` procedure. This can be done with any execution mode. We will use the `write` mode in this example. Estimating the algorithm is useful to understand the memory impact that running the algorithm on your graph will have. When you later actually run the algorithm in one of the execution modes the system will perform an estimation. If the estimation shows that there is a very high probability of the execution going over its memory limitations, the execution is prohibited. To read more about this, see Automatic estimation and execution blocking.

For more details on `estimate` in general, see Memory Estimation.

The following will estimate the memory requirements for running the algorithm in write mode:
``````MATCH (source:Location {name: 'A'})
CALL gds.allShortestPaths.delta.write.estimate('myGraph', {
sourceNode: source,
relationshipWeightProperty: 'cost',
writeRelationshipType: 'PATH'
})
YIELD nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory
RETURN nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory``````
Table 13. Results
nodeCount relationshipCount bytesMin bytesMax requiredMemory

6

9

368

576

"[368 Bytes ... 576 Bytes]"

### Stream

In the `stream` execution mode, the algorithm returns the shortest path for each source-target-pair. This allows us to inspect the results directly or post-process them in Cypher without any side effects.

For more details on the `stream` mode in general, see Stream.

The following will run the algorithm and stream results:
``````MATCH (source:Location {name: 'A'})
CALL gds.allShortestPaths.delta.stream('myGraph', {
sourceNode: source,
relationshipWeightProperty: 'cost',
delta: 3.0
})
YIELD index, sourceNode, targetNode, totalCost, nodeIds, costs, path
RETURN
index,
gds.util.asNode(sourceNode).name AS sourceNodeName,
gds.util.asNode(targetNode).name AS targetNodeName,
totalCost,
[nodeId IN nodeIds | gds.util.asNode(nodeId).name] AS nodeNames,
costs,
nodes(path) as path
ORDER BY index``````
Table 14. Results
index sourceNodeName targetNodeName totalCost nodeNames costs path

0

"A"

"A"

0.0

[A]

[0.0]

[Node[0]]

1

"A"

"B"

50.0

[A, B]

[0.0, 50.0]

[Node[0], Node[1]]

2

"A"

"C"

50.0

[A, C]

[0.0, 50.0]

[Node[0], Node[2]]

3

"A"

"D"

90.0

[A, B, D]

[0.0, 50.0, 90.0]

[Node[0], Node[1], Node[3]]

4

"A"

"E"

120.0

[A, B, D, E]

[0.0, 50.0, 90.0, 120.0]

[Node[0], Node[1], Node[3], Node[4]]

5

"A"

"F"

160.0

[A, B, D, E, F]

[0.0, 50.0, 90.0, 120.0, 160.0]

[Node[0], Node[1], Node[3], Node[4], Node[5]]

The result shows the total cost of the shortest path between node `A` and all other reachable nodes in the graph. It also shows ordered lists of node ids that were traversed to find the shortest paths as well as the accumulated costs of the visited nodes. This can be verified in the example graph. Cypher Path objects can be returned by the `path` return field. The Path objects contain the node objects and virtual relationships which have a `cost` property.

### Mutate

The `mutate` execution mode updates the named graph with new relationships. Each new relationship represents a path from source node to target node. The relationship type is configured using the `mutateRelationshipType` option. The total path cost is stored using the `totalCost` property.

The `mutate` mode is especially useful when multiple algorithms are used in conjunction.

For more details on the `mutate` mode in general, see Mutate.

The following will run the algorithm in `mutate` mode:
``````MATCH (source:Location {name: 'A'})
CALL gds.allShortestPaths.delta.mutate('myGraph', {
sourceNode: source,
relationshipWeightProperty: 'cost',
mutateRelationshipType: 'PATH'
})
YIELD relationshipsWritten
RETURN relationshipsWritten``````
Table 15. Results
relationshipsWritten

6

After executing the above query, the in-memory graph will be updated with new relationships of type `PATH`. The new relationships will store a single property `totalCost`.

 The relationships produced are always directed, even if the input graph is undirected.

### Write

The `write` execution mode updates the Neo4j database with new relationships. Each new relationship represents a path from source node to target node. The relationship type is configured using the `writeRelationshipType` option. The total path cost is stored using the `totalCost` property. The intermediate node ids are stored using the `nodeIds` property. The accumulated costs to reach an intermediate node are stored using the `costs` property.

For more details on the `write` mode in general, see Write.

The following will run the algorithm in `write` mode:
``````MATCH (source:Location {name: 'A'})
CALL gds.allShortestPaths.delta.write('myGraph', {
sourceNode: source,
relationshipWeightProperty: 'cost',
writeRelationshipType: 'PATH',
writeNodeIds: true,
writeCosts: true
})
YIELD relationshipsWritten
RETURN relationshipsWritten``````
Table 16. Results
relationshipsWritten

6

The above query will write 6 relationships of type `PATH` back to Neo4j. The relationships store three properties describing the path: `totalCost`, `nodeIds` and `costs`.

 The relationships written are always directed, even if the input graph is undirected.