/* verify_q0002.c: a second, independent check of IIS-Q-0002 (cap set, isitsolved.com); it uses no structure of the blocking pairs. */ /* Build: cc -O2 -std=c99 -Wall -Wextra -o verify_q0002 verify_q0002.c Run: ./verify_q0002 [ROWS_FILE] */ #include #include #include #include #include #define NCAP 236 #define NPTS 2187 #define NO 24 #define MAXE (NO * 10) #define MAXV 200 #define KCHECK 4 static int pw3[8]; static unsigned char dig[NPTS][7]; static unsigned char iscap[NPTS]; static int capidx[NPTS]; static int capv[NCAP]; static int sec1[NPTS], sec2[NPTS]; static int dir[1093]; static int failures = 0; static const int DESIGN[10][3] = {{1, 2, 3}, {2, 3, 6}, {1, 2, 4}, {2, 4, 5}, {1, 3, 5}, {2, 5, 6}, {1, 4, 6}, {3, 4, 5}, {1, 5, 6}, {3, 4, 6}}; static const int EXPECT_CLASS[][4] = { {0, 0, 56, 1}, {0, 1, 10, 12}, {0, 2, 14, 60}, {0, 3, 13, 80}, {0, 4, 12, 240}, {0, 5, 11, 192}, {0, 6, 16, 32}, {1, 0, 56, 1}, {1, 1, 10, 12}, {1, 2, 14, 60}, {1, 3, 13, 80}, {1, 4, 12, 240}, {1, 5, 11, 192}, {1, 6, 16, 32}, {2, 0, 38, 1}, {2, 2, 25, 60}, {2, 3, 16, 160}, {2, 4, 18, 240}, {2, 5, 15, 192}, {2, 6, 11, 32}, {2, 6, 30, 32}}; static const int EXPECT_TAU[25] = {-1, 10, 16, 18, 22, 25, 30, 35, 38, 39, 44, 50, 57, 61, 64, 69, 74, 77, 78, 83, 88, 94, 100, 110, 120}; static const int EXPECT_COST[25] = {-1, 10, 16, 18, 22, 25, 30, 35, 38, 39, 44, 50, 57, 62, 66, 72, 78, 82, 84, 90, 96, 103, 110, 121, 132}; static void check(int cond, const char *what) { printf("%s: %s\n", cond ? "PASS" : "FAIL", what); if (!cond) failures++; } static int padd(int a, int b) { int c = 0, i; for (i = 0; i < 7; i++) c += (dig[a][i] + dig[b][i]) % 3 * pw3[6 - i]; return c; } static int pneg(int a) { int c = 0, i; for (i = 0; i < 7; i++) if (dig[a][i]) c += (3 - dig[a][i]) * pw3[6 - i]; return c; } static int pthird(int a, int b) { int c = 0, i; for (i = 0; i < 7; i++) c += (6 - dig[a][i] - dig[b][i]) % 3 * pw3[6 - i]; return c; } static int in_design(const int *sup) { int b; for (b = 0; b < 10; b++) if (DESIGN[b][0] == sup[0] && DESIGN[b][1] == sup[1] && DESIGN[b][2] == sup[2]) return 1; return 0; } static int cap_rule(const unsigned char *x) { int i, w = 0, n2 = 0, ns = 0, sup[6]; for (i = 1; i < 7; i++) if (x[i]) { w++; sup[ns++] = i; if (x[i] == 2) n2++; } if (x[0] == 2) return w == 1; if (w == 6) return n2 % 2 == 0; if (w == 3) return (x[0] == 0) == (in_design(sup) == 1); return 0; } typedef struct { uint64_t w[4]; } bs; static const bs BS0 = {{0, 0, 0, 0}}; static int bs_get(bs a, int i) { return (int)(a.w[i >> 6] >> (i & 63)) & 1ULL; } static void bs_set(bs *a, int i) { a->w[i >> 6] |= 1ULL << (i & 63); } static bs bs_and(bs a, bs b) { bs r; int k; for (k = 0; k < 4; k++) r.w[k] = a.w[k] & b.w[k]; return r; } static bs bs_clear(bs a, int i) { a.w[i >> 6] &= ~(1ULL << (i & 63)); return a; } static int bs_empty(bs a) { return !(a.w[0] | a.w[1] | a.w[2] | a.w[3]); } static int bs_count(bs a) { return __builtin_popcountll(a.w[0]) + __builtin_popcountll(a.w[1]) + __builtin_popcountll(a.w[2]) + __builtin_popcountll(a.w[3]); } static int bs_low(bs a) { int k; for (k = 0; k < 4; k++) if (a.w[k]) return k * 64 + __builtin_ctzll(a.w[k]); return -1; } static int ea[MAXE], eb[MAXE]; static int nedges = 0; static int elist[NO][10]; static unsigned char lab[NCAP][NCAP]; static int oz[NO], oinfo[NPTS]; static int maxpart = 0; static bs adj[NCAP]; static unsigned int stampv[NCAP], cur_stamp = 0; static int invlist[NCAP], ninv = 0; static bs cur_forced; static long long cover_calls = 0; static void graph_begin(bs forced) { cur_stamp++; ninv = 0; cur_forced = forced; } static void graph_add_edge(int e) { int a = ea[e], b = eb[e]; if (bs_get(cur_forced, a) || bs_get(cur_forced, b)) return; bs_set(&adj[a], b); bs_set(&adj[b], a); if (stampv[a] != cur_stamp) { stampv[a] = cur_stamp; invlist[ninv++] = a; } if (stampv[b] != cur_stamp) { stampv[b] = cur_stamp; invlist[ninv++] = b; } } static void graph_end(void) { int i; for (i = 0; i < ninv; i++) adj[invlist[i]] = BS0; } /* Size of a smallest vertex cover on the points of alive: a point with one neighbor puts that neighbor into the cover; else the search tries both ends of an edge. */ static int cover(bs alive, const int *cand, int nc) { int i, u, v, r1, r2, taken = 0; restart: u = -1; for (i = 0; i < nc; i++) { int a = cand[i], d; if (!bs_get(alive, a)) continue; d = bs_count(bs_and(adj[a], alive)); if (d == 1) { v = bs_low(bs_and(adj[a], alive)); alive = bs_clear(alive, v); taken++; goto restart; } if (d >= 2 && u < 0) u = a; } if (u < 0) return taken; cover_calls++; v = bs_low(bs_and(adj[u], alive)); r1 = 1 + cover(bs_clear(alive, u), cand, nc); r2 = 1 + cover(bs_clear(alive, v), cand, nc); return taken + (r1 < r2 ? r1 : r2); } /* The same number by complete enumeration of the sets of points of the part, in increasing size. */ static int cover_brute(bs alive, const int *cand, int nc) { int size, m, idx, a, ok; bs left; if (nc > 22) { printf("FAIL: a connected part of a blocking graph has more than 22 points\n"); exit(1); } for (size = 0; size <= nc; size++) for (m = 0; m < (1 << nc); m++) { if (__builtin_popcount(m) != size) continue; left = alive; for (idx = 0; idx < nc; idx++) if (m >> idx & 1) left = bs_clear(left, cand[idx]); ok = 1; for (idx = 0; idx < nc && ok; idx++) { a = cand[idx]; if (bs_get(left, a) && !bs_empty(bs_and(adj[a], left))) ok = 0; } if (ok) return size; } return nc; } /* The graph is a union of connected parts; a smallest cover is the union of smallest covers of the parts. mode 0: search; mode 1: enumeration. */ static int total_cover(int mode) { bs alive = BS0, rest, ca, nb; int i, total = 0, comp[NCAP], nc = 0, queue[NCAP], qh, qt; for (i = 0; i < ninv; i++) bs_set(&alive, invlist[i]); rest = alive; while (!bs_empty(rest)) { int s = bs_low(rest); nc = 0; qh = qt = 0; queue[qt++] = s; rest = bs_clear(rest, s); while (qh < qt) { int a = queue[qh++]; comp[nc++] = a; nb = bs_and(adj[a], rest); for (;;) { int b = bs_low(nb); if (b < 0) break; nb = bs_clear(nb, b); rest = bs_clear(rest, b); queue[qt++] = b; } } if (nc > maxpart) maxpart = nc; ca = BS0; for (i = 0; i < nc; i++) bs_set(&ca, comp[i]); total += mode == 0 ? cover(ca, comp, nc) : cover_brute(ca, comp, nc); } return total; } static void build_graph(uint32_t T, bs f) { int i, j; graph_begin(f); for (i = 0; i < NO; i++) if (T >> i & 1) for (j = 0; j < 10; j++) graph_add_edge(elist[i][j]); } int main(int argc, char **argv) { int i, j, k, p, q, r, t, n, v, ndir, npairs, bad1, bad2, lines2, cnt, zi, no = 0, okpairs, nf; int hist[64], ocnt[3], mark[NCAP], deg[NCAP], degh[NCAP + 1], third[NO][NO]; int lo, hi, b, ok, ok1, ok2, nn; long long total_pd = 0, s, noncap = 0, mism = 0, ncheck = 0; static long long dtau[25][MAXV], dcost[25][MAXV]; static const long long c24[25] = {1, 24, 276, 2024, 10626, 42504, 134596, 346104, 735471, 1307504, 1961256, 2496144, 2704156, 2496144, 1961256, 1307504, 735471, 346104, 134596, 42504, 10626, 2024, 276, 24, 1}; static const int expected_hist[12][2] = {{10, 24}, {11, 416}, {12, 480}, {13, 160}, {14, 120}, {15, 192}, {16, 224}, {18, 240}, {25, 60}, {30, 32}, {38, 1}, {56, 2}}; bs f; uint32_t T; clock_t t0; pw3[0] = 1; for (i = 1; i < 8; i++) pw3[i] = 3 * pw3[i - 1]; for (p = 0; p < NPTS; p++) for (i = 0; i < 7; i++) dig[p][i] = (unsigned char)(p / pw3[6 - i] % 3); printf("== A. construction by the rule of Table 2\n"); n = 0; for (p = 0; p < NPTS; p++) { capidx[p] = -1; oinfo[p] = -1; if (cap_rule(dig[p])) { iscap[p] = 1; capidx[p] = n; capv[n++] = p; } } check(n == NCAP, "the rule of Table 2 gives 236 points"); ocnt[0] = ocnt[1] = ocnt[2] = 0; for (i = 0; i < n; i++) ocnt[dig[capv[i]][0]]++; check(ocnt[0] == 112 && ocnt[1] == 112 && ocnt[2] == 12, "the layers have 112, 112 and 12 points"); if (argc > 1) { FILE *fw = fopen(argv[1], "w"); if (!fw) { printf("FAIL: cannot open the rows file\n"); return 1; } for (i = 0; i < n; i++) fprintf(fw, "%d%d%d%d%d%d%d\n", dig[capv[i]][0], dig[capv[i]][1], dig[capv[i]][2], dig[capv[i]][3], dig[capv[i]][4], dig[capv[i]][5], dig[capv[i]][6]); fclose(fw); printf("the 236 sorted rows are in the rows file\n"); } printf("== B. the cap property, two methods\n"); npairs = bad1 = 0; for (i = 0; i < NCAP; i++) for (j = i + 1; j < NCAP; j++) { t = pthird(capv[i], capv[j]); npairs++; if (iscap[t]) bad1++; else sec1[t]++; } check(npairs == 27730 && bad1 == 0, "method 1 (third points of all 27730 pairs of cap points): none is in the cap"); ndir = 0; for (p = 1; p < NPTS; p++) { i = 0; while (!dig[p][i]) i++; if (dig[p][i] == 1) dir[ndir++] = p; } bad2 = lines2 = 0; for (p = 0; p < NPTS; p++) for (i = 0; i < ndir; i++) { q = padd(p, dir[i]); r = padd(p, padd(dir[i], dir[i])); cnt = iscap[p] + iscap[q] + iscap[r]; total_pd++; if (cnt == 3) bad2++; if (cnt == 2) { lines2++; if (!iscap[p]) sec2[p]++; else if (!iscap[q]) sec2[q]++; else sec2[r]++; } } check(ndir == 1093 && total_pd == 2390391, "method 2: 1093 directions and 3 x 796797 = 2390391 pairs (point, direction)"); check(bad2 == 0, "method 2 (all lines): no line has three cap points"); check(lines2 == 83190, "method 2: 3 x 27730 = 83190 pairs (point, direction) have two cap points"); printf("== C. secant counts\n"); n = 0; for (p = 0; p < NPTS; p++) if (!iscap[p] && sec2[p] != 3 * sec1[p]) n++; check(n == 0, "the two methods agree: method 2 finds each secant three times"); for (i = 0; i < 64; i++) hist[i] = 0; n = 0; for (p = 0; p < NPTS; p++) if (!iscap[p] && sec1[p]) { hist[sec1[p]]++; n++; } check(n == 1951, "1951 outside points have a secant"); ok = 1; nn = 0; s = 0; for (i = 0; i < 64; i++) if (hist[i]) { s += (long long)i * hist[i]; nn++; for (j = 0; j < 12; j++) if (expected_hist[j][0] == i && expected_hist[j][1] == hist[i]) break; if (j == 12) ok = 0; } check(ok && nn == 12 && s == 27730, "secant histogram 10:24, 11:416, 12:480, 13:160, 14:120, 15:192, 16:224, 18:240, 25:60, 30:32, 38:1, 56:2"); printf("HISTOGRAM"); for (i = 0; i < 64; i++) if (hist[i]) printf(" %d:%d", i, hist[i]); printf("\n"); { static int cnt[3][7][64]; memset(cnt, 0, sizeof cnt); for (p = 0; p < NPTS; p++) if (!iscap[p] && sec1[p]) { int w = 0; for (i = 1; i < 7; i++) w += dig[p][i] != 0; cnt[dig[p][0]][w][sec1[p]]++; } n = nn = 0; for (i = 0; i < 3; i++) for (j = 0; j < 7; j++) for (v = 0; v < 64; v++) nn += cnt[i][j][v] > 0; for (i = 0; i < 21; i++) n += cnt[EXPECT_CLASS[i][0]][EXPECT_CLASS[i][1]][EXPECT_CLASS[i][2]] == EXPECT_CLASS[i][3]; check(n == 21 && nn == 21, "by (prefix, suffix weight, secant count): 21 classes of outside points; the number of points of each class agrees with the table of the report"); for (i = 0; i < 21; i++) printf("CLASS %d %d %d %d\n", EXPECT_CLASS[i][0], EXPECT_CLASS[i][1], EXPECT_CLASS[i][2], cnt[EXPECT_CLASS[i][0]][EXPECT_CLASS[i][1]][EXPECT_CLASS[i][2]]); } printf("== D. the claim of IIS-Q-0002\n"); no = 0; n = 0; for (p = 0; p < NPTS; p++) if (!iscap[p] && sec1[p] == 10) { int w = 0; for (i = 1; i < 7; i++) w += dig[p][i] != 0; if (w == 1 && dig[p][0] != 2) n++; oz[no] = p; oinfo[p] = no; no++; } check(no == NO && n == NO, "O has 24 points; each has the form (t, a e_i) with t in {0,1}, a in {1,2}, i in 1..6"); if (no != NO) { printf("\nSTOPPED\n"); return 1; } printf("O:"); for (zi = 0; zi < no; zi++) { printf(" "); for (i = 0; i < 7; i++) printf("%d", dig[oz[zi]][i]); } printf("\n"); okpairs = 1; nedges = 0; for (zi = 0; zi < no; zi++) { int ne = 0, nz = pneg(oz[zi]); memset(mark, 0, sizeof mark); for (i = 0; i < NCAP; i++) for (j = i + 1; j < NCAP; j++) if (padd(capv[i], capv[j]) == nz) { if (ne < 10 && nedges < MAXE) { elist[zi][ne] = nedges; ea[nedges] = i; eb[nedges] = j; } if (lab[i][j]) okpairs = 0; lab[i][j] = (unsigned char)(zi + 1); nedges++; ne++; mark[i]++; mark[j]++; } if (ne != 10) okpairs = 0; for (i = 0; i < NCAP; i++) if (mark[i] > 1) okpairs = 0; } check(okpairs && nedges == 240, "each point of O has 10 pairs with 20 different endpoints, and the 240 pairs are 240 different pairs"); if (!okpairs || nedges != 240) { printf("\nSTOPPED\n"); return 1; } memset(deg, 0, sizeof deg); for (i = 0; i < NCAP; i++) for (j = i + 1; j < NCAP; j++) if (lab[i][j]) { deg[i]++; deg[j]++; } memset(degh, 0, sizeof degh); for (i = 0; i < NCAP; i++) degh[deg[i]]++; check(degh[0] == 76 && degh[3] == 160 && degh[0] + degh[3] == NCAP, "incidence histogram of the 240 pairs on the 236 cap points: 0 for 76 points, 3 for 160 points"); printf("== E. all 2^24 subsets of O: cover size and insertion cost\n"); fflush(stdout); t0 = clock(); for (i = 0; i < NO; i++) for (j = i + 1; j < NO; j++) third[i][j] = pthird(oz[i], oz[j]); for (T = 0; T < (1u << NO); T++) { f = BS0; nf = 0; k = __builtin_popcount(T); for (i = 0; i < NO; i++) if (T >> i & 1) for (j = i + 1; j < NO; j++) if (T >> j & 1) { p = third[i][j]; if (iscap[p]) { if (!bs_get(f, capidx[p])) { bs_set(&f, capidx[p]); nf++; } } else if (oinfo[p] >= 0 && (T >> oinfo[p] & 1)) noncap++; } build_graph(T, BS0); t = total_cover(0); graph_end(); if (t < 0 || t >= MAXV) { printf("FAIL: a cover size is out of range\n"); return 1; } dtau[k][t]++; if (k <= KCHECK) { build_graph(T, BS0); ncheck++; if (total_cover(1) != t) mism++; graph_end(); } build_graph(T, f); t = nf + total_cover(0); graph_end(); if (t < 0 || t >= MAXV) { printf("FAIL: a cost is out of range\n"); return 1; } dcost[k][t]++; } printf("time of the enumeration: %.1f s; calls of the branch search: %lld\n", (double)(clock() - t0) / CLOCKS_PER_SEC, cover_calls); ok = 1; for (k = 0; k <= 24; k++) { s = 0; for (v = 0; v < MAXV; v++) s += dtau[k][v]; if (s != c24[k]) ok = 0; } check(ok && noncap == 0, "the cover table has C(24,k) subsets for each k, and no subset of O has a line"); check(ncheck == 12951 && mism == 0, "for each of the 12951 subsets with at most 4 points, the branch search and the complete enumeration give the same cover size"); check(maxpart == 4, "in each graph of this run, the largest connected part has 4 points"); printf("\nTAU k least number_of_subsets largest number_of_subsets ceil(10k/3)\n"); ok1 = ok2 = 1; for (k = 0; k <= 24; k++) { lo = hi = -1; b = (10 * k + 2) / 3; for (v = 0; v < MAXV; v++) if (dtau[k][v]) { if (lo < 0) lo = v; hi = v; } printf("TAU %2d %3d %8lld %3d %8lld %3d\n", k, lo, dtau[k][lo], hi, dtau[k][hi], b); if (k >= 1 && lo < (10 > b ? 10 : b)) ok1 = 0; if (k >= 1 && lo != EXPECT_TAU[k]) ok2 = 0; } check(ok1, "for each k the least cover size is at least max(10, ceil(10k/3))"); check(ok2, "the least cover size for k = 1..24 is 10, 16, 18, 22, 25, 30, 35, 38, 39, 44, 50, 57, 61, 64, 69, 74, 77, 78, 83, 88, 94, 100, 110, 120"); printf("\nDIST k tau number_of_subsets\n"); for (k = 0; k <= 24; k++) for (v = 0; v < MAXV; v++) if (dtau[k][v]) printf("DIST %2d %3d %8lld\n", k, v, dtau[k][v]); printf("\nJOINTALL k cost:number_of_subsets (all 2^24 subsets, direct computation)\n"); for (k = 1; k <= 24; k++) { printf("JOINTALL k=%d", k); for (v = 0; v < MAXV; v++) if (dcost[k][v]) printf(" %d:%lld", v, dcost[k][v]); printf("\n"); } printf("\nJOINT k cost:number_of_subsets (k = 1..4)\n"); for (k = 1; k <= 4; k++) { printf("JOINT k=%d", k); for (v = 0; v < MAXV; v++) if (dcost[k][v]) printf(" %d:%lld", v, dcost[k][v]); printf("\n"); } printf("\nJMIN k least_cost largest_cost size_of_largest_cap_(C-D)_union_T\n"); ok1 = 1; for (k = 1; k <= 24; k++) { lo = hi = -1; for (v = 0; v < MAXV; v++) if (dcost[k][v]) { if (lo < 0) lo = v; hi = v; } printf("JMIN k=%d %d %d %d\n", k, lo, hi, 236 - lo + k); if (lo != EXPECT_COST[k]) ok1 = 0; } check(ok1, "the least insertion cost for k = 1..24 is 10, 16, 18, 22, 25, 30, 35, 38, 39, 44, 50, 57, 62, 66, 72, 78, 82, 84, 90, 96, 103, 110, 121, 132"); printf("\nUNLOCK budget largest_k_with_tau_at_most_budget largest_k_with_cost_at_most_budget\n"); for (v = 0; v <= 21; v++) { int a1 = 0, a2 = 0, kk, tt; for (kk = 0; kk <= 24; kk++) for (tt = 0; tt <= v; tt++) { if (dtau[kk][tt] && kk > a1) a1 = kk; if (dcost[kk][tt] && kk > a2) a2 = kk; } printf("UNLOCK %2d %2d %2d\n", v, a1, a2); } if (failures == 0) printf("\nALL CHECKS PASSED\n"); else printf("\n%d CHECK(S) FAILED\n", failures); return failures == 0 ? 0 : 1; }