Note

This documents the development version of NetworkX. Documentation for the current release can be found here.

# networkx.algorithms.flow.capacity_scaling¶

`capacity_scaling`(G, demand='demand', capacity='capacity', weight='weight', heap=<class 'networkx.utils.heaps.BinaryHeap'>)[source]

Find a minimum cost flow satisfying all demands in digraph G.

This is a capacity scaling successive shortest augmenting path algorithm.

G is a digraph with edge costs and capacities and in which nodes have demand, i.e., they want to send or receive some amount of flow. A negative demand means that the node wants to send flow, a positive demand means that the node want to receive flow. A flow on the digraph G satisfies all demand if the net flow into each node is equal to the demand of that node.

Parameters
GNetworkX graph

DiGraph or MultiDiGraph on which a minimum cost flow satisfying all demands is to be found.

demandstring

Nodes of the graph G are expected to have an attribute demand that indicates how much flow a node wants to send (negative demand) or receive (positive demand). Note that the sum of the demands should be 0 otherwise the problem in not feasible. If this attribute is not present, a node is considered to have 0 demand. Default value: ‘demand’.

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’.

weightstring

Edges of the graph G are expected to have an attribute weight that indicates the cost incurred by sending one unit of flow on that edge. If not present, the weight is considered to be 0. Default value: ‘weight’.

heapclass

Type of heap to be used in the algorithm. It should be a subclass of `MinHeap` or implement a compatible interface.

If a stock heap implementation is to be used, `BinaryHeap` is recommended over `PairingHeap` for Python implementations without optimized attribute accesses (e.g., CPython) despite a slower asymptotic running time. For Python implementations with optimized attribute accesses (e.g., PyPy), `PairingHeap` provides better performance. Default value: `BinaryHeap`.

Returns
flowCostinteger

Cost of a minimum cost flow satisfying all demands.

flowDictdictionary

If G is a digraph, a dict-of-dicts keyed by nodes such that flowDict[u][v] is the flow on edge (u, v). If G is a MultiDiGraph, a dict-of-dicts-of-dicts keyed by nodes so that flowDict[u][v][key] is the flow on edge (u, v, key).

Raises
NetworkXError

This exception is raised if the input graph is not directed, not connected.

NetworkXUnfeasible

This exception is raised in the following situations:

• The sum of the demands is not zero. Then, there is no flow satisfying all demands.

• There is no flow satisfying all demand.

NetworkXUnbounded

This exception is raised if the digraph G has a cycle of negative cost and infinite capacity. Then, the cost of a flow satisfying all demands is unbounded below.

Notes

This algorithm does not work if edge weights are floating-point numbers.

Examples

A simple example of a min cost flow problem.

```>>> G = nx.DiGraph()
>>> flowCost, flowDict = nx.capacity_scaling(G)
>>> flowCost
24
>>> flowDict
{'a': {'b': 4, 'c': 1}, 'd': {}, 'b': {'d': 4}, 'c': {'d': 1}}
```

It is possible to change the name of the attributes used for the algorithm.

```>>> G = nx.DiGraph()