Guest User
Using app without sign in

Practice book
Graph Theory Through Problems300 Exercises with Hints, Worked Algorithms and Complete Solutions
By Mohammed Islam Hadjoudj
Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
The maximum flow, sent the cheapest way
The cheapest maximum flow: push flow along the cheapest augmenting path of the residual network, again and again
An edge's weight is its capacity; its cost is what each unit of flow pays on it. The canvas shows capacity @ cost.
Select an algorithm and generate steps to begin visualization
The minimum-cost flow problem asks for the cheapest way to send flow through a network in which every edge has both a capacity and a cost per unit. Here the amount sent is the maximum flow from the source to the sink, so the result is a minimum-cost maximum flow. It covers shortest paths, maximum flow, the assignment problem and the transportation problem in a single model.
The successive shortest path algorithm starts with no flow and repeatedly sends flow along the cheapest augmenting path in the residual network. Sending flow back over an edge refunds its cost, so reverse arcs have negative costs and the cheapest path is found with Bellman-Ford. The path carries as much as its tightest arc allows, the flow and the cost grow, and the residual network is updated. When no augmenting path is left, the flow is maximum, and since each step used the cheapest path, no rerouting could make it cheaper: the residual network has no negative-cost cycle. The running time is O(F·VE) for a flow of value F; Dijkstra with potentials speeds it up.
Minimum-cost flow plans freight and supply chains, assigns workers to tasks, routes traffic and data at the lowest cost, schedules production and transport, and solves the transportation and assignment problems as special cases. It is one of the central models of operations research and network optimization courses, and a frequent tool in competitive programming.
The successive shortest path method sends flow one cheapest augmenting path at a time. Reverse residual edges carry negative cost, so a later path can undo part of an earlier one and get a refund.
MinCostFlow(graph, s, t):
flow = 0, cost = 0
while the residual graph has a path from s to t:
P = the cheapest such path // Bellman-Ford
b = the smallest residual capacity on P
for each edge (u, v) on P:
push b on (u, v)
residual(v, u) += b, cost(v, u) = -cost(u, v)
flow = flow + b
cost = cost + b * cost(P)
return flow, costA flow is cheapest for its value exactly when its residual graph contains no cycle of negative cost. Augmenting along a cheapest path preserves that property, so after every augmentation the current flow is the cheapest way to send that much, and the last one is the cheapest maximum flow. Bellman-Ford is used because the reverse edges have negative costs.
Send as much as possible from S to T at the lowest total cost on the example this page loads. Each edge has a capacity and a cost per unit, written capacity / cost.
Example graph: Directed edges S-A 4/1, S-B 3/4, A-B 3/1, A-C 2/5, B-D 5/1, C-T 4/1, D-T 3/2 and D-C 2/1.
The cheapest maximum flow is 7 units at a total cost of 45. In the final flow A-B carries only 2 of the 3 units that path 1 put on it, which shows why reverse edges are essential: without the refund on A-B, the fourth unit could not have been routed at all. Every intermediate flow was also optimal for its value, so the cheapest way to send 4 units costs 22 and the cheapest way to send 6 costs 36.
Time: O(F V E) with Bellman-Ford · Space: O(V + E)
Each augmentation needs one shortest path computation in the residual graph, O(VE) with Bellman-Ford. With integer capacities each augmentation sends at least one unit, so there are at most F augmentations, where F is the value of the flow, giving O(F V E). That is pseudo-polynomial, because F can be exponential in the size of the input. Johnson-style potentials make every reduced cost nonnegative after the first round, so Dijkstra can replace Bellman-Ford and the bound becomes O(F (E + V log V)). Strongly polynomial algorithms exist, such as minimum mean cycle cancelling and Orlin's algorithm, and the network simplex method is the fastest in practice.
Min-cost flow generalises several classical optimisation problems. When a special structure applies, a dedicated algorithm is simpler.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Maximum flow (Dinic) | Edges have capacities but no costs, and only the amount of flow matters. | O(V^2 E) |
| Transportation problem | Supplies and demands sit on two sides of a complete bipartite network, as in shipping from plants to warehouses. | special case |
| Hungarian method | Every supply and demand is 1, so the problem is an assignment of workers to jobs. | O(n^3) |
| Network simplex | Large instances in practice. It is the simplex method specialised to networks and usually the fastest choice. | fast in practice |
| Linear programming | The model has side constraints that do not fit a pure network, such as budgets shared between edges. | polynomial |
Read the full article: Max-Flow Min-Cut Theorem & Network Flow
Read the full article: Max-Flow & Min-Cut Interview Questions
Related algorithms: Maximum Flow, Dinic's Algorithm, Bellman-Ford Algorithm, Hopcroft-Karp Matching