33A Rust network flow simulator and stanchion queue optimizer.
44
55Stanchion posts and retractable belts form directed networks where people
6- flow from entry to service. This library models those networks using
7- classical network flow, queueing theory, and dynamic flow algorithms.
6+ flow from entry to service. This library models those networks using
7+ classical network flow, queueing theory, and dynamic flow algorithms,
8+ and answers two operational questions: when should you connect two posts
9+ (add a belt), and when should you remove one?
810
911## Algorithms
1012
11- ** Max-flow**
12- - Dinic (O(V^2 E); O(E sqrt(V)) for unit graphs)
13- - Push-relabel FIFO with gap heuristic (O(V^2 sqrt(E)))
14- - Capacity scaling (O(m^2 log U); handles large integer capacities)
13+ ** Max-flow** &mdash ; find $f^* = \max |f|$ subject to capacity and conservation
1514
16- ** Min-cost flow**
17- - Successive shortest paths with Johnson potentials
18- - Network simplex (best practical MCF; spanning-tree pivots)
19- - Cycle canceling via Bellman-Ford negative-cycle detection
15+ | Algorithm | Complexity | Notes |
16+ | ---| ---| ---|
17+ | Dinic | $O(V^2 E)$; $O(E\sqrt{V})$ unit graphs | general workhorse |
18+ | Push-relabel (FIFO + gap) | $O(V^2\sqrt{E})$ | better on dense graphs |
19+ | Capacity scaling | $O(m^2 \log U)$ | best when $U \gg V$ |
2020
21- ** Routing**
22- - Dijkstra (O((V+E) log V); non-negative costs)
23- - Bellman-Ford (O(VE); handles negative costs, detects negative cycles)
24- - Floyd-Warshall all-pairs shortest paths (O(V^3))
25- - All simple s-t paths (DFS with cap)
21+ ** Min-cost flow** &mdash ; minimise $\sum_e w(e)\, f(e)$ subject to $|f| = F^* $
22+
23+ | Algorithm | Complexity | Notes |
24+ | ---| ---| ---|
25+ | Successive shortest paths | $O(F(E + V\log V))$ | Dijkstra + Johnson potentials |
26+ | Network simplex | $O(nm\log n)$ empirical | best in practice |
27+ | Cycle canceling | pseudo-polynomial | simplest to verify |
28+
29+ ** Routing** &mdash ; shortest paths on the original arc-cost graph
30+
31+ - Dijkstra: $O((V+E)\log V)$, non-negative costs
32+ - Bellman-Ford: $O(VE)$, handles negative costs, detects negative cycles
33+ - Floyd-Warshall: $O(V^3)$ all-pairs
34+ - All simple $s$-$t$ paths via DFS
2635
2736** Queueing**
28- - Jackson network steady-state analysis (traffic equations, M/M/1)
29- - Event-driven simulation with Poisson arrivals and exponential service
37+
38+ - Jackson network steady-state: solve $(I - R^\top)\lambda = \gamma$, apply product-form theorem
39+ - Event-driven simulation: Poisson arrivals, exponential service, validates against Jackson $L = \rho/(1-\rho)$
3040
3141## Getting started
3242
@@ -50,7 +60,8 @@ cargo kani
5060
5161## Configuration
5262
53- Networks can be described in JSON:
63+ Networks are described in JSON. Each node carries a service rate $\mu_i$;
64+ each edge carries a capacity $c_ {ij}$ and cost $w_ {ij}$.
5465
5566``` json
5667{
@@ -87,17 +98,49 @@ let result = Dinic.max_flow(&net.graph, s, t)?;
8798println! (" max flow: {}" , result . max_flow);
8899```
89100
90- ## Formal verification
101+ ## Theory
102+
103+ The core result is the ** max-flow min-cut theorem** :
104+
105+ $$ \max_f |f| \;=\; \min_{(S,T)} \sum_{\substack{(u,v)\in E \\ u\in S,\, v\in T}} c(u,v) $$
106+
107+ The min-cut identifies saturated belts; adding capacity to any cut arc
108+ directly raises throughput.
109+
110+ A flow is ** min-cost** iff the residual graph $G_f$ contains no negative-cost
111+ cycle. The reduced cost under potentials $\pi$ is
112+
113+ $$ \bar{w}(u,v) = w(u,v) + \pi(u) - \pi(v) \ge 0 $$
91114
92- Core graph and flow invariants are verified with [ Kani] ( https://github.com/model-checking/kani ) :
115+ for all arcs in $G_f$ at optimality (complementary slackness).
116+
117+ For the stochastic layer, Jackson's theorem gives the product-form steady state
118+ of an open queueing network: each node $i$ with utilisation $\rho_i = \lambda_i/\mu_i < 1$
119+ behaves as an independent $M/M/1$ queue with mean length
120+
121+ $$ L_i = \frac{\rho_i}{1 - \rho_i}, \qquad W_i = \frac{1}{\mu_i - \lambda_i} $$
122+
123+ where $\lambda_i$ solves the traffic equations
124+ $\lambda_i = \gamma_i + \sum_j \lambda_j r_ {ji}$.
125+
126+ The max-weight scheduler at each slot picks
127+
128+ $$ i^* = \arg\max_i\; w_i Q_i(t) $$
129+
130+ and is throughput-optimal by the Lyapunov drift argument of
131+ Tassiulas and Ephremides (1992).
132+
133+ Full derivations are in ` doc/theory.md ` and ` doc/background.md ` .
134+
135+ ## Formal verification
93136
94- - XOR back-edge pairing (forward at even index, back at ` i ^ 1 ` )
95- - ` push_flow ` antisymmetry (pushing ` d ` on arc, ` -d ` on back-edge)
96- - Fresh residual capacity equals original capacity
97- - ` is_forward_edge ` correctly identifies even-indexed edges
98- - Source/sink presence checked at build time
137+ Core invariants are verified with [ Kani] ( https://github.com/model-checking/kani ) :
99138
100- Run proofs:
139+ - XOR back-edge pairing: forward arc at index $i$ (even), back-arc at $i \oplus 1$
140+ - ` push_flow ` antisymmetry: pushing $\delta$ on arc $i$ and $-\delta$ on $i \oplus 1$
141+ - Residual capacity of a fresh graph equals $c(e)$
142+ - ` is_forward_edge ` $\iff$ even index
143+ - ` build() ` without source or sink returns an error
101144
102145``` sh
103146nix develop .# kani
@@ -108,24 +151,29 @@ cargo kani
108151
109152```
110153src/
111- graph/ DiGraph, GraphBuilder, ResidualGraph
112- flow/ MaxFlowSolver trait + Dinic, PushRelabel, CapacityScaling
154+ graph/ DiGraph, GraphBuilder, ResidualGraph (XOR back-edge store)
155+ flow/ MaxFlowSolver + Dinic, PushRelabel, CapacityScaling
113156 flow/min_cost MinCostFlowSolver + SSP, NetworkSimplex, CycleCanceling
114- routing/ Dijkstra, Bellman-Ford, Floyd-Warshall
115- queue/ JacksonNetwork, traffic equations
116- sim/ event-driven SimulationEngine
157+ routing/ Dijkstra, Bellman-Ford, Floyd-Warshall, all simple paths
158+ queue/ JacksonNetwork, traffic equations, steady-state analysis
159+ sim/ event-driven SimulationEngine, Poisson/exponential processes
117160 scheduling/ MaxWeightScheduler (Tassiulas-Ephremides)
118- dynamic/ time-expanded graph for dynamic flows
161+ dynamic/ time-expanded graph for flows with transit times
119162 opt/ min-cut identification, stanchion placement decisions
120- config/ JSON config load/build
121- kani_proofs/ Kani formal verification harnesses
163+ config/ JSON network config (serde)
164+ kani_proofs Kani verification harnesses
122165doc/
123- theory.md mathematical background for all algorithms
166+ theory.md condensed mathematical reference
167+ background.md explanatory background, intuition, and architecture guide
168+ examples/
169+ airport_security.json 6-node example network
124170```
125171
126172## References
127173
128174- Ahuja, Magnanti, Orlin. * Network Flows* . Prentice Hall, 1993.
129175- Williamson. * Network Flow Algorithms* . Cambridge, 2019.
130- - MIT 6.854 Advanced Algorithms lecture notes.
131- - Tassiulas and Ephremides (1992). Max-weight scheduling and network stability.
176+ - MIT 6.854 Advanced Algorithms lecture notes (Karger, 2008).
177+ - Tassiulas and Ephremides. "Stability properties of constrained queueing systems
178+ and scheduling policies for maximum throughput in multihop radio networks."
179+ * IEEE Transactions on Automatic Control* 37(12), 1992.
0 commit comments