min_cost_flow

Contents

min_cost_flow#

graph_tool.flow.min_cost_flow(g, supply, capacity, cost, residual=None)[git][source]#

Calculate the flow of minimum cost that satisfies the vertex supplies and demands.

Parameters:
gGraph

Graph to be used.

supplyVertexPropertyMap

Vertex property map with the supply of each vertex: positive values are sources of flow, negative values are sinks (demands). The values must sum to zero.

capacityEdgePropertyMap

Edge property map with the edge capacities.

costEdgePropertyMap

Edge property map with the cost per unit of flow along each edge.

residualEdgePropertyMap (optional, default: None)

Edge property map where the residuals should be stored. It must have the same value type as capacity.

Returns:
residualEdgePropertyMap

Edge property map with the residual capacities (capacity - flow).

See also

min_cost_max_flow

Minimum-cost maximum flow

push_relabel_max_flow

Maximum flow with the push-relabel algorithm

Notes

The flow \(f_e \le u_e\) along each edge must satisfy the conservation constraints

\[\sum_{e \in \text{out}(v)} f_e - \sum_{e \in \text{in}(v)} f_e = b_v\]

for every vertex \(v\), where \(b_v\) is its supply and \(u_e\) the capacity of the edge, and among such flows the one with the smallest total cost \(\sum_e c_e f_e\) is returned, which is the minimum-cost flow problem [ahuja-network-1993]. If the supplies cannot be routed within the capacities, a ValueError is raised.

The problem is reduced to min_cost_max_flow() on a copy of the graph in which an additional source vertex is connected to the vertices with positive supply, and those with negative supply to an additional target vertex, with capacities given by the supplies and zero cost. The graph itself is not modified.

A common use is the optimal transport (earth mover’s) distance between two distributions on the vertices of a network, which is the cost of the flow with supplies given by their difference, edge costs given by the edge lengths, and unbounded capacities.

References

[ahuja-network-1993]

Ravindra K. Ahuja, Thomas L. Magnanti and James B. Orlin, “Network Flows: Theory, Algorithms, and Applications”, Prentice Hall, 1993.

Examples

We move three units from one corner of a square lattice to the opposite corner, and one unit to the center, with each edge able to carry two units at unit cost, which gives the total cost \(3\times 8 + 4 = 28\):

>>> g = gt.lattice([5, 5])
>>> g.set_directed(True)
>>> g.add_edge_list(g.get_edges()[:,::-1])
>>> cap = g.new_ep("int", 2)
>>> cost = g.new_ep("int", 1)
>>> supply = g.new_vp("int")
>>> supply[0] = 4
>>> supply[24] = -3
>>> supply[12] = -1
>>> res = gt.min_cost_flow(g, supply, cap, cost)
>>> flow = cap.a - res.a
>>> print((flow * cost.a).sum())
28

The flow can be visualized by drawing only the edges that carry it, with widths proportional to their flows, and the vertices with positive and negative supply in red and blue, respectively:

>>> pos = g.new_vp("vector<double>",
...                vals=[(v % 5, v // 5) for v in range(25)])
>>> f = g.new_ep("int", vals=flow)
>>> u = gt.GraphView(g, efilt=f.a > 0)
>>> vcolor = g.new_vp("string", vals=np.where(supply.a > 0, "#f28c8e",
...                                   np.where(supply.a < 0, "#9bbedc",
...                                            "#cccccc")))
>>> gt.graph_draw(u, pos, vertex_fill_color=vcolor, vertex_text=supply,
...               edge_pen_width=gt.prop_to_size(f, mi=2, ma=5, power=1),
...               edge_color="#e41a1c", output="min_cost_flow-lattice.pdf")
<...>
../_images/min_cost_flow-lattice.png

Flow of minimum cost on a square lattice, with the supplies of the vertices as labels. The vertices with positive supply are shown in red, those with negative supply in blue, and only the edges that carry flow are drawn, with widths proportional to it.#