/* killcensus.c - exact kill-graph component census on a cyclic tile gap word. * * The kill graph (served producer research/a3-08-adjacent-pairs.js): nodes * (i,sigma), sigma in {0,1} (sigma=0 is residue 0, sigma=1 is residue p-2); * edge (i,sigma)->(i+1,sigma') iff sigma'-sigma = g_i (mod p). In/out degree * <= 1, so components are paths (no cycles here because an inert gap exists). * The streaming machine below is the producer's makeKillMachine transcribed to * C; l0,l1 are the lengths of the two pending chains, indexed by their end * sigma. The walk starts just after an inert gap (class X) so no component * crosses the seam. * * Usage: killcensus [p2 ...] [> summary.json] * Writes .p

.comp.txt : one line per component of size L >= 3, * listing its L-1 internal gap values in order. */ #include #include #include #include static long runs[1 << 20]; static uint16_t ring[4096]; static unsigned long long edges; static long nfed; static int P; static FILE *cf; static long ncomp3, ncomp_ge3_fail; static long st0, st1; /* start slots of the two pending chains */ static void bump(long L, long start) { if (L < 0 || L >= (1 << 20)) { fprintf(stderr, "L overflow %ld\n", L); exit(2); } runs[L]++; if (L >= 3) { if (L - 1 > 4096) { fprintf(stderr, "ring overflow L=%ld\n", L); exit(2); } ncomp3++; fprintf(cf, "%ld", start); for (long j = L - 1; j >= 1; j--) fprintf(cf, " %d", ring[(nfed - j) & 4095]); fprintf(cf, "\n"); } } int main(int argc, char **argv) { if (argc < 4) { fprintf(stderr, "usage: killcensus gaps.bin D p...\n"); return 2; } const char *path = argv[1]; long D = atol(argv[2]); FILE *f = fopen(path, "rb"); if (!f) { perror("open"); return 2; } uint16_t *g = malloc(2 * (size_t)D); if (fread(g, 2, (size_t)D, f) != (size_t)D) { fprintf(stderr, "short read\n"); return 2; } fclose(f); long *hist = calloc(1 << 16, sizeof(long)); unsigned long long sum = 0; int mn = 1 << 20, mx = 0; long ndist = 0; for (long i = 0; i < D; i++) { int v = g[i]; hist[v]++; sum += v; if (v < mn) mn = v; if (v > mx) mx = v; } int alldiv6 = 1; for (int v = 0; v < (1 << 16); v++) if (hist[v]) { ndist++; if (v % 6) alldiv6 = 0; } printf("{\n \"D\": %ld, \"sum_gaps\": %llu, \"min\": %d, \"max\": %d, " "\"ndistinct\": %ld, \"all_div6\": %d,\n \"hist\": {", D, sum, mn, mx, ndist, alldiv6); int first = 1; for (int v = 0; v < (1 << 16); v++) if (hist[v]) { printf("%s\"%d\":%ld", first ? "" : ", ", v, hist[v]); first = 0; } printf("},\n \"folds\": [\n"); for (int a = 3; a < argc; a++) { P = atoi(argv[a]); long cut = -1; for (long i = 0; i < D; i++) { int m = g[i] % P; if (m != 0 && m != 2 && m != P - 2) { cut = i; break; } } if (cut < 0) { fprintf(stderr, "no inert gap for p=%d\n", P); return 2; } memset(runs, 0, sizeof(runs)); edges = 0; nfed = 0; ncomp3 = 0; ncomp_ge3_fail = 0; long l0 = 1, l1 = 1; st0 = st1 = (cut + 1) % D; char cpath[600]; snprintf(cpath, sizeof(cpath), "%s.p%d.comp.txt", path, P); cf = fopen(cpath, "w"); if (!cf) { perror("comp out"); return 2; } for (long j = 1; j <= D; j++) { long idx = cut + j; if (idx >= D) idx -= D; long tgt = idx + 1; if (tgt >= D) tgt -= D; int gv = g[idx]; int m = gv % P; if (m == 0) { l0++; l1++; edges += 2; } else if (m == 2) { bump(l0, st0); l0 = l1 + 1; st0 = st1; l1 = 1; st1 = tgt; edges++; } else if (m == P - 2) { bump(l1, st1); l1 = l0 + 1; st1 = st0; l0 = 1; st0 = tgt; edges++; } else { bump(l0, st0); bump(l1, st1); l0 = 1; l1 = 1; st0 = st1 = tgt; } ring[nfed & 4095] = (uint16_t)gv; nfed++; } fclose(cf); long ncomp = 0, maxL = 0; unsigned long long nodes = 0; for (long L = 1; L < (1 << 20); L++) if (runs[L]) { ncomp += runs[L]; nodes += (unsigned long long)L * runs[L]; if (L > maxL) maxL = L; } /* histogram functionals of this fold */ long N0 = 0, NP = 0, NM = 0; for (int v = 0; v < (1 << 16); v++) { if (!hist[v]) continue; int m = v % P; if (m == 0) N0 += hist[v]; else if (m == 2) NP += hist[v]; else if (m == P - 2) NM += hist[v]; } printf(" {\"p\": %d, \"cut\": %ld, \"nodes\": %llu, \"nodes_2D\": %lld, " "\"edges\": %llu, \"pairs_weight\": %lld, \"ncomp\": %ld, \"maxL\": %ld, " "\"ncomp_ge3\": %ld, \"N0\": %ld, \"NP\": %ld, \"NM\": %ld, \"runs\": {", P, cut, nodes, 2LL * D, edges, 2LL * N0 + NP + NM, ncomp, maxL, ncomp3, N0, NP, NM); first = 1; for (long L = 1; L < (1 << 20); L++) if (runs[L]) { printf("%s\"%ld\":%ld", first ? "" : ", ", L, runs[L]); first = 0; } printf("}}%s\n", a + 1 < argc ? "," : ""); } printf(" ]\n}\n"); return 0; }