Design decisions

43. Flows and matching (std.flow, 2026-11-21)

The area's design is spec/lib/flows.md; these are its calls.

  • 43.1 Two networks, one arc vocabulary. FlowGraph<V> (Dinic) and CostFlow<V> (successive shortest paths with potentials) stay separate types sharing Arc and the accessors flow, cap, source, target. The cost solver is min_cost_flow, not flow(s, t), because flow(a) already means an arc's flow. Reason: one name, one meaning; their solvers have different cost contracts. Reverse: merge into one type with an optional cost per arc.
  • 43.2 Objectives are named, never negated. min_weight_matching exists beside max_weight_matching; bounded flows are max_bounded_flow/min_bounded_flow. Reason: research/frictions/cf2062g.md, where a minimum was written as -max(-w). Reverse: keep one of each pair.
  • 43.3 Reductions are functions over the user's domains. max_closure, the matchings, bounded flows and gomory_hu take domains and edge tables ([E](u: V, v: V, ...)) and build their network, source and sink inside a fresh domain. Reason: the corpus flattens Item + Pair + Src + Snk into one numbered domain (noi06p4 K11, dmopc20c7p5, noi09p4); the general fix is sum domains (spec §21.11), a core feature, and until it lands the library keeps the extra vertices out of user code. Reverse: once sum domains land these stay; nothing to undo.
  • 43.4 cap: none is 2^60. An unbounded arc is a capacity no finite cut picks, with room for eight of them in a sum before 63-bit overflow. Reason: matches the shipped FlowGraph.add; a true infinity would need a sum type in the inner loop. Reverse: a Cap enum.
  • 43.5 Min-cost flow returns to std. CostFlow replaces examples/local-lib's MinCostFlow, reversing its 0.2.2 relocation (section 19); max_matching and the bounded flows move up with it. Reason: the area survey (design section 1) puts min-cost flow beside Dinic in every contest library (AtCoder Library's mcf_graph, KACTL), and a second network type outside std split the Arc vocabulary. Reverse: move CostFlow back to the local library.
  • 43.6 global_min_cut is dense Stoer-Wagner. O(V^3) time and O(V^2) memory over a List matrix, not the O(VE + V^2 log V) heap form. Reason: global-cut problems have V <= ~500, where the dense scan beats the heap and has no Int.MAX sentinel trick (merged vertices are dropped by an alive table). Reverse: switch the phase scan to MinHeap if a port needs sparse V in the thousands.
  • 43.7 max_weight_general_matching(edges) is the dense O(V^3) weighted blossom. It takes [E](u, v, w) like gomory_hu and returns (total, mate: [V]?V) like general_matching; it maximizes weight, not size, so edges with w <= 0 are dropped. No perfect: option and no min_ twin yet: min-weight perfect matching is the same call on big - w, and no corpus port asks for it. Reason: contest instances have V <= ~500, where the (2V+1)^2 edge table is a few MB and beats sparse forms. Reverse: add perfect: Bool = false (labels start at big), or a sparse priority-queue variant if a port needs V in the thousands.
  • 43.8 std.flow hot loops run in unchecked:. Dinic's BFS and path search, CostFlow.slope, Hungarian, Hopcroft-Karp, general_matching's search and the blossom slack scans index arc and label tables by ids the library itself created, so the checks never fire; dropping them takes min-cost flow from 1.41x to 1.14x matched C++. Reverse: remove the unchecked: lines if a check-elision pass proves these ids in range. Amended: unchecked: drops every index-validity check in the C++, which includes the pos check of a domain position made by V.nth(i) or V(label), as well as list and array bounds. It keeps the check on a store into a range type (x: 0..=100 = i), since that guards a narrowed width and skipping it would truncate silently rather than crash. cpl run keeps both checks. Hungarian's 2.25M-step scan pays a pos call per step otherwise (39 ms of 373 ms). Reverse: drop the Raw wrapper in vouch_pos.
  • 43.9 FlowGraph.min_cut(s) traps unless a completed max_flow(s, t) came since the last add or reset. One ?V field records it; reading the cut after a limit: stop, an edit, or from another source was silently wrong before. No min_cut(s, t) returning (value, side): no corpus port reads an s-t side (cf1404e, dmopc20c7p5, noi06p4 print the value), so it would only rename max_flow. Reverse: drop the field, or add the pair form when two ports read the side.