"""Recompute Problem 3.6 parameters from paths; Python 3, no dependencies."""
from itertools import permutations, product


def test_path(n, k):
    values = []
    for orientation in product((0, 1), repeat=n-1):
        arcs = {(i+b, i+1-b) for i,b in enumerate(orientation)}
        opt = [n]*4  # plus strong, plus weak, union strong, union weak
        for order in permutations(range(n)):
            index = {v:i for i,v in enumerate(order)}
            peak = [0]*4
            for y in range(n):
                counts = [0]*4
                for x in range(n):
                    if abs(y-x)>k: continue
                    step = 1 if x>=y else -1
                    p = list(range(y,x+step,step))
                    if any(index[v]<index[x] for v in p): continue
                    strong = all(index[v]>index[y] for v in p[1:-1])
                    edges = list(zip(p,p[1:]))
                    plus = all(e in arcs for e in edges)
                    minus = all((v,u) in arcs for u,v in edges)
                    for i,yes in enumerate((plus and strong, plus, (plus or minus) and strong, plus or minus)):
                        counts[i] += int(yes)
                peak = list(map(max,zip(peak,counts)))
            opt = list(map(min,zip(opt,peak)))
        values.append(tuple(opt))
    return values

if __name__=='__main__':
    n,k=4,3
    vals=test_path(n,k)
    C,W=map(max,zip(*(v[2:] for v in vals)))
    target=((C+1)//2,(W+1)//2)
    if W!=3 or any(v[1]!=1 or v[:2]==target for v in vals):
        raise RuntimeError((vals,target))
    print('P4, radius',k,'two-sided graph parameters',(C,W),'target',target,
          'attainable plus pairs',sorted({v[:2] for v in vals}))
