Prize-Collecting 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
A spanning tree is a graph such that there is exactly one path between any two nodes in the set. A graph can have many possible spanning tree subsets depending on the set of nodes/relationships selected.
Given a weighted graph where each node has a prize, the Prize-Collecting Steiner Tree problem asks for the spanning tree that satisfies the following conditions:
-
the sum of prizes for the nodes in the graph is maximized.
-
the sum of weights of relationships and prizes for nodes not in the tree is minimized.
The two constraints can combined to form a single maximization problem by simpling subtracting the second constraint for the former.
The Prize-Collecting Steiner Tree is NP-Complete and no efficient exact algorithms is known. The Neo4j GDS Library implements a practical 2-approximate algorithm from the literature. This means that the returned answer should be at least half as good as the optimal answer.
Considerations
By default, the Prize-Collecting Steiner Tree problem considers prizes only for nodes.
In some cases, however, it can be useful to also consider prizes on relationships.
The GDS implementation can handle prizes for relationships through the following transformation:
Given a relationship with weight w
and prize p
,
we suggest to replace w
with w' = w - p
.
This should be done as a pre-processing step prior to projecting the in-memory graph.
Syntax
CALL gds.prizeSteinerTree.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 algorithm-specifics 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. |
|
String |
|
yes |
Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted. |
|
prizeProperty |
String |
|
no |
The name of node property that denotes a node’s price. |
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.prizeSteinerTree.stats(
graphName: String,
configuration: Map
)
YIELD
effectiveNodeCount: Integer,
totalWeight: Float,
sumOfPrizes: 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 algorithm-specifics 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. |
|
String |
|
yes |
Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted. |
|
prizeProperty |
String |
|
no |
The name of node property that denotes a node’s price. |
Name | Type | Description |
---|---|---|
effectiveNodeCount |
Integer |
The number of nodes in the spanning tree. |
totalWeight |
Float |
The sum of the weights of the relationships in the spanning tree. |
sumOfPrizes |
Float |
The sum of prizes for the nodes 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.prizeSteinerTree.mutate(
graphName: String,
configuration: Map
)
YIELD
effectiveNodeCount: Integer,
totalWeight: Float,
sumOfPrizes: Float,
preProcessingMillis: Integer,
computeMillis: Integer,
mutateMillis: Integer,
relationshipsWritten: 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 algorithm-specifics 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. |
|
String |
|
yes |
Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted. |
|
prizeProperty |
String |
|
no |
The name of node property that denotes a node’s price. |
Name | Type | Description |
---|---|---|
effectiveNodeCount |
Integer |
The number of nodes in the spanning tree. |
totalWeight |
Float |
The sum of the weights of the relationships in the spanning tree. |
sumOfPrizes |
Float |
The sum of prizes for the nodes in the spanning tree. |
preProcessingMillis |
Integer |
Milliseconds for preprocessing the data. |
computeMillis |
Integer |
Milliseconds for running the algorithm. |
mutateMillis |
Integer |
Milliseconds for writing result data back. |
relationshipsWritten |
Integer |
The number of relationships added to the in-memory graph. |
configuration |
Map |
The configuration used for running the algorithm. |
CALL gds.prizeSteinerTree.write(
graphName: String,
configuration: Map
)
YIELD
effectiveNodeCount: Integer,
totalWeight: Float,
sumOfPrizes: Float,
preProcessingMillis: Integer,
computeMillis: Integer,
writeMillis: Integer,
relationshipsWritten: 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 algorithm-specifics 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. |
|
String |
|
yes |
Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted. |
|
prizeProperty |
String |
|
no |
The name of node property that denotes a node’s price. |
Name | Type | Description |
---|---|---|
effectiveNodeCount |
Integer |
The number of nodes in the spanning tree. |
totalWeight |
Float |
The sum of the weights of the relationships in the spanning tree. |
sumOfPrizes |
Float |
The sum of prizes for the nodes in the spanning tree. |
preProcessingMillis |
Integer |
Milliseconds for preprocessing the data. |
computeMillis |
Integer |
Milliseconds for running the algorithm. |
writeMillis |
Integer |
Milliseconds for writing result data back. |
relationshipsWritten |
Integer |
The number of relationships written to the graph. |
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 Prize-Collecting Steiner Tree algorithm 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', prize: 5.0}),
(b:Place {id: 'B', prize: 20.0}),
(c:Place {id: 'C',prize: 11.0}),
(d:Place {id: 'D',prize: 10.0}),
(e:Place {id: 'E',prize: 8.0}),
(f:Place {id: 'F',prize: 1.0}),
(a)-[:LINK {cost:10}]->(f),
(a)-[:LINK {cost:3}]->(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,
{
sourceNodeProperties: source { .prize },
targetNodeProperties: target { .prize },
relationshipProperties: r { .cost }
},
{ undirectedRelationshipTypes: ['*'] }
)
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 stream
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.
CALL gds.prizeSteinerTree.stream.estimate('graph', {
relationshipWeightProperty: 'cost',
prizeProperty: 'prize'
})
YIELD nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory
RETURN nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory
nodeCount | relationshipCount | bytesMin | bytesMax | requiredMemory |
---|---|---|---|---|
6 |
14 |
3897 |
561616 |
"[3897 Bytes ... 548 KiB]" |
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.
CALL gds.prizeSteinerTree.stream('graph', {
relationshipWeightProperty: 'cost',
prizeProperty: 'prize'
})
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" |
"B" |
3.0 |
"B" |
"C" |
1.0 |
"D" |
"C" |
4.0 |
"E" |
"C" |
6.0 |
The algorithm finds a tree containing A,B,C,D, and E. The node F is skipped because it’s price is very low and connecting it with the other nodes would yield an inferior solution.
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.
CALL gds.prizeSteinerTree.stats('graph', {
relationshipWeightProperty: 'cost',
prizeProperty: 'prize'
})
YIELD effectiveNodeCount, totalWeight, sumOfPrizes
RETURN effectiveNodeCount, totalWeight, sumOfPrizes
effectiveNodeCount | totalWeight | sumOfPrizes |
---|---|---|
5 |
14.0 |
54.0 |
The stats mode provides us with information about the total sum weights of the relationships in the connected tree, which is 14.0
, as well as the sum of prizes for nodes A,B,C,D, and E, which is 54.0
.
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.
CALL gds.prizeSteinerTree.mutate('graph', {
relationshipWeightProperty: 'cost',
prizeProperty: 'prize',
mutateProperty: 'weight',
mutateRelationshipType: 'STEINER'
})
YIELD effectiveNodeCount, totalWeight, sumOfPrizes, relationshipsWritten
RETURN effectiveNodeCount, totalWeight, sumOfPrizes, relationshipsWritten
effectiveNodeCount | totalWeight | sumOfPrizes | relationshipsWritten |
---|---|---|---|
5 |
14.0 |
54.0 |
4 |
The mutate mode updates the in-memory graph graph
with new relationship type
called STEINER
with a single property weight
.
From the relationshipsWritten
column, we can see that exactly four such relationships were added.
They connect the nodes in the steiner tree, and their property is the cost of each connection.
The relationships added back to the graph are always directed, even if the input graph is undirected. They point from |
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.
CALL gds.prizeSteinerTree.write('graph', {
relationshipWeightProperty: 'cost',
prizeProperty: 'prize',
writeProperty: 'weight',
writeRelationshipType: 'STEINER'
})
YIELD effectiveNodeCount, totalWeight, sumOfPrizes, relationshipsWritten
RETURN effectiveNodeCount, totalWeight, sumOfPrizes, relationshipsWritten
effectiveNodeCount | totalWeight | sumOfPrizes | relationshipsWritten |
---|---|---|---|
5 |
14.0 |
54.0 |
4 |
This query writes back to the database four new relationships
each of type STEINER
with a single property weight
.
The relationships added back are always directed, even if the input graph is undirected. They point from |