min_cost_max_flow#
- graph_tool.flow.min_cost_max_flow(g, source, target, capacity, cost, residual=None)[git][source]#
Calculate the maximum flow of minimum cost between two vertices.
- Parameters:
- g
Graph Graph to be used.
- source
Vertex The source vertex.
- target
Vertex The target (or “sink”) vertex.
- capacity
EdgePropertyMap Edge property map with the edge capacities.
- cost
EdgePropertyMap Edge property map with the cost per unit of flow along each edge.
- residual
EdgePropertyMap(optional, default:None) Edge property map where the residuals should be stored. It must have the same value type as
capacity.
- g
- Returns:
- residual
EdgePropertyMap Edge property map with the residual capacities (capacity - flow).
- residual
See also
min_cost_flowMinimum-cost flow with vertex supplies
edmonds_karp_max_flowMaximum flow with the Edmonds-Karp algorithm
push_relabel_max_flowMaximum flow with the push-relabel algorithm
Notes
Among all flows from
sourcetotargetwith the maximum value, the one that minimizes the total cost \(\sum_e c_e f_e\) is returned, where \(f_e\) is the flow along edge \(e\) and \(c_e\) its cost [ahuja-network-1993].If all costs are non-negative, the successive shortest path algorithm is used, which sends flow along shortest paths of the residual graph with respect to costs reduced by vertex potentials. Its complexity is \(O(U(E + V\log V))\), where \(U\) is the value of the maximum flow (with integer capacities). If some costs are negative, a maximum flow is computed first with
push_relabel_max_flow(), and the cycles of negative cost in the residual graph are then canceled [boost-cycle-canceling], which takes time \(O(CVE)\), where \(C\) is the reduction of the cost obtained.Integer costs are handled exactly; floating-point costs are converted to
double. The graph must be directed.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 use the edge lengths in the example network as costs:
>>> g = gt.load_graph("../flow-example.xml.gz") >>> cap = g.ep.cap >>> pos = g.vp.pos >>> cost = g.new_ep("double") >>> x = pos.get_2d_array().T >>> u = gt.edge_endpoint_property(g, g.vertex_index, "source").a >>> v = gt.edge_endpoint_property(g, g.vertex_index, "target").a >>> cost.a = np.linalg.norm(x[u] - x[v], axis=1) >>> src, tgt = g.vertex(0), g.vertex(1) >>> res = gt.min_cost_max_flow(g, src, tgt, cap, cost) >>> res.a = cap.a - res.a # the actual flow >>> print(sum([res[e] for e in tgt.in_edges()])) 44.8905957841... >>> print((res.a * cost.a).sum()) 69.0970420300... >>> gt.graph_draw(g, pos=pos, edge_pen_width=gt.prop_to_size(res, mi=0, ma=5, power=1), output="example-min-cost-max-flow.pdf") <...>
Edge flows of the maximum flow of minimum cost, with costs given by the edge lengths. The source and target are on the upper left and lower right corners, respectively. The edge flows are represented by the edge width.#