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
Given a source node, a target node and relationships with capacity constraints, the max-flow algorithm assigns a flow to each relationship to achieve maximal transport from source to target.
The flow is a scalar property for each relationship and must satisfy:
-
Flow into a node equals flow out of a node (preservation)
-
Flow is restricted by the capacity of a relationship
The source nodes are given either as a list of nodes, or a list of nodes paired with a scalar. The latter is interpreted as a maximal out-flow from each source node where as for the former input option, out-flow is restricted only by the capacity of the relationships.
For the target nodes, there is an equivalent option, where the scalar determines how much flow each target node accepts. It is unrestricted if not given.
The Neo4j GDS Library implementation is based on a parallel push-relabel algorithm from this paper, with some modifications.
Syntax
This section covers the syntax used to execute the 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.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. |
|
sourceNodes |
List of Nodes or Integers or List of pairs Node or Integer and Scalar |
|
no |
Source nodes given as nodes or node ids. Can be paired with a scalar limiting the out-flow/supply per node like [ [src1, supply1], [src2, supply2],… ]. |
targetNodes |
List of Nodes or Integers or List of pairs Node or Integer and Scalar |
|
no |
Target nodes given as nodes or node ids. Can be paired with a scalar limiting the in-flow/demand per node like [ [tgt1, demand1], [tgt2, demand2],… ]. |
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.stats(
graphName: String,
configuration: Map
)
YIELD
totalFlow: 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. |
|
sourceNodes |
List of Nodes or Integers or List of pairs Node or Integer and Scalar |
|
no |
Source nodes given as nodes or node ids. Can be paired with a scalar limiting the out-flow/supply per node like [ [src1, supply1], [src2, supply2],… ]. |
targetNodes |
List of Nodes or Integers or List of pairs Node or Integer and Scalar |
|
no |
Target nodes given as nodes or node ids. Can be paired with a scalar limiting the in-flow/demand per node like [ [tgt1, demand1], [tgt2, demand2],… ]. |
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. |
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.mutate(
graphName: String,
configuration: Map
)
YIELD
totalFlow: 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. |
|
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. |
|
sourceNodes |
List of Nodes or Integers or List of pairs Node or Integer and Scalar |
|
no |
Source nodes given as nodes or node ids. Can be paired with a scalar limiting the out-flow/supply per node like [ [src1, supply1], [src2, supply2],… ]. |
targetNodes |
List of Nodes or Integers or List of pairs Node or Integer and Scalar |
|
no |
Target nodes given as nodes or node ids. Can be paired with a scalar limiting the in-flow/demand per node like [ [tgt1, demand1], [tgt2, demand2],… ]. |
| Name | Type | Description |
|---|---|---|
totalFlow |
Float |
The net-flow to all target nodes. |
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.write(
graphName: String,
configuration: Map
)
YIELD
totalFlow: 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. |
|
sourceNodes |
List of Nodes or Integers or List of pairs Node or Integer and Scalar |
|
no |
Source nodes given as nodes or node ids. Can be paired with a scalar limiting the out-flow/supply per node like [ [src1, supply1], [src2, supply2],… ]. |
targetNodes |
List of Nodes or Integers or List of pairs Node or Integer and Scalar |
|
no |
Target nodes given as nodes or node ids. Can be paired with a scalar limiting the in-flow/demand per node like [ [tgt1, demand1], [tgt2, demand2],… ]. |
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. |
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 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 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 {capacity: 10}]->(f),
(a)-[:LINK {capacity: 3}]->(b),
(a)-[:LINK {capacity: 7}]->(e),
(b)-[:LINK {capacity: 1}]->(c),
(c)-[:LINK {capacity: 4}]->(d),
(c)-[:LINK {capacity: 6}]->(e),
(f)-[:LINK {capacity: 3}]->(d);
MATCH (source:Place)-[r:LINK]->(target:Place)
RETURN gds.graph.project(
'graph',
source,
target,
{
relationshipProperties: r { .capacity }
}
)
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'}), (d:Place {id: 'D'})
CALL gds.maxFlow.stream.estimate('graph', {
sourceNodes: [a],
targetNodes: [d],
capacityProperty: 'capacity'
})
YIELD nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory
RETURN nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory
| nodeCount | relationshipCount | bytesMin | bytesMax | requiredMemory |
|---|---|---|---|---|
6 |
7 |
2304 |
2304 |
"2304 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'}), (d:Place {id: 'D'})
CALL gds.maxFlow.stream('graph', {
sourceNodes: [a],
targetNodes: [d],
capacityProperty: 'capacity'
})
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" |
1.0 |
"A" |
"F" |
3.0 |
"B" |
"C" |
1.0 |
"C" |
"D" |
1.0 |
"F" |
"D" |
3.0 |
The algorithm leads the flow from source (A) to target (D) through B-C and F respectively. Along the two paths the lowest capacity (bottleneck) is 1 and 3. This gives a total flow of 4 from node A to node D.
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'})
CALL gds.maxFlow.stats('graph', {
sourceNodes: [a],
targetNodes: [d],
capacityProperty: 'capacity'
})
YIELD totalFlow
RETURN totalFlow
| totalFlow |
|---|
4.0 |
The stats mode provides us with information about the total net-flow to the target nodes (D), which is 4.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 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'}), (d:Place {id: 'D'})
CALL gds.maxFlow.mutate('graph', {
sourceNodes: [a],
targetNodes: [d],
capacityProperty: 'capacity',
mutateProperty: 'flow',
mutateRelationshipType: 'FLOW_REL'
})
YIELD totalFlow, relationshipsWritten
RETURN totalFlow, relationshipsWritten
| totalFlow | relationshipsWritten |
|---|---|
4.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'}), (d:Place {id: 'D'})
CALL gds.maxFlow.write('graph', {
sourceNodes: [a],
targetNodes: [d],
capacityProperty: 'capacity',
writeProperty: 'flow',
writeRelationshipType: 'FLOW_REL'
})
YIELD totalFlow, relationshipsWritten
RETURN totalFlow, relationshipsWritten
| totalFlow | relationshipsWritten |
|---|---|
4.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. |
Supply and demand example
If there is a restriction on how much a source/target node can output/receive, this can be modeled using supply and demand. For example, how much produce can be transported from a set of production facilities (with a given supply/production per entity) to a set of destinations (each with a specific demand)?
MATCH (a:Place {id: 'A'}), (b:Place {id: 'B'}), (d:Place {id: 'D'}), (e:Place {id: 'E'})
CALL gds.maxFlow.stream('graph', {
sourceNodes: [[a, 9.0], [b, 5.0]],
targetNodes: [[d, 50.0], [e, 10.0]],
capacityProperty: 'capacity'
})
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" |
"E" |
7.0 |
"A" |
"F" |
2.0 |
"B" |
"C" |
1.0 |
"C" |
"D" |
1.0 |
"F" |
"D" |
2.0 |