# boykov_kolmogorov#

boykov_kolmogorov(G, s, t, capacity='capacity', residual=None, value_only=False, cutoff=None)[source]#

Find a maximum single-commodity flow using Boykov-Kolmogorov algorithm.

This function returns the residual network resulting after computing the maximum flow. See below for details about the conventions NetworkX uses for defining residual networks.

This algorithm has worse case complexity $$O(n^2 m |C|)$$ for $$n$$ nodes, $$m$$ edges, and $$|C|$$ the cost of the minimum cut . This implementation uses the marking heuristic defined in  which improves its running time in many practical problems.

Parameters:
GNetworkX graph

Edges of the graph are expected to have an attribute called ‘capacity’. If this attribute is not present, the edge is considered to have infinite capacity.

snode

Source node for the flow.

tnode

Sink node for the flow.

capacitystring

Edges of the graph G are expected to have an attribute capacity that indicates how much flow the edge can support. If this attribute is not present, the edge is considered to have infinite capacity. Default value: ‘capacity’.

residualNetworkX graph

Residual network on which the algorithm is to be executed. If None, a new residual network is created. Default value: None.

value_onlybool

If True compute only the value of the maximum flow. This parameter will be ignored by this algorithm because it is not applicable.

cutoffinteger, float

If specified, the algorithm will terminate when the flow value reaches or exceeds the cutoff. In this case, it may be unable to immediately determine a minimum cut. Default value: None.

Returns:
RNetworkX DiGraph

Residual network after computing the maximum flow.

Raises:
NetworkXError

The algorithm does not support MultiGraph and MultiDiGraph. If the input graph is an instance of one of these two classes, a NetworkXError is raised.

NetworkXUnbounded

If the graph has a path of infinite capacity, the value of a feasible flow on the graph is unbounded above and the function raises a NetworkXUnbounded.

Notes

The residual network R from an input graph G has the same nodes as G. R is a DiGraph that contains a pair of edges (u, v) and (v, u) iff (u, v) is not a self-loop, and at least one of (u, v) and (v, u) exists in G.

For each edge (u, v) in R, R[u][v]['capacity'] is equal to the capacity of (u, v) in G if it exists in G or zero otherwise. If the capacity is infinite, R[u][v]['capacity'] will have a high arbitrary finite value that does not affect the solution of the problem. This value is stored in R.graph['inf']. For each edge (u, v) in R, R[u][v]['flow'] represents the flow function of (u, v) and satisfies R[u][v]['flow'] == -R[v][u]['flow'].

The flow value, defined as the total flow into t, the sink, is stored in R.graph['flow_value']. If cutoff is not specified, reachability to t using only edges (u, v) such that R[u][v]['flow'] < R[u][v]['capacity'] induces a minimum s-t cut.

References



Boykov, Y., & Kolmogorov, V. (2004). An experimental comparison of min-cut/max-flow algorithms for energy minimization in vision. Pattern Analysis and Machine Intelligence, IEEE Transactions on, 26(9), 1124-1137. https://doi.org/10.1109/TPAMI.2004.60



Vladimir Kolmogorov. Graph-based Algorithms for Multi-camera Reconstruction Problem. PhD thesis, Cornell University, CS Department, 2003. pp. 109-114. https://web.archive.org/web/20170809091249/https://pub.ist.ac.at/~vnk/papers/thesis.pdf

Examples

>>> from networkx.algorithms.flow import boykov_kolmogorov


The functions that implement flow algorithms and output a residual network, such as this one, are not imported to the base NetworkX namespace, so you have to explicitly import them from the flow package.

>>> G = nx.DiGraph()
>>> R = boykov_kolmogorov(G, "x", "y")
>>> flow_value = nx.maximum_flow_value(G, "x", "y")
>>> flow_value
3.0
>>> flow_value == R.graph["flow_value"]
True


A nice feature of the Boykov-Kolmogorov algorithm is that a partition of the nodes that defines a minimum cut can be easily computed based on the search trees used during the algorithm. These trees are stored in the graph attribute trees of the residual network.

>>> source_tree, target_tree = R.graph["trees"]
>>> partition = (set(source_tree), set(G) - set(source_tree))


Or equivalently:

>>> partition = (set(G) - set(target_tree), set(target_tree))