Min Cost Flow Slotting › Example
Machine Learning from ScratchAll chapters Min Cost Flow Slotting › ExampleOne section · Sections in this chapter4 / 8 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let's work through a concrete example that brings together all the concepts we've discussed. We'll see how the mathematical formulation translates into a real optimization problem, and we'll trace through the logic to understand why the optimal solution takes the form it does.
Setting Up the Problem: A Supply Chain Scenario
Imagine you're managing a supply chain with the following structure:
- Factory (Node 0): Produces 10 units of product (a supply node with )
- Warehouse (Node 1): A storage facility that neither produces nor consumes (a transshipment node with )
- Distribution Center (Node 2): Another intermediate facility (a transshipment node with )
- Market (Node 3): Requires exactly 10 units to satisfy demand (a demand node with )
The network has five possible shipping routes, each with different capacities and costs:
- Factory → Warehouse: Can handle up to 15 units, costs 4 per unit
- Factory → Distribution: Can handle up to 8 units, costs 6 per unit
- Warehouse → Market: Can handle up to 10 units, costs 2 per unit
- Distribution → Market: Can handle up to 12 units, costs 3 per unit
- Warehouse → Distribution: Can handle up to 5 units, costs 1 per unit (a shortcut route)
Notice that we have multiple pathways from the factory to the market. Our goal is to find the cheapest way to route all 10 units from the factory to the market while respecting capacity limits.
Here's a visual representation of this network:

This diagram shows the network structure we'll analyze. The Factory (node 0) supplies 10 units, the Market (node 3) demands 10 units, and the Warehouse and Distribution Center are transshipment nodes. Each edge is labeled with its unit cost and capacity limit.
Translating to Mathematical Formulation
Step 1: Define Decision Variables
We need to decide how much flow to send through each of the five edges:
- = flow from Factory to Warehouse
- = flow from Factory to Distribution
- = flow from Warehouse to Market
- = flow from Distribution to Market
- = flow from Warehouse to Distribution
Each of these should be non-negative (we can't send negative flow), and we'll determine their optimal values through optimization.
Step 2: Construct the Objective Function
Our goal is to minimize total cost. The objective function follows the general form:
For our specific network, this expands to:
Substituting the unit costs:
where:
- : Unit cost for Factory → Warehouse
- : Unit cost for Factory → Distribution
- : Unit cost for Warehouse → Market
- : Unit cost for Distribution → Market
- : Unit cost for Warehouse → Distribution
Each term represents the cost contribution from one edge: the unit cost multiplied by the flow amount. Notice that the Warehouse → Distribution route () has the lowest unit cost (1), which might make it attractive if we can use it effectively.
Step 3: Apply Flow Conservation Constraints
Flow conservation ensures that flow is balanced at each node. For each node , we apply the general form:
Let's work through each node:
-
Factory (Node 0): This is a supply node with . Applying the flow conservation constraint:
Simplified:
This says: all flow leaving the factory (the two outgoing edges) must equal the 10 units the factory produces. There are no incoming edges to the factory, so this is simply a supply constraint.
-
Warehouse (Node 1): This is a transshipment node with . Applying the flow conservation constraint:
Simplified: , or equivalently
This says: all flow entering the warehouse (from the factory) must equal all flow leaving (to the market and to the distribution center). The warehouse neither creates nor destroys flow.
-
Distribution (Node 2): This is also a transshipment node with . Applying the flow conservation constraint:
Simplified: , or equivalently
This says: flow entering the distribution center (from the factory and from the warehouse) must equal flow leaving (to the market).
-
Market (Node 3): This is a demand node with . Applying the flow conservation constraint:
Simplified: , or equivalently
This says: all flow entering the market (from the warehouse and from the distribution center) must equal the 10 units of demand.
Step 4: Apply Capacity Constraints
Each edge has a maximum capacity that cannot be exceeded. The general form is:
For our specific network, this gives us:
- (Factory → Warehouse)
- (Factory → Distribution)
- (Warehouse → Market)
- (Distribution → Market)
- (Warehouse → Distribution)
where:
- : Capacity of Factory → Warehouse edge
- : Capacity of Factory → Distribution edge
- : Capacity of Warehouse → Market edge
- : Capacity of Distribution → Market edge
- : Capacity of Warehouse → Distribution edge
These constraints prevent us from overloading any shipping route.
Finding the Optimal Solution
Now we have a complete optimization problem. Let's reason through what the optimal solution should look like, then verify with computation.
Intuitive Reasoning:
The cheapest route from Factory to Market is Factory → Warehouse → Market, with total cost per unit. However, the Warehouse → Market edge has capacity 10, which is exactly our demand. So we could send all 10 units this way... but wait, let's check if there's a better strategy.
There is another option: the Warehouse → Distribution route costs only 1 per unit, and Distribution → Market costs 3 per unit. So Factory → Warehouse → Distribution → Market costs per unit, which is more expensive than the direct Warehouse → Market route.
However, the Factory → Distribution route costs 6 per unit, and Distribution → Market costs 3 per unit, for a total of per unit. This is the most expensive direct path.
The optimal strategy balances these trade-offs. Since Warehouse → Market has capacity 10 (exactly our demand), we could use it fully. But we also need to consider whether splitting flow might be beneficial.
Finding the Optimal Solution:
Now we have a complete optimization problem. Let's think through the solution strategy:
Strategy 1: Use the cheapest path exclusively
The path Factory → Warehouse → Market has total cost per unit calculated as:
If we send all 10 units this way:
- (within capacity )
- (exactly at capacity )
Total cost calculation:
This is feasible and seems attractive. But is it optimal?
Strategy 2: Split between two direct paths
We could send some units Factory → Warehouse → Market and some Factory → Distribution → Market:
- Factory → Warehouse → Market: cost per unit = cost units per flow unit
- Factory → Distribution → Market: cost per unit = cost units per flow unit
Since the first path is cheaper, we'd want to use it to full capacity (10 units), leaving 0 for the second path. This gives us Strategy 1 again.
Strategy 3: Use the shortcut route
What about using the Warehouse → Distribution shortcut? The path Factory → Warehouse → Distribution → Market has cost per unit:
This is more expensive than the direct Warehouse → Market route (cost 6 per unit). So this doesn't help.
The Optimal Solution:
By solving the optimization problem (either algebraically or using a solver), we find that Strategy 1 is optimal:
- (all 10 units: Factory → Warehouse)
- (all 10 units: Warehouse → Market)
- , , (unused routes)
Total Cost Calculation:
The total cost is computed using the objective function:
Why This Makes Sense:
This solution is optimal because:
- The cheapest path (Factory → Warehouse → Market) has sufficient capacity to handle all demand
- Using it exclusively minimizes cost at 6 per unit
- All alternative paths cost more (9 per unit for Factory → Distribution → Market, 8 per unit if we use the shortcut)
The key insight is that when the cheapest path has sufficient capacity, we should use it exclusively. The solver would only split flow across multiple paths if:
- No single path has enough capacity for all demand, OR
- There are additional constraints (like minimum flow requirements) that force splitting, OR
- The cost structure makes splitting beneficial (which doesn't happen in this example)
Verifying the Constraints:
Let's verify this solution satisfies all constraints:
Flow Conservation Constraints:
For each node , we verify that :
-
Factory (Node 0): Flow out - Flow in = Supply
-
Warehouse (Node 1): Flow out - Flow in = Supply
-
Distribution (Node 2): Flow out - Flow in = Supply
-
Market (Node 3): Flow out - Flow in = Supply
For each edge , we verify that :
- :
- :
- : (at capacity)
- :
- :
All constraints are satisfied, and we've achieved the minimum possible cost. This example illustrates how the mathematical formulation guides us to the optimal solution systematically, even when the problem seems simple enough to solve by intuition.