#!/usr/bin/env python3 """Check two full low-four-cycle censuses by independent incidence isomorphism and DP.""" import argparse from collections import Counter, defaultdict from itertools import combinations from pathlib import Path N = 13 FULL = (1 << N) - 1 TARGET = 64_803_969 def transpose(rows): return tuple(sum(((row >> c) & 1) << r for r, row in enumerate(rows)) for c in range(N)) def incidence_graph(rows): columns = transpose(rows) return tuple(tuple(j + N for j in range(N) if mask & (1 << j)) for mask in rows) + \ tuple(tuple(i for i in range(N) if mask & (1 << i)) for mask in columns) def same_incidence(first, second): """Simultaneous color refinement and complete search over corresponding cells.""" def solve(left, right): while True: keys = {} def refine(graph, colors): return tuple(keys.setdefault( (colors[v], tuple(sorted(colors[w] for w in neighbors))), len(keys)) for v, neighbors in enumerate(graph)) new_left = refine(first, left) new_right = refine(second, right) if Counter(new_left) != Counter(new_right): return False stable = len(set(left)) == len(keys) left, right = new_left, new_right if stable: break cells = defaultdict(list) for vertex, color in enumerate(left): cells[color].append(vertex) if len(cells) == 2 * N: lookup = {color: vertex for vertex, color in enumerate(right)} mapping = [lookup[color] for color in left] return all({mapping[w] for w in neighbors} == set(second[mapping[v]]) for v, neighbors in enumerate(first)) color = min((c for c in cells if len(cells[c]) >= 2), key=lambda c: len(cells[c])) vertex = cells[color][0] new_color = len(cells) left = left[:vertex] + (new_color,) + left[vertex + 1:] return any(solve(left, right[:i] + (new_color,) + right[i + 1:]) for i, candidate in enumerate(right) if candidate == color) colors = (0,) * N + (1,) * N return solve(colors, colors) def permanent(rows): counts = [0] * (1 << N) counts[0] = 1 for used in range(FULL): free = rows[used.bit_count()] & ~used while free: bit = free & -free counts[used | bit] += counts[used] free ^= bit return counts[FULL] def read_census(path): groups = defaultdict(list) counts = Counter() minima = {} for line in path.read_text(encoding='ascii').splitlines(): if not line or line.startswith('#'): continue score, p4, p9, masks = line.split() rows = tuple(int(mask, 16) for mask in masks.split(',')) if (len(rows) != N or any(not 0 <= row <= FULL or row.bit_count() != 4 for row in rows) or any(col.bit_count() != 4 for col in transpose(rows))): raise ValueError(f'Invalid 4-regular incidence matrix in {path}') c4 = sum((m := (a & b).bit_count()) * (m - 1) // 2 for a, b in combinations(rows, 2)) actual = c4, permanent(rows), permanent(tuple(FULL ^ row for row in rows)) if actual != (int(score), int(p4), int(p9)) or not 0 <= c4 <= 7: raise ValueError(f'Incorrect label in {path}: {line}') groups[actual].append((incidence_graph(rows), incidence_graph(transpose(rows)))) counts[c4] += 1 minima[c4] = min(minima.get(c4, actual[2]), actual[2]) for key, group in groups.items(): for (left, _), (right, transposed) in combinations(group, 2): if same_incidence(left, right) or same_incidence(left, transposed): raise ValueError(f'Repeated equivalence class in {path}: {key}') print(f'{path.name}: {sum(counts.values())} distinct classes checked by independent isomorphism') for c4 in range(8): print(f' C4={c4}: classes={counts[c4]} min complementary permanent={minima.get(c4, "-")}') if counts[0] != 1 or minima[0] != TARGET or any(p9 <= TARGET for c4, _, p9 in groups if c4): raise ValueError(f'Low-C4 design or strict bound failure in {path}') return groups def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('first', type=Path) parser.add_argument('second', type=Path) args = parser.parse_args() first, second = read_census(args.first), read_census(args.second) if first.keys() != second.keys(): raise ValueError('Censuses have different C4 and permanent triples') for key, group in first.items(): remaining = second[key].copy() for graph, _ in group: matched = next((i for i, (other, other_t) in enumerate(remaining) if same_incidence(graph, other) or same_incidence(graph, other_t)), None) if matched is None: raise ValueError(f'Unmatched class for {key}') remaining.pop(matched) if remaining: raise ValueError(f'Extra classes for {key}') print('Complete bijection by incidence isomorphism, allowing transpose.') if __name__ == '__main__': main()