"""Seven-vertex counterexample check for arXiv:2610.19850v3, Definition 20."""
from itertools import combinations


def subsets(xs):
    return (frozenset(s) for k in range(len(xs) + 1) for s in combinations(sorted(xs), k))


def adj(graph, v):
    return {next(iter(e - {v})) for e in graph if v in e}


def comp(graph, v, allowed):
    seen = {v}
    queue = [v]
    for x in queue:
        unseen = (adj(graph, x) & allowed) - seen
        seen.update(unseen)
        queue.extend(sorted(unseen))
    return frozenset(seen)


def components(graph, allowed):
    remain = set(allowed)
    while remain:
        c = comp(graph, min(remain), remain)
        yield c
        remain -= c


def clique(graph, vertices):
    return all(frozenset(e) in graph for e in combinations(vertices, 2))


def chordal(graph, vset):
    # A graph is chordal iff it has a simplicial elimination ordering.
    remain = set(vset)
    while remain:
        v = next((v for v in sorted(remain) if clique(graph, adj(graph, v) & remain)), None)
        if v is None:
            return False
        remain.remove(v)
    return True


def triangulation_pmcs(g, vertices):
    pairs = {frozenset(e) for e in combinations(vertices, 2)}
    completions = [s for s in subsets(pairs - g) if chordal(g | s, vertices)]
    minimal = [s for s in completions if not any(t < s for t in completions)]
    result = set()
    for s in minimal:
        h = g | s
        cliques = [x for x in subsets(vertices) if clique(h, x)]
        result.update(x for x in cliques if not any(x < y for y in cliques))
    return minimal, result


def component_pmcs(g, vertices):
    # Bouchitté–Todinca characterization: no full component; each nonedge is covered.
    for omega in subsets(vertices):
        borders = [set().union(*(adj(g, v) for v in c)) - c for c in components(g, vertices - omega)]
        if any(b == omega for b in borders):
            continue
        if all(frozenset(e) in g or any(set(e) <= b for b in borders) for e in combinations(omega, 2)):
            yield omega


def closest(g, vertices, x, a, b):
    # X-close (a,b): inclusion-minimal (a,b)-separator in X with smallest a-side.
    separating = [(s, comp(g, a, vertices - s)) for s in subsets(x - {a, b})]
    separating = [(s, c) for s, c in separating if b not in c]
    separating = [(s, c) for s, c in separating if not any(t < s for t, d in separating)]
    best = [s for s, c in separating if all(c <= d for t, d in separating)]
    if not separating:
        return None
    assert len(best) == 1
    return best[0]


def neighbors(g, vertices, pmcs, omega):
    out = set()
    for u in omega:
        x = omega | adj(g, u)
        for b in omega - {u}:
            s = closest(g, vertices, x, b, u)
            if s is None:
                continue
            target = x & (s | comp(g, b, vertices - s))
            if target in pmcs and target != omega:
                out.add(target)
    return out


def reach(source, edges):
    seen = {source}
    queue = [source]
    for u in queue:
        new = edges[u] - seen
        seen.update(new)
        queue.extend(new)
    return seen


def key(s):
    return tuple(sorted(s))


def main():
    vertices = set(range(7))
    g = {frozenset((i, i + 1)) for i in range(2, 6)} | {frozenset((a, b)) for a in (0, 1) for b in range(2, 7)}
    assert not any(clique(g, s) and len(list(components(g, vertices - s))) > 1 for s in subsets(vertices))
    minimal, pmcs = triangulation_pmcs(g, vertices)
    assert pmcs == set(component_pmcs(g, vertices))
    edges = {w: neighbors(g, vertices, pmcs, w) for w in pmcs}
    sources = pmcs - set().union(*edges.values())
    assert sources == {frozenset((0, 1, 3, 4)), frozenset((0, 1, 4, 5))}
    all_pairs = len(list(combinations(vertices, 2)))
    print(f'edges={len(g)} fill_subsets={2**(all_pairs-len(g))} minimal_triangulations={len(minimal)} PMCs={len(pmcs)} clique_separator=False')
    for w in sorted(pmcs, key=key):
        print(f'{key(w)} -> {sorted(map(key, edges[w]))}; reach={len(reach(w, edges))}')
    assert all(reach(w, edges) != pmcs for w in pmcs)
    print(f'zero_indegree={sorted(map(key, sources))}')
    print('PASS: independent PMC lists agree; no singleton reaches all PMCs')


if __name__ == '__main__':
    main()
