Minimum cost maximum flow
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 min-cost max flow (MCMF) algorithm solves the problem of finding the maximum flow between a source node and a target node, with the lowest cost.
For solving only maximum flow, without looking at costs, see maximum flow
The flow is a non-negative scalar for every relationship, restricted by capacity value of the relationship. For a node in the graph, the sum of incoming flow matches the sum of outgoing flow, with two exceptions:
-
Net-outflow of source nodes is either unbounded, or bounded by the supply parameter, if given.
-
Net-inflow of target nodes (sinks) is either unbounded, or bounded by the demand parameter, if given.
Whereas the regular maximum flow problem is to simply assign flows to relationships to maximize the total transport from sources to targets, MCMF deals with a second problem: finding the cheapest such assignment without lowering the total flow.
In MCMF, each relationship is also equipped with a cost value. This represents the unit-cost per flow, so that the cost per relationship is its flow times its cost. Summing this for all relationships in the graph gives the cost for the entire flow assignment. By redirecting the flow, the total cost is to be minimized while keeping the total flow maximal.
To run the algorithm the user needs to provide, source and target node(s), optionally with supply and demand values, and also to specify which relationship property that corresponds to capacity and cost respectively.
The Neo4j GDS Library implementation is a cost-scaling push-relabel based on this paper. The algorithm is guaranteed to produce an optimal solution for integer costs and capacities, using a fixed number of iterations. We allow running the algorithm also for double values using the same bounds.
Syntax
This section covers the syntax used to execute the Minimum cost maximum flow algorithm 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.
CALL gds.maxFlow.minCost.stream(
graphName: String,
configuration: Map
)
YIELD
source: Integer,
target: Integer,
flow: 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 |
|
no |
Name of the relationship property to use as capacity. |
|
String |
|
no |
Name of the relationship property to use as cost. |
|
sourceNodes |
List of Nodes or Integers or Integer |
|
no |
Source nodes given as nodes or node ids from which flow comes to the network. |
targetNodes |
List of Nodes or Integers or Integer |
|
no |
Target nodes given as nodes or node ids to which the flow is deposited. |
nodeCapacityProperty |
String |
|
yes |
If defined, nodes with the given property are restricted on the total flow it can process based on their pr |
alpha |
Integer |
|
yes |
Rate of cost-scaling in the refinement phase of the algorithm. Tuning can improve speed. |
1. In a GDS Session, the default is the number of available processors. |
||||
| Name | Type | Description |
|---|---|---|
source |
Integer |
The first node of the returned relationship. |
target |
Integer |
The second node of the returned relationship. |
flow |
Float |
The flow over the returned relationship. |
CALL gds.maxFlow.minCost.stats(
graphName: String,
configuration: Map
)
YIELD
totalFlow: Float,
totalCost: 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 |
|
no |
Name of the relationship property to use as capacity. |
|
String |
|
no |
Name of the relationship property to use as cost. |
|
sourceNodes |
List of Nodes or Integers or Integer |
|
no |
Source nodes given as nodes or node ids from which flow comes to the network. |
targetNodes |
List of Nodes or Integers or Integer |
|
no |
Target nodes given as nodes or node ids to which the flow is deposited. |
nodeCapacityProperty |
String |
|
yes |
If defined, nodes with the given property are restricted on the total flow it can process based on their pr |
alpha |
Integer |
|
yes |
Rate of cost-scaling in the refinement phase of the algorithm. Tuning can improve speed. |
2. In a GDS Session, the default is the number of available processors. |
||||
| Name | Type | Description |
|---|---|---|
totalFlow |
Float |
The net-flow to all target nodes. |
totalCost |
Float |
The cost associated with the returned flow |
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.maxFlow.minCost.mutate(
graphName: String,
configuration: Map
)
YIELD
totalFlow: Float,
totalCost: 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 flow 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. |
|
Boolean |
|
yes |
If disabled the progress percentage will not be logged. |
|
String |
|
yes |
An ID that can be provided to more easily track the algorithm’s progress. |
|
String |
|
no |
Name of the relationship property to use as capacity. |
|
String |
|
no |
Name of the relationship property to use as cost. |
|
sourceNodes |
List of Nodes or Integers or Integer |
|
no |
Source nodes given as nodes or node ids from which flow comes to the network. |
targetNodes |
List of Nodes or Integers or Integer |
|
no |
Target nodes given as nodes or node ids to which the flow is deposited. |
nodeCapacityProperty |
String |
|
yes |
If defined, nodes with the given property are restricted on the total flow it can process based on their pr |
alpha |
Integer |
|
yes |
Rate of cost-scaling in the refinement phase of the algorithm. Tuning can improve speed. |
| Name | Type | Description |
|---|---|---|
totalFlow |
Float |
The net-flow to all target nodes. |
totalCost |
Float |
The cost associated with the returned flow |
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.maxFlow.minCost.write(
graphName: String,
configuration: Map
)
YIELD
totalFlow: Float,
totalCost: 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 flow is written. |
|
String |
|
no |
Name of the relationship property to use as capacity. |
|
String |
|
no |
Name of the relationship property to use as cost. |
|
sourceNodes |
List of Nodes or Integers or Integer |
|
no |
Source nodes given as nodes or node ids from which flow comes to the network. |
targetNodes |
List of Nodes or Integers or Integer |
|
no |
Target nodes given as nodes or node ids to which the flow is deposited. |
nodeCapacityProperty |
String |
|
yes |
If defined, nodes with the given property are restricted on the total flow it can process based on their pr |
alpha |
Integer |
|
yes |
Rate of cost-scaling in the refinement phase of the algorithm. Tuning can improve speed. |
3. In a GDS Session, the default is the number of available processors. |
||||
| Name | Type | Description |
|---|---|---|
totalFlow |
Float |
The net-flow to all target nodes. |
totalCost |
Float |
The cost associated with the returned flow |
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 Minimum cost maximum flow 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 graph graph of a handful nodes connected in a particular pattern. The example graph looks like this:
CREATE (a:Place {id: 'A', constraint: 10.0}),
(b:Place {id: 'B', constraint: 4.0}),
(c:Place {id: 'C'}),
(d:Place {id: 'D'}),
(e:Place {id: 'E', constriant: 20.0}),
(a)-[:LINK {capacity: 7, cost: 100}]->(b),
(b)-[:LINK {capacity: 10, cost: 250}]->(c),
(b)-[:LINK {capacity: 5, cost: 150}]->(d),
(c)-[:LINK {capacity: 15, cost: 200}]->(e),
(d)-[:LINK {capacity: 15, cost: 200}]->(e);
MATCH (source:Place)-[r:LINK]->(target:Place)
RETURN gds.graph.project(
'graph',
source,
target,
{
relationshipProperties: r { .capacity, .cost },
sourceNodeProperties: source { .constraint },
targetNodeProperties: target { .constraint }
}
)
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.
MATCH (a:Place {id: 'A'}), (e:Place {id: 'E'})
CALL gds.maxFlow.minCost.stream.estimate('graph', {
sourceNodes: [a],
targetNodes: [e],
capacityProperty: 'capacity',
costProperty: 'cost'
})
YIELD nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory
RETURN nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory
| nodeCount | relationshipCount | bytesMin | bytesMax | requiredMemory |
|---|---|---|---|---|
5 |
5 |
3620 |
3620 |
"3620 Bytes" |
Stream
In the stream execution mode, the algorithm returns the flow 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.
MATCH (a:Place {id: 'A'}), (e:Place {id: 'E'})
CALL gds.maxFlow.minCost.stream('graph', {
sourceNodes: [a],
targetNodes: [e],
capacityProperty: 'capacity',
costProperty: 'cost'
})
YIELD source, target, flow
RETURN gds.util.asNode(source).id AS src, gds.util.asNode(target).id AS tgt, flow
ORDER BY src, tgt
| src | tgt | flow |
|---|---|---|
"A" |
"B" |
7.0 |
"B" |
"C" |
2.0 |
"B" |
"D" |
5.0 |
"C" |
"E" |
2.0 |
"D" |
"E" |
5.0 |
The algorithm leads the flow from source (A) to target (E) through B-D and B-C respectively. There’s a bottleneck at A-B blocking more flow to E, so it is clear that the maximum flow is 7. From B there are two paths to the target, B→D→E (cost 350) and B→C→E (cost 450). Since the former is cheaper it is fully utilized, and what’s left is sent along the latter more expensive route.
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'}), (e:Place {id: 'E'})
CALL gds.maxFlow.minCost.stats('graph', {
sourceNodes: [a],
targetNodes: [e],
capacityProperty: 'capacity',
costProperty: 'cost'
})
YIELD totalFlow, totalCost
RETURN totalFlow, totalCost
| totalFlow | totalCost |
|---|---|
7.0 |
3350.0 |
The stats mode provides us with information about the total net-flow to the target nodes (E), which is 7.0 and its cost 3350. The total cost can be compared to the per-relationship unit cost and flow value, see results above.
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 flow 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'}), (e:Place {id: 'E'})
CALL gds.maxFlow.minCost.mutate('graph', {
sourceNodes: [a],
targetNodes: [e],
capacityProperty: 'capacity',
costProperty: 'cost',
mutateProperty: 'flow',
mutateRelationshipType: 'FLOW_REL'
})
YIELD totalFlow, totalCost, relationshipsWritten
RETURN totalFlow, totalCost, relationshipsWritten
| totalFlow | totalCost | relationshipsWritten |
|---|---|---|
7.0 |
3350.0 |
5 |
The mutate mode updates the in-memory graph graph with new relationship type
called FLOW_REL with a single property flow.
From the relationshipsWritten column, we can see that exactly five such relationships were added.
They connect the nodes of the flow graph, and their property is the flow over each relationship.
|
The relationships added back to the graph are always directed, even if the input graph is undirected. They point in the order of the flow. |
Write
The write execution mode extends the stats mode with an important side effect: writing the flow 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'}), (e:Place {id: 'E'})
CALL gds.maxFlow.minCost.write('graph', {
sourceNodes: [a],
targetNodes: [e],
capacityProperty: 'capacity',
costProperty: 'cost',
writeProperty: 'flow',
writeRelationshipType: 'FLOW_REL'
})
YIELD totalFlow, totalCost, relationshipsWritten
RETURN totalFlow, totalCost, relationshipsWritten
| totalFlow | totalCost | relationshipsWritten |
|---|---|---|
7.0 |
3350.0 |
5 |
This query writes back to the database five new relationships
each of type FLOW_REL with a single property flow.
|
The relationships added back are always directed, even if the input graph is undirected. They point in the order of the flow. |
Node capacity requirements
If there is a restriction on how much specific nodes can output/receive, this can be modeled using the nodeCapacity parameter property.
For example, source facilities might have a cap on the amount of products they can produce.
Target facilities might also have constraints on the amount of products they can store.
In the example below, we pass the constraint node property as the value of the nodeCapacity parameter to model these additional requirements.
MATCH (a:Place {id: 'A'}), (b:Place {id: 'B'}), (c:Place {id: 'C'}), (e:Place {id: 'E'})
CALL gds.maxFlow.minCost.stream('graph', {
sourceNodes: [a, b],
targetNodes: e,
capacityProperty: 'capacity',
nodeCapacityProperty: 'constraint',
costProperty: 'cost'
})
YIELD source, target, flow
RETURN gds.util.asNode(source).id AS src, gds.util.asNode(target).id AS tgt, flow
ORDER BY src, tgt
| src | tgt | flow |
|---|---|---|
"A" |
"B" |
7.0 |
"B" |
"C" |
6.0 |
"B" |
"D" |
5.0 |
"C" |
"E" |
6.0 |
"D" |
"E" |
5.0 |