netgen-rs
A Rust port of NETGEN, the classic network flow problem generator described in:
Klingman, Napier, and Stutz, "NETGEN: A program for generating large scale capacitated assignment, transportation, and minimum-cost flow network problems," Management Science 20, 814–820 (1974).
Generates minimum-cost flow, assignment, and maximum flow problems in DIMACS format. Produces byte-identical output to the reference C implementation for the same inputs.
Install
CLI usage
Parameters can be passed as command-line arguments or via stdin. Each problem requires 15 whitespace-separated integers:
seed problem_number nodes sources sinks density mincost maxcost supply tsources tsinks hicost% capacitated% mincap maxcap
# Pass parameters directly as arguments
# Or pipe via stdin (supports multiple problems in sequence)
|
# Assignment problem (sources+sinks=nodes, sources=sinks, supply=sources)
# Max flow problem (mincost=maxcost=1)
# Multiple problems from a file
When no arguments are given, netgen_rs reads from stdin. Processing stops at EOF or when seed/problem ≤ 0.
Parameters
| Parameter | Description |
|---|---|
seed |
Positive random seed (deterministic output for same seed) |
problem_number |
Problem identifier (appears in output header) |
nodes |
Total number of nodes |
sources |
Number of source nodes (including transshipment) |
sinks |
Number of sink nodes (including transshipment) |
density |
Number of arcs to generate |
mincost |
Minimum arc cost |
maxcost |
Maximum arc cost |
supply |
Total supply across all sources |
tsources |
Number of transshipment sources |
tsinks |
Number of transshipment sinks |
hicost% |
Percentage of skeleton arcs assigned maximum cost (0–100) |
capacitated% |
Percentage of arcs to be capacitated (0–100) |
mincap |
Minimum arc capacity |
maxcap |
Maximum arc capacity |
Problem type detection
The problem type is inferred from the parameters (matching the original NETGEN behavior):
- Assignment —
sources + sinks = nodes,sources = sinks,supply = sources, and no transshipment nodes - Maximum flow —
mincost = maxcost = 1 - Minimum-cost flow — everything else
Library usage
Add the library to your Cargo.toml:
Generate a network
use ;
let params = NetgenParams ;
let result = generate.unwrap;
for arc in &result.arcs
for in result.supply.iter.enumerate
Write DIMACS output
use ;
let params = from_slice
.expect;
let result = generate.unwrap;
// Write to stdout
let stdout = stdout;
write_dimacs.unwrap;
// Or get as a string
let dimacs = to_dimacs_string.unwrap;
Provenance
The reference C code (in netgen_original/) is the BCJL-patched version of Norbert Schlenker's C implementation, with overflow fixes by Joseph Cheriyan that prevent infinite loops for networks with more than 2^15 nodes. This Rust port preserves the same overflow fixes using f64 casts and removes the static MAXNODES/MAXARCS limits in favor of dynamic allocation.