# Check of the cost version of the Dinitz-Garg-Goemans conjecture on one explicit instance.
# Supports: https://vibemathlab.com/news/dinitz-garg-goemans-counterexample-2026
# Written for vibemathlab.com, Nov 27, 2026.
# Run: python3 dinitz-garg-goemans-check.py   (Python 3, standard library only: itertools)
# Expected output: the fractional flow that fills every link costs 58; of the 8 one-path-per-customer routings,
# 4 stay within the load bound, and the cheapest of those costs 60. The last line reads "cheapest routing within the bound costs 60 > fractional cost 58 ? True".
# The original header of the program follows unchanged.
#
# My own check of the counterexample to Goemans's cost-preserving rounding conjecture (Dinitz-Garg-Goemans),
# with the instance numbers read from the Archive of Formal Proofs theory file as text. Exact integers.
from itertools import product
arcs = {  # name: (tail, head, capacity = fractional load, cost)
 'S_T1': ('S','T1',10,2), 'S_T2': ('S','T2',6,3), 'S_U': ('S','U',24,0), 'U_V': ('U','V',14,0), 'U_T3': ('U','T3',10,2),
 'V_T1': ('V','T1',5,0), 'V_W': ('V','W',9,0), 'W_T2': ('W','T2',4,0), 'W_T3': ('W','T3',5,0)}
demand = {'T1': 15, 'T2': 10, 'T3': 15}
def paths(u, t):
    if u == t: yield []; return
    for a, (x, y, _, _) in arcs.items():
        if x == u:
            for p in paths(y, t): yield [a] + p
P = {t: list(paths('S', t)) for t in demand}
print('paths:', {t: P[t] for t in P})
cost = lambda p: sum(arcs[a][3] for a in p)
# 1. a fractional flow that saturates every arc (load = capacity), meeting every demand; solve by trying splits
best = None
import itertools
# each terminal has 2 paths here; split x_t on the first path, demand - x_t on the second
for xs in product(*[range(demand[t] + 1) for t in demand]):
    load = {a: 0 for a in arcs}
    for t, x in zip(demand, xs):
        p0, p1 = P[t]
        for a in p0: load[a] += x
        for a in p1: load[a] += demand[t] - x
    if all(load[a] <= arcs[a][2] for a in arcs):
        c = sum(arcs[a][3] * load[a] for a in arcs)
        best = (c, xs, load) if best is None or c < best[0] else best
        if all(load[a] == arcs[a][2] for a in arcs): sat = (c, xs)
print('fractional flow saturating every arc (integer splits):', sat, ' min cost of any feasible integer split:', best[0])
fload = {a: arcs[a][2] for a in arcs}; frac_cost = sum(arcs[a][3] * fload[a] for a in arcs)
dmax = max(demand.values())
print('fractional cost at full load:', frac_cost, ' max demand:', dmax)
# 2. every unsplittable routing (one path per terminal) with load <= fractional load + dmax on every arc
costs = []
for choice in product(*[P[t] for t in demand]):
    load = {a: 0 for a in arcs}
    for t, p in zip(demand, choice):
        for a in p: load[a] += demand[t]
    ok = all(load[a] <= fload[a] + dmax for a in arcs)
    c = sum(arcs[a][3] * load[a] for a in arcs)
    print('  routing', [ '+'.join(p) for p in choice], 'cost', c, 'within bound' if ok else 'violates bound')
    if ok: costs.append(c)
print('cheapest routing within the bound costs', min(costs), '> fractional cost', frac_cost, '?', min(costs) > frac_cost)
