Documentation

CslibFrontier.Probability.HashFamily.Collisions

Expected collisions of universal hash families #

A hash function h : X → Y sorts a finite key set S into buckets {x ∈ S | h x = j}. The number orderedCollisionCount h S of ordered pairs of distinct keys sharing a bucket controls both injectivity and the total squared bucket size:

For an ε-universal family (Cslib.Probability.IsUniversalHash) and |S| = n, linearity of expectation bounds the expected number of ordered collisions by n (n - 1) ε, that is twice the C(n, 2) ε bound on unordered collisions. With a universal family into m values (Cslib.Probability.IsTwoUniversal, ε = 1 / m) this gives the two estimates behind Fredman–Komlós–Szemerédi perfect hashing [FKS84], in the form of [CLRS09], Section 11.5:

CLRS states the two probability bounds for m = n² and m = n, with strict inequalities (< 1 / 2, and secondary storage ≥ 4n). The forms here allow any m at least that large and bound the probability by ≤ 1 / 2; they are exactly what the construction's success tests use.

Expectations are ∑' i, seed i * f i in ℝ≥0∞ over an arbitrary seed law, and probabilities are PMF.toOuterMeasure. These are information-theoretic statements; evaluation cost is separate.

Main definitions #

Main results #

References #

def Cslib.Probability.orderedCollisionCount {X : Type u_2} {Y : Type u_3} [DecidableEq Y] (h : X → Y) (S : Finset X) :

The number of ordered pairs (x, y) of distinct elements of S that h maps to the same value. Each unordered colliding pair is counted twice.

theorem Cslib.Probability.orderedCollisionCount_eq_sum {X : Type u_2} {Y : Type u_3} [DecidableEq Y] (h : X → Y) (S : Finset X) :
orderedCollisionCount h S = ∑ p ∈ S.offDiag, if h p.1 = h p.2 then 1 else 0
theorem Cslib.Probability.orderedCollisionCount_eq_zero_iff {X : Type u_2} {Y : Type u_3} [DecidableEq Y] {h : X → Y} {S : Finset X} :

h is injective on S exactly when no ordered pair of distinct elements collides.

theorem Cslib.Probability.two_le_orderedCollisionCount {X : Type u_2} {Y : Type u_3} [DecidableEq Y] {h : X → Y} {S : Finset X} (hS : ¬Set.InjOn h ↑S) :

A collision of x ≠ y is counted as (x, y) and as (y, x).

theorem Cslib.Probability.sum_card_filter_sq {X : Type u_2} {Y : Type u_3} [DecidableEq Y] [Fintype Y] (h : X → Y) (S : Finset X) :
∑ j : Y, {x ∈ S | h x = j}.card ^ 2 = S.card + orderedCollisionCount h S

The sum of the squared bucket sizes counts each key with itself and every ordered colliding pair: ∑ⱼ bⱼ² = |S| + #{(x, y) : x ≠ y, h x = h y}.

theorem Cslib.Probability.IsUniversalHash.tsum_orderedCollisionCount_le {ι : Type u_1} {X : Type u_2} {Y : Type u_3} [DecidableEq Y] {seed : PMF ι} {hash : ι → X → Y} {ε : ENNReal} (hh : IsUniversalHash seed hash ε) (S : Finset X) :
∑' (i : ι), seed i * ↑(orderedCollisionCount (hash i) S) ≤ ↑(S.card * (S.card - 1)) * ε

Expected collisions of an ε-universal family: on a set of n keys the expected number of ordered colliding pairs is at most n (n - 1) ε, twice the C(n, 2) ε bound for unordered pairs ([CLRS09], Section 11.5).

theorem Cslib.Probability.IsUniversalHash.tsum_sum_card_filter_sq_le {ι : Type u_1} {X : Type u_2} {Y : Type u_3} [DecidableEq Y] [Fintype Y] {seed : PMF ι} {hash : ι → X → Y} {ε : ENNReal} (hh : IsUniversalHash seed hash ε) (S : Finset X) :
∑' (i : ι), seed i * ↑(∑ j : Y, {x ∈ S | hash i x = j}.card ^ 2) ≤ ↑S.card + ↑(S.card * (S.card - 1)) * ε

The expected sum of squared bucket sizes is n plus the expected number of ordered collisions, so at most n + n (n - 1) ε for an ε-universal family.

theorem Cslib.Probability.IsUniversalHash.toOuterMeasure_not_injOn_le {ι : Type u_1} {X : Type u_2} {Y : Type u_3} {seed : PMF ι} {hash : ι → X → Y} {ε : ENNReal} (hh : IsUniversalHash seed hash ε) (S : Finset X) :
seed.toOuterMeasure {i : ι | ¬Set.InjOn (hash i) ↑S} ≤ ↑(S.card.choose 2) * ε

Union bound over pairs: an ε-universal family is injective on S except with probability at most C(|S|, 2) · ε.

theorem Cslib.Probability.IsTwoUniversal.toOuterMeasure_not_injOn_le {ι : Type u_1} {X : Type u_2} {Y : Type u_3} [Fintype Y] {seed : PMF ι} {hash : ι → X → Y} (hh : IsTwoUniversal seed hash) (S : Finset X) (hS : S.card ^ 2 ≤ Fintype.card Y) :
seed.toOuterMeasure {i : ι | ¬Set.InjOn (hash i) ↑S} ≤ 2⁻¹

Perfect hashing into n² cells ([CLRS09], Section 11.5): a universal family into at least n² values is injective on a fixed set of n keys except with probability at most 1 / 2. CLRS states the strict bound < 1 / 2 for exactly n² values; the non-strict form is what the secondary level of the construction uses.

theorem Cslib.Probability.IsTwoUniversal.tsum_sum_card_filter_sq_le {ι : Type u_1} {X : Type u_2} {Y : Type u_3} [DecidableEq Y] [Fintype Y] {seed : PMF ι} {hash : ι → X → Y} (hh : IsTwoUniversal seed hash) (S : Finset X) (hS : S.card ≤ Fintype.card Y) :
∑' (i : ι), seed i * ↑(∑ j : Y, {x ∈ S | hash i x = j}.card ^ 2) ≤ ↑(2 * S.card - 1)

Linear total secondary space ([CLRS09], Section 11.5): with a universal family into at least n values, the expected sum of squared bucket sizes is at most 2n - 1.

theorem Cslib.Probability.IsTwoUniversal.toOuterMeasure_four_mul_lt_sum_card_filter_sq_le {ι : Type u_1} {X : Type u_2} {Y : Type u_3} [DecidableEq Y] [Fintype Y] {seed : PMF ι} {hash : ι → X → Y} (hh : IsTwoUniversal seed hash) (S : Finset X) (hS : S.card ≤ Fintype.card Y) :
seed.toOuterMeasure {i : ι | 4 * S.card < ∑ j : Y, {x ∈ S | hash i x = j}.card ^ 2} ≤ 2⁻¹

Markov bound for the top level ([CLRS09], Section 11.5): with a universal family into at least n values, the sum of squared bucket sizes exceeds 4n with probability at most 1 / 2. CLRS bounds the probability of ≥ 4n by < 1 / 2 for exactly n values; the top-level test of the construction accepts ≤ 4n, so it needs the form stated here.

theorem Cslib.Probability.IsTwoUniversal.sum_ite_toReal_le_card_div_of_imp {ι : Type u_1} {X : Type u_2} {Y : Type u_3} [Fintype ι] [Fintype Y] {seed : PMF ι} {hash : ι → X → Y} (hh : IsTwoUniversal seed hash) (S : Finset X) (x : X) {P : ι → Prop} {x✝ : DecidablePred P} (hP : ∀ (i : ι), P i → ∃ y ∈ S, y ≠ x ∧ hash i y = hash i x) :
(∑ i : ι, if P i then (seed i).toReal else 0) ≤ ↑S.card / ↑(Fintype.card Y)

If an event on hash seeds forces a collision of x with another key of S, a universal family into Y bounds its probability by |S| / |Y|.