"""Rebuild the four pinned witnesses and check their graph6 and exact parameters.""" import argparse from collections import Counter, deque from fractions import Fraction from itertools import combinations import json from pathlib import Path import sys HUBS = ("a1", "b1", "a2", "b2") LEAVES = ("L_a1", "L_a2") CASES = ( ("counterexample_f1_n170.json", 21, 40, True, False, 271, 41087, 2), ("counterexample_f1_22_38_n170.json", 22, 38, True, False, 272, 41089, 4), ("counterexample_f1_22_38_plus_e_n170.json", 22, 38, True, True, 273, 41086, 1), ("counterexample_f2_2conn_n268.json", 35, 62, False, False, 433, 107340, 6), ) def require(condition, description): if not condition: raise ValueError(description) def pair(left, right): return frozenset((left, right)) def construction(t, s, with_leaves, with_extra): names = set(HUBS) links = {pair("a1", "b1"), pair("a2", "b2")} for i in range(1, t + 1): p, q, x, z = [f"{symbol}_{i}" for symbol in "pqxz"] names.update((p, q, x, z)) links.update(pair(u, v) for u, v in ( ("a1", p), (p, q), (q, "a2"), ("a1", x), (p, x), ("a2", z), (q, z), )) for j in range(1, s + 1): u, v = f"u_{j}", f"v_{j}" names.update((u, v)) links.update((pair("b1", u), pair(u, v), pair(v, "b2"))) if with_leaves: names.update(LEAVES) links.update(pair("a" + str(i), leaf) for i, leaf in enumerate(LEAVES, 1)) if with_extra: links.add(pair("u_1", "u_2")) return names, links def graph6(labels, edges): """Encode the adjacency upper triangle under the submitted vertex ordering.""" n = len(labels) values = [n] if n <= 62 else [63, (n >> 12) & 63, (n >> 6) & 63, n & 63] bits = [pair(labels[i], labels[j]) in edges for j in range(n) for i in range(j)] bits.extend([False] * (-len(bits) % 6)) values.extend(sum(int(flag) << (5 - shift) for shift, flag in enumerate(bits[i:i+6])) for i in range(0, len(bits), 6)) return "".join(chr(v + 63) for v in values) def distances(adjacency, root, removed=None): result = {root: 0} queue = deque([root]) while queue: vertex = queue.popleft() for neighbor in adjacency[vertex]: if neighbor != removed and neighbor not in result: result[neighbor] = result[vertex] + 1 queue.append(neighbor) return result def verify(path, t, s, with_leaves, with_extra, m, expected_sum, expected_excess): witness = json.loads(path.read_text(encoding="utf-8")) labels = witness["vertex_labels"] n = len(labels) require(n == 4*t + 2*s + 4 + 2*with_leaves == witness["n"], "order") require(len(set(labels)) == n, "duplicate vertex label") indexed_edges = witness["edges"] require(all(0 <= u < n and 0 <= v < n and u != v for u, v in indexed_edges), "non-simple edge") edges = {pair(labels[u], labels[v]) for u, v in indexed_edges} require(len(edges) == len(indexed_edges) == witness["m"] == m, "size") require(construction(t, s, with_leaves, with_extra) == (set(labels), edges), "edge list disagrees with the stated construction") require(graph6(labels, edges) == witness["graph6"], "graph6 mismatch") adjacency = {v: set() for v in labels} for u, v in edges: adjacency[u].add(v) adjacency[v].add(u) require(all(len(adjacency[u] & adjacency[v]) <= 1 for u, v in combinations(labels, 2)), "contains a four-cycle") dist = {v: distances(adjacency, v) for v in labels} require(all(len(d) == n for d in dist.values()), "not connected") ecc = {v: max(d.values()) for v, d in dist.items()} radius = min(ecc.values()) center = {v for v, e in ecc.items() if e == radius} excluded = set(("b1", "b2", *LEAVES)) if with_leaves else set() require(radius == witness["radius"] == 4 and center == set(labels) - excluded, "radius or center") require(len(center) == witness["center_size"], "center count") require(all(ecc[v] == 5 for v in excluded), "noncenter eccentricities") cuts = {v for v in labels if len(distances(adjacency, labels[1] if v == labels[0] else labels[0], v)) != n-1} require(cuts == ({"a1", "a2"} if with_leaves else set()), "cut vertices") masks = [sum(1 << i for i, name in enumerate(labels) if name in adjacency[v]) for v in labels] goal = (1 << n) - 1 require(set(labels[i] for i in witness["total_dominating_set"]) == set(HUBS), "recorded total dominating set") covered = 0 for v in HUBS: covered |= masks[labels.index(v)] require(covered == goal, "four hubs fail total domination") # A TDS of size two would extend to one of size three, by monotonicity. require(not any(x | y | z == goal for x, y, z in combinations(masks, 3)), "found a TDS of size at most three") require(witness["gamma_t"] == 4, "stored total domination number") center_pairs = {pair(u,v): dist[u][v] for u, v in combinations(sorted(center), 2)} count = len(center_pairs) total = sum(center_pairs.values()) excess = total - 3*count require(count == witness["M"] and total == witness["S"] == expected_sum and excess == witness["S_minus_3M"] == expected_excess > 0, "center metrics mismatch") print(f"{path.name}: n={n} m={m} r={radius} gamma_t=4 |C|={len(center)} " f"M={count} S={total} S-3M={excess} average={Fraction(total, count)}") print(f" C4-free; eccentricities={dict(sorted(Counter(ecc.values()).items()))}; " f"cuts={sorted(cuts)}; center distances={dict(sorted(Counter(center_pairs.values()).items()))}") return center_pairs def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("data", type=Path, help="folder with the four pinned graph JSON files") args = parser.parse_args() print(f"Python {sys.version.split()[0]}") results = [verify(args.data / name, *properties) for name, *properties in CASES] base, enriched = results[1:3] require(base.keys() == enriched.keys(), "the extra edge changes the center") changes = {ends: val - enriched[ends] for ends, val in base.items() if val != enriched[ends]} require(changes == {pair("u_1","u_2"): 1, pair("u_1","v_2"): 1, pair("u_2","v_1"): 1}, "unexpected extra-edge distance changes") print("The additional edge shortens exactly three center pairs, each by one.") if __name__ == "__main__": main()