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

AlgorithmFunctionComplexityBest For
Network Simplexmin_cost_flow/5O(V²E) practicalLarge 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

Problem Formulation

Given:

Find flow f(e) that:

Use Cases

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 :digraph or optimized C libraries
  • Specialized MCF solvers for production-scale optimization

References

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

  • Infeasible

    The demands of the network cannot be satisfied given the edge capacities.

  • Unbounded

    The network contains a negative-cost cycle with infinite capacity.

  • UnbalancedDemands

    The 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 network
  • get_demand: Node demand mapping
  • get_capacity: Edge capacity mapping
  • get_cost: Edge unit cost mapping
Search Document