max_flow_min_cost(G, s, t, capacity='capacity', weight='weight')
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.
This algorithm is not guaranteed to work if edge weights or demands are floating point numbers (overflows and roundoff errors can cause problems). As a workaround you can use integer numbers by multiplying the relevant edge attributes by a convenient constant factor (eg 100).
DiGraph on which a minimum cost flow satisfying all demands is to be found.
Source of the flow.
Destination of the flow.
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'.
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'.
This exception is raised if the input graph is not directed or not connected.
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.
Dictionary of dictionaries keyed by nodes such that flowDict[u][v] is the flow edge (u, v).
Returns a maximum (s, t)-flow of minimum cost.
>>> 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)
... mincost = nx.cost_of_flow(G, mincostFlow)
... mincost 373
>>> from networkx.algorithms.flow import maximum_flow
... maxFlow = maximum_flow(G, 1, 7)[1]
... nx.cost_of_flow(G, maxFlow) >= mincost True
>>> mincostFlowValue = sum((mincostFlow[u][7] for u in G.predecessors(7))) - sum(See :
... (mincostFlow[7][v] for v in G.successors(7))
... )
... mincostFlowValue == nx.maximum_flow_value(G, 1, 7) True
The following pages refer to to this document either explicitly or contain code examples using this.
networkx.algorithms.flow.mincost.min_cost_flow_cost
networkx.algorithms.flow.mincost.min_cost_flow
networkx.algorithms.flow.mincost.cost_of_flow
networkx.algorithms.flow.mincost.max_flow_min_cost
networkx.algorithms.flow.networksimplex.network_simplex
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them