# Minimum Directed Steiner Tree

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

Given a source node and a list of target nodes, a directed spanning tree in which there exists a path from the source node to each of the target nodes is called a Directed Steiner Tree.

The Minimum Directed Steiner Tree problem asks for the steiner tree that minimizes the sum of all relationship weights in tree.

The Minimum Directed Steiner Tree problem is known to be NP-Complete and no efficient exact algorithms have been proposed in the literature. The Neo4j GDS Library offers an efficient implementation of a well-known heuristic for Steiner Tree related problems.

The implemented algorithm works on a number of steps. At each step, the shortest path from the source to one of the undiscovered targets is found and added to the result. Following that, the weights in the relationships in this path are reduced to zero, and the algorithm continues similarly by finding the next closest unvisited target node.

With a careful implementation, the above heuristic can run efficiently even for graphs of large size. In addition, the parallel shortest path algorithm of Delta-Stepping is used to further speed-up computations.

## Considerations

As the Minimum Directed Steiner Tree algorithm relies on shortest-paths, it will not work for graphs with negative relationship weights.

The Minimum Directed Steiner Tree problem is a variant of the more general Minimum Steiner Tree problem defined for undirected graphs. The Minimum Steiner Tree problem accepts as input only a set of target nodes. The aim is then to find a spanning tree of minimum weight connecting these target nodes.

It is possible to use the GDS implementation to find a solution for Minimum Steiner Tree problem by arbitrarily selecting one of the target nodes to fill the role of the source node.

## Syntax

Spanning Tree syntax per mode
Run the algorithm in stream mode on a named graph.
``````CALL gds.steinerTree.stream(
graphName: String,
configuration: Map
)
YIELD
nodeId: Integer,
parentId: Integer,
weight: Float``````
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

`null`

n/a

The starting source node ID.

targetNodes

List of Integer

`null`

n/a

The list of target nodes

relationshipWeightProperty

String

`null`

yes

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

delta

Float

`2.0`

yes

The bucket width for grouping nodes with the same tentative distance to the source node. Look into the Delta-Stepping documentation for more information.

applyRerouting

Boolean

`false`

yes

If specified, the algorithm will try to improve the outcome via an additional post-processing heuristic.

Table 3. Results
Name Type Description

nodeId

Integer

A node in the discovered spanning tree.

parentId

Integer

The parent of nodeId in the spanning tree or nodeId if it is equal to the source node.

weight

Float

The weight of the relationship from parentId to nodeId.

Run the algorithm in stats mode on a named graph.
``````CALL gds.steinerTree.stats(
graphName: String,
configuration: Map
)
YIELD
effectiveNodeCount: Integer,
effectiveTargetNodesCount: Integer,
totalWeight: Float,
preProcessingMillis: Integer,
computeMillis: 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

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

`null`

n/a

The starting source node ID.

targetNodes

List of Integer

`null`

n/a

The list of target nodes

relationshipWeightProperty

String

`null`

yes

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

delta

Float

`2.0`

yes

The bucket width for grouping nodes with the same tentative distance to the source node. Look into the Delta-Stepping documentation for more information.

applyRerouting

Boolean

`false`

yes

If specified, the algorithm will try to improve the outcome via an additional post-processing heuristic.

Table 6. Results
Name Type Description

effectiveNodeCount

Integer

The number of nodes in the spanning tree.

effectiveTargetNodesCount

Integer

The number of target nodes in the spanning tree.

totalWeight

Float

The sum of the weights of the relationships in the spanning tree.

preProcessingMillis

Integer

Milliseconds for preprocessing the data.

computeMillis

Integer

Milliseconds for running the algorithm.

configuration

Map

The configuration used for running the algorithm.

Run the Spanning Tree algorithm in mutate mode on a named graph.
``````CALL gds.steinerTree.mutate(
graphName: String,
configuration: Map
)
YIELD
effectiveNodeCount: Integer,
effectiveTargetNodesCount: Integer,
totalWeight: Float,
relationshipsWritten: Integer,
preProcessingMillis: Integer,
computeMillis: Integer,
mutateMillis: 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

mutateRelationshipType

String

`n/a`

no

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

mutateProperty

String

`n/a`

no

The relationship property in the GDS graph to which the weight is written.

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

`null`

n/a

The starting source node ID.

targetNodes

List of Integer

`null`

n/a

The list of target nodes

relationshipWeightProperty

String

`null`

yes

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

delta

Float

`2.0`

yes

The bucket width for grouping nodes with the same tentative distance to the source node. Look into the Delta-Stepping documentation for more information.

applyRerouting

Boolean

`false`

yes

If specified, the algorithm will try to improve the outcome via an additional post-processing heuristic.

Table 9. Results
Name Type Description

effectiveNodeCount

Integer

The number of nodes in the spanning tree.

effectiveTargetNodesCount

Integer

The number of target nodes in the spanning tree.

totalWeight

Float

The sum of the weights of the relationships in the spanning tree.

relationshipsWritten

Integer

The number of relationships added to the in-memory graph.

preProcessingMillis

Integer

Milliseconds for preprocessing the data.

computeMillis

Integer

Milliseconds for running the algorithm.

mutateMillis

Integer

Milliseconds for writing result data back.

configuration

Map

The configuration used for running the algorithm.

Run the Spanning Tree algorithm in write mode on a named graph.
``````CALL gds.steinerTree.write(
graphName: String,
configuration: Map
)
YIELD
effectiveNodeCount: Integer,
effectiveTargetNodesCount: Integer,
totalWeight: Float,
relationshipsWritten: Integer,
preProcessingMillis: Integer,
computeMillis: Integer,
writeMillis: 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.

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.

writeProperty

String

`n/a`

no

The relationship property in the Neo4j database to which the weight is written.

sourceNode

Integer

`null`

n/a

The starting source node ID.

targetNodes

List of Integer

`null`

n/a

The list of target nodes

relationshipWeightProperty

String

`null`

yes

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

delta

Float

`2.0`

yes

The bucket width for grouping nodes with the same tentative distance to the source node. Look into the Delta-Stepping documentation for more information.

applyRerouting

Boolean

`false`

yes

If specified, the algorithm will try to improve the outcome via an additional post-processing heuristic.

Table 12. Results
Name Type Description

effectiveNodeCount

Integer

The number of nodes in the spanning tree.

effectiveTargetNodesCount

Integer

The number of target nodes in the spanning tree.

totalWeight

Float

The sum of the weights of the relationships in the spanning tree.

relationshipsWritten

Integer

The number of relationships written to the graph.

preProcessingMillis

Integer

Milliseconds for preprocessing the data.

computeMillis

Integer

Milliseconds for running the algorithm.

writeMillis

Integer

Milliseconds for writing result data back.

configuration

Map

The configuration used for running the algorithm.

## 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 Steiner Tree heuristic 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 road network graph of a handful nodes connected in a particular pattern. The example graph looks like this:

The following will create the sample graph depicted in the figure:
``````CREATE (a:Place {id: 'A'}),
(b:Place {id: 'B'}),
(c:Place {id: 'C'}),
(d:Place {id: 'D'}),
(e:Place {id: 'E'}),
(f:Place {id: 'F'}),
The following will project and store a named graph:
``````CALL gds.graph.project(
'graph',
'Place',
{
properties: 'cost'
}
}
)``````

### 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 `stats` 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 stats mode:
``````MATCH (a:Place{id: 'A'}), (d:Place{id: 'D'}),(e:Place{id: 'E'}),(f:Place{id: 'F'})
CALL gds.steinerTree.stats.estimate('graph', {
sourceNode: a,
targetNodes: [d, e, f],
relationshipWeightProperty: 'cost'
})
YIELD nodeCount, relationshipCount, requiredMemory
RETURN nodeCount, relationshipCount, requiredMemory``````
Table 13. Results
nodeCount relationshipCount requiredMemory

6

7

"[672 Bytes ... 864 Bytes]"

### Stream

In the `stream` execution mode, the algorithm returns the weight for each relationship. 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 Minimum Directed Steiner Tree algorithm in stream mode and return results for each valid node.
``````MATCH (a:Place{id: 'A'}), (d:Place{id: 'D'}),(e:Place{id: 'E'}),(f:Place{id: 'F'})
CALL gds.steinerTree.stream('graph', {
sourceNode: a,
targetNodes: [d, e, f],
relationshipWeightProperty: 'cost'
})
YIELD nodeId,parentId, weight
RETURN gds.util.asNode(nodeId).id AS node, gds.util.asNode(parentId).id AS parent,weight
ORDER BY node``````
Table 14. Results
node parent weight

"A"

"A"

0.0

"B"

"A"

1.0

"C"

"B"

1.0

"D"

"C"

4.0

"E"

"C"

6.0

"F"

"A"

10.0

The algorithm first finds the shortest path from A to D. Then, even though the relationship weight from A to E is less than the sum of weighted path A,B,C,E, the algorithm realizes that the relationships between A and B as well as B and C are already included in the solution and therefore reaching E via C is a better alternative. Finally, the algorithm adds the relationship between A and F in the solution and terminates.

### Stats

In the `stats` execution mode, the algorithm returns a single row containing a summary of the algorithm result. This execution mode does not have any side effects. It can be useful for evaluating algorithm performance by inspecting the `computeMillis` return item. In the examples below we will omit returning the timings. The full signature of the procedure can be found in the syntax section.

For more details on the `stats` mode in general, see Stats.

The following will run the Minimum Weight Spanning Tree algorithm and return its statistics.
``````MATCH (a:Place{id: 'A'}), (d:Place{id: 'D'}),(e:Place{id: 'E'}),(f:Place{id: 'F'})
CALL gds.steinerTree.stats('graph', {
sourceNode: a,
targetNodes: [d, e, f],
relationshipWeightProperty: 'cost'
})
YIELD effectiveNodeCount, totalWeight
RETURN effectiveNodeCount, totalWeight``````
Table 15. Results
effectiveNodeCount totalWeight

6

22.0

### Write

The `write` execution mode extends the `stats` mode with an important side effect: writing the weight for each relationship as a property to the Neo4j database. The name of the new property is specified using the mandatory configuration parameter `writeProperty`. The result is a single summary row, similar to `stats`, but with some additional metrics. The `write` mode enables directly persisting the results to the database.

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

The following will run the Minimum Directed Steiner Tree algorithm and write the result back to the graph.
``````MATCH (a:Place{id: 'A'}), (d:Place{id: 'D'}),(e:Place{id: 'E'}),(f:Place{id: 'F'})
CALL gds.steinerTree.write('graph', {
sourceNode: a,
targetNodes: [d, e, f],
relationshipWeightProperty: 'cost',
writeProperty: 'steinerWeight',
writeRelationshipType: 'STEINER'
})
YIELD preProcessingMillis, computeMillis, writeMillis, effectiveNodeCount
RETURN preProcessingMillis, computeMillis, writeMillis, effectiveNodeCount;``````
To find the relationships included in the minimum spanning tree, we can run the following query:
``````MATCH path = (a:Place {id: 'A'})-[:STEINER*]-()
WITH relationships(path) AS rels
UNWIND rels AS rel
WITH DISTINCT rel AS rel
RETURN startNode(rel).id AS Source, endNode(rel).id AS Destination, rel.steinerWeight AS weight
ORDER BY Source, Destination``````
Table 16. Results
Source Destination weight

"A"

"B"

1.0

"A"

"F"

10.0

"B"

"C"

1.0

"C"

"D"

4.0

"C"

"E"

6.0

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

### Mutate

The `mutate` execution mode extends the `stats` mode with an important side effect: updating the named graph with a new relationship property containing the weight for that relationship. The name of the new property is specified using the mandatory configuration parameter `mutateProperty`. The result is a single summary row, similar to `stats`, but with some additional metrics. 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 Minimum Directed Steiner Tree algorithm and mutate the in-memory graph.
``````MATCH (a:Place{id: 'A'}), (d:Place{id: 'D'}),(e:Place{id: 'E'}),(f:Place{id: 'F'})
CALL gds.steinerTree.mutate('graph', {
sourceNode: a,
targetNodes: [d, e, f],
relationshipWeightProperty: 'cost',
mutateProperty: 'steinerWeight',
mutateRelationshipType: 'STEINER'
})
YIELD relationshipsWritten
RETURN relationshipsWritten``````
Table 17. Results
relationshipsWritten

5

 The relationships added back to the graph are always directed, even if the input graph is undirected.

### Rerouting examples

It is also possible to try and augment the solution discovered by the heuristic via a post-processing rerouting phase. This option can be enabled by setting `applyRerouting: true` in the configuration.

The algorithm supports two forms of rerouting: simple and extended. Extended is more involved than simple and can get better quality improvements, but it requires having an inverse index for the adjacency list.

#### Simple Rerouting

The rerouting phase re-examines the relationships in the discovered steiner tree and tries to reroute nodes (that is change their parent with another node in the tree) so as to decrease the cost. After the rerouting phase some nodes might end up being childless, that is not part of any path between the source and a target. Such nodes are then removed from the returned solution.

Note that there is no guarantee that enabling rerouting will always lead to an improvement in quality.

The following will run the Minimum Directed Steiner Tree algorithm with rerouting in stream mode and return results for each valid node.
``````MATCH (a:Place{id: 'A'}), (d:Place{id: 'D'}),(e:Place{id: 'E'}),(f:Place{id: 'F'})
CALL gds.steinerTree.stream('graph', {
sourceNode: a,
targetNodes: [d, e, f],
relationshipWeightProperty: 'cost',
applyRerouting: true
})
YIELD nodeId,parentId, weight
RETURN gds.util.asNode(nodeId).id AS node, gds.util.asNode(parentId).id AS parent, weight
ORDER BY node``````
Table 18. Results
node parent weight

"A"

"A"

0.0

"B"

"A"

1.0

"C"

"B"

1.0

"D"

"F"

3.0

"E"

"C"

6.0

"F"

"A"

10.0

As can be seen, thanks to the rerouting step, D’s parent has been replaced with F and the overall weight of the steiner tree is reduced by 2.

#### Extended Rerouting

We now demonstrate the usage of extended rerouting. For that, first we need to project the graph once more, this time creating an inverse index.

``````CALL gds.graph.project(
'inverseGraph',
'Place',
{
properties: 'cost', indexInverse: true
}
}
)``````

We know repeat the algorithm; this time with the extended rerouting heuristic.

``````MATCH (a:Place{id: 'A'}), (d:Place{id: 'D'}),(e:Place{id: 'E'}),(f:Place{id: 'F'})
CALL gds.steinerTree.stream('inverseGraph', {
sourceNode: a,
targetNodes: [d, e, f],
relationshipWeightProperty: 'cost',
applyRerouting: true
})
YIELD nodeId,parentId, weight
RETURN gds.util.asNode(nodeId).id AS node, gds.util.asNode(parentId).id AS parent, weight
ORDER BY node``````
Table 19. Results
node parent weight

"A"

"A"

0.0

"D"

"F"

3.0

"E"

"A"

7.0

"F"

"A"

10.0

As you can see, thanks to the extended rerouting, we can further reduce the cost and return the optimal steiner tree having a weight of 20.

 Unlike the main algorithm, the rerouting phase runs sequentially and is not affected by the concurrency parameter.