#!/usr/bin/env python3 """Check a C4-free edge-list certificate for a subgraph of Q_n. A route stores ``result.json`` with two keys: {"n": 7, "edges": [[0, 1], [0, 2], ...]} Each pair is an unordered edge between integer vertices in ``[0, 2^n)``. The check accepts exactly when every pair differs in one bit, no pair is repeated, and no facial 4-cycle is present. It reports the number of accepted edges, which is a certified lower bound on ``f(n)``; it does not certify an upper bound or an exact value of ``f(n)``. """ from __future__ import annotations import argparse import json import sys from pathlib import Path MAX_N = 16 def fail(message: str) -> int: print(f"FAIL: {message}") return 1 def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("route", type=Path, help="route directory containing result.json") parser.add_argument("--metrics", type=Path, help="where to write the JSON metrics") return parser.parse_args() def as_int(value: object) -> int | None: # JSON booleans are ints in Python; they are not valid vertex numbers. if isinstance(value, bool) or not isinstance(value, int): return None return value def main() -> int: args = parse_args() result_path = args.route / "result.json" if not result_path.is_file(): return fail(f"missing {result_path}") try: result = json.loads(result_path.read_text(encoding="utf-8")) except (OSError, json.JSONDecodeError) as exc: return fail(f"cannot read {result_path}: {exc}") if not isinstance(result, dict): return fail("result.json must be a JSON object") n = as_int(result.get("n")) if n is None or n < 1 or n > MAX_N: return fail(f"n must be an integer in 1..{MAX_N}") raw_edges = result.get("edges") if not isinstance(raw_edges, list): return fail("edges must be a list of [u, v] pairs") vertex_count = 1 << n edges: set[tuple[int, int]] = set() for index, pair in enumerate(raw_edges): if not isinstance(pair, list) or len(pair) != 2: return fail(f"edge {index} is not a two-element list") u = as_int(pair[0]) v = as_int(pair[1]) if u is None or v is None: return fail(f"edge {index} has a non-integer endpoint") if not (0 <= u < vertex_count and 0 <= v < vertex_count): return fail(f"edge {index} has an endpoint outside [0, {vertex_count})") if u == v: return fail(f"edge {index} is a loop") delta = u ^ v if delta & (delta - 1) != 0: return fail(f"edge {index} is not a single-bit edge") if u > v: u, v = v, u if (u, v) in edges: return fail(f"edge {index} duplicates {u}-{v}") edges.add((u, v)) # Every facial 4-cycle is {u, u xor 2^i, u xor 2^j, u xor (2^i xor 2^j)}. # Iterating over every u repeats each face four times, which is harmless # for an existence test and avoids relying on a vertex-canonical form. for u in range(vertex_count): for i in range(n): bit_i = 1 << i for j in range(i + 1, n): bit_j = 1 << j a = u b = u ^ bit_i c = u ^ bit_j d = u ^ bit_i ^ bit_j if ( (a, b) in edges or (b, a) in edges ) and ( (a, c) in edges or (c, a) in edges ) and ( (b, d) in edges or (d, b) in edges ) and ( (c, d) in edges or (d, c) in edges ): return fail( f"4-cycle at vertices {a}, {b}, {c}, {d} " f"along directions {i}, {j}" ) metrics = { "score": len(edges), "n": n, "edges": len(edges), "density": len(edges) / (n * (1 << (n - 1))), } if args.metrics: args.metrics.write_text(json.dumps(metrics) + "\n", encoding="utf-8") print(f"PASS: {len(edges)} C4-free edges in Q_{n}") return 0 if __name__ == "__main__": sys.exit(main())