yog/flow/network_simplex
Network Simplex algorithm for minimum cost flow optimization.
This module solves the minimum cost flow problem: find the cheapest way to send a given amount of flow through a network where edges have both capacities and costs per unit of flow.
Algorithm
| Algorithm | Function | Complexity | Best For |
|---|---|---|---|
| Network Simplex | min_cost_flow/5 | O(V²E) practical | Large sparse networks |
The Network Simplex is a specialized version of the simplex algorithm for network flow problems. It maintains a spanning tree of basic edges and iteratively pivots to improve the solution, similar to how the transportation simplex works for assignment problems.
Key Concepts
- Minimum Cost Flow: Route flow to satisfy demands at minimum total cost
- Node Demands: Supply nodes (negative demand) and demand nodes (positive demand)
- Edge Costs: Price per unit of flow on each edge
- Potentials (π): Dual variables for reduced cost computation
- Reduced Costs: Modified costs ensuring optimality conditions
- Spanning Tree: Basis for the simplex method on networks
Problem Formulation
Given:
- Graph G = (V, E) with capacities u(e) and costs c(e)
- Node demands b(v) where Σb(v) = 0 (conservation)
Find flow f(e) that:
- Minimizes: Σ c(e) × f(e) (total cost)
- Subject to: 0 ≤ f(e) ≤ u(e) (capacity constraints)
- And: flow conservation at each node
Use Cases
- Transportation planning: Minimize shipping costs with vehicle capacities
- Supply chain: Optimize distribution from warehouses to retailers
- Telecommunications: Route calls at minimum cost with link capacities
- Workforce scheduling: Assign workers to shifts minimizing total cost
- Circulation problems: Fleet management and inventory routing
Performance Notes
This implementation uses list-based data structures for the internal spanning tree representation. While algorithmically correct, random access and updates are O(n) rather than O(1). For large flow networks (1000+ nodes/edges), this may impact performance. Consider using alternative approaches for very large-scale problems:
- Erlang FFI to
:digraphor optimized C libraries- Specialized MCF solvers for production-scale optimization
References
- Wikipedia: Minimum-Cost Flow Problem
- Wikipedia: Network Simplex Algorithm
- Network Flows - Ahuja, Magnanti, Orlin
- CP-Algorithms: Min-Cost Flow
Types
pub type EnteringEdge =
#(Int, Int, Int, Int)
A flow vector assigning an amount of flow to each edge.
pub type FlowMap =
List(#(Int, Int, Int))
Returned when Network Simplex succeeds.
pub type MinCostFlowResult {
MinCostFlowResult(cost: Int, flow: List(#(Int, Int, Int)))
}
Constructors
-
MinCostFlowResult(cost: Int, flow: List(#(Int, Int, Int)))Arguments
- cost
-
The optimal minimum cost of the flow routing.
- flow
-
The flow amounts assigned to each edge to achieve the minimum cost.
Errors that can occur during Network Simplex optimization.
pub type NetworkSimplexError {
Infeasible
Unbounded
UnbalancedDemands
}
Constructors
-
InfeasibleThe demands of the network cannot be satisfied given the edge capacities.
-
UnboundedThe network contains a negative-cost cycle with infinite capacity.
-
UnbalancedDemandsThe sum of all node demands does not equal 0.
Values
pub fn min_cost_flow(
graph: model.Graph(n, e),
get_demand: fn(n) -> Int,
get_capacity: fn(e) -> Int,
get_cost: fn(e) -> Int,
) -> Result(MinCostFlowResult, NetworkSimplexError)
Solves the Minimum Cost Flow problem using the Network Simplex algorithm.
Returns either the optimal flow assignment or an error.
Time Complexity: O(V²E) worst case.
Parameters
graph: The flow networkget_demand: Node demand mappingget_capacity: Edge capacity mappingget_cost: Edge unit cost mapping