Min Cost Flow Slotting › Example

Michael BrenndoerferSeptember 1, 202515 min read
Machine Learning from ScratchAll chapters
Part 9 of 9Optimization
Chapter 63Min Cost Flow Slotting › Example
Reading section 4 of 8 · Example · 15 minSection 4 of 8 · 15 min

Book contents

Section 4 of 8 · 15 min read

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 b0=10b_0 = 10)
  • Warehouse (Node 1): A storage facility that neither produces nor consumes (a transshipment node with b1=0b_1 = 0)
  • Distribution Center (Node 2): Another intermediate facility (a transshipment node with b2=0b_2 = 0)
  • Market (Node 3): Requires exactly 10 units to satisfy demand (a demand node with b3=−10b_3 = -10)

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:

Out[7]:
Visualization
Network diagram showing supply chain with factory, warehouse, distribution, and market nodes connected by edges labeled with capacities and costs.
Supply chain network example showing factory, warehouse, distribution center, and market nodes connected by edges with capacity and cost labels. The factory supplies 10 units, the market demands 10 units, and the warehouse and distribution center act as transshipment nodes. Each edge displays its capacity limit and unit cost, illustrating the multiple pathways available for routing flow from source to sink. This network structure demonstrates how minimum cost flow problems model real-world logistics and supply chain optimization.

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:

  • x01x_{01} = flow from Factory to Warehouse
  • x02x_{02} = flow from Factory to Distribution
  • x13x_{13} = flow from Warehouse to Market
  • x23x_{23} = flow from Distribution to Market
  • x12x_{12} = 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:

min⁡∑(i,j)∈Ecij⋅xij\min \sum_{(i,j) \in E} c_{ij} \cdot x_{ij}

For our specific network, this expands to:

min⁡c01⋅x01+c02⋅x02+c13⋅x13+c23⋅x23+c12⋅x12\min c_{01} \cdot x_{01} + c_{02} \cdot x_{02} + c_{13} \cdot x_{13} + c_{23} \cdot x_{23} + c_{12} \cdot x_{12}

Substituting the unit costs:

min⁡4x01+6x02+2x13+3x23+1x12\min 4x_{01} + 6x_{02} + 2x_{13} + 3x_{23} + 1x_{12}

where:

  • c01=4c_{01} = 4: Unit cost for Factory → Warehouse
  • c02=6c_{02} = 6: Unit cost for Factory → Distribution
  • c13=2c_{13} = 2: Unit cost for Warehouse → Market
  • c23=3c_{23} = 3: Unit cost for Distribution → Market
  • c12=1c_{12} = 1: 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 (x12x_{12}) 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 ii, we apply the general form:

∑j:(i,j)∈Exij−∑j:(j,i)∈Exji=bi\sum_{j: (i,j) \in E} x_{ij} - \sum_{j: (j,i) \in E} x_{ji} = b_i

Let's work through each node:

  • Factory (Node 0): This is a supply node with b0=10b_0 = 10. Applying the flow conservation constraint:

    ∑j:(0,j)∈Ex0j−∑j:(j,0)∈Exj0=x01+x02−0=10=b0\sum_{j: (0,j) \in E} x_{0j} - \sum_{j: (j,0) \in E} x_{j0} = x_{01} + x_{02} - 0 = 10 = b_0

    Simplified: x01+x02=10x_{01} + x_{02} = 10

    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 b1=0b_1 = 0. Applying the flow conservation constraint:

    ∑j:(1,j)∈Ex1j−∑j:(j,1)∈Exj1=(x13+x12)−x01=0=b1\sum_{j: (1,j) \in E} x_{1j} - \sum_{j: (j,1) \in E} x_{j1} = (x_{13} + x_{12}) - x_{01} = 0 = b_1

    Simplified: x13+x12−x01=0x_{13} + x_{12} - x_{01} = 0, or equivalently x01=x13+x12x_{01} = x_{13} + x_{12}

    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 b2=0b_2 = 0. Applying the flow conservation constraint:

    ∑j:(2,j)∈Ex2j−∑j:(j,2)∈Exj2=x23−(x02+x12)=0=b2\sum_{j: (2,j) \in E} x_{2j} - \sum_{j: (j,2) \in E} x_{j2} = x_{23} - (x_{02} + x_{12}) = 0 = b_2

    Simplified: x23−x02−x12=0x_{23} - x_{02} - x_{12} = 0, or equivalently x02+x12=x23x_{02} + x_{12} = x_{23}

    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 b3=−10b_3 = -10. Applying the flow conservation constraint:

    ∑j:(3,j)∈Ex3j−∑j:(j,3)∈Exj3=0−(x13+x23)=−10=b3\sum_{j: (3,j) \in E} x_{3j} - \sum_{j: (j,3) \in E} x_{j3} = 0 - (x_{13} + x_{23}) = -10 = b_3

    Simplified: −x13−x23=−10-x_{13} - x_{23} = -10, or equivalently x13+x23=10x_{13} + x_{23} = 10

    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:

0≤xij≤uij∀(i,j)∈E0 \leq x_{ij} \leq u_{ij} \quad \forall (i,j) \in E

For our specific network, this gives us:

  • 0≤x01≤u01=150 \leq x_{01} \leq u_{01} = 15 (Factory → Warehouse)
  • 0≤x02≤u02=80 \leq x_{02} \leq u_{02} = 8 (Factory → Distribution)
  • 0≤x13≤u13=100 \leq x_{13} \leq u_{13} = 10 (Warehouse → Market)
  • 0≤x23≤u23=120 \leq x_{23} \leq u_{23} = 12 (Distribution → Market)
  • 0≤x12≤u12=50 \leq x_{12} \leq u_{12} = 5 (Warehouse → Distribution)

where:

  • u01=15u_{01} = 15: Capacity of Factory → Warehouse edge
  • u02=8u_{02} = 8: Capacity of Factory → Distribution edge
  • u13=10u_{13} = 10: Capacity of Warehouse → Market edge
  • u23=12u_{23} = 12: Capacity of Distribution → Market edge
  • u12=5u_{12} = 5: 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 4+2=64 + 2 = 6 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 4+1+3=84 + 1 + 3 = 8 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 6+3=96 + 3 = 9 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:

c01+c13=4+2=6 cost units per flow unitc_{01} + c_{13} = 4 + 2 = 6 \text{ cost units per flow unit}

If we send all 10 units this way:

  • x01=10x_{01} = 10 (within capacity u01=15u_{01} = 15)
  • x13=10x_{13} = 10 (exactly at capacity u13=10u_{13} = 10)

Total cost calculation:

Total Cost=c01⋅x01+c13⋅x13=4⋅10+2⋅10=40+20=60 cost units\text{Total Cost} = c_{01} \cdot x_{01} + c_{13} \cdot x_{13} = 4 \cdot 10 + 2 \cdot 10 = 40 + 20 = 60 \text{ cost units}

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 = c01+c13=4+2=6c_{01} + c_{13} = 4 + 2 = 6 cost units per flow unit
  • Factory → Distribution → Market: cost per unit = c02+c23=6+3=9c_{02} + c_{23} = 6 + 3 = 9 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:

c01+c12+c23=4+1+3=8 cost units per flow unitc_{01} + c_{12} + c_{23} = 4 + 1 + 3 = 8 \text{ cost units per flow 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:

  • x01=10x_{01} = 10 (all 10 units: Factory → Warehouse)
  • x13=10x_{13} = 10 (all 10 units: Warehouse → Market)
  • x02=0x_{02} = 0, x23=0x_{23} = 0, x12=0x_{12} = 0 (unused routes)

Total Cost Calculation:

The total cost is computed using the objective function:

Total Cost=∑(i,j)∈Ecij⋅xij=c01⋅x01+c02⋅x02+c13⋅x13+c23⋅x23+c12⋅x12=4⋅10+6⋅0+2⋅10+3⋅0+1⋅0=40+0+20+0+0=60 cost units\begin{aligned} \text{Total Cost} &= \sum_{(i,j) \in E} c_{ij} \cdot x_{ij} \\ &= c_{01} \cdot x_{01} + c_{02} \cdot x_{02} + c_{13} \cdot x_{13} + c_{23} \cdot x_{23} + c_{12} \cdot x_{12} \\ &= 4 \cdot 10 + 6 \cdot 0 + 2 \cdot 10 + 3 \cdot 0 + 1 \cdot 0 \\ &= 40 + 0 + 20 + 0 + 0 \\ &= 60 \text{ cost units} \end{aligned}

Why This Makes Sense:

This solution is optimal because:

  1. The cheapest path (Factory → Warehouse → Market) has sufficient capacity to handle all demand
  2. Using it exclusively minimizes cost at 6 per unit
  3. 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 ii, we verify that ∑j:(i,j)∈Exij−∑j:(j,i)∈Exji=bi\sum_{j: (i,j) \in E} x_{ij} - \sum_{j: (j,i) \in E} x_{ji} = b_i:

  • Factory (Node 0): Flow out - Flow in = Supply

    ∑j:(0,j)∈Ex0j−∑j:(j,0)∈Exj0=x01+x02−0=10+0=10=b0\sum_{j: (0,j) \in E} x_{0j} - \sum_{j: (j,0) \in E} x_{j0} = x_{01} + x_{02} - 0 = 10 + 0 = 10 = b_0
  • Warehouse (Node 1): Flow out - Flow in = Supply

    ∑j:(1,j)∈Ex1j−∑j:(j,1)∈Exj1=x13+x12−x01=10+0−10=0=b1\sum_{j: (1,j) \in E} x_{1j} - \sum_{j: (j,1) \in E} x_{j1} = x_{13} + x_{12} - x_{01} = 10 + 0 - 10 = 0 = b_1
  • Distribution (Node 2): Flow out - Flow in = Supply

    ∑j:(2,j)∈Ex2j−∑j:(j,2)∈Exj2=x23−(x02+x12)=0−(0+0)=0=b2\sum_{j: (2,j) \in E} x_{2j} - \sum_{j: (j,2) \in E} x_{j2} = x_{23} - (x_{02} + x_{12}) = 0 - (0 + 0) = 0 = b_2
  • Market (Node 3): Flow out - Flow in = Supply

    ∑j:(3,j)∈Ex3j−∑j:(j,3)∈Exj3=0−(x13+x23)=0−(10+0)=−10=b3\sum_{j: (3,j) \in E} x_{3j} - \sum_{j: (j,3) \in E} x_{j3} = 0 - (x_{13} + x_{23}) = 0 - (10 + 0) = -10 = b_3

Capacity Constraints:

For each edge (i,j)(i,j), we verify that 0≤xij≤uij0 \leq x_{ij} \leq u_{ij}:

  • x01=10x_{01} = 10: 0≤10≤15=u010 \leq 10 \leq 15 = u_{01}
  • x02=0x_{02} = 0: 0≤0≤8=u020 \leq 0 \leq 8 = u_{02}
  • x13=10x_{13} = 10: 0≤10≤10=u130 \leq 10 \leq 10 = u_{13} (at capacity)
  • x23=0x_{23} = 0: 0≤0≤12=u230 \leq 0 \leq 12 = u_{23}
  • x12=0x_{12} = 0: 0≤0≤5=u120 \leq 0 \leq 5 = u_{12}

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.