Min Cost Flow Slotting › Formula
Machine Learning from ScratchAll chapters Min Cost Flow Slotting › FormulaOne section · Sections in this chapter2 / 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
To solve the Minimum Cost flow problem mathematically, we need to express the network and its flows in a precise framework that accounts for cost. This process reveals why each component is needed and how they work together to create a solvable optimization problem.
The Intuition: What Are We Really Trying to Do?
Before diving into mathematical notation, let's clarify what we're trying to achieve. Imagine you're managing a supply chain: you have factories producing goods, warehouses storing them, and markets demanding them. You need to route products through this network in a way that:
- Satisfies all demands - Every market gets exactly what it needs
- Respects all capacities - You can't ship more than your trucks, pipelines, or facilities can handle
- Minimizes total cost - Among all feasible routing strategies, you want the cheapest one
The Minimum Cost flow problem formalizes this intuition. But to solve it mathematically, we need precise mathematical definitions of "network," "flow," and "cost."
Building the Network: Nodes and Edges
The first step is representing our problem as a directed graph, a mathematical structure that captures the relationships in our system. Graphs naturally model connections: which locations can send to which other locations, which resources can flow through which pathways.
We define our network as a directed graph where:
- : The directed graph representing our network
- : The set of nodes (vertices) - these represent locations, resources, time periods, or any entities between which flow can occur
- : The set of directed edges (arcs) - these represent the pathways through which flow can travel, with direction indicating the allowed flow direction
The directed nature matters: a factory can ship to a warehouse, but the reverse might not make physical sense. The graph structure captures these one-way relationships naturally.
Characterizing Nodes by Their Role in the Network
Not all nodes play the same role in our network. Some nodes are sources - they generate flow (like factories producing goods). Others are sinks - they consume flow (like markets demanding products). Still others are intermediate nodes - they neither generate nor consume, but simply pass flow through (like warehouses or distribution centers).
We capture this distinction mathematically through a supply value for each node :
where:
- : Node index, where (ranges over all nodes in the network)
- : Supply value at node (units of flow)
- If : Node is a supply node (source) that generates units of flow
- If : Node is a demand node (sink) that requires units of flow
- If : Node is a transshipment node that neither generates nor consumes net flow
Why do we need this distinction? Because all flow needs to originate somewhere and terminate somewhere. The supply values tell us exactly how much flow needs to enter the network (from sources) and how much needs to exit (to sinks). This is the mathematical way of saying "we need to satisfy all demands using available supplies."
Characterizing Edges: Capacity and Cost
Just as nodes have different roles, edges have different properties. Each edge represents a pathway from node to node , and we need to describe two key properties:
where:
- : Directed edge from node to node , where
- : Source node of the edge (where flow originates)
- : Destination node of the edge (where flow terminates)
-
Capacity : The maximum amount of flow that can pass through edge . This represents physical or operational limits - a truck can only carry so much, a pipeline has a maximum throughput, a worker can only handle so many tasks per hour. Units: same as flow units.
-
Cost : The cost per unit of flow sent through edge . This captures the expense, time, or penalty associated with using this particular pathway. Units: cost per unit of flow.
Why do we need both? Capacity constraints prevent us from making physically infeasible allocations (you can't ship 1000 units through a pipeline that can only handle 100). Cost values allow us to distinguish between different feasible pathways. Even if multiple routes can handle the flow, we want to choose the cheapest one.
Decision Variables: What Are We Choosing?
Now we come to the heart of the optimization problem: what decisions are we making? In the Minimum Cost flow problem, our decision is simple but powerful: for each edge in the network, we need to decide how much flow to send through it.
We represent these decisions as decision variables for each edge :
where:
- : Decision variable representing the amount of flow sent through edge (units of flow)
- : Edge from node to node , where
- : "For all" - this constraint applies to every edge in the network
The non-negativity constraint () is needed because flow can only go in the direction of the edge. If we wanted to allow reverse flow, we would need a separate edge pointing in the opposite direction.
These decision variables are what the optimization algorithm will determine. Once we've set up our constraints and objective function, the solver will find the values of all that minimize cost while satisfying all constraints.
The Objective: Minimizing Total Cost
Our goal is to find the cheapest way to route flow through the network. But what does "cheapest" mean mathematically?
The total cost of a solution is the sum of costs across all edges, where each edge's contribution is its unit cost multiplied by the amount of flow we send through it. This gives us our objective function:
where:
- : Minimization operator - we seek to minimize the total cost
- : Sum over all edges in the edge set
- : Unit cost per flow on edge (cost per unit of flow)
- : Flow amount on edge (units of flow)
- : Total cost contribution from edge (cost units)
This formula assumes that costs are linear in the amount of flow. Sending 10 units through an edge costs exactly 10 times as much as sending 1 unit. This linearity makes the problem computationally tractable, and it's a reasonable assumption for many real-world scenarios (though not all; some problems require non-linear cost functions, which would need different solution approaches).
The minimization operator tells us we're seeking the solution that makes this sum as small as possible, subject to all the constraints we're about to define.
Flow Conservation: The Fundamental Law
We need to ensure that flow is conserved throughout the network. Flow doesn't magically appear or disappear at nodes. This conservation principle expresses physical reality: what goes into a node needs to come out (or be consumed), and what comes out needs to have come in (or been generated).
For each node , we write the flow conservation constraint:
Let's visualize what this means at a single node:



This visualization shows how flow conservation works at a single node. Blue arrows represent flow entering the node, green arrows represent flow leaving the node. The equation Flow Out - Flow In = Supply must hold for each node type.
where:
- : Node index, where (this constraint applies to every node)
- : Sum of all flow leaving node (flow on edges that start at )
- : Index over all nodes such that edge exists
- : Flow amount on edge leaving node
- : Sum of all flow entering node (flow on edges that end at )
- : Index over all nodes such that edge exists
- : Flow amount on edge entering node
- : Supply value at node (positive for sources, negative for sinks, zero for transshipment)
Let's break this down carefully:
- The first sum represents all flow leaving node (flow on edges that start at )
- The second sum represents all flow entering node (flow on edges that end at )
- Their difference equals , the supply at node
Why is this the right constraint? Consider what happens at different types of nodes:
-
Supply nodes (): More flow leaves than enters. The difference equals the supply generated at this node. For example, if a factory generates 10 units, then , meaning 10 more units leave than enter.
-
Demand nodes (): More flow enters than leaves. The difference is negative, matching the negative demand. For example, if a market needs 10 units, then , meaning 10 more units enter than leave.
-
Transshipment nodes (): Flow in equals flow out. These nodes simply pass flow through without generating or consuming any net amount.
This single constraint type elegantly handles all three node types. Without this constraint, we could have solutions where flow appears or disappears at nodes, which would be physically meaningless.
Capacity Constraints: Respecting Physical Limits
While flow conservation ensures flow is balanced, it doesn't prevent us from trying to send more flow through an edge than it can physically handle. We need capacity constraints to enforce the limits we defined for each edge:
where:
- : Flow amount on edge (units of flow)
- : Capacity (maximum flow) allowed on edge (units of flow)
- : "For all edges" - this constraint applies to every edge in the network
- : Non-negativity constraint (flow cannot be negative)
- : Capacity constraint (flow cannot exceed the edge capacity)
This constraint specifies that the flow on edge should be:
- At least zero (we already had this from the non-negativity of decision variables)
- At most (the capacity limit we defined for this edge)
Why is this necessary? Without capacity constraints, the optimal solution might try to route all flow through a single cheap edge, even if that edge can only handle a fraction of the required flow. Capacity constraints force the solver to distribute flow across multiple pathways, respecting the physical realities of the network.
Together with flow conservation, capacity constraints ensure that our solution is mathematically valid and physically feasible.
The Complete Mathematical Formulation
Now we can assemble all these pieces into the complete Minimum Cost flow problem:
Objective Function:
Subject to:
Flow Conservation Constraints:
Variable Definitions:
- : Directed graph with node set and edge set
- : Node indices, where
- : Directed edge from node to node , where
- : Decision variable - flow amount on edge (units of flow)
- : Unit cost per flow on edge (cost per unit of flow)
- : Capacity (maximum flow) on edge (units of flow)
- : Supply value at node (positive for sources, negative for sinks, zero for transshipment)
- : "For all" quantifier
This formulation is a linear programming problem, a special type of optimization problem where both the objective function and all constraints are linear in the decision variables. This linearity allows us to use highly efficient specialized algorithms that can solve even large-scale problems quickly.
Why This Formulation Works: Key Mathematical Properties
The Minimum Cost flow formulation has several beautiful mathematical properties that make it both theoretically elegant and practically useful:
-
Linear Programming Structure: The MCF problem is a special case of linear programming with a particularly nice structure. The network topology creates a constraint matrix with special properties (totally unimodular), which enables the use of highly efficient specialized algorithms that are much faster than general-purpose linear programming solvers.
-
Integrality Property: If all supplies , demands, and capacities are integers, then there exists an optimal solution where all flows are also integers. This matters for practical applications where fractional allocations don't make sense; you can't ship 3.7 trucks or assign 2.3 workers to a shift. The integrality property guarantees that we'll get whole-number solutions automatically, without needing to add integer constraints (which would make the problem much harder to solve).
-
Duality and Economic Interpretation: The MCF problem has a natural dual interpretation involving node potentials (often interpreted as prices). The dual variables tell us the marginal value of increasing supply or capacity at different nodes. If the dual variable at a demand node is high, it means that location is a bottleneck - increasing supply there would significantly reduce total cost.
These properties aren't just mathematical curiosities - they're what make the Minimum Cost flow problem both solvable and interpretable in real-world applications. The linear structure enables fast computation, the integrality property ensures practical solutions, and the duality provides insights for decision-makers.