Inclusion and exclusion

Inclusion, exclusion and pigeonholes

Two principles that carry a surprising share of combinatorics: counting a union by adding and subtracting overlaps, and the fact that too many things in too few boxes must collide. Both are here on actual data and as formulas.

On sets you can list

For sets A1,…,AmA_1, \dots, A_m,

∣A1∪⋯∪Am∣=∑i∣Ai∣−∑i<j∣Ai∩Aj∣+∑i<j<k∣Ai∩Aj∩Ak∣−⋯ |A_1 \cup \dots \cup A_m| = \sum_i |A_i| - \sum_{i<j} |A_i \cap A_j| + \sum_{i<j<k} |A_i \cap A_j \cap A_k| - \cdots

inclusionExclusion(sets) computes each level of that sum, so the answer comes with its working.

import { inclusionExclusion } from "@ratmath/discrete";

const r = inclusionExclusion([[1, 2, 3, 4], [3, 4, 5], [4, 5, 6, 7]], { universe: 10 });
r.union;    // 7n
r.none;     // 3n: of a universe of 10, how many are in none of the sets
r.terms;    // [{ size: 1, sign: 1, sum: 11n, count: 3n },
            //  { size: 2, sign: -1, sum: 5n, count: 3n },
            //  { size: 3, sign: 1, sum: 1n, count: 1n }]     11 − 5 + 1 = 7

Sets are arrays or Sets. universe is a count, an array or a Set. Up to 20 sets are accepted, since the formula has 2m−12^m - 1 terms; intersections that are already empty are not explored further.

By formula: the sieve

Most uses never list anything. There are mm "bad" properties, the number of objects with any particular kk of them is a known NkN_k (the same for every choice, by symmetry), and the number with none is

∑k=0m(−1)k(mk)Nk. \sum_{k=0}^{m} (-1)^k \binom{m}{k} N_k.
import { sieve, factorial } from "@ratmath/discrete";

// Derangements of 5: property i is "i stays in place"; fixing k of them leaves (5 − k)! permutations.
const d = sieve(5, (k) => factorial(5 - k));
d.none;     // 44n
d.all;      // 120n
d.union;    // 76n: permutations with at least one fixed point
d.terms.map((t) => `${t.sign > 0 ? "+" : "-"}${t.choices}·${t.each}`);
// ["+1·120", "-5·24", "+10·6", "-10·2", "+5·1", "-1·1"]

// Functions from 6 things onto 3: property i is "value i is never taken".
sieve(3, (k) => BigInt(3 - k) ** 6n).none;   // 540n = 3!·S(6, 3)

The derangement sum simplifies to n!∑k(−1)k/k!n!\sum_k (-1)^k/k!, which is why the proportion of derangements tends to 1/e1/e. The second example is the formula for surjections, and dividing by k!k! gives the Stirling numbers of the second kind.

Pigeonholes

If nn things go into hh boxes, some box holds at least ⌈n/h⌉\lceil n/h \rceil. pigeonhole states the bound; collision finds an actual pair in the same box, given a function that says which box a thing goes in.

import { pigeonhole, collision } from "@ratmath/discrete";

pigeonhole(13, 12);    // { atLeast: 2n, atMost: 1n, forced: true, statement: "13 items in 12 boxes: some box holds at least 2" }
pigeonhole(100, 7).atLeast;    // 15n

// Among any 12 whole numbers, two differ by a multiple of 11: the boxes are the remainders.
collision([3, 14, 25, 7, 18, 40, 2, 9, 61, 33, 12, 5], (x) => x % 11);
// { key: 3, items: [3, 14], indices: [0, 1] }
collision([1, 2, 3]);   // null: all different

The principle only says a collision exists; it does not say where. That gap is real: it is easy to prove that two people in a large city have the same number of hairs and hard to find them. collision closes the gap for data you hold.

Try itEdit the code and run it with @ratmath/discrete and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs