Warning

This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.

max_flow_min_cost

max_flow_min_cost(G, s, t, capacity='capacity', weight='weight')[source]

Return a maximum (s, t)-flow of minimum cost.

G is a digraph with edge costs and capacities. There is a source node s and a sink node t. This function finds a maximum flow from s to t whose total cost is minimized.

Parameters:

G : NetworkX graph

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

s: node label

Source of the flow.

t: node label

Destination of the flow.

capacity: string

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

weight: string

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

Returns:

flowDict: dictionary

Dictionary of dictionaries keyed by nodes such that flowDict[u][v] is the flow edge (u, v).

Raises:

NetworkXError

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

NetworkXUnbounded

This exception is raised if there is an infinite capacity path from s to t in G. In this case there is no maximum flow. This exception is also raised if the digraph G has a cycle of negative cost and infinite capacity. Then, the cost of a flow is unbounded below.

Examples

>>> G = nx.DiGraph()
>>> G.add_edges_from([(1, 2, {'capacity': 12, 'weight': 4}),
...                   (1, 3, {'capacity': 20, 'weight': 6}),
...                   (2, 3, {'capacity': 6, 'weight': -3}),
...                   (2, 6, {'capacity': 14, 'weight': 1}),
...                   (3, 4, {'weight': 9}),
...                   (3, 5, {'capacity': 10, 'weight': 5}),
...                   (4, 2, {'capacity': 19, 'weight': 13}),
...                   (4, 5, {'capacity': 4, 'weight': 0}),
...                   (5, 7, {'capacity': 28, 'weight': 2}),
...                   (6, 5, {'capacity': 11, 'weight': 1}),
...                   (6, 7, {'weight': 8}),
...                   (7, 4, {'capacity': 6, 'weight': 6})])
>>> mincostFlow = nx.max_flow_min_cost(G, 1, 7)
>>> nx.cost_of_flow(G, mincostFlow)
373
>>> from networkx.algorithms.flow import ford_fulkerson
>>> R = ford_fulkerson(G, 1, 7)
>>> maxFlow = R.graph['flow_dict']
>>> nx.cost_of_flow(G, maxFlow)
428
>>> mincostFlowValue = (sum((mincostFlow[u][7] for u in G.predecessors(7)))
...                     - sum((mincostFlow[7][v] for v in G.successors(7))))
>>> mincostFlowValue == nx.maximum_flow_value(G, 1, 7)
True