"""Independent check of iis:2611.00055 against arXiv:2611.33681 v1, Section 3.2.

Definitions transcribed from the paper (not from the author's script):

  x is weakly (k,+)-reachable from y in G⃗ (order σ) iff  x ≤_σ y  and there is a directed
  path y -> ... -> x of length ≤ k such that EVERY vertex z of that path satisfies x ≤_σ z.
  Strong version: additionally every INTERNAL vertex z satisfies y <_σ z.
  (k,-) is the same with the path traversed x -> ... -> y, x still the σ-least vertex.

  W^{k,+}(y), S^{k,+}(y), W^{k,-}(y), S^{k,-}(y) are the corresponding vertex sets.
  W^k(y) := W^{k,+}(y) ∪ W^{k,-}(y),  S^k(y) := S^{k,+}(y) ∪ S^{k,-}(y).

  wcol⃗_k(G⃗)  = min_σ max_y |W^k(y)|          col⃗_k(G⃗)  = min_σ max_y |S^k(y)|
  wcol⃗⁺_k(G⃗) = min_σ max_y |W^{k,+}(y)|      col⃗⁺_k(G⃗) = min_σ max_y |S^{k,+}(y)|
  wcol⃗_k(G)   = max over orientations G⃗ of wcol⃗_k(G⃗)   (same for col and for the + versions)

Problem 3.6 asks: for every G and k, is there an orientation G⃗ with
      col⃗⁺_k(G⃗) = ⌈col⃗_k(G)/2⌉   and   wcol⃗⁺_k(G⃗) = ⌈wcol⃗_k(G)/2⌉ ?
"""
from itertools import permutations, product
from math import ceil

V = [0, 1, 2, 3]
EDGES = [(0, 1), (1, 2), (2, 3)]
K = 3


def orientations():
    """Each edge independently oriented; 2^3 = 8 orientations."""
    for bits in product([0, 1], repeat=len(EDGES)):
        arcs = set()
        for (u, v), b in zip(EDGES, bits):
            arcs.add((u, v) if b == 0 else (v, u))
        yield frozenset(arcs)


def directed_paths(arcs, k):
    """All simple directed paths (as vertex tuples) of length 1..k, keyed (start, end)."""
    adj = {v: [] for v in V}
    for (u, v) in arcs:
        adj[u].append(v)
    out = []
    def walk(path):
        if len(path) - 1 >= 1:
            out.append(tuple(path))
        if len(path) - 1 == k:
            return
        for w in adj[path[-1]]:
            if w not in path:
                walk(path + [w])
    for s in V:
        walk([s])
    return out


def sets_for(arcs, order, k):
    pos = {v: i for i, v in enumerate(order)}
    paths = directed_paths(arcs, k)
    Wp = {y: set() for y in V}; Wm = {y: set() for y in V}
    Sp = {y: set() for y in V}; Sm = {y: set() for y in V}
    for y in V:
        Wp[y].add(y); Wm[y].add(y); Sp[y].add(y); Sm[y].add(y)
    for path in paths:
        a, b = path[0], path[-1]           # a -> ... -> b
        # (k,+) : counted vertex x = b (endpoint), start y = a
        x, y = b, a
        if pos[x] <= pos[y] and all(pos[x] <= pos[z] for z in path):
            Wp[y].add(x)
            if all(pos[y] < pos[z] for z in path[1:-1]):
                Sp[y].add(x)
        # (k,-) : counted vertex x = a (startpoint), path runs x -> ... -> y
        x, y = a, b
        if pos[x] <= pos[y] and all(pos[x] <= pos[z] for z in path):
            Wm[y].add(x)
            if all(pos[y] < pos[z] for z in path[1:-1]):
                Sm[y].add(x)
    return Wp, Wm, Sp, Sm


def params(arcs, k):
    best_w = best_c = best_wp = best_cp = None
    for order in permutations(V):
        Wp, Wm, Sp, Sm = sets_for(arcs, order, k)
        w = max(len(Wp[y] | Wm[y]) for y in V)
        c = max(len(Sp[y] | Sm[y]) for y in V)
        wp = max(len(Wp[y]) for y in V)
        cp = max(len(Sp[y]) for y in V)
        best_w = w if best_w is None else min(best_w, w)
        best_c = c if best_c is None else min(best_c, c)
        best_wp = wp if best_wp is None else min(best_wp, wp)
        best_cp = cp if best_cp is None else min(best_cp, cp)
    return best_w, best_c, best_wp, best_cp


def main():
    rows = []
    for arcs in orientations():
        w, c, wp, cp = params(arcs, K)
        rows.append((sorted(arcs), w, c, wp, cp))
        print(f"{str(sorted(arcs)):42s} wcol={w} col={c} wcol+={wp} col+={cp}")
    W = max(r[1] for r in rows)     # wcol⃗_K(G) = max over orientations
    C = max(r[2] for r in rows)
    print()
    print(f"graph parameters for P4 at k={K}:  wcol = {W},  col = {C}")
    print(f"required RHS of Problem 3.6: ceil(wcol/2) = {ceil(W/2)}, ceil(col/2) = {ceil(C/2)}")
    ok = [r for r in rows if r[4] == ceil(C / 2) and r[3] == ceil(W / 2)]
    print(f"orientations satisfying BOTH equalities: {len(ok)} of {len(rows)}")
    for r in rows:
        print(f"   orientation {r[0]}: col+ == ceil(col/2)? {r[4] == ceil(C/2)}; "
              f"wcol+ == ceil(wcol/2)? {r[3] == ceil(W/2)}")


if __name__ == "__main__":
    main()
