learngraphtheory.org

Guest User

Using app without sign in

Cover of the book Graph Theory Through Problems

Practice book

Graph Theory Through Problems

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

Available inKindlePaperback

13 chapters

Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on Amazon
Algorithm Selection

Select an algorithm and generate steps to begin visualization

Min-Cost Flow Calculator

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

Time: O(F·VE)
Space: O(V + E)
Use Case: Supply chains, assignment and transportation problems, least-cost routing
Showing the example graph for this algorithm.

An edge's weight is its capacity; its cost is what each unit of flow pays on it. The canvas shows capacity @ cost.

Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Min-Cost Max Flow

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.

How it works

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.

Applications

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.

Pseudocode

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, cost

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

Worked example, step by step

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.

  1. Path 1, cost 5. The cheapest route is S-A-B-D-T at 1 + 1 + 1 + 2 = 5 per unit. Its smallest capacity is 3, on A-B and on D-T, so 3 units are sent for a cost of 15. Both A-B and D-T are now full.
  2. Path 2, cost 7. The cheapest remaining route is S-A-C-T at 1 + 5 + 1 = 7, limited to 1 unit by what is left on S-A. Flow 4, cost 22.
  3. Path 3, cost 7. S-B-D-C-T also costs 4 + 1 + 1 + 1 = 7 per unit. B-D and D-C both have 2 units left, so 2 units are sent. Flow 6, cost 36.
  4. Path 4, cost 9. The last route is S-B-A-C-T, and it uses the reverse of A-B, whose cost is -1. A new unit enters at B and takes the place on B-D of a unit that used to come from A, and that unit is redirected from A over A-C to T. The net cost is 4 - 1 + 5 + 1 = 9. Flow 7, cost 45.
  5. No path left. Both edges out of S are now full, 4 on S-A and 3 on S-B, so no residual path reaches T. That also proves 7 is the maximum flow, since those two edges form a cut of capacity 7.

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.

Complexity, and where it comes from

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.

When to use Min-Cost Max Flow, and when not to

Min-cost flow generalises several classical optimisation problems. When a special structure applies, a dedicated algorithm is simpler.

AlternativePrefer it whenCost
Maximum flow (Dinic)Edges have capacities but no costs, and only the amount of flow matters.O(V^2 E)
Transportation problemSupplies and demands sit on two sides of a complete bipartite network, as in shipping from plants to warehouses.special case
Hungarian methodEvery supply and demand is 1, so the problem is an assignment of workers to jobs.O(n^3)
Network simplexLarge instances in practice. It is the simplex method specialised to networks and usually the fastest choice.fast in practice
Linear programmingThe model has side constraints that do not fit a pure network, such as budgets shared between edges.polynomial

Common pitfalls

  • Giving reverse edges cost 0. A reverse residual edge must cost minus the cost of the forward edge, because sending flow back cancels the original cost. With cost 0 the algorithm stops recognising refunds and can return a flow that is more expensive than necessary.
  • Using Dijkstra without potentials. Dijkstra fails on negative costs, and reverse edges always introduce them. Either use Bellman-Ford, or keep vertex potentials and run Dijkstra on the reduced costs.
  • Starting with a negative cycle. If the original network already contains a cycle of negative cost with spare capacity, the zero flow is not optimal for value 0 and the successive shortest path method gives wrong answers. Cancel such cycles first.
  • Confusing max flow at min cost with min cost for a given flow. Often the requirement is to ship exactly a demand d, not as much as possible. Stop the augmentation when the flow reaches d, sending only part of the last path if necessary.

Frequently asked questions

What is the minimum cost flow problem?
Given a network in which every edge has a capacity and a cost per unit of flow, it asks for the cheapest way to send a required amount of flow from sources to sinks. A common version asks for the maximum possible flow from s to t and, among all maximum flows, the cheapest.
What is the successive shortest path algorithm?
It starts with zero flow and repeatedly sends as much as possible along the cheapest path from source to sink in the residual graph, until no such path remains. Because reverse edges have negative cost, each path can partly undo earlier decisions when that is cheaper.
Why do reverse edges have negative cost?
Pushing a unit backward along an edge means removing a unit that was sent forward, so its cost is refunded. Without negative reverse costs, the shortest path search would not see that rerouting existing flow can be cheaper than leaving it in place.
What is the time complexity of min cost flow?
With successive shortest paths and Bellman-Ford it is O(F V E), where F is the flow value. With potentials and Dijkstra it is O(F (E + V log V)). Strongly polynomial algorithms exist, and network simplex is the usual choice for large practical instances.
What problems are special cases of min cost flow?
Shortest paths, maximum flow, the assignment problem, the transportation problem and the transshipment problem are all special cases. This is why min cost flow is a central model in operations research and logistics.

Related algorithms: Maximum Flow, Dinic's Algorithm, Bellman-Ford Algorithm, Hopcroft-Karp Matching

Interactive Controls

Zoom Controls

35%
Nodes: 6
Edges: 8