yog/flow/network_simplex

Network Simplex algorithm for minimum cost flow optimization.

Experimental: This module has not been extensively tested against edge cases, large networks, or adversarial inputs. The API and error behavior may change in future releases. Use with caution in production.

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/4O(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:

Example

import yog
import yog/flow/network_simplex

let graph =
  yog.directed()
  |> yog.add_node(1, 10)   // supply 10
  |> yog.add_node(2, 0)    // transshipment
  |> yog.add_node(3, -10)  // demand 10
  |> yog.add_edges([
    #(1, 2, #(5, 2)),   // capacity 5, cost 2
    #(1, 3, #(10, 5)),  // capacity 10, cost 5
    #(2, 3, #(5, 1)),   // capacity 5, cost 1
  ])

let result = network_simplex.min_cost_flow(
  graph,
  get_demand: fn(d) { d },
  get_capacity: fn(e) { e.0 },
  get_cost: fn(e) { e.1 },
)

// result = Ok(MinCostFlowResult(cost: 40, flow: [#(1, 2, 5), #(1, 3, 5), #(2, 3, 5)]))

Use Cases

Known Limitations

References

Types

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
  Timeout
}

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.

  • Timeout

    The algorithm did not converge within the allowed number of pivots.

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