Skip to the workspace
Pebellslearn by building
Flow & Matching  ›  Lesson

Min-Cost Flow & Assignment

Min-Cost Flow & Assignment 34 minPush each unit along the cheapest residual path, found with Bellman-Ford because reverse edges are always negative — and watch the assignment problem fall out as the special case where every pipe has capacity one

dsa / Min-Cost Flow & Assignment

Explore each scene at your own pace.

01 · From matching to pricing

From matching to pricing

Min-Cost Flow & Assignment

Definition
Minimum-cost flow sends a required amount through a network whose edges have both a capacity and a cost per unit, at the smallest total cost. It repeatedly pushes along the cheapest augmenting path, found with Bellman-Ford because residual edges carry negative costs.
In plain words
Now every pipe has a price as well as a width, and you must move a set amount as cheaply as you can. You always send the next unit along the cheapest route that still has room, and undoing an earlier choice refunds its price.

In short: Max flow asks how much can move. Min-cost flow asks how cheaply a given amount can move, when every pipe also has a price per unit. The algorithm is the familiar one, with the search for an augmenting path replaced by a search for the cheapest one: push units along the cheapest route with capacity left, update the residual graph, repeat. Because every reverse edge refunds its forward cost (its weight is the negative of the original), the residual graph always has negative edges — so the search uses Bellman-Ford, not Dijkstra, even though every pipe you started with had a non-negative cost. The assignment problem — pairing rows to columns for the least total cost — turns out to be the exact same algorithm on a network where every pipe has capacity one.

A tiny bipartite graph, r1 and r2 on the left, c1 and c2 on the right, every pipe capacity one, with costs 4, 1, 2 and 0 on the four pipes and a cost-matrix table beside it; r1-c2 plus r2-c1 is bolded at a total of 3, cheaper than r1-c1 plus r2-c2 at 4

From matching to pricing

Section 17's assign_drivers found the biggest matching, with no notion of one driver-job pairing being better than another. Give every pipe in that same network a cost — what it's worth to Meridian to have ana driving the truck instead of the van — and "the best assignment" becomes a min-cost flow question with every capacity equal to 1. The four-pipe graph above is exactly that: r1-c1 costing 4 instead of r1-c2 costing 1 is the difference between a good pairing and a bad one, and checking both totals (4 vs. 3) is the entire Hungarian algorithm's idea in miniature. A real Hungarian solver avoids trying every permutation; this lesson's exercise uses permutations directly, since the matrices Meridian deals with here are small enough that it doesn't matter.

Scene 1 of 8
Project context and further resources
You're building a piece ofMeridian — the dispatcher
This piece — cheapest_dispatch(): Moves a given volume for the lowest total cost, routing the costliest units last.
Scenario Dispatch has an order to fill, not just a capacity to max out: 5 loads need to move, and every route costs something different per load. The cheap route alone can't carry all 5 — the question is how to fill the rest for as little as possible.
cheapest_dispatch.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
cheapest_dispatch(edges, source, sink, send) → dictHow much actually shipped, and the lowest total cost of shipping it.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.