// docs/find-vs-check.svg: finding against checking, on Clay's own example (house a quarter of n students, no two on the // list of pairs who may not share). For n = 20 to 140, fifteen random lists each (a planted dorm, each other pair listed // with probability 20/n): the steps a plain search takes to find a dorm, median and middle half, and the pairs a checker // reads to check one, k(k-1)/2. Log scale, so steady multiplication is a straight line. Its only words are numbers, so // the README captions it. The search is the one the site's playground runs (the column’s first playground, pnp-dorm, replaced by pnp-circuit on 11-28), copied. // node scripts/find-vs-check.mjs > docs/find-vs-check.svg (about 2.5 minutes) // Clay's dorm problem (an independent set): n students, a list of pairs who may not share, house k with no such pair. const Dorm = (function () { function rng(seed) { var s = seed >>> 0; return function () { s = (s + 0x6d2b79f5) >>> 0; var t = s; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296 } } // a hidden dorm of k students with no pair inside; every other pair is on the list with probability p function planted(n, k, p, seed) { var r = rng(seed), perm = [], i, j for (i = 0; i < n; i++) perm.push(i) for (i = n - 1; i > 0; i--) { j = Math.floor(r() * (i + 1)); var t = perm[i]; perm[i] = perm[j]; perm[j] = t } var hid = new Uint8Array(n), edges = []; perm.slice(0, k).forEach(function (v) { hid[v] = 1 }) for (var u = 0; u < n; u++) for (var v = u + 1; v < n; v++) if (!(hid[u] && hid[v]) && r() < p) edges.push([u, v]) return { n: n, k: k, edges: edges } } // checking a proposed dorm: each pair of the chosen looked up in the list once function check(inst, chosen) { var bad = new Set(inst.edges.map(function (e) { return e[0] * inst.n + e[1] })), out = [], reads = 0 for (var i = 0; i < chosen.length; i++) for (var j = i + 1; j < chosen.length; j++) { reads++; var u = Math.min(chosen[i], chosen[j]), v = Math.max(chosen[i], chosen[j]); if (bad.has(u * inst.n + v)) out.push([u, v]) } return { ok: chosen.length >= inst.k && out.length === 0, conflicts: out, reads: reads } } // the plain search, as a generator a page can run a slice at a time: take the candidate with the most conflicts // (then drop its conflicts) or leave it; give up a branch when the chosen and the candidates cannot reach k function search(inst) { var n = inst.n, k = inst.k, W = Math.ceil(n / 32), nb = [], i for (i = 0; i < n; i++) nb.push(new Uint32Array(W)) inst.edges.forEach(function (e) { nb[e[0]][e[1] >> 5] |= 1 << (e[1] & 31); nb[e[1]][e[0] >> 5] |= 1 << (e[0] & 31) }) function pop(x) { x -= (x >>> 1) & 0x55555555; x = (x & 0x33333333) + ((x >>> 2) & 0x33333333); return (((x + (x >>> 4)) & 0x0f0f0f0f) * 0x01010101) >>> 24 } function count(c) { var s = 0; for (var w = 0; w < W; w++) s += pop(c[w]); return s } var res = { steps: 0, found: null }, chosen = [] function* go(c, size) { res.steps++ if ((res.steps & 4095) === 0) yield res.steps if (chosen.length >= k) { res.found = chosen.slice(); return } if (chosen.length + size < k) return var best = -1, bd = -1, w, q for (w = 0; w < W; w++) { var x = c[w]; while (x) { var b = x & -x, v = (w << 5) + 31 - Math.clz32(b); x ^= b var d = 0; for (q = 0; q < W; q++) d += pop(c[q] & nb[v][q]); if (d > bd) { bd = d; best = v } } } if (bd === 0) { // no conflicts left among the candidates: take them all var all = []; for (w = 0; w < W; w++) { var y = c[w]; while (y) { var bb = y & -y; y ^= bb; all.push((w << 5) + 31 - Math.clz32(bb)) } } if (chosen.length + all.length >= k) res.found = chosen.concat(all).slice(0, k); return } var take = new Uint32Array(W), leave = Uint32Array.from(c) for (w = 0; w < W; w++) take[w] = c[w] & ~nb[best][w] take[best >> 5] &= ~(1 << (best & 31)); leave[best >> 5] &= ~(1 << (best & 31)) chosen.push(best); yield* go(take, count(take)); chosen.pop(); if (res.found) return yield* go(leave, size - 1) } var all0 = new Uint32Array(W); for (i = 0; i < n; i++) all0[i >> 5] |= 1 << (i & 31) var gen = go(all0, n) return { res: res, step: function (until) { while (until()) { if (gen.next().done) return true } return false } } } function solve(inst) { var s = search(inst); s.step(function () { return true }); return s.res } // the race's instances: k = n/4 as in Clay's example, conflicts with probability 20/n function race(n, seed) { return planted(n, n / 4, Math.min(0.9, 20 / n), 1000 * n + seed) } return { rng: rng, planted: planted, check: check, search: search, solve: solve, race: race } })() const NS = [20, 28, 36, 44, 52, 60, 68, 76, 84, 92, 100, 108, 116, 124, 132, 140], RUNS = 15 const rows = NS.map(n => { const st = [] for (let s = 1; s <= RUNS; s++) { const inst = Dorm.race(n, 100 + s), r = Dorm.solve(inst); if (!r.found || !Dorm.check(inst, r.found).ok) throw Error(`n=${n}`); st.push(r.steps) } st.sort((a, b) => a - b) const q = f => st[Math.round(f * (RUNS - 1))], k = n / 4 console.error(`n ${n}: median ${q(0.5)} steps (middle half ${q(0.25)}..${q(0.75)}); check ${k * (k - 1) / 2} pairs`) return { n, lo: q(0.25), mid: q(0.5), hi: q(0.75), check: k * (k - 1) / 2 } }) const W = 720, H = 340, L = 56, R = W - 96, T = 12, B = H - 28, NMAX = 144, EMAX = Math.ceil(Math.log10(rows.at(-1).hi)) const X = n => L + n / NMAX * (R - L), Y = v => B - Math.log10(v) / EMAX * (B - T), num = v => (Math.round(v * 10) / 10).toString() const sup = n => String(n).replace(/[0-9]/g, d => '⁰¹²³⁴⁵⁶⁷⁸⁹'[d]) const out = [``, 'Steps to find a dorm against pairs read to check one', '', ``] for (let e = 0; e <= EMAX; e++) { const y = Math.round(Y(10 ** e)) + 0.5; out.push(`${e === 0 ? '1' : e === 1 ? '10' : '10' + sup(e)}`) } for (const n of [0, 20, 40, 60, 80, 100, 120, 140]) out.push(`${n}`) out.push(``) out.push(``) out.push(``) out.push(...rows.map(r => ``)) out.push(``) const last = rows.at(-1) out.push(`${last.mid.toLocaleString('en-US')}`) out.push(`${last.check}`) out.push('') console.log(out.join('\n'))