"""Finite checks of the weighted double counts in Erdos_Pentagon_Proof.tex. Enumerate triangle-free extensions of one fixed labelled pentagon by three labelled vertices. The first five vertices span the cycle 0, 1, 2, 3, 4, 0. Asymmetric positive integer weights let the checks distinguish choices of root. The checked identities hold for arbitrary weights and require no optimization. Run with Python 3.10 or later. There are no third-party dependencies. """ from itertools import combinations, product from math import prod VERTICES = tuple(range(8)) BITS = tuple(1 << v for v in VERTICES) WEIGHTS = (2, 5, 3, 1, 4, 6, 1, 2) SUBSETS = tuple(tuple(v for v in VERTICES if mask & BITS[v]) for mask in range(1 << len(VERTICES))) MASS = tuple(sum(WEIGHTS[v] for v in group) for group in SUBSETS) WEIGHT = tuple(prod(WEIGHTS[v] for v in group) for group in SUBSETS) FIVE_SETS = tuple(mask for mask, group in enumerate(SUBSETS) if len(group) == 5) PAIRS = tuple(combinations(VERTICES, 2)) def extensions(): """Yield adjacency bitmasks of every allowed graph on these labels.""" cycle = [0] * 8 for i in range(5): j = (i + 1) % 5 cycle[i] |= BITS[j] cycle[j] |= BITS[i] independent = [mask for mask in range(32) if all(not (cycle[v] & mask) for v in SUBSETS[mask])] new_edges = tuple(combinations(range(5, 8), 2)) for attachments in product(independent, repeat=3): base = cycle[:] for i, neighbours in enumerate(attachments, start=5): base[i] = neighbours for j in SUBSETS[neighbours]: base[j] |= BITS[i] # Choosing at most two edges avoids a triangle on the three new vertices. for size in range(3): for choice in combinations(new_edges, size): if any(base[i] & base[j] for i, j in choice): continue adj = base[:] for i, j in choice: adj[i] |= BITS[j] adj[j] |= BITS[i] yield adj def check(adj): # A 2-regular simple graph on five vertices is a pentagon. cycles = [c for c in FIVE_SETS if all((adj[v] & c).bit_count() == 2 for v in SUBSETS[c])] edges = [(i, j, BITS[i] | BITS[j]) for i, j in PAIRS if adj[i] & BITS[j]] opposite_edge_total = [0] * 8 unused_neighbour_total = [0] * 8 single_neighbour_total = 0 for v in VERTICES: for i, j, edge in edges: if edge & adj[v]: continue a = MASS[adj[v] & adj[i]] b = MASS[adj[v] & adj[j]] c = MASS[adj[v] & ~(adj[i] | adj[j])] opposite_edge_total[v] += WEIGHT[edge] * a * b unused_neighbour_total[v] += WEIGHTS[v] * WEIGHT[edge] * c single_neighbour_total += WEIGHTS[v] * WEIGHT[edge] * a * b * c for v in VERTICES: assert opposite_edge_total[v] == sum(WEIGHT[c] // WEIGHTS[v] for c in cycles if BITS[v] & c) assert single_neighbour_total == sum( WEIGHT[c] * sum(WEIGHTS[v] for v in VERTICES if (c & adj[v]).bit_count() == 1) for c in cycles ) completions = {f: sum(WEIGHT[c] // WEIGHT[f] for c in cycles if (f & c) == f) for i, j, f in edges} matchings_at = [0] * 8 by_matching = 0 for (i, j, f), (k, l, g) in combinations(edges, 2): if (f | adj[i] | adj[j]) & g: continue for v in (i, j, k, l): matchings_at[v] += WEIGHT[f | g] by_matching += WEIGHT[f | g] * (completions[f] + completions[g]) assert unused_neighbour_total == matchings_at by_cycle = 0 for c in cycles: cycle_edges = [g for x, y, g in edges if (g & c) == g] for i, j, f in edges: missed = sum(not (adj[v] & c) for v in (i, j)) inducing = sum(not ((f | adj[i] | adj[j]) & g) for g in cycle_edges) assert missed <= inducing by_cycle += WEIGHT[c] * WEIGHT[f] * inducing assert by_cycle == by_matching if __name__ == "__main__": count = 0 for graph in extensions(): check(graph) count += 1 print(f"All checks passed for {count:,} labelled extensions of a fixed pentagon.")