Skip to main content

network_simplex_int

Function network_simplex_int 

Source
pub fn network_simplex_int(
    node_demands: &[i64],
    src: &[usize],
    tgt: &[usize],
    cap: &[i64],
    wt: &[i64],
) -> NetworkSimplexIntSolution
Expand description

Solve an exact-integer minimum-cost-flow problem via primal network simplex.

This is the first-class Rust entry point for the exact-integer path. It is a byte-exact-integer solver — every intermediate value is i64, there is no f64 rounding anywhere, and the result is a deterministic function of the inputs in the order supplied.

Parameters:

  • node_demands: one entry per node (node index = position). NetworkX convention: positive = sink (consumes flow), negative = source (supplies flow). For a feasible problem the demands must sum to zero.
  • src, tgt: parallel arrays of edge endpoints, given as node indices into node_demands. src.len() == tgt.len() == cap.len() == wt.len() is the number of edges. Callers are expected to have already dropped self-loops and zero-capacity edges (matching the NetworkX extraction step).
  • cap: per-edge capacity. Use CAP_INF (i64::MAX) for an infinite-capacity edge.
  • wt: per-edge cost (weight) incurred per unit of flow.

Returns a NetworkSimplexIntSolution; on NetworkSimplexStatus::Optimal, flows[i] is the integer flow on edge i in the same order the edges were supplied, and cost is the exact minimum total cost.

§Determinism & exact-integer guarantees

Given identical inputs, this function always returns identical output (status, cost, and per-edge flows) — the tie-breaking in the entering-edge block search and the leaving-edge selection are fixed, order-dependent rules. No floating-point arithmetic participates in the flow or cost values.

§Example

ⓘ
use fnx_algorithms::network_simplex_int::{network_simplex_int, NetworkSimplexStatus, CAP_INF};

// Node 0 supplies 4 (demand -4), node 3 consumes 4 (demand +4).
let demands = [-4, 0, 0, 4];
let src = [0, 0, 1, 2];
let tgt = [1, 2, 3, 3];
let cap = [4, 4, 2, 4];
let wt  = [1, 2, 1, 1];
let sol = network_simplex_int(&demands, &src, &tgt, &cap, &wt);
assert_eq!(sol.status, NetworkSimplexStatus::Optimal);
assert_eq!(sol.cost, 10);
let _ = CAP_INF;