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) andCostFlow<V>(successive shortest paths with potentials) stay separate types sharingArcand the accessorsflow,cap,source,target. The cost solver ismin_cost_flow, notflow(s, t), becauseflow(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_matchingexists besidemax_weight_matching; bounded flows aremax_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 andgomory_hutake domains and edge tables ([E](u: V, v: V, ...)) and build their network, source and sink inside a fresh domain. Reason: the corpus flattensItem + Pair + Src + Snkinto one numbered domain (noi06p4K11,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: noneis 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 shippedFlowGraph.add; a true infinity would need a sum type in the inner loop. Reverse: aCapenum.
- 43.5 Min-cost flow returns to
std.CostFlowreplacesexamples/local-lib'sMinCostFlow, reversing its 0.2.2 relocation (section 19);max_matchingand 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'smcf_graph, KACTL), and a second network type outsidestdsplit theArcvocabulary. Reverse: moveCostFlowback to the local library.
- 43.6
global_min_cutis dense Stoer-Wagner. O(V^3) time and O(V^2) memory over aListmatrix, 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 noInt.MAXsentinel trick (merged vertices are dropped by analivetable). Reverse: switch the phase scan toMinHeapif 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)likegomory_huand returns(total, mate: [V]?V)likegeneral_matching; it maximizes weight, not size, so edges withw <= 0are dropped. Noperfect:option and nomin_twin yet: min-weight perfect matching is the same call onbig - 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: addperfect: Bool = false(labels start atbig), or a sparse priority-queue variant if a port needs V in the thousands.
- 43.8
std.flowhot loops run inunchecked:. 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 theunchecked:lines if a check-elision pass proves these ids in range. Amended:unchecked:drops every index-validity check in the C++, which includes theposcheck of a domain position made byV.nth(i)orV(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 runkeeps both checks. Hungarian's 2.25M-step scan pays aposcall per step otherwise (39 ms of 373 ms). Reverse: drop theRawwrapper invouch_pos.
- 43.9
FlowGraph.min_cut(s)traps unless a completedmax_flow(s, t)came since the lastaddorreset. One?Vfield records it; reading the cut after alimit:stop, an edit, or from another source was silently wrong before. Nomin_cut(s, t)returning(value, side): no corpus port reads an s-t side (cf1404e, dmopc20c7p5, noi06p4 print the value), so it would only renamemax_flow. Reverse: drop the field, or add the pair form when two ports read the side.