Minimum Directed Steiner Tree
Glossary
 Directed

Directed trait. The algorithm is welldefined 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 welldefined 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 NPComplete and no efficient exact algorithms have been proposed in the literature. The Neo4j GDS Library offers an efficient implementation of a wellknown 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 DeltaStepping is used to further speedup computations.
Considerations
As the Minimum Directed Steiner Tree algorithm relies on shortestpaths, 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
CALL gds.steinerTree.stream(
graphName: String,
configuration: Map
)
YIELD
nodeId: Integer,
parentId: Integer,
weight: Float
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

List of String 

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

List of String 

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

Integer 

yes 
The number of concurrent threads used for running the algorithm. 

String 

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

Boolean 

yes 
If disabled the progress percentage will not be logged. 

sourceNode 
Integer 

n/a 
The starting source node ID. 
targetNodes 
List of Integer 

n/a 
The list of target nodes 
String 

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

delta 
Float 

yes 
The bucket width for grouping nodes with the same tentative distance to the source node. Look into the DeltaStepping documentation for more information. 
applyRerouting 
Boolean 

yes 
If specified, the algorithm will try to improve the outcome via an additional postprocessing heuristic. 
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. 
CALL gds.steinerTree.stats(
graphName: String,
configuration: Map
)
YIELD
effectiveNodeCount: Integer,
effectiveTargetNodesCount: Integer,
totalWeight: Float,
preProcessingMillis: Integer,
computeMillis: Integer,
configuration: Map
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

List of String 

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

List of String 

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

Integer 

yes 
The number of concurrent threads used for running the algorithm. 

String 

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

Boolean 

yes 
If disabled the progress percentage will not be logged. 

sourceNode 
Integer 

n/a 
The starting source node ID. 
targetNodes 
List of Integer 

n/a 
The list of target nodes 
String 

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

delta 
Float 

yes 
The bucket width for grouping nodes with the same tentative distance to the source node. Look into the DeltaStepping documentation for more information. 
applyRerouting 
Boolean 

yes 
If specified, the algorithm will try to improve the outcome via an additional postprocessing heuristic. 
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. 
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
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

mutateRelationshipType 
String 

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

no 
The relationship property in the GDS graph to which the weight is written. 
List of String 

yes 
Filter the named graph using the given node labels. 

List of String 

yes 
Filter the named graph using the given relationship types. 

Integer 

yes 
The number of concurrent threads used for running the algorithm. 

String 

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

sourceNode 
Integer 

n/a 
The starting source node ID. 
targetNodes 
List of Integer 

n/a 
The list of target nodes 
String 

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

delta 
Float 

yes 
The bucket width for grouping nodes with the same tentative distance to the source node. Look into the DeltaStepping documentation for more information. 
applyRerouting 
Boolean 

yes 
If specified, the algorithm will try to improve the outcome via an additional postprocessing heuristic. 
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 inmemory 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. 
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
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

List of String 

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

List of String 

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

Integer 

yes 
The number of concurrent threads used for running the algorithm. 

String 

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

Boolean 

yes 
If disabled the progress percentage will not be logged. 

Integer 

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

writeRelationshipType 
String 

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

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

sourceNode 
Integer 

n/a 
The starting source node ID. 
targetNodes 
List of Integer 

n/a 
The list of target nodes 
String 

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

delta 
Float 

yes 
The bucket width for grouping nodes with the same tentative distance to the source node. Look into the DeltaStepping documentation for more information. 
applyRerouting 
Boolean 

yes 
If specified, the algorithm will try to improve the outcome via an additional postprocessing heuristic. 
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 Cypher projections as the norm. Native projections will be deprecated in a future release. 
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:
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'}),
(a)[:LINK {cost:10}]>(f),
(a)[:LINK {cost:1}]>(b),
(a)[:LINK {cost:7}]>(e),
(b)[:LINK {cost:1}]>(c),
(c)[:LINK {cost:4}]>(d),
(c)[:LINK {cost:6}]>(e),
(f)[:LINK {cost:3}]>(d);
MATCH (source:Place)[r:LINK]>(target:Place)
RETURN gds.graph.project(
'graph',
source,
target,
{ relationshipProperties: r { .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.
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
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 postprocess them in Cypher without any side effects.
For more details on the stream
mode in general, see Stream.
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
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.
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
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.
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;
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
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.
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
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 postprocessing 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 reexamines 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.
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
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.
MATCH (source:Place)[r:LINK]>(target:Place)
RETURN gds.graph.project(
'inverseGraph',
source,
target,
{
relationshipType: 'LINK',
relationshipProperties: r { .cost }
},
{ inverseIndexedRelationshipTypes: ['LINK'] }
)
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
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. 