"""Check conditional avoidance against uniform counts of labelled injections.""" from collections import Counter from itertools import combinations_with_replacement, permutations from math import prod def check(catalogue_size): all_labels = (1 << catalogue_size) - 1 slices = avoidance_checks = disjoint_pairs = 0 for capacity in range(1, catalogue_size + 1): assignments = list(permutations(range(catalogue_size), capacity)) for sizes in combinations_with_replacement(range(1, catalogue_size + 1), capacity): choices = [size - step for step, size in enumerate(sizes)] if min(choices) < 1: continue slices += 1 occupied = Counter( sum(1 << label for label in assignment) for assignment in assignments if all(label < size for label, size in zip(assignment, sizes)) ) total = sum(occupied.values()) assert total == prod(choices) counts = [] for banned in range(all_labels + 1): direct = sum(count for mask, count in occupied.items() if not mask & banned) predicted = prod( max(0, available - (banned & ((1 << size) - 1)).bit_count()) for size, available in zip(sizes, choices) ) assert direct == predicted counts.append(direct) avoidance_checks += 1 for first in range(all_labels + 1): complement = all_labels ^ first second = complement while second: assert counts[first | second] * total <= counts[first] * counts[second] disjoint_pairs += 1 second = (second - 1) & complement return slices, avoidance_checks, disjoint_pairs if __name__ == "__main__": totals = tuple(map(sum, zip(*(check(n) for n in range(1, 7))))) print("Feasible sorted prefix profiles, avoidance counts, disjoint-set comparisons:", *totals)