bundles / scipy latest / scipy / sparse / csgraph / _flow / maximum_flow
cython_function_or_method
scipy.sparse.csgraph._flow:maximum_flow
Signature
def maximum_flow ( csgraph , source , sink ) Summary
Maximize the flow between two vertices in a graph.
Extended Summary
Parameters
csgraph: csr_arrayThe square matrix representing a directed graph whose (i, j)'th entry is an integer representing the capacity of the edge between vertices i and j.
source: intThe source vertex from which the flow flows.
sink: intThe sink vertex to which the flow flows.
method: {'edmonds_karp', 'dinic'}, optionalThe method/algorithm to be used for computing the maximum flow. Following methods are supported,
Default is 'dinic'.
Returns
res: MaximumFlowResultA maximum flow represented by a
MaximumFlowResultwhich includes the value of the flow inflow_value, and the flow graph inflow.
Raises
: TypeError:if the input graph is not in CSR format.
: ValueError:if the capacity values are not integers, or the source or sink are out of bounds.
Notes
This solves the maximum flow problem on a given directed weighted graph: A flow associates to every edge a value, also called a flow, less than the capacity of the edge, so that for every vertex (apart from the source and the sink vertices), the total incoming flow is equal to the total outgoing flow. The value of a flow is the sum of the flow of all edges leaving the source vertex, and the maximum flow problem consists of finding a flow whose value is maximal.
By the max-flow min-cut theorem, the maximal value of the flow is also the total weight of the edges in a minimum cut.
To solve the problem, we provide Edmonds--Karp [1] and Dinic's algorithm [4]. The implementation of both algorithms strive to exploit sparsity. The time complexity of the former and its space complexity is . The latter achieves its performance by building level graphs and finding blocking flows in them. Its time complexity is and its space complexity is .
The maximum flow problem is usually defined with real valued capacities, but we require that all capacities are integral to ensure convergence. When dealing with rational capacities, or capacities belonging to for some fixed , it is possible to reduce the problem to the integral case by scaling all capacities accordingly.
Solving a maximum-flow problem can be used for example for graph cuts optimization in computer vision [3].
Examples
Perhaps the simplest flow problem is that of a graph of only two vertices with an edge from source (0) to sink (1):: (0) --5--> (1) Here, the maximum flow is simply the capacity of the edge:import numpy as np from scipy.sparse import csr_array from scipy.sparse.csgraph import maximum_flow graph = csr_array([[0, 5], [0, 0]])✓
maximum_flow(graph, 0, 1).flow_value maximum_flow(graph, 0, 1, method='edmonds_karp').flow_value✗
graph = csr_array([[0, 5, 0], [0, 0, 3], [0, 0, 0]])
✓maximum_flow(graph, 0, 2).flow_value
✗graph = csr_array([[0, 16, 13, 0, 0, 0], [0, 0, 10, 12, 0, 0], [0, 4, 0, 0, 14, 0], [0, 0, 9, 0, 0, 20], [0, 0, 0, 7, 0, 4], [0, 0, 0, 0, 0, 0]])✓
maximum_flow(graph, 0, 5).flow_value
✗graph = csr_array([[0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 1, 0]]) print(graph.toarray()) i, j = graph.shape n = graph.nnz indptr = np.concatenate([[0], graph.indptr + i, np.arange(n + i + 1, n + i + j + 1), [n + i + j]]) indices = np.concatenate([np.arange(1, i + 1), graph.indices + i + 1, np.repeat(i + j + 1, j)]) data = np.ones(n + i + j, dtype=int) graph_flow = csr_array((data, indices, indptr)) print(graph_flow.toarray())✓
result = maximum_flow(graph_flow, 0, i+j+1, method='dinic') matching = result.flow[1:i+1, i+1:i+j+1] print(matching.toarray())✓
Aliases
-
scipy.sparse.csgraph.maximum_flow