Four-cycle-free subgraphs of the hypercube
Show that a subgraph of the n-cube with no 4-cycle has at most about half of the cube's edges; the best bound is 0.60318.

| 1 | #!/usr/bin/env python3 |
| 2 | """Check a C4-free edge-list certificate for a subgraph of Q_n. |
| 3 | |
| 4 | A route stores ``result.json`` with two keys: |
| 5 | |
| 6 | {"n": 7, "edges": [[0, 1], [0, 2], ...]} |
| 7 | |
| 8 | Each pair is an unordered edge between integer vertices in ``[0, 2^n)``. |
| 9 | The check accepts exactly when every pair differs in one bit, no pair is |
| 10 | repeated, and no facial 4-cycle is present. It reports the number of |
| 11 | accepted edges, which is a certified lower bound on ``f(n)``; it does not |
| 12 | certify an upper bound or an exact value of ``f(n)``. |
| 13 | """ |
| 14 | |
| 15 | from __future__ import annotations |
| 16 | |
| 17 | import argparse |
| 18 | import json |
| 19 | import sys |
| 20 | from pathlib import Path |
| 21 | |
| 22 | MAX_N = 16 |
| 23 | |
| 24 | |
| 25 | def fail(message: str) -> int: |
| 26 | print(f"FAIL: {message}") |
| 27 | return 1 |
| 28 | |
| 29 | |
| 30 | def parse_args() -> argparse.Namespace: |
| 31 | parser = argparse.ArgumentParser(description=__doc__) |
| 32 | parser.add_argument("route", type=Path, help="route directory containing result.json") |
| 33 | parser.add_argument("--metrics", type=Path, help="where to write the JSON metrics") |
| 34 | return parser.parse_args() |
| 35 | |
| 36 | |
| 37 | def as_int(value: object) -> int | None: |
| 38 | # JSON booleans are ints in Python; they are not valid vertex numbers. |
| 39 | if isinstance(value, bool) or not isinstance(value, int): |
| 40 | return None |
| 41 | return value |
| 42 | |
| 43 | |
| 44 | def main() -> int: |
| 45 | args = parse_args() |
| 46 | result_path = args.route / "result.json" |
| 47 | if not result_path.is_file(): |
| 48 | return fail(f"missing {result_path}") |
| 49 | |
| 50 | try: |
| 51 | result = json.loads(result_path.read_text(encoding="utf-8")) |
| 52 | except (OSError, json.JSONDecodeError) as exc: |
| 53 | return fail(f"cannot read {result_path}: {exc}") |
| 54 | |
| 55 | if not isinstance(result, dict): |
| 56 | return fail("result.json must be a JSON object") |
| 57 | |
| 58 | n = as_int(result.get("n")) |
| 59 | if n is None or n < 1 or n > MAX_N: |
| 60 | return fail(f"n must be an integer in 1..{MAX_N}") |
| 61 | |
| 62 | raw_edges = result.get("edges") |
| 63 | if not isinstance(raw_edges, list): |
| 64 | return fail("edges must be a list of [u, v] pairs") |
| 65 | |
| 66 | vertex_count = 1 << n |
| 67 | edges: set[tuple[int, int]] = set() |
| 68 | for index, pair in enumerate(raw_edges): |
| 69 | if not isinstance(pair, list) or len(pair) != 2: |
| 70 | return fail(f"edge {index} is not a two-element list") |
| 71 | u = as_int(pair[0]) |
| 72 | v = as_int(pair[1]) |
| 73 | if u is None or v is None: |
| 74 | return fail(f"edge {index} has a non-integer endpoint") |
| 75 | if not (0 <= u < vertex_count and 0 <= v < vertex_count): |
| 76 | return fail(f"edge {index} has an endpoint outside [0, {vertex_count})") |
| 77 | if u == v: |
| 78 | return fail(f"edge {index} is a loop") |
| 79 | delta = u ^ v |
| 80 | if delta & (delta - 1) != 0: |
| 81 | return fail(f"edge {index} is not a single-bit edge") |
| 82 | if u > v: |
| 83 | u, v = v, u |
| 84 | if (u, v) in edges: |
| 85 | return fail(f"edge {index} duplicates {u}-{v}") |
| 86 | edges.add((u, v)) |
| 87 | |
| 88 | # Every facial 4-cycle is {u, u xor 2^i, u xor 2^j, u xor (2^i xor 2^j)}. |
| 89 | # Iterating over every u repeats each face four times, which is harmless |
| 90 | # for an existence test and avoids relying on a vertex-canonical form. |
| 91 | for u in range(vertex_count): |
| 92 | for i in range(n): |
| 93 | bit_i = 1 << i |
| 94 | for j in range(i + 1, n): |
| 95 | bit_j = 1 << j |
| 96 | a = u |
| 97 | b = u ^ bit_i |
| 98 | c = u ^ bit_j |
| 99 | d = u ^ bit_i ^ bit_j |
| 100 | if ( |
| 101 | (a, b) in edges or (b, a) in edges |
| 102 | ) and ( |
| 103 | (a, c) in edges or (c, a) in edges |
| 104 | ) and ( |
| 105 | (b, d) in edges or (d, b) in edges |
| 106 | ) and ( |
| 107 | (c, d) in edges or (d, c) in edges |
| 108 | ): |
| 109 | return fail( |
| 110 | f"4-cycle at vertices {a}, {b}, {c}, {d} " |
| 111 | f"along directions {i}, {j}" |
| 112 | ) |
| 113 | |
| 114 | metrics = { |
| 115 | "score": len(edges), |
| 116 | "n": n, |
| 117 | "edges": len(edges), |
| 118 | "density": len(edges) / (n * (1 << (n - 1))), |
| 119 | } |
| 120 | if args.metrics: |
| 121 | args.metrics.write_text(json.dumps(metrics) + "\n", encoding="utf-8") |
| 122 | |
| 123 | print(f"PASS: {len(edges)} C4-free edges in Q_{n}") |
| 124 | return 0 |
| 125 | |
| 126 | |
| 127 | if __name__ == "__main__": |
| 128 | sys.exit(main()) |