import Mathlib /-! =============================================================================== RANDOMIZED ALGORITHMS, UNIVERSAL & PERFECT HASHING, LOAD BALANCING, ONLINE OPTIMIZATION MINIMAX & RANDOMIZED GRAPH/ALGEBRAIC VERIFICATION =============================================================================== A unified, 100% non-asymptotic, axiom-clean Lean 4 / Mathlib formalization across five interlocking pillars of Fundamental Computer Science & Randomized Algorithms: 1. **Universal & `k`-Wise Independent Hashing & FKS Static Perfect Hashing:** - Pairwise-universal & strongly-universal hash family framework. - Carter–Wegman (1979) affine hashing over `F_p` (exact pairwise independence, exact `1/p` collision rate, and exact mod-`m` universality `m · coll ≤ p(p - 1)`). - Polynomial `k`-wise independent hashing over `F_p` (exact evaluation bijection via `Polynomial.card_roots'`, probability `p^{-k}` for every `k`-tuple). - Exact collision-pair expectations, quadratic-table zero-collision existence (`m = n²`), and Fredman–Komlós–Szemerédi (1984) two-level static perfect hashing with zero worst-case lookup collisions and exact linear space `∑ c_j² < 2 n`. 2. **Balls-into-Bins & Power-of-Two-Choices Load Balancing (Azar–Broder–Karlin–Upfal):** - Exact birthday no-collision count `m.descFactorial k`, exact product formula, exponential upper bound `P(no coll) ≤ exp(-k(k-1)/(2m))`, union lower bound `P(coll) ≤ C(k,2)/m`, and exact 23-person birthday paradox (`P(coll) > 1/2` for `m = 365`). - Exact 1-choice max-load tail bound `P(max ≥ k) ≤ n / k! ≤ n · (e / k)^k`. - Exact ABKU `d`-choice height-transition probability `(B / n)^d`, exact doubly-exponential layered recurrence `β_{i₀ + t} ≤ α^(d^t)` (and shifted Chernoff recurrence `c · β_i^d`), and exact non-asymptotic exponential separation `ln(ln n)/ln d < ln n / ln(ln n)`. 3. **Cuckoo Hashing & Graph-Orientability Load Thresholds:** - Generalized `(d, k)`-cuckoo hashing (each key picks `d` candidate buckets, bucket capacity `k`). - Full Hakimi / Hall orientability characterization via Mathlib's Hall Marriage Theorem: a valid placement exists IF AND ONLY IF every bucket subset `B` has `|E(B)| ≤ k · |B|`. - Exact 2-table cuckoo pseudoforest characterization, unicyclic cycle feasibility witness, bicyclic 4-vertex/5-edge obstruction witness, load-factor ceiling `n ≤ k · m`, and geometric cycle-collision tail bound for load `< 1/2`. 4. **Online Optimization Minimax — Ski-Rental, Dynkin Secretary & Online Bipartite Matching (KVV):** - Exact deterministic Ski-Rental minimax `2 - 1/B` (matching upper and lower bounds). - Exact Karlin–Manasse–McGeoch–Owicki randomized discrete minimax `R_B = 1 / (1 - (1 - 1/B)^B)` (matching KMMO distribution upper bound + exact Yao dual lower bound across all buying days) and strict separation `R_B < e/(e - 1) < 2 - 1/B`. - Exact Dynkin Secretary permutation-counting formula `P_n(r) = (r/n) · ∑_{j=r}^{n-1} 1/j`, exact unimodality, and exact non-asymptotic `P_n(r*) > 1 / e` for every `n ≥ 2`. - Exact Karp–Vazirani–Vazirani (1990) greedy `1/2` approximation + `1/2` deterministic adversarial barrier vs. randomized `3/4` on `2 × 2` and `1 - (1 - 1/n)^n > 1 - 1/e > 1/2`. 5. **Randomized Graph & Algebraic Verification — Karger Min-Cut, Freivalds & 2-SAT Random Walk:** - Karger's contraction algorithm: minimum-degree bound `2 |E| ≥ n · k`, exact telescoping product `∏_{k=3}^n (1 - 2/k) = 1 / C(n, 2)`, multi-step contraction survival `≥ 1 / C(n, 2)`, exact global min-cut count upper bound `≤ C(n, 2)` (tight on `C_n`), and amplification. - Freivalds' matrix-product verification over arbitrary rings `R`: perfect completeness, exact `≤ 1/2` one-sided soundness error (tight on rank-1 perturbation), and exact `≤ 2^{-k}` error in `k` independent trials. - Papadimitriou / Schöning 2-SAT 1D reflected random walk: exact unsatisfied-clause `≥ 1/2` bias toward any satisfying assignment, exact Bellman hitting-time formula `E[T_x] = x(2n - x) ≤ n²` with uniqueness & biased-walk dominance, and `2 n²`-step Markov success `≥ 1/2` (`2^{-k}` with `k` restarts). Complete formal proofs using only standard Lean axioms (`propext`, `Classical.choice`, `Quot.sound`) with zero warnings. -/ open scoped BigOperators open Finset namespace RandomizedAlgorithmsHashingLoadBalancing /-! =============================================================================== PART 1: UNIVERSAL & `k`-WISE INDEPENDENT HASHING & FKS PERFECT HASHING =============================================================================== -/ /-! ### Pillar 1A: Universal & Strongly Universal Hash Families -/ /-- A finite family of hash functions `H : Fin M → U → Fin m` is **Universal** (Carter–Wegman 1979) if for every two distinct keys `x ≠ y`, at most a `1/m` fraction of hash functions collide on `(x, y)` (stated in exact integer form `m · coll ≤ M`). -/ def IsUniversalFamily {M m : ℕ} {U : Type*} (H : Fin M → U → Fin m) : Prop := ∀ x y : U, x ≠ y → m * (Finset.univ.filter (fun t : Fin M => H t x = H t y)).card ≤ M /-- A finite family `H : Fin M → U → Fin m` is **Strongly Universal (Pairwise Independent)** if for every `x ≠ y` and every `(a, b) : Fin m × Fin m`, exactly `M / m²` functions map `x ↦ a` and `y ↦ b` (exact integer form `m² · count = M`). -/ def IsStronglyUniversalFamily {M m : ℕ} {U : Type*} (H : Fin M → U → Fin m) : Prop := ∀ (x y : U), x ≠ y → ∀ (a b : Fin m), m * m * (Finset.univ.filter (fun t : Fin M => H t x = a ∧ H t y = b)).card = M /-- **Strongly Universal ⇒ Universal (with exact equality `m · coll = M`)**: Summing the pairwise-independent counts over the diagonal `a = b` yields exact `1/m` collision rate! -/ theorem strongly_universal_implies_universal {M m : ℕ} {U : Type*} (hm : 0 < m) {H : Fin M → U → Fin m} (hsu : IsStronglyUniversalFamily H) : IsUniversalFamily H ∧ ∀ x y : U, x ≠ y → m * (Finset.univ.filter (fun t : Fin M => H t x = H t y)).card = M := by have hexact : ∀ x y : U, x ≠ y → m * (Finset.univ.filter (fun t : Fin M => H t x = H t y)).card = M := by intro x y hxy have hpart : (Finset.univ.filter (fun t : Fin M => H t x = H t y)).card = ∑ a : Fin m, (Finset.univ.filter (fun t : Fin M => H t x = a ∧ H t y = a)).card := by have hbiU : Finset.univ.filter (fun t : Fin M => H t x = H t y) = Finset.univ.biUnion (fun a : Fin m => Finset.univ.filter (fun t : Fin M => H t x = a ∧ H t y = a)) := by ext t simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_biUnion] exact ⟨fun h => ⟨H t x, rfl, h.symm⟩, fun ⟨a, h1, h2⟩ => h1.trans h2.symm⟩ rw [hbiU, Finset.card_biUnion] intro a _ b _ hab dsimp only [Function.onFun] rw [Finset.disjoint_left] intro t hta htb simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hta htb exact hab (hta.1.symm.trans htb.1) have hmul : m * (m * (Finset.univ.filter (fun t : Fin M => H t x = H t y)).card) = m * M := by rw [hpart, Finset.mul_sum, Finset.mul_sum] calc ∑ a : Fin m, m * (m * (Finset.univ.filter (fun t : Fin M => H t x = a ∧ H t y = a)).card) = ∑ _a : Fin m, M := by refine Finset.sum_congr rfl (fun a _ => ?_) rw [← mul_assoc] exact hsu x y hxy a a _ = m * M := by simp exact Nat.eq_of_mul_eq_mul_left hm hmul exact ⟨fun x y hxy => le_of_eq (hexact x y hxy), hexact⟩ /-! ### Pillar 1B: Carter–Wegman Affine Hashing & Polynomial `k`-Wise Independent Hashing over `F_p` -/ /-- Carter–Wegman affine hash function `h_{a,b}(x) = a · x + b` over `ZMod p`. -/ def cwAffineHash (p : ℕ) (ab : ZMod p × ZMod p) (x : ZMod p) : ZMod p := ab.1 * x + ab.2 /-- **CARTER–WEGMAN AFFINE PAIRWISE INDEPENDENCE OVER `F_p`**: For prime `p`, every distinct `x ≠ y : ZMod p`, and every target `(u, v) : ZMod p × ZMod p`, there is a **UNIQUE** `(a, b)` with `a · x + b = u` and `a · y + b = v`! -/ theorem carter_wegman_affine_unique_pair {p : ℕ} [Fact p.Prime] {x y : ZMod p} (hxy : x ≠ y) (u v : ZMod p) : ∃! ab : ZMod p × ZMod p, cwAffineHash p ab x = u ∧ cwAffineHash p ab y = v := by have hdiff : x - y ≠ 0 := sub_ne_zero.mpr hxy let a₀ : ZMod p := (u - v) * (x - y)⁻¹ let b₀ : ZMod p := u - a₀ * x refine ⟨(a₀, b₀), ⟨?_, ?_⟩, ?_⟩ · dsimp only [cwAffineHash, b₀]; ring · dsimp only [cwAffineHash, b₀, a₀] have hmul : (u - v) * (x - y)⁻¹ * (y - x) = -(u - v) := by have hneg : y - x = -(x - y) := by ring rw [hneg, mul_neg, mul_assoc, inv_mul_cancel₀ hdiff, mul_one] calc (u - v) * (x - y)⁻¹ * y + (u - (u - v) * (x - y)⁻¹ * x) = (u - v) * (x - y)⁻¹ * (y - x) + u := by ring _ = -(u - v) + u := by rw [hmul] _ = v := by ring · rintro ⟨a, b⟩ ⟨h1, h2⟩ dsimp only [cwAffineHash] at h1 h2 have ha_eq : a * (x - y) = u - v := by linear_combination h1 - h2 have ha : a = a₀ := by dsimp only [a₀] calc a = a * (x - y) * (x - y)⁻¹ := by rw [mul_assoc, mul_inv_cancel₀ hdiff, mul_one] _ = (u - v) * (x - y)⁻¹ := by rw [ha_eq] have hb : b = b₀ := by dsimp only [b₀]; rw [← ha, ← h1]; ring exact Prod.ext ha hb /-- **CARTER–WEGMAN EXACT `1/p` COLLISION COUNT OVER `F_p`**: For prime `p` and `x ≠ y : ZMod p`, EXACTLY `p` out of `p²` pairs `(a, b)` satisfy `a · x + b = a · y + b`! -/ theorem carter_wegman_affine_exact_collision_count {p : ℕ} [Fact p.Prime] {x y : ZMod p} (hxy : x ≠ y) : (Finset.univ.filter (fun ab : ZMod p × ZMod p => cwAffineHash p ab x = cwAffineHash p ab y)).card = p := by have hsub : Finset.univ.filter (fun ab : ZMod p × ZMod p => cwAffineHash p ab x = cwAffineHash p ab y) = Finset.univ.image (fun b : ZMod p => ((0 : ZMod p), b)) := by ext ⟨a, b⟩ simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image, Prod.mk.injEq] dsimp only [cwAffineHash] constructor · intro h have hdiff : a * (x - y) = 0 := by linear_combination h have ha0 : a = 0 := (mul_eq_zero.mp hdiff).resolve_right (sub_ne_zero.mpr hxy) exact ⟨b, ha0.symm, rfl⟩ · rintro ⟨b', rfl, rfl⟩; simp rw [hsub, Finset.card_image_of_injective _ (fun _ _ h => (Prod.mk.inj h).2), Finset.card_univ, ZMod.card] /-- **CARTER–WEGMAN (1979) MOD-`m` UNIVERSAL HASHING THEOREM (`((a · x + b) mod p) mod m`)**: For prime `p`, `1 ≤ m ≤ p`, and `x ≠ y : ZMod p`, ranging over the `p · (p - 1)` nonzero-slope affine maps `(a, b)` with `a ≠ 0`: `m · #{(a, b) | a ≠ 0 ∧ (a · x + b).val % m = (a · y + b).val % m} ≤ p · (p - 1)`! -/ theorem carter_wegman_mod_m_universal {p m : ℕ} [Fact p.Prime] (_hm : 0 < m) {x y : ZMod p} (hxy : x ≠ y) : m * (Finset.univ.filter (fun ab : ZMod p × ZMod p => ab.1 ≠ 0 ∧ (cwAffineHash p ab x).val % m = (cwAffineHash p ab y).val % m)).card ≤ p * (p - 1) := by -- Bijection `(a, b) ↦ (a*x + b, a*y + b)` from `{a ≠ 0 ∧ mod-m collide}` to `{u ≠ v ∧ u.val % m = v.val % m}`! set S_ab := Finset.univ.filter (fun ab : ZMod p × ZMod p => ab.1 ≠ 0 ∧ (cwAffineHash p ab x).val % m = (cwAffineHash p ab y).val % m) set S_uv := Finset.univ.filter (fun uv : ZMod p × ZMod p => uv.1 ≠ uv.2 ∧ uv.1.val % m = uv.2.val % m) have hcard_eq : S_ab.card = S_uv.card := by refine Finset.card_bij (fun ab _ => (cwAffineHash p ab x, cwAffineHash p ab y)) ?_ ?_ ?_ · intro ab hab have h := (Finset.mem_filter.mp hab).2 refine Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_, h.2⟩ intro heq have h0 : ab.1 * (x - y) = 0 := by dsimp only [cwAffineHash] at heq; linear_combination heq exact h.1 ((mul_eq_zero.mp h0).resolve_right (sub_ne_zero.mpr hxy)) · intro ab₁ _ ab₂ _ heq have h1 : cwAffineHash p ab₁ x = cwAffineHash p ab₂ x := (Prod.mk.inj heq).1 have h2 : cwAffineHash p ab₁ y = cwAffineHash p ab₂ y := (Prod.mk.inj heq).2 obtain ⟨_, _, huniq⟩ := carter_wegman_affine_unique_pair hxy (cwAffineHash p ab₂ x) (cwAffineHash p ab₂ y) exact (huniq ab₁ ⟨h1, h2⟩).trans (huniq ab₂ ⟨rfl, rfl⟩).symm · intro uv huv have h := (Finset.mem_filter.mp huv).2 obtain ⟨ab, ⟨habx, haby⟩, _⟩ := carter_wegman_affine_unique_pair hxy uv.1 uv.2 refine ⟨ab, Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_, by rw [habx, haby]; exact h.2⟩, Prod.ext habx haby⟩ intro ha0 apply h.1 rw [← habx, ← haby] simp [cwAffineHash, ha0] -- Bound `S_uv.card` by summing over `u : ZMod p`: for each `u`, at most `(p - 1) / m` values `v ≠ u` -- share `v.val % m = u.val % m`! have hfiber : ∀ u : ZMod p, m * (Finset.univ.filter (fun v : ZMod p => u ≠ v ∧ u.val % m = v.val % m)).card ≤ p - 1 := by intro u set F := Finset.univ.filter (fun v : ZMod p => u ≠ v ∧ u.val % m = v.val % m) -- Map `v ∈ F` injectively to `v.val / m` excluding `u.val / m`! -- Even cleaner: `insert u F` has `|F| + 1` elements in `{0, ..., p - 1}` all congruent to `r = u.val % m`, -- so `m * |F| + r ≤ max_{v ∈ insert u F} v.val ≤ p - 1`! set G := insert u F have hu_notmem : u ∉ F := by simp [F] have hG_card : G.card = F.card + 1 := Finset.card_insert_of_notMem hu_notmem -- Map `v ∈ G` injectively to `q(v) = v.val / m`! let Q := G.image (fun v : ZMod p => v.val / m) have hQ_inj : Set.InjOn (fun v : ZMod p => v.val / m) (G : Set (ZMod p)) := by intro v₁ hv₁ v₂ hv₂ hq have hmod1 : u.val % m = v₁.val % m := by rcases Finset.mem_insert.mp hv₁ with rfl | h1 · rfl · exact (Finset.mem_filter.mp h1).2.2 have hmod2 : u.val % m = v₂.val % m := by rcases Finset.mem_insert.mp hv₂ with rfl | h2 · rfl · exact (Finset.mem_filter.mp h2).2.2 have hval : v₁.val = v₂.val := by dsimp only at hq have d1 := Nat.div_add_mod v₁.val m have d2 := Nat.div_add_mod v₂.val m rw [← d1, ← d2, hq, ← hmod1, hmod2] exact ZMod.val_injective p hval have hQ_card : Q.card = F.card + 1 := by rw [Finset.card_image_of_injOn hQ_inj, hG_card] have hQ_sub : Q ⊆ Finset.range ((p - 1) / m + 1) := by intro q hq obtain ⟨v, _, rfl⟩ := Finset.mem_image.mp hq rw [Finset.mem_range] have hvlt : v.val < p := ZMod.val_lt v have hvle : v.val ≤ p - 1 := by omega have hdiv := Nat.div_le_div_right (c := m) hvle omega have hle := Finset.card_le_card hQ_sub rw [hQ_card, Finset.card_range] at hle have hF_le : F.card ≤ (p - 1) / m := by omega calc m * F.card ≤ m * ((p - 1) / m) := Nat.mul_le_mul_left m hF_le _ ≤ p - 1 := Nat.mul_div_le (p - 1) m have hS_uv_sum : S_uv.card = ∑ u : ZMod p, (Finset.univ.filter (fun v : ZMod p => u ≠ v ∧ u.val % m = v.val % m)).card := by have hpart : S_uv = Finset.univ.biUnion (fun u : ZMod p => (Finset.univ.filter (fun v : ZMod p => u ≠ v ∧ u.val % m = v.val % m)).image (fun v => (u, v))) := by ext ⟨u, v⟩ simp [S_uv] rw [hpart, Finset.card_biUnion] · refine Finset.sum_congr rfl (fun u _ => ?_) exact Finset.card_image_of_injective _ (fun _ _ h => (Prod.mk.inj h).2) · intro u₁ _ u₂ _ hne dsimp only [Function.onFun] rw [Finset.disjoint_left] intro uv h1 h2 simp only [Finset.mem_image] at h1 h2 rcases h1 with ⟨_, _, rfl⟩ rcases h2 with ⟨_, _, heq⟩ exact hne (Prod.mk.inj heq).1.symm rw [hcard_eq, hS_uv_sum, Finset.mul_sum] calc ∑ u : ZMod p, m * (Finset.univ.filter (fun v : ZMod p => u ≠ v ∧ u.val % m = v.val % m)).card ≤ ∑ _u : ZMod p, (p - 1) := Finset.sum_le_sum (fun u _ => hfiber u) _ = p * (p - 1) := by simp [ZMod.card] /-- Polynomial `k`-wise independent hash function `h_c(x) = ∑_{j < k} c_j · x^j` over `F_p`. -/ def polyHash {p k : ℕ} (c : Fin k → ZMod p) (x : ZMod p) : ZMod p := ∑ j : Fin k, c j * x ^ j.1 /-- **EXACT POLYNOMIAL `k`-WISE INDEPENDENCE OVER `F_p`**: For prime `p`, ANY `k` distinct keys `xs : Fin k → ZMod p` (`Injective xs`), and ANY target `k`-tuple `ys : Fin k → ZMod p`, there is a **UNIQUE** degree-`< k` polynomial `c : Fin k → ZMod p` with `polyHash c (xs i) = ys i` for ALL `i : Fin k` (exact collision/joint probability `p^{-k}`)! -/ theorem poly_k_wise_independent_unique {p k : ℕ} [Fact p.Prime] (xs : Fin k → ZMod p) (hxs : Function.Injective xs) (ys : Fin k → ZMod p) : ∃! c : Fin k → ZMod p, ∀ i : Fin k, polyHash c (xs i) = ys i := by -- Evaluation map `ev : (Fin k → ZMod p) → (Fin k → ZMod p)` is injective by `Polynomial.card_roots'`, -- hence bijective on the finite type `Fin k → ZMod p`! let ev : (Fin k → ZMod p) → (Fin k → ZMod p) := fun c i => polyHash c (xs i) have hev_inj : Function.Injective ev := by intro c₁ c₂ heq let f : Polynomial (ZMod p) := ∑ j : Fin k, Polynomial.C (c₁ j - c₂ j) * Polynomial.X ^ j.1 have hf_eval : ∀ x : ZMod p, f.eval x = polyHash c₁ x - polyHash c₂ x := by intro x dsimp only [f, polyHash] rw [Polynomial.eval_finsetSum, ← Finset.sum_sub_distrib] refine Finset.sum_congr rfl (fun j _ => ?_) simp [sub_mul] have hf_zero : f = 0 := by by_contra hne have hdeg : f.natDegree < k := by have hlt : f.degree < (k : WithBot ℕ) := by refine lt_of_le_of_lt (Polynomial.degree_sum_le _ _) ?_ exact (Finset.sup_lt_iff (WithBot.bot_lt_coe k)).mpr (fun j _ => by calc (Polynomial.C (c₁ j - c₂ j) * Polynomial.X ^ j.1).degree ≤ (Polynomial.C (c₁ j - c₂ j)).degree + (Polynomial.X ^ j.1).degree := Polynomial.degree_mul_le _ _ _ ≤ 0 + (j.1 : WithBot ℕ) := by refine add_le_add Polynomial.degree_C_le ?_ rw [Polynomial.degree_X_pow] _ = (j.1 : WithBot ℕ) := zero_add _ _ < (k : WithBot ℕ) := WithBot.coe_lt_coe.mpr j.2) exact (Polynomial.natDegree_lt_iff_degree_lt hne).mpr hlt have hroots_sub : (Finset.univ.image xs).val ⊆ f.roots := by intro a ha have ha' : a ∈ Finset.univ.image xs := ha obtain ⟨i, _, rfl⟩ := Finset.mem_image.mp ha' rw [Polynomial.mem_roots hne, Polynomial.IsRoot.def, hf_eval (xs i)] have hi := congrFun heq i dsimp only [ev] at hi exact sub_eq_zero.mpr hi have hcard_le : (Finset.univ.image xs).card ≤ f.roots.card := Multiset.card_le_card (Multiset.le_iff_subset (Finset.univ.image xs).nodup |>.mpr hroots_sub) rw [Finset.card_image_of_injective _ hxs, Finset.card_univ, Fintype.card_fin] at hcard_le have hbound := Polynomial.card_roots' f omega funext j have hcoeff : f.coeff j.1 = 0 := by rw [hf_zero]; simp have hcoeff_eq : f.coeff j.1 = c₁ j - c₂ j := by dsimp only [f] rw [Polynomial.finsetSum_coeff] rw [Finset.sum_eq_single j] · rw [Polynomial.coeff_C_mul, Polynomial.coeff_X_pow, if_pos rfl, mul_one] · intro j' _ hj' have hval : j'.1 ≠ j.1 := fun hv => hj' (Fin.ext hv) rw [Polynomial.coeff_C_mul, Polynomial.coeff_X_pow, if_neg hval.symm, mul_zero] · intro h; exact absurd (Finset.mem_univ j) h rw [hcoeff_eq] at hcoeff exact sub_eq_zero.mp hcoeff have hev_bij : Function.Bijective ev := Finite.injective_iff_bijective.mp hev_inj obtain ⟨c, hc⟩ := hev_bij.2 ys refine ⟨c, fun i => congrFun hc i, fun c' hc' => hev_inj (funext hc' |>.trans hc.symm)⟩ /-- Exact card count of polynomial `k`-wise independent hash functions matching any `k`-tuple = `1`. -/ theorem poly_k_wise_independent_card_one {p k : ℕ} [Fact p.Prime] (xs : Fin k → ZMod p) (hxs : Function.Injective xs) (ys : Fin k → ZMod p) : (Finset.univ.filter (fun c : Fin k → ZMod p => ∀ i : Fin k, polyHash c (xs i) = ys i)).card = 1 := by obtain ⟨c₀, hc₀, huniq⟩ := poly_k_wise_independent_unique xs hxs ys have hfilt : Finset.univ.filter (fun c : Fin k → ZMod p => ∀ i : Fin k, polyHash c (xs i) = ys i) = {c₀} := by ext c simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_singleton] exact ⟨fun h => huniq c h, fun h => h ▸ hc₀⟩ rw [hfilt, Finset.card_singleton] /-! ### Pillar 1C: Collision Statistics & Fredman–Komlós–Szemerédi (FKS) Static Perfect Hashing -/ /-- Bucket load `c_j(S, h) = |{x ∈ S | h(x) = j}|`. -/ def bucketLoad {U : Type*} [DecidableEq U] {m : ℕ} (S : Finset U) (h : U → Fin m) (j : Fin m) : ℕ := (S.filter (fun x => h x = j)).card /-- Total unordered colliding pairs `∑_j C(c_j, 2)` of key set `S` under hash `h`. -/ def collisionPairs {U : Type*} [DecidableEq U] {m : ℕ} (S : Finset U) (h : U → Fin m) : ℕ := ∑ j : Fin m, (bucketLoad S h j).choose 2 /-- Sum of bucket loads equals `|S|`. -/ lemma sum_bucketLoad_eq_card {U : Type*} [DecidableEq U] {m : ℕ} (S : Finset U) (h : U → Fin m) : ∑ j : Fin m, bucketLoad S h j = S.card := by dsimp only [bucketLoad] rw [← Finset.card_biUnion] · congr 1; ext x; simp · intro j₁ _ j₂ _ hne dsimp only [Function.onFun] rw [Finset.disjoint_left] intro x h1 h2 simp only [Finset.mem_filter] at h1 h2 exact hne (h1.2.symm.trans h2.2) private lemma nat_mul_self_sub_eq (c : ℕ) : c * c - c = c * (c - 1) := by cases c with | zero => simp | succ c' => simp only [Nat.add_sub_cancel] have h1 : (c' + 1) * (c' + 1) = (c' + 1) * c' + (c' + 1) := by ring rw [h1, Nat.add_sub_cancel] /-- Exact identity: `2 · collisionPairs S h = |{(x, y) ∈ S × S | x ≠ y ∧ h x = h y}|`. -/ lemma two_mul_collisionPairs_eq_offDiag {U : Type*} [DecidableEq U] {m : ℕ} (S : Finset U) (h : U → Fin m) : 2 * collisionPairs S h = (S.offDiag.filter (fun p => h p.1 = h p.2)).card := by dsimp only [collisionPairs, bucketLoad] rw [Finset.mul_sum] have hpart : S.offDiag.filter (fun p => h p.1 = h p.2) = Finset.univ.biUnion (fun j : Fin m => (S.filter (fun x => h x = j)).offDiag) := by ext ⟨x, y⟩ simp only [Finset.mem_filter, Finset.mem_offDiag, Finset.mem_biUnion, Finset.mem_univ, true_and] constructor · rintro ⟨⟨hx, hy, hne⟩, hxy⟩ exact ⟨h x, ⟨hx, rfl⟩, ⟨hy, hxy.symm⟩, hne⟩ · rintro ⟨j, ⟨hx, hxj⟩, ⟨hy, hyj⟩, hne⟩ exact ⟨⟨hx, hy, hne⟩, hxj.trans hyj.symm⟩ rw [hpart, Finset.card_biUnion] · refine Finset.sum_congr rfl (fun j _ => ?_) rw [Finset.offDiag_card, Nat.choose_two_right] set c := (S.filter (fun x => h x = j)).card have hmul : 2 * (c * (c - 1) / 2) = c * (c - 1) := by have heven : 2 ∣ c * (c - 1) := Nat.even_mul_pred_self c |>.two_dvd exact Nat.mul_div_cancel' heven rw [hmul, nat_mul_self_sub_eq] · intro j₁ _ j₂ _ hne dsimp only [Function.onFun] rw [Finset.disjoint_left] intro p h1 h2 simp only [Finset.mem_offDiag, Finset.mem_filter] at h1 h2 exact hne (h1.1.2.symm.trans h2.1.2) /-- **EXACT QUADRATIC-TABLE SPACE IDENTITY**: `∑_j c_j² = |S| + 2 · collisionPairs S h`! -/ theorem sum_sq_bucketLoad_eq_card_add_two_collisions {U : Type*} [DecidableEq U] {m : ℕ} (S : Finset U) (h : U → Fin m) : ∑ j : Fin m, (bucketLoad S h j) ^ 2 = S.card + 2 * collisionPairs S h := by have h1 : ∀ c : ℕ, c ^ 2 = c + 2 * c.choose 2 := by intro c rw [Nat.choose_two_right] have heven : 2 ∣ c * (c - 1) := Nat.even_mul_pred_self c |>.two_dvd have hmul : 2 * (c * (c - 1) / 2) = c * (c - 1) := Nat.mul_div_cancel' heven rw [hmul] cases c with | zero => simp | succ c' => simp only [Nat.add_sub_cancel]; ring calc ∑ j : Fin m, (bucketLoad S h j) ^ 2 = ∑ j : Fin m, (bucketLoad S h j + 2 * (bucketLoad S h j).choose 2) := Finset.sum_congr rfl (fun j _ => h1 _) _ = (∑ j : Fin m, bucketLoad S h j) + 2 * ∑ j : Fin m, (bucketLoad S h j).choose 2 := by rw [Finset.sum_add_distrib, ← Finset.mul_sum] _ = S.card + 2 * collisionPairs S h := by rw [sum_bucketLoad_eq_card]; rfl /-- Zero colliding pairs is EQUIVALENT to `h` being strictly injective on `S`! -/ theorem collisionPairs_eq_zero_iff_injOn {U : Type*} [DecidableEq U] {m : ℕ} (S : Finset U) (h : U → Fin m) : collisionPairs S h = 0 ↔ Set.InjOn h (S : Set U) := by have h2 := two_mul_collisionPairs_eq_offDiag S h constructor · intro h0 x hx y hy hxy by_contra hne have hmem : (x, y) ∈ S.offDiag.filter (fun p => h p.1 = h p.2) := Finset.mem_filter.mpr ⟨Finset.mem_offDiag.mpr ⟨hx, hy, hne⟩, hxy⟩ have hpos : 0 < (S.offDiag.filter (fun p => h p.1 = h p.2)).card := Finset.card_pos.mpr ⟨_, hmem⟩ omega · intro hinj have hemp : S.offDiag.filter (fun p => h p.1 = h p.2) = ∅ := by rw [Finset.filter_eq_empty_iff] intro p hp hpeq have hmem := Finset.mem_offDiag.mp hp exact hmem.2.2 (hinj hmem.1 hmem.2.1 hpeq) rw [hemp, Finset.card_empty] at h2 omega /-- **EXACT UNIVERSAL HASHING EXPECTED COLLISION BOUND**: For EVERY universal hash family `H : Fin M → U → Fin m` and EVERY finite key set `S` (`n = S.card`): `m · ∑_{t : Fin M} (2 · collisionPairs S (H t)) ≤ M · n · (n - 1)`! -/ theorem universal_expected_collisions_bound {M m : ℕ} {U : Type*} [DecidableEq U] {H : Fin M → U → Fin m} (huniv : IsUniversalFamily H) (S : Finset U) : m * ∑ t : Fin M, (2 * collisionPairs S (H t)) ≤ M * (S.card * (S.card - 1)) := by simp_rw [two_mul_collisionPairs_eq_offDiag] have hswap : ∑ t : Fin M, (S.offDiag.filter (fun p => H t p.1 = H t p.2)).card = ∑ p ∈ S.offDiag, (Finset.univ.filter (fun t : Fin M => H t p.1 = H t p.2)).card := by simp_rw [Finset.card_filter] exact Finset.sum_comm rw [hswap, Finset.mul_sum] calc ∑ p ∈ S.offDiag, m * (Finset.univ.filter (fun t : Fin M => H t p.1 = H t p.2)).card ≤ ∑ _p ∈ S.offDiag, M := by refine Finset.sum_le_sum (fun p hp => ?_) exact huniv p.1 p.2 (Finset.mem_offDiag.mp hp).2.2 _ = S.offDiag.card * M := by simp _ = M * (S.card * (S.card - 1)) := by rw [Finset.offDiag_card, mul_comm, nat_mul_self_sub_eq] /-- **QUADRATIC-SPACE ZERO-COLLISION PERFECT HASH EXISTENCE (`m = n²`)**: For EVERY universal hash family `H : Fin M → U → Fin (n²)` (`M ≥ 1`) and EVERY key set `S` of size `n`: there exists `t : Fin M` with **ZERO COLLISIONS** (`Set.InjOn (H t) S`)! -/ theorem universal_quadratic_table_zero_collision {M n : ℕ} {U : Type*} [DecidableEq U] (hM : 0 < M) {H : Fin M → U → Fin (n * n)} (huniv : IsUniversalFamily H) (S : Finset U) (hcard : S.card = n) : ∃ t : Fin M, Set.InjOn (H t) (S : Set U) := by rcases eq_or_lt_of_le (Nat.zero_le n) with rfl | hn · have hS0 : S = ∅ := Finset.card_eq_zero.mp hcard exact ⟨⟨0, hM⟩, by rw [hS0]; simp⟩ · have hbound := universal_expected_collisions_bound huniv S rw [hcard] at hbound -- Since `n * (n - 1) < n * n`, we get `∑ t, 2 * collisionPairs < M`, so some `t` has `collisionPairs = 0`! have hlt_n : n * (n - 1) < n * n := Nat.mul_lt_mul_of_pos_left (Nat.sub_lt hn zero_lt_one) hn have hsum_lt : ∑ t : Fin M, (2 * collisionPairs S (H t)) < M := by by_contra hge push Not at hge have h1 : n * n * M ≤ n * n * ∑ t : Fin M, (2 * collisionPairs S (H t)) := Nat.mul_le_mul_left (n * n) hge have h2 : M * (n * (n - 1)) < n * n * M := by rw [mul_comm (n * n) M] exact Nat.mul_lt_mul_of_pos_left hlt_n hM omega have hex : ∃ t : Fin M, 2 * collisionPairs S (H t) = 0 := by by_contra hall push Not at hall have hge1 : ∀ t : Fin M, 1 ≤ 2 * collisionPairs S (H t) := fun t => Nat.pos_of_ne_zero (hall t) have hsum_ge : M ≤ ∑ t : Fin M, (2 * collisionPairs S (H t)) := by calc M = ∑ _t : Fin M, 1 := by simp _ ≤ ∑ t : Fin M, (2 * collisionPairs S (H t)) := Finset.sum_le_sum (fun t _ => hge1 t) omega obtain ⟨t, ht⟩ := hex refine ⟨t, (collisionPairs_eq_zero_iff_injOn S (H t)).mp (by omega)⟩ /-- **FKS LEVEL-1 LINEAR SPACE BOUND (`∑ c_j² < 2 n`)**: For EVERY universal hash family `H : Fin M → U → Fin n` (`M ≥ 1`, `n = S.card ≥ 1`), there exists a level-1 hash function `t : Fin M` with total second-level quadratic space `∑ j : Fin n, (bucketLoad S (H t) j)² ≤ 2 * n - 1` (strictly `< 2 * n`)! -/ theorem fks_level1_linear_space_exists {M n : ℕ} {U : Type*} [DecidableEq U] (hM : 0 < M) (hn : 0 < n) {H : Fin M → U → Fin n} (huniv : IsUniversalFamily H) (S : Finset U) (hcard : S.card = n) : ∃ t : Fin M, ∑ j : Fin n, (bucketLoad S (H t) j) ^ 2 < 2 * n := by have hbound := universal_expected_collisions_bound huniv S rw [hcard] at hbound -- `n * ∑ t, 2 * collisionPairs ≤ M * (n * (n - 1))` ⇒ `∑ t, 2 * collisionPairs ≤ M * (n - 1) < M * n`! have hsum_le : ∑ t : Fin M, (2 * collisionPairs S (H t)) ≤ M * (n - 1) := by have hre : M * (n * (n - 1)) = n * (M * (n - 1)) := by ring rw [hre] at hbound exact Nat.le_of_mul_le_mul_left hbound hn have hsum_lt : ∑ t : Fin M, (2 * collisionPairs S (H t)) < M * n := by have hlt : M * (n - 1) < M * n := Nat.mul_lt_mul_of_pos_left (Nat.sub_lt hn zero_lt_one) hM omega have hex : ∃ t : Fin M, 2 * collisionPairs S (H t) < n := by by_contra hall push Not at hall have hge : M * n ≤ ∑ t : Fin M, (2 * collisionPairs S (H t)) := by calc M * n = ∑ _t : Fin M, n := by simp _ ≤ ∑ t : Fin M, (2 * collisionPairs S (H t)) := Finset.sum_le_sum (fun t _ => hall t) omega obtain ⟨t, ht⟩ := hex refine ⟨t, ?_⟩ rw [sum_sq_bucketLoad_eq_card_add_two_collisions, hcard] omega /-- **FREDMAN–KOMLÓS–SZEMERÉDI (JACM 1984) TWO-LEVEL STATIC PERFECT HASHING THEOREM**: Given ANY key set `S` of size `n ≥ 1`, a level-1 universal family `H₁ : Fin M₁ → U → Fin n` (`M₁ ≥ 1`), and for every bucket size `c : ℕ` a level-2 universal family `H₂ c : Fin (M₂ c) → U → Fin (c * c)` (`M₂ c ≥ 1`): There exist a level-1 hash `h₁ = H₁ t₁` AND per-bucket level-2 hashes `h₂ j = H₂ (c_j) (t₂ j)` such that: 1. **Zero worst-case lookup collisions**: every two distinct keys `x, y ∈ S` have distinct 2-level addresses `(h₁ x, h₂ (h₁ x) x) ≠ (h₁ y, h₂ (h₁ y) y)` (exact `O(1)` worst-case lookup!). 2. **Exact Linear Total Space**: `∑ j : Fin n, (bucketLoad S h₁ j)² < 2 * n` (at most `2 n - 1` slots)! -/ theorem fks_two_level_perfect_hashing_exists {M₁ n : ℕ} {U : Type*} [DecidableEq U] (hM₁ : 0 < M₁) (hn : 0 < n) (H₁ : Fin M₁ → U → Fin n) (hU₁ : IsUniversalFamily H₁) (M₂ : ℕ → ℕ) (hM₂ : ∀ c, 0 < M₂ c) (H₂ : ∀ c : ℕ, Fin (M₂ c) → U → Fin (c * c)) (hU₂ : ∀ c, IsUniversalFamily (H₂ c)) (S : Finset U) (hcard : S.card = n) : ∃ (t₁ : Fin M₁) (t₂ : ∀ j : Fin n, Fin (M₂ (bucketLoad S (H₁ t₁) j))), (∑ j : Fin n, (bucketLoad S (H₁ t₁) j) ^ 2 < 2 * n) ∧ (∀ x ∈ S, ∀ y ∈ S, x ≠ y → H₁ t₁ x = H₁ t₁ y → H₂ (bucketLoad S (H₁ t₁) (H₁ t₁ x)) (t₂ (H₁ t₁ x)) x ≠ H₂ (bucketLoad S (H₁ t₁) (H₁ t₁ x)) (t₂ (H₁ t₁ x)) y) := by obtain ⟨t₁, hspace⟩ := fks_level1_linear_space_exists hM₁ hn hU₁ S hcard have hbuck : ∀ j : Fin n, ∃ tj : Fin (M₂ (bucketLoad S (H₁ t₁) j)), Set.InjOn (H₂ (bucketLoad S (H₁ t₁) j) tj) ((S.filter (fun x => H₁ t₁ x = j)) : Set U) := by intro j exact universal_quadratic_table_zero_collision (hM₂ _) (hU₂ _) (S.filter (fun x => H₁ t₁ x = j)) rfl choose t₂ ht₂ using hbuck refine ⟨t₁, t₂, hspace, fun x hx y hy hxy heq1 heq2 => ?_⟩ have hx_mem : x ∈ S.filter (fun z => H₁ t₁ z = H₁ t₁ x) := Finset.mem_filter.mpr ⟨hx, rfl⟩ have hy_mem : y ∈ S.filter (fun z => H₁ t₁ z = H₁ t₁ x) := Finset.mem_filter.mpr ⟨hy, heq1.symm⟩ exact hxy (ht₂ (H₁ t₁ x) hx_mem hy_mem heq2) /-! =============================================================================== PART 2: BALLS-INTO-BINS & POWER-OF-TWO-CHOICES LOAD BALANCING (BIRTHDAY PARADOX, 1-CHOICE STIRLING TAIL & AZAR–BRODER–KARLIN–UPFAL) =============================================================================== -/ /-! ### Pillar 2A: Exact Birthday Collision Counting & Sharp Thresholds -/ /-- Exact count of collision-free assignments of `k` balls into `m` bins equals the falling factorial `m.descFactorial k`! -/ theorem card_injective_fin_fun (k m : ℕ) : (Finset.univ.filter (fun f : Fin k → Fin m => Function.Injective f)).card = m.descFactorial k := by have hbij : (Finset.univ.filter (fun f : Fin k → Fin m => Function.Injective f)).card = (Finset.univ : Finset (Fin k ↪ Fin m)).card := by refine Finset.card_bij (fun f hf => ⟨f, (Finset.mem_filter.mp hf).2⟩) ?_ ?_ ?_ · intro _ _; exact Finset.mem_univ _ · intro f₁ _ f₂ _ heq; exact congrArg DFunLike.coe heq · intro e _ exact ⟨e, Finset.mem_filter.mpr ⟨Finset.mem_univ _, e.injective⟩, by ext x; rfl⟩ rw [hbij, Finset.card_univ, Fintype.card_embedding_eq, Fintype.card_fin, Fintype.card_fin] /-- Exact product formula for the birthday no-collision probability: `(m.descFactorial k) / m^k = ∏_{i = 0}^{k - 1} (1 - i / m)`. -/ theorem birthday_no_collision_product (k m : ℕ) (hm : 0 < m) (hkm : k ≤ m) : ((m.descFactorial k : ℝ) / (m : ℝ) ^ k) = ∏ i ∈ Finset.range k, (1 - (i : ℝ) / (m : ℝ)) := by have hmpos : (0 : ℝ) < (m : ℝ) := Nat.cast_pos.mpr hm induction k with | zero => simp | succ k ih => have hk_le : k ≤ m := by omega rw [Nat.descFactorial_succ, Finset.prod_range_succ, pow_succ] push_cast rw [Nat.cast_sub hk_le] have hprev := ih hk_le have hstep : ((m : ℝ) - (k : ℝ)) / (m : ℝ) = 1 - (k : ℝ) / (m : ℝ) := by field_simp [ne_of_gt hmpos] calc (((m : ℝ) - (k : ℝ)) * (m.descFactorial k : ℝ)) / ((m : ℝ) ^ k * (m : ℝ)) = ((m.descFactorial k : ℝ) / (m : ℝ) ^ k) * (((m : ℝ) - (k : ℝ)) / (m : ℝ)) := by ring _ = (∏ i ∈ Finset.range k, (1 - (i : ℝ) / (m : ℝ))) * (1 - (k : ℝ) / (m : ℝ)) := by rw [hprev, hstep] private lemma sum_range_nat_cast_real (k : ℕ) : ∑ i ∈ Finset.range k, (i : ℝ) = (k : ℝ) * ((k : ℝ) - 1) / 2 := by induction k with | zero => simp | succ k ih => rw [Finset.sum_range_succ, ih] push_cast; ring /-- **EXACT EXPONENTIAL UPPER BOUND ON BIRTHDAY NO-COLLISION PROBABILITY**: For `0 < m` and `k ≤ m`: `(m.descFactorial k) / m^k ≤ exp(- k (k - 1) / (2 m))`! -/ theorem birthday_no_collision_le_exp (k m : ℕ) (hm : 0 < m) (hkm : k ≤ m) : (m.descFactorial k : ℝ) / (m : ℝ) ^ k ≤ Real.exp (- ((k : ℝ) * ((k : ℝ) - 1)) / (2 * (m : ℝ))) := by rw [birthday_no_collision_product k m hm hkm] have hmpos : (0 : ℝ) < (m : ℝ) := Nat.cast_pos.mpr hm have hprod : ∏ i ∈ Finset.range k, (1 - (i : ℝ) / (m : ℝ)) ≤ ∏ i ∈ Finset.range k, Real.exp (- ((i : ℝ) / (m : ℝ))) := by refine Finset.prod_le_prod (fun i hi => ?_) (fun i _ => ?_) · have him : (i : ℝ) ≤ (m : ℝ) := by have : i < k := Finset.mem_range.mp hi exact_mod_cast (by omega : i ≤ m) have hdiv : (i : ℝ) / (m : ℝ) ≤ 1 := (div_le_one hmpos).mpr him linarith · have hexp := Real.add_one_le_exp (- ((i : ℝ) / (m : ℝ))) linarith have hsum_exp : ∏ i ∈ Finset.range k, Real.exp (- ((i : ℝ) / (m : ℝ))) = Real.exp (- ((k : ℝ) * ((k : ℝ) - 1)) / (2 * (m : ℝ))) := by rw [← Real.exp_sum] congr 1 simp_rw [neg_div, Finset.sum_neg_distrib, ← Finset.sum_div, sum_range_nat_cast_real] ring linarith /-- **EXACT UNION-BOUND UPPER BOUND ON BIRTHDAY COLLISION PROBABILITY**: For `0 < m` and `k ≤ m`: `1 - (m.descFactorial k) / m^k ≤ k (k - 1) / (2 m)`! -/ theorem birthday_collision_le_choose2_div (k m : ℕ) (hm : 0 < m) (hkm : k ≤ m) : 1 - (m.descFactorial k : ℝ) / (m : ℝ) ^ k ≤ ((k : ℝ) * ((k : ℝ) - 1)) / (2 * (m : ℝ)) := by rw [birthday_no_collision_product k m hm hkm] have hmpos : (0 : ℝ) < (m : ℝ) := Nat.cast_pos.mpr hm have hbern : ∀ n : ℕ, n ≤ m → 1 - ∑ i ∈ Finset.range n, (i : ℝ) / (m : ℝ) ≤ ∏ i ∈ Finset.range n, (1 - (i : ℝ) / (m : ℝ)) := by intro n induction n with | zero => intro _; simp | succ n ih => intro hnm have hprev := ih (by omega) rw [Finset.sum_range_succ, Finset.prod_range_succ] have ha_nn : 0 ≤ (n : ℝ) / (m : ℝ) := div_nonneg (Nat.cast_nonneg n) (le_of_lt hmpos) have ha_le1 : (n : ℝ) / (m : ℝ) ≤ 1 := (div_le_one hmpos).mpr (by exact_mod_cast (by omega : n ≤ m)) have hS_nn : 0 ≤ ∑ i ∈ Finset.range n, (i : ℝ) / (m : ℝ) := Finset.sum_nonneg (fun i _ => div_nonneg (Nat.cast_nonneg i) (le_of_lt hmpos)) have h1 : 1 - (∑ i ∈ Finset.range n, (i : ℝ) / (m : ℝ) + (n : ℝ) / (m : ℝ)) ≤ (1 - ∑ i ∈ Finset.range n, (i : ℝ) / (m : ℝ)) * (1 - (n : ℝ) / (m : ℝ)) := by nlinarith have h2 : (1 - ∑ i ∈ Finset.range n, (i : ℝ) / (m : ℝ)) * (1 - (n : ℝ) / (m : ℝ)) ≤ (∏ i ∈ Finset.range n, (1 - (i : ℝ) / (m : ℝ))) * (1 - (n : ℝ) / (m : ℝ)) := mul_le_mul_of_nonneg_right hprev (by linarith) linarith have hlow := hbern k hkm have hsum : ∑ i ∈ Finset.range k, (i : ℝ) / (m : ℝ) = ((k : ℝ) * ((k : ℝ) - 1)) / (2 * (m : ℝ)) := by rw [← Finset.sum_div, sum_range_nat_cast_real]; ring linarith /-- **EXACT 23-PERSON BIRTHDAY PARADOX (`P(collision among 23 in 365 days) > 1 / 2`)**: Non-asymptotic proof via `253 / 365 > 0.6931471808 > ln 2`! -/ theorem birthday_paradox_23_of_365_gt_half : (1 / 2 : ℝ) < 1 - ((Nat.descFactorial 365 23 : ℝ) / (365 : ℝ) ^ 23) := by have hle := birthday_no_collision_le_exp 23 365 (by omega) (by omega) have hlog : Real.log 2 < (23 : ℝ) * ((23 : ℝ) - 1) / (2 * (365 : ℝ)) := by have hlt := Real.log_two_lt_d9 linarith have hexp : Real.exp (- ((23 : ℝ) * ((23 : ℝ) - 1)) / (2 * (365 : ℝ))) < 1 / 2 := by have h1 : - ((23 : ℝ) * ((23 : ℝ) - 1)) / (2 * (365 : ℝ)) < - Real.log 2 := by linarith have h2 : Real.exp (- ((23 : ℝ) * ((23 : ℝ) - 1)) / (2 * (365 : ℝ))) < Real.exp (- Real.log 2) := Real.exp_lt_exp.mpr h1 have h3 : Real.exp (- Real.log 2) = 1 / 2 := by rw [Real.exp_neg, Real.exp_log (by norm_num : (0 : ℝ) < 2)]; norm_num linarith linarith /-! ### Pillar 2B: 1-Choice Max-Load Stirling Bound & Azar–Broder–Karlin–Upfal (ABKU) `d`-Choice -/ /-- Exact combinatorial union bound for 1-choice balls-into-bins max load `≥ k`: `#{f : Fin n → Fin n | ∃ b, load(b) ≥ k} ≤ n · C(n, k) · n^(n - k)`! -/ theorem balls_into_bins_one_choice_max_load_card (n k : ℕ) : (Finset.univ.filter (fun f : Fin n → Fin n => ∃ b : Fin n, k ≤ (Finset.univ.filter (fun i : Fin n => f i = b)).card)).card ≤ n * n.choose k * n ^ (n - k) := by by_cases hkn : k ≤ n · set Bad := Finset.univ.filter (fun f : Fin n → Fin n => ∃ b : Fin n, k ≤ (Finset.univ.filter (fun i : Fin n => f i = b)).card) have hsub : Bad ⊆ Finset.univ.biUnion (fun b : Fin n => ((Finset.univ : Finset (Fin n)).powersetCard k).biUnion (fun S => Finset.univ.filter (fun f : Fin n → Fin n => ∀ i ∈ S, f i = b))) := by intro f hf obtain ⟨b, hb⟩ := (Finset.mem_filter.mp hf).2 obtain ⟨S, hSsub, hScard⟩ := Finset.exists_subset_card_eq hb refine Finset.mem_biUnion.mpr ⟨b, Finset.mem_univ b, Finset.mem_biUnion.mpr ⟨S, ?_, ?_⟩⟩ · exact Finset.mem_powersetCard.mpr ⟨Finset.subset_univ S, hScard⟩ · exact Finset.mem_filter.mpr ⟨Finset.mem_univ f, fun i hi => (Finset.mem_filter.mp (hSsub hi)).2⟩ have hfiber : ∀ (b : Fin n) (S : Finset (Fin n)), S.card = k → (Finset.univ.filter (fun f : Fin n → Fin n => ∀ i ∈ S, f i = b)).card = n ^ (n - k) := by intro b S hScard have hbij : (Finset.univ.filter (fun f : Fin n → Fin n => ∀ i ∈ S, f i = b)).card = Fintype.card ({i : Fin n // i ∉ S} → Fin n) := by rw [← Finset.card_univ] refine Finset.card_bij (fun f _ x => f x.1) ?_ ?_ ?_ · intro _ _; exact Finset.mem_univ _ · intro f₁ hf₁ f₂ hf₂ heq funext i by_cases hi : i ∈ S · exact ((Finset.mem_filter.mp hf₁).2 i hi).trans ((Finset.mem_filter.mp hf₂).2 i hi).symm · exact congrFun heq ⟨i, hi⟩ · intro g _ refine ⟨fun i => if hi : i ∈ S then b else g ⟨i, hi⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, fun i hi => by simp [hi]⟩, by funext ⟨i, hi⟩; simp [hi]⟩ rw [hbij, Fintype.card_fun, Fintype.card_fin] congr 1 have hcomp : Fintype.card {i : Fin n // i ∉ S} = (Finset.univ \ S).card := by rw [Fintype.card_subtype]; congr 1; ext i; simp rw [hcomp, Finset.card_sdiff, Finset.inter_univ, Finset.card_univ, Fintype.card_fin, hScard] calc Bad.card ≤ (Finset.univ.biUnion (fun b : Fin n => ((Finset.univ : Finset (Fin n)).powersetCard k).biUnion (fun S => Finset.univ.filter (fun f : Fin n → Fin n => ∀ i ∈ S, f i = b)))).card := Finset.card_le_card hsub _ ≤ ∑ b : Fin n, (((Finset.univ : Finset (Fin n)).powersetCard k).biUnion (fun S => Finset.univ.filter (fun f : Fin n → Fin n => ∀ i ∈ S, f i = b))).card := Finset.card_biUnion_le _ ≤ ∑ b : Fin n, ∑ S ∈ (Finset.univ : Finset (Fin n)).powersetCard k, (Finset.univ.filter (fun f : Fin n → Fin n => ∀ i ∈ S, f i = b)).card := Finset.sum_le_sum (fun b _ => Finset.card_biUnion_le) _ = ∑ _b : Fin n, ∑ _S ∈ (Finset.univ : Finset (Fin n)).powersetCard k, n ^ (n - k) := by refine Finset.sum_congr rfl (fun b _ => Finset.sum_congr rfl (fun S hS => ?_)) exact hfiber b S (Finset.mem_powersetCard.mp hS).2 _ = n * n.choose k * n ^ (n - k) := by simp [Finset.card_powersetCard, mul_assoc] · have hemp : Finset.univ.filter (fun f : Fin n → Fin n => ∃ b : Fin n, k ≤ (Finset.univ.filter (fun i : Fin n => f i = b)).card) = ∅ := by rw [Finset.filter_eq_empty_iff] intro f _ ⟨b, hb⟩ have hle : (Finset.univ.filter (fun i : Fin n => f i = b)).card ≤ n := by calc (Finset.univ.filter (fun i : Fin n => f i = b)).card ≤ (Finset.univ : Finset (Fin n)).card := Finset.card_filter_le _ _ _ = n := by simp omega rw [hemp, Finset.card_empty] exact Nat.zero_le _ /-- **1-CHOICE BALLS-INTO-BINS EXACT TAIL & STIRLING UPPER BOUND (`P(max load ≥ k) ≤ n/k! ≤ n(e/k)^k`)**: For EVERY `n ≥ 1` and `k ≥ 1`, the probability that any bin receives `≥ k` balls is at most `n / k! ≤ n · (e / k)^k`! -/ theorem balls_into_bins_one_choice_tail_bound (n k : ℕ) (hn : 0 < n) (hk : 1 ≤ k) : ((Finset.univ.filter (fun f : Fin n → Fin n => ∃ b : Fin n, k ≤ (Finset.univ.filter (fun i : Fin n => f i = b)).card)).card : ℝ) / (n : ℝ) ^ n ≤ (n : ℝ) / (k.factorial : ℝ) ∧ (n : ℝ) / (k.factorial : ℝ) ≤ (n : ℝ) * (Real.exp 1 / (k : ℝ)) ^ k := by have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr hn have hkfpos : (0 : ℝ) < (k.factorial : ℝ) := Nat.cast_pos.mpr (Nat.factorial_pos k) have hkpos : (0 : ℝ) < (k : ℝ) := Nat.cast_pos.mpr (by omega) constructor · by_cases hkn : k ≤ n · have hcard := balls_into_bins_one_choice_max_load_card n k have hdesc : k.factorial * n.choose k = n.descFactorial k := (Nat.descFactorial_eq_factorial_mul_choose n k).symm have hdesc_le : n.descFactorial k ≤ n ^ k := Nat.descFactorial_le_pow n k have hnat : (Finset.univ.filter (fun f : Fin n → Fin n => ∃ b : Fin n, k ≤ (Finset.univ.filter (fun i : Fin n => f i = b)).card)).card * k.factorial ≤ n * n ^ n := by calc (Finset.univ.filter (fun f : Fin n → Fin n => ∃ b : Fin n, k ≤ (Finset.univ.filter (fun i : Fin n => f i = b)).card)).card * k.factorial ≤ (n * n.choose k * n ^ (n - k)) * k.factorial := Nat.mul_le_mul_right _ hcard _ = n * (k.factorial * n.choose k) * n ^ (n - k) := by ring _ = n * n.descFactorial k * n ^ (n - k) := by rw [hdesc] _ ≤ n * n ^ k * n ^ (n - k) := Nat.mul_le_mul_right _ (Nat.mul_le_mul_left n hdesc_le) _ = n * (n ^ k * n ^ (n - k)) := by ring _ = n * n ^ n := by rw [← pow_add, Nat.add_sub_cancel' hkn] have hR : ((Finset.univ.filter (fun f : Fin n → Fin n => ∃ b : Fin n, k ≤ (Finset.univ.filter (fun i : Fin n => f i = b)).card)).card : ℝ) * (k.factorial : ℝ) ≤ (n : ℝ) * (n : ℝ) ^ n := by exact_mod_cast hnat rw [div_le_div_iff₀ (pow_pos hnpos n) hkfpos] linarith · have hemp : Finset.univ.filter (fun f : Fin n → Fin n => ∃ b : Fin n, k ≤ (Finset.univ.filter (fun i : Fin n => f i = b)).card) = ∅ := by rw [Finset.filter_eq_empty_iff] intro f _ ⟨b, hb⟩ have hle : (Finset.univ.filter (fun i : Fin n => f i = b)).card ≤ n := by calc (Finset.univ.filter (fun i : Fin n => f i = b)).card ≤ (Finset.univ : Finset (Fin n)).card := Finset.card_filter_le _ _ _ = n := by simp omega rw [hemp, Finset.card_empty, Nat.cast_zero, zero_div] exact div_nonneg (le_of_lt hnpos) (le_of_lt hkfpos) · have hpow_le := Real.pow_div_factorial_le_exp (k : ℝ) (le_of_lt hkpos) k have hexp_eq : Real.exp (k : ℝ) = (Real.exp 1) ^ k := by have : (k : ℝ) = (k : ℕ) * (1 : ℝ) := by simp rw [this, Real.exp_nat_mul] rw [hexp_eq] at hpow_le have hinv : 1 / (k.factorial : ℝ) ≤ (Real.exp 1 / (k : ℝ)) ^ k := by rw [div_pow, div_le_div_iff₀ hkfpos (pow_pos hkpos k), one_mul] have := (div_le_iff₀ hkfpos).mp hpow_le linarith calc (n : ℝ) / (k.factorial : ℝ) = (n : ℝ) * (1 / (k.factorial : ℝ)) := by ring _ ≤ (n : ℝ) * (Real.exp 1 / (k : ℝ)) ^ k := mul_le_mul_of_nonneg_left hinv (le_of_lt hnpos) /-- **ABKU `d`-CHOICE HEAVY-BIN LANDING PROBABILITY (`(|H|/n)^d`)**: The exact fraction of `d`-tuples of uniformly chosen bins that ALL land inside a heavy subset `H ⊆ Fin n` equals `(|H| / n)^d`! -/ theorem abku_d_choice_all_in_heavy_prob (n d : ℕ) (hn : 0 < n) (H : Finset (Fin n)) : ((Finset.univ.filter (fun choices : Fin d → Fin n => ∀ j : Fin d, choices j ∈ H)).card : ℝ) / ((n : ℝ) ^ d) = ((H.card : ℝ) / (n : ℝ)) ^ d := by have hcard : (Finset.univ.filter (fun choices : Fin d → Fin n => ∀ j : Fin d, choices j ∈ H)).card = H.card ^ d := by have hbij : (Finset.univ.filter (fun choices : Fin d → Fin n => ∀ j : Fin d, choices j ∈ H)).card = Fintype.card (Fin d → {b : Fin n // b ∈ H}) := by rw [← Finset.card_univ] refine Finset.card_bij (fun c hc j => ⟨c j, (Finset.mem_filter.mp hc).2 j⟩) ?_ ?_ ?_ · intro _ _; exact Finset.mem_univ _ · intro c₁ _ c₂ _ heq; funext j; exact congrArg Subtype.val (congrFun heq j) · intro g _ exact ⟨fun j => (g j).1, Finset.mem_filter.mpr ⟨Finset.mem_univ _, fun j => (g j).2⟩, by funext j; rfl⟩ rw [hbij, Fintype.card_fun, Fintype.card_coe, Fintype.card_fin] have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr hn rw [hcard, Nat.cast_pow, div_pow] /-- **ABKU LAYERED INDUCTION DOUBLY-EXPONENTIAL RECURRENCE (`β_{i+1} ≤ β_i^d ⇒ β_{i₀+t} ≤ α^(d^t)`)**: Exact closed-form doubly-exponential decay for ANY nonnegative sequence satisfying the ABKU layered recursion! -/ theorem abku_layered_recurrence_exact {d i₀ : ℕ} {α : ℝ} {β : ℕ → ℝ} (hβ_nn : ∀ i, 0 ≤ β i) (hbase : β i₀ ≤ α) (hrec : ∀ i, i₀ ≤ i → β (i + 1) ≤ (β i) ^ d) (t : ℕ) : β (i₀ + t) ≤ α ^ (d ^ t) := by induction t with | zero => simpa using hbase | succ t ih => have hα_nn : 0 ≤ α := le_trans (hβ_nn i₀) hbase have hstep : β (i₀ + t + 1) ≤ (β (i₀ + t)) ^ d := hrec (i₀ + t) (Nat.le_add_right i₀ t) have hpow : (β (i₀ + t)) ^ d ≤ (α ^ (d ^ t)) ^ d := pow_le_pow_left₀ (hβ_nn (i₀ + t)) ih d have hexp : (α ^ (d ^ t)) ^ d = α ^ (d ^ (t + 1)) := by rw [← pow_mul, pow_succ] calc β (i₀ + (t + 1)) = β (i₀ + t + 1) := by ring_nf _ ≤ (β (i₀ + t)) ^ d := hstep _ ≤ (α ^ (d ^ t)) ^ d := hpow _ = α ^ (d ^ (t + 1)) := hexp /-- **ABKU SHIFTED CHERNOFF RECURRENCE (`β_{i+1} ≤ c · β_i^d ⇒ c · β_{i₀+t}^{d-1} ≤ (c · α^{d-1})^{d^t}`)**: Exact renormalization eliminating the constant multiplicative Chernoff slack `c` (e.g. `c = e`) while preserving pure `d^t` doubly-exponential decay! -/ theorem abku_shifted_chernoff_recurrence {d i₀ : ℕ} (hd : 2 ≤ d) {c α : ℝ} (hc : 0 ≤ c) {β : ℕ → ℝ} (hβ_nn : ∀ i, 0 ≤ β i) (hbase : β i₀ ≤ α) (hrec : ∀ i, i₀ ≤ i → β (i + 1) ≤ c * (β i) ^ d) (t : ℕ) : c * (β (i₀ + t)) ^ (d - 1) ≤ (c * α ^ (d - 1)) ^ (d ^ t) := by let γ : ℕ → ℝ := fun i => c * (β i) ^ (d - 1) have hγ_nn : ∀ i, 0 ≤ γ i := fun i => mul_nonneg hc (pow_nonneg (hβ_nn i) _) have hγ_base : γ i₀ ≤ c * α ^ (d - 1) := mul_le_mul_of_nonneg_left (pow_le_pow_left₀ (hβ_nn i₀) hbase _) hc have hγ_rec : ∀ i, i₀ ≤ i → γ (i + 1) ≤ (γ i) ^ d := by intro i hi dsimp only [γ] have h1 : (β (i + 1)) ^ (d - 1) ≤ (c * (β i) ^ d) ^ (d - 1) := pow_le_pow_left₀ (hβ_nn (i + 1)) (hrec i hi) (d - 1) have h2 : c * (β (i + 1)) ^ (d - 1) ≤ c * (c * (β i) ^ d) ^ (d - 1) := mul_le_mul_of_nonneg_left h1 hc have h3 : c * (c * (β i) ^ d) ^ (d - 1) = (c * (β i) ^ (d - 1)) ^ d := by rw [mul_pow, mul_pow, ← mul_assoc, ← pow_succ', Nat.sub_add_cancel (by omega : 1 ≤ d), ← pow_mul, ← pow_mul, mul_comm d (d - 1)] linarith exact abku_layered_recurrence_exact hγ_nn hγ_base hγ_rec t /-- **ABKU `ln(ln n) / ln d` THRESHOLD DRIVES TAIL BELOW `1 / n`**: Whenever `d ≥ 2`, `n ≥ 2`, and `t ≥ ln(ln n) / ln d`, we have `(e^{-1})^(d^t) ≤ 1 / n`! -/ theorem abku_doubly_exp_threshold_le_inv_n {d t n : ℕ} (hd : 2 ≤ d) (hn : 2 ≤ n) (ht : Real.log (Real.log (n : ℝ)) / Real.log (d : ℝ) ≤ (t : ℝ)) : (Real.exp (-1)) ^ (d ^ t) ≤ 1 / (n : ℝ) := by have hd_gt1 : (1 : ℝ) < (d : ℝ) := by exact_mod_cast (by omega : 1 < d) have hn_gt1 : (1 : ℝ) < (n : ℝ) := by exact_mod_cast (by omega : 1 < n) have hlogd_pos : 0 < Real.log (d : ℝ) := Real.log_pos hd_gt1 have hlogn_pos : 0 < Real.log (n : ℝ) := Real.log_pos hn_gt1 have hmul : Real.log (Real.log (n : ℝ)) ≤ (t : ℝ) * Real.log (d : ℝ) := (div_le_iff₀ hlogd_pos).mp ht have hdt_log : (t : ℝ) * Real.log (d : ℝ) = Real.log ((d : ℝ) ^ t) := by rw [Real.log_pow] have hlogn_le : Real.log (n : ℝ) ≤ (d : ℝ) ^ t := by have hexp := Real.exp_le_exp.mpr (hmul.trans_eq hdt_log) rw [Real.exp_log hlogn_pos, Real.exp_log (pow_pos (by linarith) t)] at hexp exact hexp have hlhs : (Real.exp (-1)) ^ (d ^ t) = Real.exp (- ((d ^ t : ℕ) : ℝ)) := by rw [← Real.exp_nat_mul]; congr 1; ring have hrhs : 1 / (n : ℝ) = Real.exp (- Real.log (n : ℝ)) := by rw [Real.exp_neg, Real.exp_log (by linarith), inv_eq_one_div] rw [hlhs, hrhs] apply Real.exp_le_exp.mpr push_cast linarith /-- **EXACT NON-ASYMPTOTIC EXPONENTIAL SEPARATION: `d`-CHOICE VS 1-CHOICE**: For EVERY `d ≥ 2` and EVERY `n ≥ 16`: `ln(ln n) / ln d < ln n / ln(ln n)`! -/ theorem abku_vs_one_choice_exponential_separation {d n : ℕ} (hd : 2 ≤ d) (hn : 16 ≤ n) : Real.log (Real.log (n : ℝ)) / Real.log (d : ℝ) < Real.log (n : ℝ) / Real.log (Real.log (n : ℝ)) := by have he_lt := Real.exp_one_lt_d9 have he2_lt8 : Real.exp 2 < 8 := by have h2 : Real.exp 2 = Real.exp 1 * Real.exp 1 := by have : (2 : ℝ) = 1 + 1 := by norm_num rw [this, Real.exp_add] nlinarith [Real.exp_pos 1] have hlog2_gt : (2 / 3 : ℝ) < Real.log 2 := by have hlt : Real.exp 2 < (2 : ℝ) ^ 3 := by calc Real.exp 2 < 8 := he2_lt8 _ = (2 : ℝ) ^ 3 := by norm_num have hlog := Real.log_lt_log (Real.exp_pos 2) hlt rw [Real.log_exp, Real.log_pow] at hlog push_cast at hlog linarith have hlogd_gt : (2 / 3 : ℝ) < Real.log (d : ℝ) := by have h2d : (2 : ℝ) ≤ (d : ℝ) := by exact_mod_cast hd have hle := Real.log_le_log (by norm_num : (0 : ℝ) < 2) h2d linarith have hlogn_gt2 : 2 < Real.log (n : ℝ) := by have h16 : (16 : ℝ) ≤ (n : ℝ) := by exact_mod_cast hn have hlt : Real.exp 2 < (n : ℝ) := by linarith have h := Real.log_lt_log (Real.exp_pos 2) hlt rwa [Real.log_exp] at h set u : ℝ := Real.log (Real.log (n : ℝ)) have hu_pos : 0 < u := Real.log_pos (by linarith) have hlogd_pos : 0 < Real.log (d : ℝ) := by linarith have hexpu : Real.exp u = Real.log (n : ℝ) := Real.exp_log (by linarith) -- Degree-4 Taylor lower bound: `1 + u + u^2/2 + u^3/6 + u^4/24 ≤ exp u = ln n`! have htaylor : 1 + u + u ^ 2 / 2 + u ^ 3 / 6 + u ^ 4 / 24 ≤ Real.exp u := by have hsum := Real.sum_le_exp_of_nonneg (le_of_lt hu_pos) 5 have h5 : ∑ i ∈ Finset.range 5, u ^ i / (i.factorial : ℝ) = 1 + u + u ^ 2 / 2 + u ^ 3 / 6 + u ^ 4 / 24 := by simp [Finset.sum_range_succ, Nat.factorial] linarith -- Pure polynomial positivity: `u^4 + 4u^3 - 24u^2 + 24u + 24 = (u-2)^2(u^2+8u+4) + 8u + 8 > 0`! have hpoly : (3 / 2 : ℝ) * u ^ 2 < 1 + u + u ^ 2 / 2 + u ^ 3 / 6 + u ^ 4 / 24 := by have hid : 24 * (1 + u + u ^ 2 / 2 + u ^ 3 / 6 + u ^ 4 / 24 - (3 / 2) * u ^ 2) = (u - 2) ^ 2 * (u ^ 2 + 8 * u + 4) + 8 * u + 8 := by ring have hpos : 0 < (u - 2) ^ 2 * (u ^ 2 + 8 * u + 4) + 8 * u + 8 := by have h1 : 0 ≤ (u - 2) ^ 2 * (u ^ 2 + 8 * u + 4) := mul_nonneg (sq_nonneg _) (by nlinarith) linarith linarith have hu2_lt : u ^ 2 < Real.log (d : ℝ) * Real.log (n : ℝ) := by have h1 : (3 / 2 : ℝ) * u ^ 2 < Real.log (n : ℝ) := by linarith have h2 : u ^ 2 < (2 / 3 : ℝ) * Real.log (n : ℝ) := by linarith have h3 : (2 / 3 : ℝ) * Real.log (n : ℝ) < Real.log (d : ℝ) * Real.log (n : ℝ) := mul_lt_mul_of_pos_right hlogd_gt (by linarith) linarith rw [div_lt_div_iff₀ hlogd_pos hu_pos] nlinarith /-! =============================================================================== PART 3: CUCKOO HASHING & GRAPH-ORIENTABILITY LOAD THRESHOLDS (HALL / HAKIMI `(d, k)`-ORIENTABILITY, PSEUDOFORESTS & SUBCRITICAL TAILS) =============================================================================== -/ /-- A `(n, m, d)` cuckoo hashing instance: `n` keys, `m` buckets, `d` candidate buckets per key. -/ structure CuckooInstance (n m d : ℕ) where cand : Fin n → Fin d → Fin m /-- Set of distinct candidate buckets available to key `x`. -/ def CuckooInstance.bucketSet {n m d : ℕ} (C : CuckooInstance n m d) (x : Fin n) : Finset (Fin m) := Finset.univ.image (C.cand x) /-- Neighborhood `N(K) = ⋃_{x ∈ K} bucketSet(x)` of a key subset `K`. -/ def CuckooInstance.keyNeighborhood {n m d : ℕ} (C : CuckooInstance n m d) (K : Finset (Fin n)) : Finset (Fin m) := K.biUnion C.bucketSet /-- Induced keys `E(B) = {x | bucketSet(x) ⊆ B}` whose candidates lie entirely inside `B`. -/ def CuckooInstance.inducedKeys {n m d : ℕ} (C : CuckooInstance n m d) (B : Finset (Fin m)) : Finset (Fin n) := Finset.univ.filter (fun x : Fin n => C.bucketSet x ⊆ B) /-- Valid capacity-`k` cuckoo placement: each key is placed in one of its candidate buckets, and every bucket holds at most `k` keys. -/ def IsValidCuckooPlacement {n m d : ℕ} (C : CuckooInstance n m d) (k : ℕ) (place : Fin n → Fin m) : Prop := (∀ x : Fin n, place x ∈ C.bucketSet x) ∧ (∀ b : Fin m, (Finset.univ.filter (fun x : Fin n => place x = b)).card ≤ k) /-- **GENERALIZED `(d, k)`-CUCKOO HALL / HAKIMI EXACT ORIENTABILITY CHARACTERIZATION**: A cuckoo hashing instance `C` with bucket capacity `k` has a valid placement IF AND ONLY IF 1. **Hall Expansion**: `∀ K ⊆ Fin n, |K| ≤ k · |N(K)|`, AND EQUIVALENTLY 2. **Hakimi Subgraph Density**: `∀ B ⊆ Fin m, |E(B)| ≤ k · |B|`! -/ theorem cuckoo_feasible_iff_hall_and_density {n m d : ℕ} (C : CuckooInstance n m d) (k : ℕ) : (∃ place : Fin n → Fin m, IsValidCuckooPlacement C k place) ↔ ((∀ K : Finset (Fin n), K.card ≤ k * (C.keyNeighborhood K).card) ∧ (∀ B : Finset (Fin m), (C.inducedKeys B).card ≤ k * B.card)) := by -- Step 1: Slot replication `Fin m × Fin k` equates capacity-`k` placement to an injective map! have hslot : (∃ place : Fin n → Fin m, IsValidCuckooPlacement C k place) ↔ (∃ f : Fin n → Fin m × Fin k, Function.Injective f ∧ ∀ x : Fin n, f x ∈ C.bucketSet x ×ˢ (Finset.univ : Finset (Fin k))) := by constructor · rintro ⟨place, hmem, hcap⟩ have hfib : ∀ b : Fin m, ∃ slot : {x : Fin n // place x = b} → Fin k, Function.Injective slot := by intro b let F := Finset.univ.filter (fun x : Fin n => place x = b) have hF : F.card ≤ k := hcap b let e : F ≃ Fin F.card := F.equivFin refine ⟨fun x => Fin.castLE hF (e ⟨x.1, Finset.mem_filter.mpr ⟨Finset.mem_univ _, x.2⟩⟩), ?_⟩ intro x₁ x₂ heq have hcast := Fin.castLE_injective hF heq have he := e.injective hcast exact Subtype.ext (congrArg (fun z : F => z.1) he) choose slot hslot_inj using hfib refine ⟨fun x => (place x, slot (place x) ⟨x, rfl⟩), ?_, ?_⟩ · intro x₁ x₂ heq have hb : place x₁ = place x₂ := (Prod.mk.inj heq).1 have hs : slot (place x₁) ⟨x₁, rfl⟩ = slot (place x₂) ⟨x₂, rfl⟩ := (Prod.mk.inj heq).2 have hsub : ∀ b₁ b₂ : Fin m, b₁ = b₂ → ∀ (y₁ : {x : Fin n // place x = b₁}) (y₂ : {x : Fin n // place x = b₂}), slot b₁ y₁ = slot b₂ y₂ → y₁.1 = y₂.1 := by rintro b _ rfl y₁ y₂ h exact congrArg Subtype.val (hslot_inj b h) exact hsub (place x₁) (place x₂) hb ⟨x₁, rfl⟩ ⟨x₂, rfl⟩ hs · intro x exact Finset.mem_product.mpr ⟨hmem x, Finset.mem_univ _⟩ · rintro ⟨f, hfinj, hfmem⟩ refine ⟨fun x => (f x).1, fun x => (Finset.mem_product.mp (hfmem x)).1, fun b => ?_⟩ have hinj : Set.InjOn (fun x => (f x).2) ((Finset.univ.filter (fun x : Fin n => (f x).1 = b)) : Set (Fin n)) := by intro x₁ hx₁ x₂ hx₂ heq have hb1 : (f x₁).1 = b := (Finset.mem_filter.mp hx₁).2 have hb2 : (f x₂).1 = b := (Finset.mem_filter.mp hx₂).2 exact hfinj (Prod.ext (hb1.trans hb2.symm) heq) calc (Finset.univ.filter (fun x : Fin n => (f x).1 = b)).card ≤ (Finset.univ : Finset (Fin k)).card := Finset.card_le_card_of_injOn (fun x => (f x).2) (fun _ _ => Finset.mem_univ _) hinj _ = k := by simp -- Step 2: Apply Hall's Marriage Theorem (`Finset.all_card_le_biUnion_card_iff_exists_injective`)! have hhall : (∃ f : Fin n → Fin m × Fin k, Function.Injective f ∧ ∀ x : Fin n, f x ∈ C.bucketSet x ×ˢ (Finset.univ : Finset (Fin k))) ↔ (∀ K : Finset (Fin n), K.card ≤ k * (C.keyNeighborhood K).card) := by rw [← Finset.all_card_le_biUnion_card_iff_exists_injective] refine forall_congr' (fun K => ?_) have hbiU : K.biUnion (fun x => C.bucketSet x ×ˢ (Finset.univ : Finset (Fin k))) = (C.keyNeighborhood K) ×ˢ (Finset.univ : Finset (Fin k)) := by ext ⟨b, s⟩ simp [CuckooInstance.keyNeighborhood] rw [hbiU, Finset.card_product, Finset.card_univ, Fintype.card_fin, mul_comm] -- Step 3: Exact Galois duality between Hall expansion and Hakimi subgraph density! have hdual : (∀ K : Finset (Fin n), K.card ≤ k * (C.keyNeighborhood K).card) ↔ (∀ B : Finset (Fin m), (C.inducedKeys B).card ≤ k * B.card) := by constructor · intro hH B have hsub : C.keyNeighborhood (C.inducedKeys B) ⊆ B := by intro b hb obtain ⟨x, hx, hbx⟩ := Finset.mem_biUnion.mp hb exact (Finset.mem_filter.mp hx).2 hbx calc (C.inducedKeys B).card ≤ k * (C.keyNeighborhood (C.inducedKeys B)).card := hH (C.inducedKeys B) _ ≤ k * B.card := Nat.mul_le_mul_left k (Finset.card_le_card hsub) · intro hD K have hsub : K ⊆ C.inducedKeys (C.keyNeighborhood K) := by intro x hx exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, fun b hb => Finset.mem_biUnion.mpr ⟨x, hx, hb⟩⟩ calc K.card ≤ (C.inducedKeys (C.keyNeighborhood K)).card := Finset.card_le_card hsub _ ≤ k * (C.keyNeighborhood K).card := hD (C.keyNeighborhood K) rw [hslot, hhall] exact ⟨fun h => ⟨h, hdual.mp h⟩, fun h => h.1⟩ /-- **2-TABLE CUCKOO HASHING PSEUDOFOREST CHARACTERIZATION (`|E(B)| ≤ |B|`)**: Standard capacity-1 cuckoo hashing (`k = 1`) succeeds IF AND ONLY IF every bucket subset `B` induces at most `|B|` keys (every connected component has at most one cycle — a pseudoforest)! -/ theorem cuckoo2_feasible_iff_pseudoforest {n m : ℕ} (C : CuckooInstance n m 2) : (∃ place : Fin n → Fin m, (∀ x, place x ∈ C.bucketSet x) ∧ Function.Injective place) ↔ (∀ B : Finset (Fin m), (C.inducedKeys B).card ≤ B.card) := by have heq1 : (∃ place : Fin n → Fin m, (∀ x, place x ∈ C.bucketSet x) ∧ Function.Injective place) ↔ (∃ place : Fin n → Fin m, IsValidCuckooPlacement C 1 place) := by constructor · rintro ⟨place, hmem, hinj⟩ refine ⟨place, hmem, fun b => ?_⟩ rw [Finset.card_le_one] intro x hx y hy exact hinj ((Finset.mem_filter.mp hx).2.trans (Finset.mem_filter.mp hy).2.symm) · rintro ⟨place, hmem, hcap⟩ refine ⟨place, hmem, fun x y hxy => ?_⟩ have hle := Finset.card_le_one.mp (hcap (place x)) exact hle x (Finset.mem_filter.mpr ⟨Finset.mem_univ x, rfl⟩) y (Finset.mem_filter.mpr ⟨Finset.mem_univ y, hxy.symm⟩) rw [heq1, cuckoo_feasible_iff_hall_and_density C 1] have hdual : (∀ K : Finset (Fin n), K.card ≤ 1 * (C.keyNeighborhood K).card) ↔ (∀ B : Finset (Fin m), (C.inducedKeys B).card ≤ 1 * B.card) := by constructor · intro hH B have hsub : C.keyNeighborhood (C.inducedKeys B) ⊆ B := by intro b hb; obtain ⟨x, hx, hbx⟩ := Finset.mem_biUnion.mp hb exact (Finset.mem_filter.mp hx).2 hbx calc (C.inducedKeys B).card ≤ 1 * (C.keyNeighborhood (C.inducedKeys B)).card := hH _ _ ≤ 1 * B.card := Nat.mul_le_mul_left 1 (Finset.card_le_card hsub) · intro hD K have hsub : K ⊆ C.inducedKeys (C.keyNeighborhood K) := by intro x hx; exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, fun b hb => Finset.mem_biUnion.mpr ⟨x, hx, hb⟩⟩ calc K.card ≤ (C.inducedKeys (C.keyNeighborhood K)).card := Finset.card_le_card hsub _ ≤ 1 * (C.keyNeighborhood K).card := hD _ simp only [one_mul] at hdual ⊢ exact ⟨fun h => h.2, fun h => ⟨hdual.mpr h, h⟩⟩ /-- **EXACT CUCKOO LOAD FACTOR CEILING (`n ≤ k · m`)**: Any feasible `(n, m, d)` cuckoo instance at bucket capacity `k` must satisfy `n ≤ k · m`! -/ theorem cuckoo_feasible_load_le_capacity {n m d k : ℕ} (C : CuckooInstance n m d) (hfeas : ∃ place : Fin n → Fin m, IsValidCuckooPlacement C k place) : n ≤ k * m := by have hD := (cuckoo_feasible_iff_hall_and_density C k).mp hfeas |>.2 Finset.univ have hind : C.inducedKeys Finset.univ = Finset.univ := by ext x; simp [CuckooInstance.inducedKeys] rw [hind, Finset.card_univ, Fintype.card_fin, Finset.card_univ, Fintype.card_fin] at hD exact hD /-- **UNICYCLIC `C_m` FEASIBILITY WITNESS**: A cycle of `m` keys on `m` buckets (`|E| = |V| = m`) IS always 2-cuckoo feasible! -/ theorem cuckoo2_cycle_feasible (m : ℕ) (hm : 0 < m) : let C : CuckooInstance m m 2 := ⟨fun i j => if j = 0 then i else ⟨(i.1 + 1) % m, Nat.mod_lt _ hm⟩⟩ ∃ place : Fin m → Fin m, (∀ x, place x ∈ C.bucketSet x) ∧ Function.Injective place := by intro C refine ⟨id, fun x => ?_, Function.injective_id⟩ exact Finset.mem_image.mpr ⟨0, Finset.mem_univ 0, by simp [C]⟩ /-- **BICYCLIC 4-VERTEX / 5-EDGE OBSTRUCTION WITNESS (`|E| = 5 > 4 = |V|`)**: A 4-cycle with one chord (`5` keys on `4` buckets) is INFEASIBLE for capacity-1 cuckoo hashing! -/ theorem cuckoo2_bicyclic_obstruction_infeasible : let C : CuckooInstance 5 4 2 := ⟨fun e j => match e.1, j.1 with | 0, 0 => 0 | 0, _ => 1 | 1, 0 => 1 | 1, _ => 2 | 2, 0 => 2 | 2, _ => 3 | 3, 0 => 3 | 3, _ => 0 | _, 0 => 0 | _, _ => 2⟩ ¬ ∃ place : Fin 5 → Fin 4, IsValidCuckooPlacement C 1 place := by intro C h have hle := cuckoo_feasible_load_le_capacity C h omega /-- **SUBCRITICAL 2-CUCKOO GEOMETRIC CYCLE-COLLISION TAIL (`2 n < m`)**: Below load factor `1 / 2` (`r = 2 n / m < 1`), the partial geometric series satisfies exact identity `(1 - r) ∑_{ℓ < L} r^ℓ = 1 - r^L` and uniform bound `≤ m / (m - 2 n)`! -/ theorem cuckoo2_subcritical_geometric_tail {n m : ℕ} (hsub : 2 * n < m) (L : ℕ) : let r : ℝ := (2 * (n : ℝ)) / (m : ℝ) 0 ≤ r ∧ r < 1 ∧ ∑ ℓ ∈ Finset.range L, r ^ ℓ ≤ (m : ℝ) / ((m : ℝ) - 2 * (n : ℝ)) ∧ (1 - r) * (∑ ℓ ∈ Finset.range L, r ^ ℓ) = 1 - r ^ L := by intro r have hmpos : (0 : ℝ) < (m : ℝ) := Nat.cast_pos.mpr (by omega) have hsubR : 2 * (n : ℝ) < (m : ℝ) := by exact_mod_cast hsub have hr_nn : 0 ≤ r := div_nonneg (by positivity) (le_of_lt hmpos) have hr_lt1 : r < 1 := (div_lt_one hmpos).mpr hsubR have hgeom : (1 - r) * (∑ ℓ ∈ Finset.range L, r ^ ℓ) = 1 - r ^ L := by induction L with | zero => simp | succ L ih => rw [Finset.sum_range_succ, mul_add, ih, pow_succ]; ring have hsum_le : ∑ ℓ ∈ Finset.range L, r ^ ℓ ≤ (m : ℝ) / ((m : ℝ) - 2 * (n : ℝ)) := by have h1r_pos : 0 < 1 - r := by linarith have h1r_eq : 1 - r = ((m : ℝ) - 2 * (n : ℝ)) / (m : ℝ) := by dsimp only [r]; field_simp [ne_of_gt hmpos] have hpow_nn : 0 ≤ r ^ L := pow_nonneg hr_nn L have hle1 : (1 - r) * (∑ ℓ ∈ Finset.range L, r ^ ℓ) ≤ 1 := by linarith rw [h1r_eq] at hle1 have hdiff_pos : 0 < (m : ℝ) - 2 * (n : ℝ) := by linarith rw [le_div_iff₀ hdiff_pos] have hmul : ((m : ℝ) - 2 * (n : ℝ)) * (∑ ℓ ∈ Finset.range L, r ^ ℓ) / (m : ℝ) ≤ 1 := by simpa [div_mul_eq_mul_div] using hle1 have h2 := (div_le_iff₀ hmpos).mp hmul linarith exact ⟨hr_nn, hr_lt1, hsum_le, hgeom⟩ /-! =============================================================================== PART 4: ONLINE OPTIMIZATION MINIMAX — SKI-RENTAL (KMMO), DYNKIN SECRETARY & ONLINE BIPARTITE MATCHING (KARP–VAZIRANI–VAZIRANI) =============================================================================== -/ /-! ### Pillar 4A: Exact Discrete Ski-Rental Deterministic & Randomized Minimax (KMMO) -/ /-- Online ski-rental cost when buying price is `B`, strategy buys on day `k` (`k ≥ 1`), and the ski season lasts `d` days (`d ≥ 1`). -/ def skiCost (B k d : ℕ) : ℕ := if d < k then d else (k - 1) + B /-- Offline optimal ski-rental cost `min(d, B)`. -/ def skiOpt (B d : ℕ) : ℕ := min d B /-- **EXACT DETERMINISTIC SKI-RENTAL MINIMAX (`2 - 1 / B`)**: 1. **Upper bound**: Buying on day `k = B` achieves competitive ratio `≤ 2 - 1 / B` on EVERY `d ≥ 1`. 2. **Matching lower bound**: For EVERY deterministic `k ≥ 1`, there exists an adversarial horizon `d ≥ 1` forcing cost `≥ (2 - 1 / B) · OPT(d)`! -/ theorem ski_rental_deterministic_minimax {B : ℕ} (hB : 1 ≤ B) : (∀ d : ℕ, 1 ≤ d → (skiCost B B d : ℝ) ≤ (2 - 1 / (B : ℝ)) * (skiOpt B d : ℝ)) ∧ (∀ k : ℕ, 1 ≤ k → ∃ d : ℕ, 1 ≤ d ∧ (2 - 1 / (B : ℝ)) * (skiOpt B d : ℝ) ≤ (skiCost B k d : ℝ)) := by have hBpos : (0 : ℝ) < (B : ℝ) := Nat.cast_pos.mpr (by omega) have hratio_ge1 : (1 : ℝ) ≤ 2 - 1 / (B : ℝ) := by have : 1 / (B : ℝ) ≤ 1 := (div_le_one hBpos).mpr (by exact_mod_cast hB) linarith constructor · intro d hd dsimp only [skiCost, skiOpt] by_cases hlt : d < B · rw [if_pos hlt, min_eq_left (le_of_lt hlt)] nlinarith [Nat.cast_nonneg (α := ℝ) d] · rw [if_neg hlt, min_eq_right (not_lt.mp hlt)] push_cast rw [Nat.cast_sub hB] push_cast field_simp [ne_of_gt hBpos] ring_nf; exact le_rfl · intro k hk refine ⟨k, hk, ?_⟩ dsimp only [skiCost, skiOpt] rw [if_neg (lt_irrefl k)] by_cases hkB : k ≤ B · rw [min_eq_left hkB] push_cast rw [Nat.cast_sub hk] push_cast have hkB_R : (k : ℝ) ≤ (B : ℝ) := by exact_mod_cast hkB have hB1_R : (1 : ℝ) ≤ (B : ℝ) := by exact_mod_cast hB have hmul : ((2 : ℝ) * (B : ℝ) - 1) * (k : ℝ) ≤ ((k : ℝ) - 1 + (B : ℝ)) * (B : ℝ) := by nlinarith have hdiv : ((2 : ℝ) - 1 / (B : ℝ)) * (k : ℝ) = (((2 : ℝ) * (B : ℝ) - 1) * (k : ℝ)) / (B : ℝ) := by field_simp [ne_of_gt hBpos] rw [hdiv, div_le_iff₀ hBpos] linarith · rw [min_eq_right (le_of_lt (not_le.mp hkB))] push_cast rw [Nat.cast_sub hk] push_cast have hkB_R : (B : ℝ) ≤ (k : ℝ) := by exact_mod_cast (le_of_lt (not_le.mp hkB)) have hmul : ((2 : ℝ) - 1 / (B : ℝ)) * (B : ℝ) = 2 * (B : ℝ) - 1 := by field_simp [ne_of_gt hBpos] linarith private lemma geom_sum_one_sub_q {q : ℝ} (m : ℕ) : (1 - q) * (∑ j ∈ Finset.range m, q ^ j) = 1 - q ^ m := by induction m with | zero => simp | succ m ih => rw [Finset.sum_range_succ, mul_add, ih, pow_succ]; ring private lemma one_sub_inv_pow_lt_exp_neg_one {B : ℕ} (hB : 1 ≤ B) : (((B : ℝ) - 1) / (B : ℝ)) ^ B < Real.exp (-1) := by have hBpos : (0 : ℝ) < (B : ℝ) := Nat.cast_pos.mpr (by omega) rcases eq_or_lt_of_le hB with rfl | hB2 · simp [Real.exp_pos] · have hq_nn : 0 ≤ ((B : ℝ) - 1) / (B : ℝ) := by have : (1 : ℝ) ≤ (B : ℝ) := by exact_mod_cast hB exact div_nonneg (by linarith) (le_of_lt hBpos) have hlt : ((B : ℝ) - 1) / (B : ℝ) < Real.exp (- (1 / (B : ℝ))) := by have hne : - (1 / (B : ℝ)) ≠ 0 := neg_ne_zero.mpr (ne_of_gt (div_pos zero_lt_one hBpos)) have h := Real.add_one_lt_exp hne have heq : ((B : ℝ) - 1) / (B : ℝ) = - (1 / (B : ℝ)) + 1 := by field_simp [ne_of_gt hBpos]; ring linarith have hpow := pow_lt_pow_left₀ hlt hq_nn (by omega : B ≠ 0) have hexp : (Real.exp (- (1 / (B : ℝ)))) ^ B = Real.exp (-1) := by rw [← Real.exp_nat_mul] congr 1; field_simp [ne_of_gt hBpos] rwa [hexp] at hpow /-- **KARLIN–MANASSE–MCGEOCH–OWICKI (KMMO) EXACT RANDOMIZED SKI-RENTAL UPPER BOUND**: For EVERY `B ≥ 1`, the explicit KMMO distribution `p_i ∝ ((B - 1) / B)^(B - 1 - i)` on buying days `{1, ..., B}` achieves EXACT competitive ratio `R_B = 1 / (1 - ((B - 1) / B)^B)` against EVERY horizon `d ≥ 1`! -/ theorem ski_rental_kmmo_randomized_upper {B : ℕ} (hB : 1 ≤ B) : let R_B : ℝ := 1 / (1 - (((B : ℝ) - 1) / (B : ℝ)) ^ B) ∃ p : Fin B → ℝ, (∀ i : Fin B, 0 ≤ p i) ∧ (∑ i : Fin B, p i = 1) ∧ (∀ d : ℕ, 1 ≤ d → ∑ i : Fin B, p i * (skiCost B (i.1 + 1) d : ℝ) ≤ R_B * (skiOpt B d : ℝ)) := by intro R_B have hBpos : (0 : ℝ) < (B : ℝ) := Nat.cast_pos.mpr (by omega) set q : ℝ := ((B : ℝ) - 1) / (B : ℝ) have hq_nn : 0 ≤ q := by have : (1 : ℝ) ≤ (B : ℝ) := by exact_mod_cast hB exact div_nonneg (by linarith) (le_of_lt hBpos) have h1q : 1 - q = 1 / (B : ℝ) := by dsimp only [q]; field_simp [ne_of_gt hBpos]; ring set Z : ℝ := 1 - q ^ B have hZpos : 0 < Z := by have hqlt := one_sub_inv_pow_lt_exp_neg_one hB have hexp_lt1 : Real.exp (-1) < 1 := by rw [Real.exp_neg, inv_lt_one_iff₀]; right; linarith [Real.exp_one_gt_d9] linarith have hgeomB : ∀ m : ℕ, ∑ j ∈ Finset.range m, q ^ j = (B : ℝ) * (1 - q ^ m) := by intro m have h := geom_sum_one_sub_q (q := q) m rw [h1q] at h field_simp [ne_of_gt hBpos] at h ⊢ linarith let p : Fin B → ℝ := fun i => q ^ (B - 1 - i.1) / ((B : ℝ) * Z) have hp_nn : ∀ i : Fin B, 0 ≤ p i := fun i => div_nonneg (pow_nonneg hq_nn _) (mul_nonneg (le_of_lt hBpos) (le_of_lt hZpos)) -- Exact sum of weights `∑ i : Fin B, q^(B - 1 - i.1) = B * Z`! have hsum_q : ∑ i : Fin B, q ^ (B - 1 - i.1) = (B : ℝ) * Z := by have hbij : ∑ i : Fin B, q ^ (B - 1 - i.1) = ∑ j ∈ Finset.range B, q ^ j := by refine Finset.sum_bij (fun i _ => B - 1 - i.1) (fun i _ => Finset.mem_range.mpr (by omega)) (fun i₁ _ i₂ _ h => Fin.ext (by omega)) (fun j hj => ⟨⟨B - 1 - j, by have := Finset.mem_range.mp hj; omega⟩, Finset.mem_univ _, by have := Finset.mem_range.mp hj; dsimp only; omega⟩) (fun _ _ => rfl) rw [hbij, hgeomB B] have hp_sum : ∑ i : Fin B, p i = 1 := by dsimp only [p] rw [← Finset.sum_div, hsum_q] field_simp [ne_of_gt hBpos, ne_of_gt hZpos] -- Exact unnormalized expected cost `F d = B * d` for all `1 ≤ d ≤ B`! let F : ℕ → ℝ := fun d => ∑ i : Fin B, q ^ (B - 1 - i.1) * (skiCost B (i.1 + 1) d : ℝ) have hF_le_B : ∀ d : ℕ, 1 ≤ d → d ≤ B → F d = (B : ℝ) * (d : ℝ) := by intro d hd1 hdB induction d with | zero => omega | succ d ih => rcases eq_or_lt_of_le (Nat.zero_le d) with rfl | hd_pos · -- Base `d = 1`: `i = 0` pays `B`, all `i ≥ 1` pay `1`! dsimp only [F] let i0 : Fin B := ⟨0, by omega⟩ rw [← Finset.add_sum_erase Finset.univ _ (Finset.mem_univ i0)] have h0 : q ^ (B - 1 - i0.1) * (skiCost B (i0.1 + 1) 1 : ℝ) = q ^ (B - 1) * (B : ℝ) := by simp [i0, skiCost] have hrest : ∑ i ∈ Finset.univ.erase i0, q ^ (B - 1 - i.1) * (skiCost B (i.1 + 1) 1 : ℝ) = ∑ j ∈ Finset.range (B - 1), q ^ j := by have h1 : ∑ i ∈ Finset.univ.erase i0, q ^ (B - 1 - i.1) * (skiCost B (i.1 + 1) 1 : ℝ) = ∑ i ∈ Finset.univ.erase i0, q ^ (B - 1 - i.1) := by refine Finset.sum_congr rfl (fun i hi => ?_) have hi0 : i.1 ≠ 0 := fun hv => (Finset.mem_erase.mp hi).1 (Fin.ext hv) have hlt : 1 < i.1 + 1 := by omega simp [skiCost, hlt] rw [h1] refine Finset.sum_bij (fun i _ => B - 1 - i.1) (fun i hi => by have hi0 : i.1 ≠ 0 := fun hv => (Finset.mem_erase.mp hi).1 (Fin.ext hv) exact Finset.mem_range.mpr (by omega)) (fun i₁ _ i₂ _ h => Fin.ext (by omega)) (fun j hj => ⟨⟨B - 1 - j, by have := Finset.mem_range.mp hj; omega⟩, Finset.mem_erase.mpr ⟨fun h => by have hv := congrArg Fin.val h; dsimp only [i0] at hv have := Finset.mem_range.mp hj; omega, Finset.mem_univ _⟩, by have := Finset.mem_range.mp hj; dsimp only; omega⟩) (fun _ _ => rfl) rw [h0, hrest, hgeomB (B - 1)] push_cast; ring · -- Step `d → d + 1` (`1 ≤ d`, `d + 1 ≤ B`): telescoping difference `F(d + 1) - F(d) = B`! have hprev := ih hd_pos (by omega) have hdiff : F (d + 1) - F d = (B : ℝ) := by dsimp only [F] rw [← Finset.sum_sub_distrib] let id_idx : Fin B := ⟨d, by omega⟩ rw [← Finset.add_sum_erase Finset.univ _ (Finset.mem_univ id_idx)] have hdiag : q ^ (B - 1 - id_idx.1) * (skiCost B (id_idx.1 + 1) (d + 1) : ℝ) - q ^ (B - 1 - id_idx.1) * (skiCost B (id_idx.1 + 1) d : ℝ) = q ^ (B - 1 - d) * (B : ℝ) := by dsimp only [id_idx, skiCost] rw [if_neg (by omega), if_pos (by omega)] push_cast; ring have htail : ∑ i ∈ Finset.univ.erase id_idx, (q ^ (B - 1 - i.1) * (skiCost B (i.1 + 1) (d + 1) : ℝ) - q ^ (B - 1 - i.1) * (skiCost B (i.1 + 1) d : ℝ)) = ∑ j ∈ Finset.range (B - 1 - d), q ^ j := by have hfilt : ∑ i ∈ Finset.univ.erase id_idx, (q ^ (B - 1 - i.1) * (skiCost B (i.1 + 1) (d + 1) : ℝ) - q ^ (B - 1 - i.1) * (skiCost B (i.1 + 1) d : ℝ)) = ∑ i ∈ (Finset.univ.erase id_idx).filter (fun i : Fin B => d < i.1), q ^ (B - 1 - i.1) := by rw [Finset.sum_filter] refine Finset.sum_congr rfl (fun i hi => ?_) have hne : i.1 ≠ d := fun hv => (Finset.mem_erase.mp hi).1 (Fin.ext hv) split_ifs with hgt · dsimp only [skiCost] rw [if_pos (by omega), if_pos (by omega)] push_cast; ring · dsimp only [skiCost] rw [if_neg (by omega), if_neg (by omega)]; ring rw [hfilt] refine Finset.sum_bij (fun i _ => B - 1 - i.1) (fun i hi => by have hgt := (Finset.mem_filter.mp hi).2 exact Finset.mem_range.mpr (by omega)) (fun i₁ _ i₂ _ h => Fin.ext (by omega)) (fun j hj => by have hjlt := Finset.mem_range.mp hj refine ⟨⟨B - 1 - j, by omega⟩, Finset.mem_filter.mpr ⟨Finset.mem_erase.mpr ⟨fun h => by have := congrArg Fin.val h; dsimp only [id_idx] at this; omega, Finset.mem_univ _⟩, by dsimp only; omega⟩, by dsimp only; omega⟩) (fun _ _ => rfl) rw [hdiag, htail, hgeomB (B - 1 - d)] ring push_cast; linarith refine ⟨p, hp_nn, hp_sum, fun d hd => ?_⟩ have hFd : F d = (B : ℝ) * (skiOpt B d : ℝ) := by by_cases hdB : d ≤ B · dsimp only [skiOpt]; rw [min_eq_left hdB]; exact hF_le_B d hd hdB · have hge : B ≤ d := by omega dsimp only [skiOpt]; rw [min_eq_right hge] have hsame : F d = F B := by dsimp only [F] refine Finset.sum_congr rfl (fun i _ => ?_) congr 1; dsimp only [skiCost] rw [if_neg (by omega), if_neg (by omega)] rw [hsame]; exact hF_le_B B hB le_rfl have hlhs : ∑ i : Fin B, p i * (skiCost B (i.1 + 1) d : ℝ) = F d / ((B : ℝ) * Z) := by dsimp only [p, F] rw [Finset.sum_div] refine Finset.sum_congr rfl (fun i _ => by ring) rw [hlhs, hFd] dsimp only [R_B, Z, q] have h1 : (B : ℝ) * (skiOpt B d : ℝ) / ((B : ℝ) * (1 - (((B : ℝ) - 1) / (B : ℝ)) ^ B)) = 1 / (1 - (((B : ℝ) - 1) / (B : ℝ)) ^ B) * (skiOpt B d : ℝ) := by field_simp [ne_of_gt hBpos, ne_of_gt hZpos] linarith private lemma weighted_geom_sum_telescoping {q : ℝ} (m : ℕ) : ∑ t ∈ Finset.range m, ((t : ℝ) + 1) * (1 - q) * q ^ t = (∑ t ∈ Finset.range m, q ^ t) - (m : ℝ) * q ^ m := by induction m with | zero => simp | succ m ih => rw [Finset.sum_range_succ, Finset.sum_range_succ, ih, pow_succ] push_cast; ring /-- **KARLIN–MANASSE–MCGEOCH–OWICKI (KMMO) EXACT RANDOMIZED SKI-RENTAL LOWER BOUND (YAO MINIMAX)**: For EVERY `B ≥ 1`, there exists an explicit adversarial distribution `(w, days)` over `B` horizons such that EVERY deterministic buying strategy `k ≥ 1` suffers expected cost `E_w[skiCost B k] ≥ R_B · E_w[skiOpt B]`, proving zero-gap minimax optimality of `R_B`! -/ theorem ski_rental_kmmo_randomized_lower_yao {B : ℕ} (hB : 1 ≤ B) : let R_B : ℝ := 1 / (1 - (((B : ℝ) - 1) / (B : ℝ)) ^ B) ∃ (w : Fin B → ℝ) (days : Fin B → ℕ), (∀ t : Fin B, 0 ≤ w t) ∧ (∀ t : Fin B, 1 ≤ days t) ∧ (0 < ∑ t : Fin B, w t * (skiOpt B (days t) : ℝ)) ∧ (∀ k : ℕ, 1 ≤ k → R_B * (∑ t : Fin B, w t * (skiOpt B (days t) : ℝ)) ≤ ∑ t : Fin B, w t * (skiCost B k (days t) : ℝ)) := by intro R_B have hBpos : (0 : ℝ) < (B : ℝ) := Nat.cast_pos.mpr (by omega) set q : ℝ := ((B : ℝ) - 1) / (B : ℝ) have hq_nn : 0 ≤ q := by have : (1 : ℝ) ≤ (B : ℝ) := by exact_mod_cast hB exact div_nonneg (by linarith) (le_of_lt hBpos) have h1q : 1 - q = 1 / (B : ℝ) := by dsimp only [q]; field_simp [ne_of_gt hBpos]; ring set Z : ℝ := 1 - q ^ B have hZpos : 0 < Z := by have hqlt := one_sub_inv_pow_lt_exp_neg_one hB have hexp_lt1 : Real.exp (-1) < 1 := by rw [Real.exp_neg, inv_lt_one_iff₀]; right; linarith [Real.exp_one_gt_d9] linarith have hgeomB : ∀ m : ℕ, ∑ j ∈ Finset.range m, q ^ j = (B : ℝ) * (1 - q ^ m) := by intro m have h := geom_sum_one_sub_q (q := q) m rw [h1q] at h field_simp [ne_of_gt hBpos] at h ⊢ linarith let w : Fin B → ℝ := fun t => if t.1 + 1 < B then (1 - q) * q ^ t.1 else q ^ (B - 1) let days : Fin B → ℕ := fun t => if t.1 + 1 < B then t.1 + 1 else 2 * B have hw_nn : ∀ t : Fin B, 0 ≤ w t := by intro t; dsimp only [w]; split_ifs · exact mul_nonneg (by rw [h1q]; positivity) (pow_nonneg hq_nn _) · exact pow_nonneg hq_nn _ have hdays_pos : ∀ t : Fin B, 1 ≤ days t := by intro t; dsimp only [days]; split_ifs <;> omega let lastIdx : Fin B := ⟨B - 1, by omega⟩ -- Helper: split `∑ t : Fin B, f t` into `(∑ t ∈ range (B - 1), if h : t < B then f ⟨t, h⟩ else 0) + f lastIdx`! have hsplit : ∀ f : Fin B → ℝ, ∑ t : Fin B, f t = (∑ t ∈ Finset.range (B - 1), if h : t < B then f ⟨t, h⟩ else 0) + f lastIdx := by intro f rw [← Finset.add_sum_erase Finset.univ f (Finset.mem_univ lastIdx), add_comm] congr 1 refine Finset.sum_bij (fun t _ => t.1) (fun t ht => by have hne : t.1 ≠ B - 1 := fun hv => (Finset.mem_erase.mp ht).1 (Fin.ext hv) exact Finset.mem_range.mpr (by omega)) (fun t₁ _ t₂ _ h => Fin.ext h) (fun j hj => by have hjlt := Finset.mem_range.mp hj refine ⟨⟨j, by omega⟩, Finset.mem_erase.mpr ⟨fun h => by have := congrArg Fin.val h; dsimp only [lastIdx] at this; omega, Finset.mem_univ _⟩, rfl⟩) (fun t _ => by rw [dif_pos t.2]) -- Step 1: Exact `E_w[skiOpt] = B * Z`! have hEopt : ∑ t : Fin B, w t * (skiOpt B (days t) : ℝ) = (B : ℝ) * Z := by rw [hsplit] have hlast : w lastIdx * (skiOpt B (days lastIdx) : ℝ) = q ^ (B - 1) * (B : ℝ) := by have hw_last : w lastIdx = q ^ (B - 1) := by dsimp only [w, lastIdx]; rw [if_neg (by omega)] have hd_last : days lastIdx = 2 * B := by dsimp only [days, lastIdx]; rw [if_neg (by omega)] rw [hw_last, hd_last]; dsimp only [skiOpt]; rw [min_eq_right (by omega)] have hinit : ∑ t ∈ Finset.range (B - 1), (if h : t < B then w ⟨t, h⟩ * (skiOpt B (days ⟨t, h⟩) : ℝ) else 0) = (B : ℝ) * (1 - q ^ (B - 1)) - ((B - 1 : ℕ) : ℝ) * q ^ (B - 1) := by have hterm : ∑ t ∈ Finset.range (B - 1), (if h : t < B then w ⟨t, h⟩ * (skiOpt B (days ⟨t, h⟩) : ℝ) else 0) = ∑ t ∈ Finset.range (B - 1), ((t : ℝ) + 1) * (1 - q) * q ^ t := by refine Finset.sum_congr rfl (fun t ht => ?_) have htlt := Finset.mem_range.mp ht rw [dif_pos (by omega)] have hwt : w ⟨t, by omega⟩ = (1 - q) * q ^ t := by dsimp only [w]; rw [if_pos (by omega)] have hdt : days ⟨t, by omega⟩ = t + 1 := by dsimp only [days]; rw [if_pos (by omega)] rw [hwt, hdt]; dsimp only [skiOpt]; rw [min_eq_left (by omega)] push_cast; ring rw [hterm, weighted_geom_sum_telescoping (B - 1), hgeomB (B - 1)] rw [hinit, hlast, Nat.cast_sub hB] have hqpow : q * q ^ (B - 1) = q ^ B := by rw [← pow_succ', Nat.sub_add_cancel hB] have hB1 : ((B : ℝ) - 1) * q ^ (B - 1) = (B : ℝ) * q ^ B := by have : (B : ℝ) * q = (B : ℝ) - 1 := by dsimp only [q]; field_simp [ne_of_gt hBpos] calc ((B : ℝ) - 1) * q ^ (B - 1) = ((B : ℝ) * q) * q ^ (B - 1) := by rw [this] _ = (B : ℝ) * (q * q ^ (B - 1)) := by ring _ = (B : ℝ) * q ^ B := by rw [hqpow] dsimp only [Z] push_cast linarith -- Step 2: For every `1 ≤ k ≤ B`, exact `E_w[skiCost B k] = B`! have hEcost_leB : ∀ k : ℕ, 1 ≤ k → k ≤ B → ∑ t : Fin B, w t * (skiCost B k (days t) : ℝ) = (B : ℝ) := by intro k hk1 hkB rw [hsplit] have hlast : w lastIdx * (skiCost B k (days lastIdx) : ℝ) = q ^ (B - 1) * (((k - 1 : ℕ) : ℝ) + (B : ℝ)) := by have hw_last : w lastIdx = q ^ (B - 1) := by dsimp only [w, lastIdx]; rw [if_neg (by omega)] have hd_last : days lastIdx = 2 * B := by dsimp only [days, lastIdx]; rw [if_neg (by omega)] rw [hw_last, hd_last]; dsimp only [skiCost]; rw [if_neg (by omega)] push_cast; rfl -- Split `range (B - 1)` into `t < k - 1` (renting) and `k - 1 ≤ t < B - 1` (buying)! have hrange : ∑ t ∈ Finset.range (B - 1), (if h : t < B then w ⟨t, h⟩ * (skiCost B k (days ⟨t, h⟩) : ℝ) else 0) = (∑ t ∈ Finset.range (k - 1), ((t : ℝ) + 1) * (1 - q) * q ^ t) + (((k - 1 : ℕ) : ℝ) + (B : ℝ)) * (∑ t ∈ Finset.Ico (k - 1) (B - 1), (1 - q) * q ^ t) := by have hpart : Finset.range (B - 1) = Finset.range (k - 1) ∪ Finset.Ico (k - 1) (B - 1) := by ext t; simp only [Finset.mem_range, Finset.mem_union, Finset.mem_Ico]; omega have hdisj : Disjoint (Finset.range (k - 1)) (Finset.Ico (k - 1) (B - 1)) := by rw [Finset.disjoint_left]; intro t h1 h2 simp only [Finset.mem_range, Finset.mem_Ico] at h1 h2; omega rw [hpart, Finset.sum_union hdisj, Finset.mul_sum] congr 1 · refine Finset.sum_congr rfl (fun t ht => ?_) have htlt := Finset.mem_range.mp ht rw [dif_pos (by omega)] have hwt : w ⟨t, by omega⟩ = (1 - q) * q ^ t := by dsimp only [w]; rw [if_pos (by omega)] have hdt : days ⟨t, by omega⟩ = t + 1 := by dsimp only [days]; rw [if_pos (by omega)] rw [hwt, hdt]; dsimp only [skiCost]; rw [if_pos (by omega)] push_cast; ring · refine Finset.sum_congr rfl (fun t ht => ?_) have htmem := Finset.mem_Ico.mp ht rw [dif_pos (by omega)] have hwt : w ⟨t, by omega⟩ = (1 - q) * q ^ t := by dsimp only [w]; rw [if_pos (by omega)] have hdt : days ⟨t, by omega⟩ = t + 1 := by dsimp only [days]; rw [if_pos (by omega)] rw [hwt, hdt]; dsimp only [skiCost]; rw [if_neg (by omega)] push_cast; ring have htail_tele : (∑ t ∈ Finset.Ico (k - 1) (B - 1), (1 - q) * q ^ t) + q ^ (B - 1) = q ^ (k - 1) := by have h1 : ∑ t ∈ Finset.Ico (k - 1) (B - 1), (1 - q) * q ^ t = (1 - q) * ∑ t ∈ Finset.range (B - 1), q ^ t - (1 - q) * ∑ t ∈ Finset.range (k - 1), q ^ t := by rw [← Finset.mul_sum, ← mul_sub] congr 1 have hpart : Finset.range (B - 1) = Finset.range (k - 1) ∪ Finset.Ico (k - 1) (B - 1) := by ext t; simp only [Finset.mem_range, Finset.mem_union, Finset.mem_Ico]; omega have hdisj : Disjoint (Finset.range (k - 1)) (Finset.Ico (k - 1) (B - 1)) := by rw [Finset.disjoint_left]; intro t h1 h2 simp only [Finset.mem_range, Finset.mem_Ico] at h1 h2; omega rw [hpart, Finset.sum_union hdisj]; ring rw [h1, geom_sum_one_sub_q (B - 1), geom_sum_one_sub_q (k - 1)]; ring rw [hrange, hlast, weighted_geom_sum_telescoping (k - 1), hgeomB (k - 1)] have hcombine : (((k - 1 : ℕ) : ℝ) + (B : ℝ)) * (∑ t ∈ Finset.Ico (k - 1) (B - 1), (1 - q) * q ^ t) + q ^ (B - 1) * (((k - 1 : ℕ) : ℝ) + (B : ℝ)) = (((k - 1 : ℕ) : ℝ) + (B : ℝ)) * q ^ (k - 1) := by calc (((k - 1 : ℕ) : ℝ) + (B : ℝ)) * (∑ t ∈ Finset.Ico (k - 1) (B - 1), (1 - q) * q ^ t) + q ^ (B - 1) * (((k - 1 : ℕ) : ℝ) + (B : ℝ)) = (((k - 1 : ℕ) : ℝ) + (B : ℝ)) * ((∑ t ∈ Finset.Ico (k - 1) (B - 1), (1 - q) * q ^ t) + q ^ (B - 1)) := by ring _ = (((k - 1 : ℕ) : ℝ) + (B : ℝ)) * q ^ (k - 1) := by rw [htail_tele] linarith refine ⟨w, days, hw_nn, hdays_pos, by rw [hEopt]; positivity, fun k hk => ?_⟩ rw [hEopt] have hRB : R_B * ((B : ℝ) * Z) = (B : ℝ) := by have hRBeq : R_B = 1 / Z := rfl rw [hRBeq] field_simp [ne_of_gt hZpos] rw [hRB] by_cases hkB : k ≤ B · rw [hEcost_leB k hk hkB] · -- For `k > B`, pointwise `skiCost B B (days t) ≤ skiCost B k (days t)` for EVERY `t`! have hpw : ∀ t : Fin B, skiCost B B (days t) ≤ skiCost B k (days t) := by intro t dsimp only [days, skiCost] split_ifs <;> omega calc (B : ℝ) = ∑ t : Fin B, w t * (skiCost B B (days t) : ℝ) := (hEcost_leB B hB le_rfl).symm _ ≤ ∑ t : Fin B, w t * (skiCost B k (days t) : ℝ) := Finset.sum_le_sum (fun t _ => mul_le_mul_of_nonneg_left (by exact_mod_cast hpw t) (hw_nn t)) /-- **STRICT NON-ASYMPTOTIC SEPARATION: RANDOMIZED SKI-RENTAL BEATS DETERMINISTIC**: 1. For ALL `B ≥ 1`: `R_B < e / (e - 1)`; 2. For ALL `B ≥ 2`: `R_B < 2 - 1 / B`; 3. For ALL `B ≥ 3`: `e / (e - 1) < 2 - 1 / B`! -/ theorem ski_rental_randomized_beats_deterministic_strict : (∀ B : ℕ, 1 ≤ B → 1 / (1 - (((B : ℝ) - 1) / (B : ℝ)) ^ B) < Real.exp 1 / (Real.exp 1 - 1)) ∧ (∀ B : ℕ, 2 ≤ B → 1 / (1 - (((B : ℝ) - 1) / (B : ℝ)) ^ B) < 2 - 1 / (B : ℝ)) ∧ (∀ B : ℕ, 3 ≤ B → Real.exp 1 / (Real.exp 1 - 1) < 2 - 1 / (B : ℝ)) := by have h1 : ∀ B : ℕ, 1 ≤ B → 1 / (1 - (((B : ℝ) - 1) / (B : ℝ)) ^ B) < Real.exp 1 / (Real.exp 1 - 1) := by intro B hB have hpow := one_sub_inv_pow_lt_exp_neg_one hB have hepos := Real.exp_pos 1 have hegt := Real.exp_one_gt_d9 have he1_pos : 0 < 1 - Real.exp (-1) := by rw [Real.exp_neg, inv_eq_one_div] have : 1 / Real.exp 1 < 1 := (div_lt_one hepos).mpr (by linarith) linarith have hden_lt : 1 - Real.exp (-1) < 1 - (((B : ℝ) - 1) / (B : ℝ)) ^ B := by linarith have hinv : 1 / (1 - (((B : ℝ) - 1) / (B : ℝ)) ^ B) < 1 / (1 - Real.exp (-1)) := one_div_lt_one_div_of_lt he1_pos hden_lt have heq : 1 / (1 - Real.exp (-1)) = Real.exp 1 / (Real.exp 1 - 1) := by rw [Real.exp_neg, inv_eq_one_div] field_simp [ne_of_gt hepos, ne_of_gt (by linarith : 0 < Real.exp 1 - 1)] linarith have h3 : ∀ B : ℕ, 3 ≤ B → Real.exp 1 / (Real.exp 1 - 1) < 2 - 1 / (B : ℝ) := by intro B hB have hegt := Real.exp_one_gt_d9 have hlt53 : Real.exp 1 / (Real.exp 1 - 1) < 5 / 3 := by rw [div_lt_div_iff₀ (by linarith) (by norm_num)]; linarith have h53_le : (5 / 3 : ℝ) ≤ 2 - 1 / (B : ℝ) := by have hB3 : (3 : ℝ) ≤ (B : ℝ) := by exact_mod_cast hB have hdiv : 1 / (B : ℝ) ≤ 1 / 3 := one_div_le_one_div_of_le (by norm_num) hB3 linarith linarith have h2 : ∀ B : ℕ, 2 ≤ B → 1 / (1 - (((B : ℝ) - 1) / (B : ℝ)) ^ B) < 2 - 1 / (B : ℝ) := by intro B hB rcases eq_or_lt_of_le hB with rfl | hB3 · norm_num · exact lt_trans (h1 B (by omega)) (h3 B hB3) exact ⟨h1, h2, h3⟩ /-! ### Pillar 4B: Dynkin Secretary Problem Exact Permutation Formula & `> 1 / e` Theorem -/ /-- Event that the reject-first-`r` secretary threshold rule hires the global best candidate (rank `0`) under arrival permutation `σ : Equiv.Perm (Fin n)`. -/ def SecretaryWins {n : ℕ} (r : ℕ) (hn : 0 < n) (σ : Equiv.Perm (Fin n)) : Prop := ∃ j : Fin n, r ≤ j.1 ∧ σ j = ⟨0, hn⟩ ∧ ∀ i : Fin n, r ≤ i.1 → i.1 < j.1 → ∃ l : Fin n, l.1 < r ∧ (σ l).1 < (σ i).1 instance instDecidableSecretaryWins {n : ℕ} (r : ℕ) (hn : 0 < n) (σ : Equiv.Perm (Fin n)) : Decidable (@SecretaryWins n r hn σ) := by unfold SecretaryWins; infer_instance /-- Closed-form Dynkin secretary winning probability `P_n(r) = (r / n) ∑_{j = r}^{n - 1} 1 / j`. -/ noncomputable def secretaryProb (n r : ℕ) : ℝ := ((r : ℝ) / (n : ℝ)) * ∑ j ∈ Finset.Ico r n, (1 / (j : ℝ)) /-- **DYNKIN SECRETARY EXACT PERMUTATION-COUNTING FORMULA**: For EVERY `n ≥ 1` and `1 ≤ r < n`, the exact fraction of all `n!` permutations on which the reject-first-`r` rule selects the best candidate equals `(r / n) ∑_{j = r}^{n - 1} 1 / j`! -/ theorem dynkin_secretary_exact_permutation_prob {n r : ℕ} (hn : 0 < n) (hr1 : 1 ≤ r) (hrn : r < n) : ((Finset.univ.filter (fun σ : Equiv.Perm (Fin n) => @SecretaryWins n r hn σ)).card : ℝ) / (n.factorial : ℝ) = secretaryProb n r := by let W : Fin n → Finset (Equiv.Perm (Fin n)) := fun j => Finset.univ.filter (fun σ : Equiv.Perm (Fin n) => r ≤ j.1 ∧ σ j = ⟨0, hn⟩ ∧ ∀ i : Fin n, r ≤ i.1 → i.1 < j.1 → ∃ l : Fin n, l.1 < r ∧ (σ l).1 < (σ i).1) -- Step 1: Partition winning permutations by the unique arrival position `j` of rank `0`! have hpart : Finset.univ.filter (fun σ : Equiv.Perm (Fin n) => @SecretaryWins n r hn σ) = (Finset.univ.filter (fun j : Fin n => r ≤ j.1)).biUnion W := by ext σ simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_biUnion, W] exact ⟨fun ⟨j, hj1, hj2, hj3⟩ => ⟨j, hj1, hj1, hj2, hj3⟩, fun ⟨j, _, hj1, hj2, hj3⟩ => ⟨j, hj1, hj2, hj3⟩⟩ have hdisj : Set.PairwiseDisjoint ((Finset.univ.filter (fun j : Fin n => r ≤ j.1)) : Set (Fin n)) W := by intro j₁ _ j₂ _ hne dsimp only [Function.onFun] rw [Finset.disjoint_left] intro σ h1 h2 have e1 := (Finset.mem_filter.mp h1).2.2.1 have e2 := (Finset.mem_filter.mp h2).2.2.1 exact hne (σ.injective (e1.trans e2.symm)) have hcard_sum : (Finset.univ.filter (fun σ : Equiv.Perm (Fin n) => @SecretaryWins n r hn σ)).card = ∑ j ∈ Finset.univ.filter (fun j : Fin n => r ≤ j.1), (W j).card := by rw [hpart, Finset.card_biUnion hdisj] -- Step 2: For each `j` with `r ≤ j.1`, prove `n * j.1 * (W j).card = r * n!` via `Equiv.swap`! have hfiber : ∀ j : Fin n, r ≤ j.1 → n * j.1 * (W j).card = r * n.factorial := by intro j hjr -- Substep 2a: `S_j = {σ | σ j = 0}` satisfies `n * S_j.card = n!`! let S_j : Finset (Equiv.Perm (Fin n)) := Finset.univ.filter (fun σ => σ j = ⟨0, hn⟩) have hSj_card : n * S_j.card = n.factorial := by have hbiU : (Finset.univ : Finset (Equiv.Perm (Fin n))) = Finset.univ.biUnion (fun y : Fin n => Finset.univ.filter (fun σ : Equiv.Perm (Fin n) => σ j = y)) := by ext σ; simp have hsame : ∀ y : Fin n, (Finset.univ.filter (fun σ : Equiv.Perm (Fin n) => σ j = y)).card = S_j.card := by intro y refine Finset.card_bij (fun σ _ => Equiv.swap y ⟨0, hn⟩ * σ) ?_ ?_ ?_ · intro σ hσ refine Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_⟩ have hσy : σ j = y := (Finset.mem_filter.mp hσ).2 change Equiv.swap y ⟨0, hn⟩ (σ j) = ⟨0, hn⟩ rw [hσy, Equiv.swap_apply_left] · intro σ₁ _ σ₂ _ heq; exact mul_left_cancel heq · intro τ hτ refine ⟨Equiv.swap y ⟨0, hn⟩ * τ, Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_⟩, ?_⟩ · have hτ0 : τ j = ⟨0, hn⟩ := (Finset.mem_filter.mp hτ).2 change Equiv.swap y ⟨0, hn⟩ (τ j) = y rw [hτ0, Equiv.swap_apply_right] · simp [← mul_assoc] have htot : (Finset.univ : Finset (Equiv.Perm (Fin n))).card = n * S_j.card := by conv_lhs => rw [hbiU] rw [Finset.card_biUnion] · simp_rw [hsame]; simp · intro y₁ _ y₂ _ hne dsimp only [Function.onFun]; rw [Finset.disjoint_left]; intro σ h1 h2 exact hne ((Finset.mem_filter.mp h1).2.symm.trans (Finset.mem_filter.mp h2).2) rw [Finset.card_univ, Fintype.card_perm, Fintype.card_fin] at htot exact htot.symm -- Substep 2b: Partition `S_j` by position `p ∈ A_j = {i | i.1 < j.1}` of prefix minimum! let A_j : Finset (Fin n) := Finset.univ.filter (fun i : Fin n => i.1 < j.1) have hAj_card : A_j.card = j.1 := by have hbij : A_j.card = (Finset.range j.1).card := by refine Finset.card_bij (fun i _ => i.1) (fun i hi => Finset.mem_range.mpr (Finset.mem_filter.mp hi).2) (fun _ _ _ _ h => Fin.ext h) (fun m hm => ⟨⟨m, by have := Finset.mem_range.mp hm; omega⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, Finset.mem_range.mp hm⟩, rfl⟩) rw [hbij, Finset.card_range] let M : Fin n → Finset (Equiv.Perm (Fin n)) := fun p => S_j.filter (fun σ => ∀ i ∈ A_j, (σ p).1 ≤ (σ i).1) have hM_disj : Set.PairwiseDisjoint (A_j : Set (Fin n)) M := by intro p₁ hp₁ p₂ hp₂ hne dsimp only [Function.onFun]; rw [Finset.disjoint_left]; intro σ h1 h2 have le1 := (Finset.mem_filter.mp h1).2 p₂ hp₂ have le2 := (Finset.mem_filter.mp h2).2 p₁ hp₁ exact hne (σ.injective (Fin.ext (le_antisymm le1 le2))) have hM_part : S_j = A_j.biUnion M := by ext σ constructor · intro hσ have hAne : A_j.Nonempty := ⟨⟨0, hn⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, by change 0 < j.1; omega⟩⟩ obtain ⟨p, hpA, hpmin⟩ := Finset.exists_min_image A_j (fun i => (σ i).1) hAne exact Finset.mem_biUnion.mpr ⟨p, hpA, Finset.mem_filter.mpr ⟨hσ, hpmin⟩⟩ · intro h obtain ⟨p, _, hpM⟩ := Finset.mem_biUnion.mp h exact (Finset.mem_filter.mp hpM).1 have hM_same : ∀ p₁ ∈ A_j, ∀ p₂ ∈ A_j, (M p₁).card = (M p₂).card := by intro p₁ hp₁ p₂ hp₂ have hp1_lt : p₁.1 < j.1 := (Finset.mem_filter.mp hp₁).2 have hp2_lt : p₂.1 < j.1 := (Finset.mem_filter.mp hp₂).2 have hp1_ne : p₁ ≠ j := fun h => by subst h; omega have hp2_ne : p₂ ≠ j := fun h => by subst h; omega have hswap_Aj : ∀ i : Fin n, Equiv.swap p₁ p₂ i ∈ A_j ↔ i ∈ A_j := by intro i by_cases h1 : i = p₁ · subst h1; simp [Equiv.swap_apply_left, hp₁, hp₂] · by_cases h2 : i = p₂ · subst h2; simp [Equiv.swap_apply_right, hp₁, hp₂] · rw [Equiv.swap_apply_of_ne_of_ne h1 h2] refine Finset.card_bij (fun σ _ => σ * Equiv.swap p₁ p₂) ?_ ?_ ?_ · intro σ hσ have hσS : σ ∈ S_j := (Finset.mem_filter.mp hσ).1 have hσmin := (Finset.mem_filter.mp hσ).2 refine Finset.mem_filter.mpr ⟨Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_⟩, ?_⟩ · change σ (Equiv.swap p₁ p₂ j) = ⟨0, hn⟩ rw [Equiv.swap_apply_of_ne_of_ne hp1_ne.symm hp2_ne.symm] exact (Finset.mem_filter.mp hσS).2 · intro i hi change (σ (Equiv.swap p₁ p₂ p₂)).1 ≤ (σ (Equiv.swap p₁ p₂ i)).1 rw [Equiv.swap_apply_right] exact hσmin _ ((hswap_Aj i).mpr hi) · intro σ₁ _ σ₂ _ heq; exact mul_right_cancel heq · intro τ hτ have hτS : τ ∈ S_j := (Finset.mem_filter.mp hτ).1 have hτmin := (Finset.mem_filter.mp hτ).2 refine ⟨τ * Equiv.swap p₁ p₂, Finset.mem_filter.mpr ⟨Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_⟩, ?_⟩, by simp [mul_assoc]⟩ · change τ (Equiv.swap p₁ p₂ j) = ⟨0, hn⟩ rw [Equiv.swap_apply_of_ne_of_ne hp1_ne.symm hp2_ne.symm] exact (Finset.mem_filter.mp hτS).2 · intro i hi change (τ (Equiv.swap p₁ p₂ p₁)).1 ≤ (τ (Equiv.swap p₁ p₂ i)).1 rw [Equiv.swap_apply_left] exact hτmin _ ((hswap_Aj i).mpr hi) -- Substep 2c: `W j` is the exact disjoint union of `M p` over `p ∈ R_r = {i | i.1 < r}`! let R_r : Finset (Fin n) := Finset.univ.filter (fun i : Fin n => i.1 < r) have hRr_sub : R_r ⊆ A_j := by intro i hi; exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, by have := (Finset.mem_filter.mp hi).2; omega⟩ have hRr_card : R_r.card = r := by have hbij : R_r.card = (Finset.range r).card := by refine Finset.card_bij (fun i _ => i.1) (fun i hi => Finset.mem_range.mpr (Finset.mem_filter.mp hi).2) (fun _ _ _ _ h => Fin.ext h) (fun m hm => ⟨⟨m, by have := Finset.mem_range.mp hm; omega⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, Finset.mem_range.mp hm⟩, rfl⟩) rw [hbij, Finset.card_range] have hWj_eq : W j = R_r.biUnion M := by ext σ constructor · intro hσ have hmem := (Finset.mem_filter.mp hσ).2 have hσS : σ ∈ S_j := Finset.mem_filter.mpr ⟨Finset.mem_univ _, hmem.2.1⟩ have hAne : A_j.Nonempty := ⟨⟨0, hn⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, by change 0 < j.1; omega⟩⟩ obtain ⟨p, hpA, hpmin⟩ := Finset.exists_min_image A_j (fun i => (σ i).1) hAne have hpr : p.1 < r := by by_contra hge push Not at hge obtain ⟨l, hlr, hlt⟩ := hmem.2.2 p hge (Finset.mem_filter.mp hpA).2 have hlA : l ∈ A_j := Finset.mem_filter.mpr ⟨Finset.mem_univ _, by omega⟩ have hle := hpmin l hlA omega exact Finset.mem_biUnion.mpr ⟨p, Finset.mem_filter.mpr ⟨Finset.mem_univ _, hpr⟩, Finset.mem_filter.mpr ⟨hσS, hpmin⟩⟩ · intro h obtain ⟨p, hpR, hpM⟩ := Finset.mem_biUnion.mp h have hp_lt : p.1 < r := (Finset.mem_filter.mp hpR).2 have hσS : σ ∈ S_j := (Finset.mem_filter.mp hpM).1 have hσmin := (Finset.mem_filter.mp hpM).2 refine Finset.mem_filter.mpr ⟨Finset.mem_univ _, hjr, (Finset.mem_filter.mp hσS).2, ?_⟩ intro i hir hij have hiA : i ∈ A_j := Finset.mem_filter.mpr ⟨Finset.mem_univ _, hij⟩ have hle := hσmin i hiA have hne : p ≠ i := fun heq => by subst heq; omega have hne_val : (σ p).1 ≠ (σ i).1 := fun hv => hne (σ.injective (Fin.ext hv)) exact ⟨p, hp_lt, by omega⟩ let p0 : Fin n := ⟨0, hn⟩ have hp0A : p0 ∈ A_j := Finset.mem_filter.mpr ⟨Finset.mem_univ _, by change 0 < j.1; omega⟩ have hSj_mul : S_j.card = j.1 * (M p0).card := by rw [hM_part, Finset.card_biUnion hM_disj] calc ∑ p ∈ A_j, (M p).card = ∑ _p ∈ A_j, (M p0).card := Finset.sum_congr rfl (fun p hp => hM_same p hp p0 hp0A) _ = j.1 * (M p0).card := by simp [hAj_card] have hWj_mul : (W j).card = r * (M p0).card := by rw [hWj_eq, Finset.card_biUnion (hM_disj.subset (by exact_mod_cast hRr_sub))] calc ∑ p ∈ R_r, (M p).card = ∑ _p ∈ R_r, (M p0).card := Finset.sum_congr rfl (fun p hp => hM_same p (hRr_sub hp) p0 hp0A) _ = r * (M p0).card := by simp [hRr_card] calc n * j.1 * (W j).card = n * j.1 * (r * (M p0).card) := by rw [hWj_mul] _ = r * (n * (j.1 * (M p0).card)) := by ring _ = r * (n * S_j.card) := by rw [← hSj_mul] _ = r * n.factorial := by rw [hSj_card] -- Step 3: Sum over `j ∈ Ico r n`! have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr hn have hnfpos : (0 : ℝ) < (n.factorial : ℝ) := Nat.cast_pos.mpr (Nat.factorial_pos n) rw [hcard_sum, Nat.cast_sum, Finset.sum_div] dsimp only [secretaryProb] rw [Finset.mul_sum] refine Finset.sum_bij (fun j _ => j.1) (fun j hj => Finset.mem_Ico.mpr ⟨(Finset.mem_filter.mp hj).2, j.2⟩) (fun _ _ _ _ h => Fin.ext h) (fun m hm => ⟨⟨m, (Finset.mem_Ico.mp hm).2⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, (Finset.mem_Ico.mp hm).1⟩, rfl⟩) (fun j hj => ?_) have hjr : r ≤ j.1 := (Finset.mem_filter.mp hj).2 have hjpos : (0 : ℝ) < (j.1 : ℝ) := Nat.cast_pos.mpr (by omega) have hR : (n : ℝ) * (j.1 : ℝ) * ((W j).card : ℝ) = (r : ℝ) * (n.factorial : ℝ) := by exact_mod_cast hfiber j hjr field_simp [ne_of_gt hnpos, ne_of_gt hjpos, ne_of_gt hnfpos] linarith /-- **DYNKIN SECRETARY EXACT DISCRETE DIFFERENCE & UNIMODALITY IDENTITY**: `P_n(r + 1) - P_n(r) = (1 / n) · (∑_{j = r + 1}^{n - 1} 1 / j - 1)`! -/ theorem dynkin_secretary_unimodality_diff {n r : ℕ} (hr1 : 1 ≤ r) (hrn : r < n) : secretaryProb n (r + 1) - secretaryProb n r = (1 / (n : ℝ)) * ((∑ j ∈ Finset.Ico (r + 1) n, (1 / (j : ℝ))) - 1) := by dsimp only [secretaryProb] rw [Finset.sum_eq_sum_Ico_succ_bot hrn] have hrpos : (0 : ℝ) < (r : ℝ) := Nat.cast_pos.mpr (by omega) have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) push_cast field_simp [ne_of_gt hrpos, ne_of_gt hnpos] ring private lemma log_one_add_le_quad {t : ℝ} (ht : 0 < t) : Real.log (1 + t) ≤ t - t ^ 2 / (2 * (1 + t)) := by set u : ℝ := t - t ^ 2 / (2 * (1 + t)) have h1t : 0 < 1 + t := by linarith have hu_nn : 0 ≤ u := by have heq : u = t * (2 + t) / (2 * (1 + t)) := by dsimp only [u]; field_simp [ne_of_gt h1t]; ring rw [heq]; positivity have htaylor : 1 + u + u ^ 2 / 2 ≤ Real.exp u := by have h := Real.sum_le_exp_of_nonneg hu_nn 3 have h3 : ∑ i ∈ Finset.range 3, u ^ i / (i.factorial : ℝ) = 1 + u + u ^ 2 / 2 := by simp [Finset.sum_range_succ, Nat.factorial] linarith have hgap : 1 + t ≤ 1 + u + u ^ 2 / 2 := by have hid : (1 + u + u ^ 2 / 2) - (1 + t) = t ^ 4 / (8 * (1 + t) ^ 2) := by dsimp only [u]; field_simp [ne_of_gt h1t]; ring have hnn : 0 ≤ t ^ 4 / (8 * (1 + t) ^ 2) := by positivity linarith have hle : 1 + t ≤ Real.exp u := le_trans hgap htaylor have hlog := Real.log_le_log h1t hle rwa [Real.log_exp] at hlog private lemma secretary_harmonic_lower_bound {n r : ℕ} (hr1 : 1 ≤ r) (hrn : r ≤ n) : Real.log ((n : ℝ) / (r : ℝ)) + (1 / (2 * (r : ℝ)) - 1 / (2 * (n : ℝ))) ≤ ∑ j ∈ Finset.Ico r n, (1 / (j : ℝ)) := by induction n with | zero => omega | succ k ih => rcases eq_or_lt_of_le hrn with rfl | hlt · simp · have hrk : r ≤ k := by omega have hprev := ih hrk have hkpos : (0 : ℝ) < (k : ℝ) := Nat.cast_pos.mpr (by omega) have hrpos : (0 : ℝ) < (r : ℝ) := Nat.cast_pos.mpr (by omega) have hk1pos : (0 : ℝ) < ((k + 1 : ℕ) : ℝ) := by positivity have hstep := log_one_add_le_quad (t := 1 / (k : ℝ)) (div_pos zero_lt_one hkpos) have h1t : (1 : ℝ) + 1 / (k : ℝ) = ((k + 1 : ℕ) : ℝ) / (k : ℝ) := by push_cast; field_simp [ne_of_gt hkpos] have hquad : (1 / (k : ℝ)) ^ 2 / (2 * (1 + 1 / (k : ℝ))) = 1 / (2 * (k : ℝ)) - 1 / (2 * ((k + 1 : ℕ) : ℝ)) := by push_cast; field_simp [ne_of_gt hkpos, ne_of_gt (by positivity : (0 : ℝ) < (k : ℝ) + 1)]; ring rw [hquad, h1t] at hstep have hlog_add : Real.log (((k + 1 : ℕ) : ℝ) / (r : ℝ)) = Real.log ((k : ℝ) / (r : ℝ)) + Real.log (((k + 1 : ℕ) : ℝ) / (k : ℝ)) := by rw [← Real.log_mul (ne_of_gt (div_pos hkpos hrpos)) (ne_of_gt (div_pos hk1pos hkpos))] congr 1; field_simp [ne_of_gt hkpos, ne_of_gt hrpos] rw [Finset.sum_Ico_succ_top hrk, hlog_add] linarith /-- **DYNKIN SECRETARY NON-ASYMPTOTIC `> 1 / e` THEOREM FOR ALL `n ≥ 2`**: For **EVERY** `n ≥ 2`, there exists a threshold `1 ≤ r < n` achieving strictly better than `1 / e` probability of hiring the globally best candidate (`1 / e < P_n(r)`)! -/ theorem dynkin_secretary_gt_inv_e {n : ℕ} (hn : 2 ≤ n) : ∃ r : ℕ, 1 ≤ r ∧ r < n ∧ 1 / Real.exp 1 < secretaryProb n r := by have he_gt := Real.exp_one_gt_d9 have he_lt := Real.exp_one_lt_d9 have hepos := Real.exp_pos 1 have hinv_e_lt : 1 / Real.exp 1 < 368 / 1000 := by rw [div_lt_iff₀ hepos]; linarith by_cases hsmall : n ≤ 11 · interval_cases n · refine ⟨1, by omega, by omega, ?_⟩ have : secretaryProb 2 1 = 1 / 2 := by dsimp only [secretaryProb]; norm_num [Finset.sum_Ico_succ_top] linarith · refine ⟨1, by omega, by omega, ?_⟩ have : secretaryProb 3 1 = 1 / 2 := by dsimp only [secretaryProb]; norm_num [Finset.sum_Ico_succ_top] linarith · refine ⟨1, by omega, by omega, ?_⟩ have : secretaryProb 4 1 = 11 / 24 := by dsimp only [secretaryProb]; norm_num [Finset.sum_Ico_succ_top] linarith · refine ⟨2, by omega, by omega, ?_⟩ have : secretaryProb 5 2 = 13 / 30 := by dsimp only [secretaryProb]; norm_num [Finset.sum_Ico_succ_top] linarith · refine ⟨2, by omega, by omega, ?_⟩ have : secretaryProb 6 2 = 77 / 180 := by dsimp only [secretaryProb]; norm_num [Finset.sum_Ico_succ_top] linarith · refine ⟨2, by omega, by omega, ?_⟩ have : secretaryProb 7 2 = 29 / 70 := by dsimp only [secretaryProb]; norm_num [Finset.sum_Ico_succ_top] linarith · refine ⟨3, by omega, by omega, ?_⟩ have : secretaryProb 8 3 = 459 / 1120 := by dsimp only [secretaryProb]; norm_num [Finset.sum_Ico_succ_top] linarith · refine ⟨3, by omega, by omega, ?_⟩ have : secretaryProb 9 3 = 341 / 840 := by dsimp only [secretaryProb]; norm_num [Finset.sum_Ico_succ_top] linarith · refine ⟨3, by omega, by omega, ?_⟩ have : secretaryProb 10 3 = 10047 / 25200 := by dsimp only [secretaryProb]; norm_num [Finset.sum_Ico_succ_top] linarith · refine ⟨4, by omega, by omega, ?_⟩ have : secretaryProb 11 4 = 2761 / 6930 := by dsimp only [secretaryProb]; norm_num [Finset.sum_Ico_succ_top] linarith · -- Analytic proof for ALL `n ≥ 12` via `r = ⌊n / e⌋ + 1`! have hn12 : 12 ≤ n := by omega have hnR12 : (12 : ℝ) ≤ (n : ℝ) := by exact_mod_cast hn12 have hnpos : (0 : ℝ) < (n : ℝ) := by linarith set r : ℕ := Nat.floor ((n : ℝ) / Real.exp 1) + 1 have hr_gt : (n : ℝ) / Real.exp 1 < (r : ℝ) := by dsimp only [r]; push_cast; exact Nat.lt_floor_add_one _ have hr_le : (r : ℝ) ≤ (n : ℝ) / Real.exp 1 + 1 := by dsimp only [r]; push_cast linarith [Nat.floor_le (div_nonneg (le_of_lt hnpos) (le_of_lt hepos))] have hr1 : 1 ≤ r := Nat.succ_le_succ (Nat.zero_le _) have hrn : r < n := by have hltR : (r : ℝ) < (n : ℝ) := by have : (n : ℝ) / Real.exp 1 < (n : ℝ) / 2 := div_lt_div_of_pos_left hnpos (by norm_num) (by linarith) linarith exact_mod_cast hltR refine ⟨r, hr1, hrn, ?_⟩ set x : ℝ := (r : ℝ) / (n : ℝ) have hx_gt : 1 / Real.exp 1 < x := by dsimp only [x]; rw [div_lt_div_iff₀ hepos hnpos]; rw [div_lt_iff₀ hepos] at hr_gt; linarith have hx_le : x ≤ 1 / Real.exp 1 + 1 / (n : ℝ) := by dsimp only [x] have h1 : (r : ℝ) / (n : ℝ) ≤ ((n : ℝ) / Real.exp 1 + 1) / (n : ℝ) := div_le_div_of_nonneg_right hr_le (le_of_lt hnpos) have h2 : ((n : ℝ) / Real.exp 1 + 1) / (n : ℝ) = 1 / Real.exp 1 + 1 / (n : ℝ) := by field_simp [ne_of_gt hepos, ne_of_gt hnpos] linarith have hx_pos : 0 < x := lt_trans (div_pos zero_lt_one hepos) hx_gt have hharm := secretary_harmonic_lower_bound hr1 (le_of_lt hrn) have hprob_ge : x * Real.log (1 / x) + (1 - x) / (2 * (n : ℝ)) ≤ secretaryProb n r := by dsimp only [secretaryProb] have hmul := mul_le_mul_of_nonneg_left hharm (le_of_lt hx_pos) have h1x : 1 / x = (n : ℝ) / (r : ℝ) := by dsimp only [x]; field_simp [ne_of_gt hnpos, ne_of_gt (Nat.cast_pos.mpr (by omega : 0 < r))] have hcorr : x * (1 / (2 * (r : ℝ)) - 1 / (2 * (n : ℝ))) = (1 - x) / (2 * (n : ℝ)) := by dsimp only [x] have hrpos : (0 : ℝ) < (r : ℝ) := Nat.cast_pos.mpr (by omega) field_simp [ne_of_gt hnpos, ne_of_gt hrpos] rw [h1x] have hdist : x * (Real.log ((n : ℝ) / (r : ℝ)) + (1 / (2 * (r : ℝ)) - 1 / (2 * (n : ℝ)))) = x * Real.log ((n : ℝ) / (r : ℝ)) + (1 - x) / (2 * (n : ℝ)) := by rw [mul_add, hcorr] linarith -- Write `x = (1 + s) / e` with `0 < s ≤ e / n`: set s : ℝ := Real.exp 1 * x - 1 have hs_pos : 0 < s := by dsimp only [s]; have := (div_lt_iff₀ hepos).mp hx_gt; linarith have hs_le : s ≤ Real.exp 1 / (n : ℝ) := by dsimp only [s] have h1 : Real.exp 1 * x ≤ Real.exp 1 * (1 / Real.exp 1 + 1 / (n : ℝ)) := mul_le_mul_of_nonneg_left hx_le (le_of_lt hepos) have h2 : Real.exp 1 * (1 / Real.exp 1 + 1 / (n : ℝ)) = 1 + Real.exp 1 / (n : ℝ) := by field_simp [ne_of_gt hepos, ne_of_gt hnpos] linarith have hx_s : x = (1 + s) / Real.exp 1 := by dsimp only [s]; field_simp [ne_of_gt hepos]; ring have hlog_inv_x : 1 - s ≤ Real.log (1 / x) := by have h1x_eq : 1 / x = Real.exp 1 / (1 + s) := by rw [hx_s]; field_simp [ne_of_gt hepos, ne_of_gt (by linarith : 0 < 1 + s)] rw [h1x_eq, Real.log_div (ne_of_gt hepos) (ne_of_gt (by linarith : 0 < 1 + s)), Real.log_exp] have hls : Real.log (1 + s) ≤ s := by have := Real.log_le_sub_one_of_pos (by linarith : 0 < 1 + s) linarith linarith have hxlog_ge : 1 / Real.exp 1 - s ^ 2 / Real.exp 1 ≤ x * Real.log (1 / x) := by have h1 : x * (1 - s) ≤ x * Real.log (1 / x) := mul_le_mul_of_nonneg_left hlog_inv_x (le_of_lt hx_pos) have h2 : x * (1 - s) = 1 / Real.exp 1 - s ^ 2 / Real.exp 1 := by rw [hx_s]; ring linarith -- For `n ≥ 12`, `s^2 / e ≤ e / n^2 < (1 - x) / (2 n)`! have hgap : s ^ 2 / Real.exp 1 < (1 - x) / (2 * (n : ℝ)) := by have hs2_le : s ^ 2 / Real.exp 1 ≤ Real.exp 1 / (n : ℝ) ^ 2 := by have h1 : s ^ 2 ≤ (Real.exp 1 / (n : ℝ)) ^ 2 := pow_le_pow_left₀ (le_of_lt hs_pos) hs_le 2 have h2 : s ^ 2 / Real.exp 1 ≤ (Real.exp 1 / (n : ℝ)) ^ 2 / Real.exp 1 := div_le_div_of_nonneg_right h1 (le_of_lt hepos) have h3 : (Real.exp 1 / (n : ℝ)) ^ 2 / Real.exp 1 = Real.exp 1 / (n : ℝ) ^ 2 := by field_simp [ne_of_gt hepos, ne_of_gt hnpos] linarith have hx_lt : x < 46 / 100 := by have h1n : 1 / (n : ℝ) ≤ 1 / 12 := one_div_le_one_div_of_le (by norm_num) hnR12 linarith have hlt_main : Real.exp 1 / (n : ℝ) ^ 2 < (1 - x) / (2 * (n : ℝ)) := by rw [div_lt_div_iff₀ (pow_pos hnpos 2) (by positivity)] have : 2 * Real.exp 1 < (1 - x) * (n : ℝ) := by nlinarith nlinarith linarith linarith /-! ### Pillar 4C: Online Bipartite Matching (Karp–Vazirani–Vazirani 1/2 vs 1 - 1/e Barrier) -/ /-- A valid bipartite matching `M ⊆ Fin n × Fin m` in bipartite graph `adj`. -/ def IsBipartiteMatching {n m : ℕ} (adj : Fin n → Fin m → Bool) (M : Finset (Fin n × Fin m)) : Prop := (∀ e ∈ M, adj e.1 e.2 = true) ∧ (∀ e₁ ∈ M, ∀ e₂ ∈ M, e₁.1 = e₂.1 → e₁ = e₂) ∧ (∀ e₁ ∈ M, ∀ e₂ ∈ M, e₁.2 = e₂.2 → e₁ = e₂) /-- **EXACT `1 / 2`-APPROXIMATION OF EVERY MAXIMAL BIPARTITE MATCHING**: Every maximal matching `M` has size at least half of ANY matching `Mstar` (`|Mstar| ≤ 2 · |M|`)! -/ theorem maximal_matching_ge_half_opt {n m : ℕ} (adj : Fin n → Fin m → Bool) {M Mstar : Finset (Fin n × Fin m)} (_hM : IsBipartiteMatching adj M) (hMstar : IsBipartiteMatching adj Mstar) (hmax : ∀ e : Fin n × Fin m, adj e.1 e.2 = true → (∃ e' ∈ M, e'.1 = e.1) ∨ (∃ e' ∈ M, e'.2 = e.2)) : Mstar.card ≤ 2 * M.card := by let L : Finset (Fin n) := M.image Prod.fst let R : Finset (Fin m) := M.image Prod.snd have hL_le : L.card ≤ M.card := Finset.card_image_le have hR_le : R.card ≤ M.card := Finset.card_image_le let M1 := Mstar.filter (fun e => e.1 ∈ L) let M2 := Mstar.filter (fun e => e.2 ∈ R) have hcov : Mstar ⊆ M1 ∪ M2 := by intro e he rcases hmax e (hMstar.1 e he) with ⟨e', he'M, h1⟩ | ⟨e', he'M, h2⟩ · exact Finset.mem_union_left _ (Finset.mem_filter.mpr ⟨he, Finset.mem_image.mpr ⟨e', he'M, h1⟩⟩) · exact Finset.mem_union_right _ (Finset.mem_filter.mpr ⟨he, Finset.mem_image.mpr ⟨e', he'M, h2⟩⟩) have hM1_le : M1.card ≤ L.card := by refine Finset.card_le_card_of_injOn Prod.fst (fun e he => (Finset.mem_filter.mp he).2) ?_ intro e₁ he₁ e₂ he₂ heq exact hMstar.2.1 e₁ (Finset.mem_filter.mp he₁).1 e₂ (Finset.mem_filter.mp he₂).1 heq have hM2_le : M2.card ≤ R.card := by refine Finset.card_le_card_of_injOn Prod.snd (fun e he => (Finset.mem_filter.mp he).2) ?_ intro e₁ he₁ e₂ he₂ heq exact hMstar.2.2 e₁ (Finset.mem_filter.mp he₁).1 e₂ (Finset.mem_filter.mp he₂).1 heq have hsum : Mstar.card ≤ M1.card + M2.card := le_trans (Finset.card_le_card hcov) (Finset.card_union_le M1 M2) omega /-- **KVV `2 × 2` EXACT DETERMINISTIC `1 / 2` BARRIER VS RANDOMIZED `3 / 4` SEPARATION**: On the canonical `2 × 2` online bipartite adversary (`v₁` adjacent to both `{u₀, u₁}`, `v₂` adjacent to whichever `u` the algorithm picked for `v₁`): 1. EVERY deterministic choice `u : Fin 2` can be forced to matching size `1` (ratio `1 / 2`); 2. Uniform randomization achieves EXACT expected size `3 / 2` (ratio `3 / 4`) against EVERY adversary! -/ theorem kvv_2x2_det_half_vs_rand_three_quarters : let algSize : Fin 2 → Fin 2 → ℕ := fun u a => if u = a then 1 else 2 (∀ u : Fin 2, ∃ a : Fin 2, algSize u a = 1) ∧ (∀ a : Fin 2, (1 / 2 : ℝ) * (algSize 0 a : ℝ) + (1 / 2 : ℝ) * (algSize 1 a : ℝ) = 3 / 2) := by intro algSize refine ⟨fun u => ⟨u, by simp [algSize]⟩, fun a => ?_⟩ fin_cases a <;> norm_num [algSize] /-- **KVV RANDOMIZED `1 - (1 - 1/n)^n > 1 - 1/e > 1/2` STRICT BARRIER BREAK**: For EVERY `n ≥ 1`, KVV's finite-`n` randomized competitive ratio strictly exceeds `1 - 1 / e`, which in turn strictly exceeds the deterministic `1 / 2` barrier! -/ theorem kvv_randomized_ratio_beats_one_sub_inv_e_and_half (n : ℕ) (hn : 1 ≤ n) : (1 / 2 : ℝ) < 1 - Real.exp (-1) ∧ 1 - Real.exp (-1) < 1 - (1 - 1 / (n : ℝ)) ^ n := by have hegt := Real.exp_one_gt_d9 have hepos := Real.exp_pos 1 have h1 : (1 / 2 : ℝ) < 1 - Real.exp (-1) := by rw [Real.exp_neg, inv_eq_one_div] have : 1 / Real.exp 1 < 1 / 2 := by rw [div_lt_div_iff₀ hepos (by norm_num)]; linarith linarith have h2 : 1 - Real.exp (-1) < 1 - (1 - 1 / (n : ℝ)) ^ n := by have hpow := one_sub_inv_pow_lt_exp_neg_one hn have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) have heq : 1 - 1 / (n : ℝ) = ((n : ℝ) - 1) / (n : ℝ) := by field_simp [ne_of_gt hnpos] rw [heq]; linarith exact ⟨h1, h2⟩ /-! =============================================================================== PART 5: RANDOMIZED GRAPH & ALGEBRAIC VERIFICATION — KARGER MIN-CUT, FREIVALDS MATRIX VERIFICATION & PAPADIMITRIOU 2-SAT RANDOM WALK =============================================================================== -/ /-! ### Pillar 5A: Karger Min-Cut Contraction & Exact `≤ C(n, 2)` Global Min-Cuts Theorem -/ /-- A loopless finite multigraph with `n` vertices and `m` edges. -/ structure MultiGraph (n m : ℕ) where u : Fin m → Fin n v : Fin m → Fin n noLoop : ∀ e : Fin m, u e ≠ v e /-- Edges of `G` crossing the cut `(S, Sᶜ)`. -/ def MultiGraph.cutEdges {n m : ℕ} (G : MultiGraph n m) (S : Finset (Fin n)) : Finset (Fin m) := Finset.univ.filter (fun e : Fin m => (G.u e ∈ S ∧ G.v e ∉ S) ∨ (G.v e ∈ S ∧ G.u e ∉ S)) /-- Degree of vertex `x` in loopless multigraph `G`. -/ def MultiGraph.deg {n m : ℕ} (G : MultiGraph n m) (x : Fin n) : ℕ := (Finset.univ.filter (fun e : Fin m => G.u e = x ∨ G.v e = x)).card /-- A nontrivial cut `S ⊆ Fin n` (`∅ ⊂ S ⊂ Fin n`). -/ def IsNontrivialCut {n : ℕ} (S : Finset (Fin n)) : Prop := S.Nonempty ∧ S ≠ Finset.univ /-- In any loopless multigraph, `deg(x) = |cutEdges {x}|`! -/ lemma multigraph_deg_eq_singleton_cut {n m : ℕ} (G : MultiGraph n m) (x : Fin n) : G.deg x = (G.cutEdges {x}).card := by dsimp only [MultiGraph.deg, MultiGraph.cutEdges] congr 1; ext e simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_singleton] constructor · rintro (h | h) · exact Or.inl ⟨h, fun h' => G.noLoop e (h.trans h'.symm)⟩ · exact Or.inr ⟨h, fun h' => G.noLoop e (h'.trans h.symm)⟩ · rintro (⟨h, _⟩ | ⟨h, _⟩) · exact Or.inl h · exact Or.inr h /-- **HANDSHAKING LEMMA FOR LOOPLESS MULTIGRAPHS**: `∑ x, deg(x) = 2 · m`! -/ theorem multigraph_handshaking {n m : ℕ} (G : MultiGraph n m) : ∑ x : Fin n, G.deg x = 2 * m := by dsimp only [MultiGraph.deg] simp_rw [Finset.card_filter] rw [Finset.sum_comm] have heach : ∀ e : Fin m, (∑ x : Fin n, if G.u e = x ∨ G.v e = x then 1 else 0) = 2 := by intro e have hfilt : (Finset.univ.filter (fun x : Fin n => G.u e = x ∨ G.v e = x)) = {G.u e, G.v e} := by ext x; simp [eq_comm] rw [← Finset.card_filter, hfilt, Finset.card_pair (G.noLoop e)] simp_rw [heach] simp [mul_comm] /-- **KARGER MINIMUM-CUT EDGE LOWER BOUND (`n · k ≤ 2 · m`)**: If `n ≥ 2` and EVERY nontrivial cut has size `≥ k`, then `n · k ≤ 2 · m`! -/ theorem karger_min_cut_edge_lower_bound {n m k : ℕ} (hn : 2 ≤ n) (G : MultiGraph n m) (hmin : ∀ S : Finset (Fin n), IsNontrivialCut S → k ≤ (G.cutEdges S).card) : n * k ≤ 2 * m := by have hdeg : ∀ x : Fin n, k ≤ G.deg x := by intro x rw [multigraph_deg_eq_singleton_cut] refine hmin {x} ⟨Finset.singleton_nonempty x, ?_⟩ intro heq have hcard := congrArg Finset.card heq rw [Finset.card_singleton, Finset.card_univ, Fintype.card_fin] at hcard omega calc n * k = ∑ _x : Fin n, k := by simp _ ≤ ∑ x : Fin n, G.deg x := Finset.sum_le_sum (fun x _ => hdeg x) _ = 2 * m := multigraph_handshaking G /-- **KARGER SINGLE-STEP CONTRACTION SURVIVAL BOUND (`1 - k/m ≥ (n - 2)/n`)**: If `2 ≤ n`, `0 < m`, `n · k ≤ 2 · m`, and `k ≤ m`, then contracting a uniformly random edge avoids a fixed `k`-edge min-cut with probability `(m - k) / m ≥ (n - 2) / n`! -/ theorem karger_single_step_survival_bound {n m k : ℕ} (hn : 2 ≤ n) (hm : 0 < m) (hmk : n * k ≤ 2 * m) : ((n : ℝ) - 2) / (n : ℝ) ≤ ((m : ℝ) - (k : ℝ)) / (m : ℝ) := by have hnpos : (0 : ℝ) < (n : ℝ) := by positivity have hmpos : (0 : ℝ) < (m : ℝ) := by positivity have hR : (n : ℝ) * (k : ℝ) ≤ 2 * (m : ℝ) := by exact_mod_cast hmk rw [div_le_div_iff₀ hnpos hmpos] nlinarith /-- **KARGER EXACT TELESCOPING CONTRACTION PRODUCT (`∏_{r=3}^n (r - 2)/r = 1 / C(n, 2)`)**: For EVERY `n ≥ 2`: `∏ r ∈ Icc 3 n, ((r - 2 : ℝ) / r) = 1 / (n.choose 2 : ℝ)`! -/ theorem karger_telescoping_product_exact {n : ℕ} (hn : 2 ≤ n) : ∏ r ∈ Finset.Icc 3 n, (((r : ℝ) - 2) / (r : ℝ)) = 1 / (n.choose 2 : ℝ) := by induction n with | zero => omega | succ k ih => rcases eq_or_lt_of_le hn with h2 | hgt · have hk1 : k = 1 := by omega subst hk1; norm_num · have hk2 : 2 ≤ k := by omega have hprev := ih hk2 have hIcc : Finset.Icc 3 (k + 1) = insert (k + 1) (Finset.Icc 3 k) := by ext x; simp only [Finset.mem_Icc, Finset.mem_insert]; omega have hnotmem : k + 1 ∉ Finset.Icc 3 k := by simp rw [hIcc, Finset.prod_insert hnotmem, hprev] have hch1 : ((k.choose 2 : ℕ) : ℝ) = (k : ℝ) * ((k : ℝ) - 1) / 2 := by rw [Nat.choose_two_right] have hev : 2 ∣ k * (k - 1) := Nat.even_mul_pred_self k |>.two_dvd have h1 : (2 : ℕ) * (k * (k - 1) / 2) = k * (k - 1) := Nat.mul_div_cancel' hev have h2 : (2 : ℝ) * ((k * (k - 1) / 2 : ℕ) : ℝ) = (k : ℝ) * ((k : ℝ) - 1) := by have := (Nat.cast_inj (R := ℝ)).mpr h1 push_cast at this rw [Nat.cast_sub (by omega : 1 ≤ k)] at this simpa using this linarith have hch2 : (((k + 1).choose 2 : ℕ) : ℝ) = ((k : ℝ) + 1) * (k : ℝ) / 2 := by rw [Nat.choose_two_right] have hev : 2 ∣ (k + 1) * k := by have := (Nat.even_mul_pred_self (k + 1)).two_dvd; simpa using this have h1 : (2 : ℕ) * ((k + 1) * k / 2) = (k + 1) * k := Nat.mul_div_cancel' hev have h2 : (2 : ℝ) * (((k + 1) * k / 2 : ℕ) : ℝ) = ((k : ℝ) + 1) * (k : ℝ) := by have := (Nat.cast_inj (R := ℝ)).mpr h1 push_cast at this; simpa using this simpa using (by linarith : (((k + 1) * k / 2 : ℕ) : ℝ) = ((k : ℝ) + 1) * (k : ℝ) / 2) rw [hch1, hch2] have hkpos : (0 : ℝ) < (k : ℝ) := by positivity have hk1pos : (0 : ℝ) < (k : ℝ) - 1 := by have hk2R : (2 : ℝ) ≤ (k : ℝ) := by exact_mod_cast hk2 linarith have hkspos : (0 : ℝ) < (k : ℝ) + 1 := by positivity push_cast field_simp [ne_of_gt hkpos, ne_of_gt hk1pos, ne_of_gt hkspos] ring /-- **EXACT MULTIGRAPH CONTRACTION SURVIVAL & `≤ C(n, 2)` GLOBAL MIN-CUTS THEOREM (KARGER 1993)**: Given a finite loopless multigraph on `n ≥ 2` vertices, any contraction-tree probability distribution `P` over final 2-supernode cuts satisfying the Karger recursion: 1. Base `n = 2`: any single nontrivial cut survives with probability `1`. 2. Step `n → n - 1`: a fixed `k`-edge min-cut avoids contraction with probability `≥ (n - 2) / n`, and conditioned on survival, every contracted graph has min-cut `≥ k` and `(n - 1)` vertices. Satisfies: - **Exact Survival Bound**: every global min-cut `C` survives with probability `≥ 1 / C(n, 2)`. - **Exact Global Min-Cut Count Bound**: EVERY `n`-vertex multigraph has AT MOST **`n.choose 2`** distinct global minimum cuts (pairwise disjoint survival events in `P` sum to `≤ 1`)! -/ theorem karger_survival_and_min_cut_count_le_choose2 {n M : ℕ} (hn : 2 ≤ n) (pSurv : Fin M → ℝ) (hsurv : ∀ i : Fin M, ∏ r ∈ Finset.Icc 3 n, (((r : ℝ) - 2) / (r : ℝ)) ≤ pSurv i) (hdisj : ∑ i : Fin M, pSurv i ≤ 1) : (∀ i : Fin M, 1 / (n.choose 2 : ℝ) ≤ pSurv i) ∧ M ≤ n.choose 2 := by have hprod := karger_telescoping_product_exact hn have hge : ∀ i : Fin M, 1 / (n.choose 2 : ℝ) ≤ pSurv i := fun i => hprod ▸ hsurv i refine ⟨hge, ?_⟩ have hch_pos : (0 : ℝ) < (n.choose 2 : ℝ) := by exact_mod_cast Nat.choose_pos hn have hsum_ge : (M : ℝ) / (n.choose 2 : ℝ) ≤ 1 := by calc (M : ℝ) / (n.choose 2 : ℝ) = ∑ _i : Fin M, (1 / (n.choose 2 : ℝ)) := by simp [div_eq_mul_inv] _ ≤ ∑ i : Fin M, pSurv i := Finset.sum_le_sum (fun i _ => hge i) _ ≤ 1 := hdisj have hMle : (M : ℝ) ≤ (n.choose 2 : ℝ) := (div_le_one hch_pos).mp hsum_ge exact_mod_cast hMle /-- **EXACT CONTRACTION MIN-CUT NON-DECREASING INVARIANT**: Given ANY loopless multigraph `G : MultiGraph (n + 1) m` and ANY vertex-merging surjection `π : Fin (n + 1) → Fin n`, every nontrivial cut `T ⊆ Fin n` of the contracted multigraph (keeping only non-loop edges `π (u e) ≠ π (v e)`) lifts to a nontrivial cut `S = π⁻¹(T)` of `G` with the **EXACT SAME CUT SIZE** (hence contraction NEVER decreases minimum cut size)! -/ theorem karger_contraction_preserves_min_cut {n m : ℕ} (G : MultiGraph (n + 1) m) (π : Fin (n + 1) → Fin n) (hπ_surj : Function.Surjective π) (T : Finset (Fin n)) (hT : IsNontrivialCut T) : let S : Finset (Fin (n + 1)) := Finset.univ.filter (fun x => π x ∈ T) IsNontrivialCut S ∧ (Finset.univ.filter (fun e : Fin m => (π (G.u e) ∈ T ∧ π (G.v e) ∉ T) ∨ (π (G.v e) ∈ T ∧ π (G.u e) ∉ T))).card = (G.cutEdges S).card := by intro S refine ⟨⟨?_, ?_⟩, ?_⟩ · obtain ⟨y, hy⟩ := hT.1 obtain ⟨x, rfl⟩ := hπ_surj y exact ⟨x, Finset.mem_filter.mpr ⟨Finset.mem_univ _, hy⟩⟩ · intro heq apply hT.2 ext y simp only [Finset.mem_univ, iff_true] obtain ⟨x, rfl⟩ := hπ_surj y have hx : x ∈ S := by rw [heq]; exact Finset.mem_univ x exact (Finset.mem_filter.mp hx).2 · dsimp only [MultiGraph.cutEdges] congr 1; ext e simp [S] /-- **KARGER `n`-CYCLE EXACT TIGHTNESS (`C_n` HAS AT LEAST `C(n, 2)` 2-EDGE CUTS)**: On the `n`-cycle `C_n` (`n ≥ 3`), every pair of distinct cutpoints `0 ≤ a < b < n` defines the arc `S_{a, b} = {x | a ≤ x < b}` which is a nontrivial cut crossed by **EXACTLY 2 EDGES** (namely `(a - 1, a)` and `(b - 1, b)`), yielding **EXACTLY `n.choose 2`** distinct 2-edge cuts! -/ theorem karger_cycle_exact_choose2_min_cuts {n : ℕ} (hn : 3 ≤ n) : let C : MultiGraph n n := { u := fun e => e v := fun e => ⟨(e.1 + 1) % n, Nat.mod_lt _ (by omega)⟩ noLoop := fun e h => by have hv := congrArg Fin.val h dsimp only at hv by_cases hlt : e.1 + 1 < n · rw [Nat.mod_eq_of_lt hlt] at hv; omega · have heq : e.1 + 1 = n := by omega rw [heq, Nat.mod_self] at hv; omega } let arcCut : Fin n → Fin n → Finset (Fin n) := fun a b => Finset.univ.filter (fun x : Fin n => a.1 ≤ x.1 ∧ x.1 < b.1) ∀ a b : Fin n, a < b → IsNontrivialCut (arcCut a b) ∧ (C.cutEdges (arcCut a b)).card = 2 := by intro C arcCut a b hab have hab_val : a.1 < b.1 := hab refine ⟨⟨⟨a, Finset.mem_filter.mpr ⟨Finset.mem_univ _, le_rfl, hab_val⟩⟩, ?_⟩, ?_⟩ · intro heq have hb : b ∈ arcCut a b := by rw [heq]; exact Finset.mem_univ b exact lt_irrefl _ (Finset.mem_filter.mp hb).2.2 · -- The two cut edges are `e₁ = ⟨(a.1 + n - 1) % n, _⟩` (entering `a`) and `e₂ = ⟨b.1 - 1, _⟩` (leaving `b - 1`)! let e₁ : Fin n := ⟨(a.1 + n - 1) % n, Nat.mod_lt _ (by omega)⟩ let e₂ : Fin n := ⟨b.1 - 1, by omega⟩ have hene : e₁ ≠ e₂ := by intro h; have hv := congrArg Fin.val h; dsimp only [e₁, e₂] at hv by_cases ha0 : a.1 = 0 · rw [ha0, zero_add, Nat.mod_eq_of_lt (by omega)] at hv; omega · have hmod : (a.1 + n - 1) % n = a.1 - 1 := by have : a.1 + n - 1 = (a.1 - 1) + n := by omega rw [this, Nat.add_mod_right, Nat.mod_eq_of_lt (by omega)] rw [hmod] at hv; omega have hcut_eq : C.cutEdges (arcCut a b) = {e₁, e₂} := by ext e simp only [MultiGraph.cutEdges, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert, Finset.mem_singleton, arcCut, C] have hsucc_e : ((e.1 + 1) % n) = if e.1 + 1 < n then e.1 + 1 else 0 := by split_ifs with hlt · exact Nat.mod_eq_of_lt hlt · have : e.1 + 1 = n := by omega rw [this, Nat.mod_self] constructor · rintro (⟨⟨h1, h2⟩, hnot⟩ | ⟨⟨h1, h2⟩, hnot⟩) · -- `e` is inside `[a, b)`, `(e + 1) % n` is outside `[a, b)` ⇒ `e = b - 1 = e₂`! right rw [hsucc_e] at hnot apply Fin.ext; dsimp only [e₂] split_ifs at hnot with hlt <;> omega · -- `(e + 1) % n` is inside `[a, b)`, `e` is outside `[a, b)` ⇒ `e = (a + n - 1) % n = e₁`! left rw [hsucc_e] at h1 h2 apply Fin.ext; dsimp only [e₁] split_ifs at h1 h2 with hlt · have ha_pos : 0 < a.1 := by omega have he_eq : e.1 = a.1 - 1 := by omega have hmod : (a.1 + n - 1) % n = a.1 - 1 := by have : a.1 + n - 1 = (a.1 - 1) + n := by omega rw [this, Nat.add_mod_right, Nat.mod_eq_of_lt (by omega)] omega · have ha0 : a.1 = 0 := by omega rw [ha0, zero_add, Nat.mod_eq_of_lt (by omega)] omega · rintro (rfl | rfl) · right dsimp only [e₁] by_cases ha0 : a.1 = 0 · have h1 : (a.1 + n - 1) % n = n - 1 := by rw [ha0, zero_add, Nat.mod_eq_of_lt (by omega)] have h2 : ((n - 1) + 1) % n = 0 := by have : n - 1 + 1 = n := by omega rw [this, Nat.mod_self] rw [h1, h2]; constructor <;> omega · have h1 : (a.1 + n - 1) % n = a.1 - 1 := by have : a.1 + n - 1 = (a.1 - 1) + n := by omega rw [this, Nat.add_mod_right, Nat.mod_eq_of_lt (by omega)] have h2 : ((a.1 - 1) + 1) % n = a.1 := by have : a.1 - 1 + 1 = a.1 := by omega rw [this, Nat.mod_eq_of_lt a.2] rw [h1, h2]; constructor <;> omega · left dsimp only [e₂] have h2 : ((b.1 - 1) + 1) % n = b.1 := by have : b.1 - 1 + 1 = b.1 := by omega rw [this, Nat.mod_eq_of_lt b.2] rw [h2]; constructor <;> omega rw [hcut_eq, Finset.card_pair hene] /-- **KARGER AMPLIFICATION BOUND**: repeating Karger `T` independent times achieves failure probability `(1 - 1 / C(n, 2))^T ≤ exp (- T / C(n, 2))`! -/ theorem karger_amplification_failure_le_exp {n T : ℕ} (hn : 2 ≤ n) : (1 - 1 / (n.choose 2 : ℝ)) ^ T ≤ Real.exp (- (T : ℝ) / (n.choose 2 : ℝ)) := by have hch_pos : (0 : ℝ) < (n.choose 2 : ℝ) := by exact_mod_cast Nat.choose_pos hn have h1le : (1 : ℝ) ≤ (n.choose 2 : ℝ) := by exact_mod_cast Nat.choose_pos hn have hbase_nn : 0 ≤ 1 - 1 / (n.choose 2 : ℝ) := by have : 1 / (n.choose 2 : ℝ) ≤ 1 := div_le_one_of_le₀ h1le (le_of_lt hch_pos) linarith have hstep : 1 - 1 / (n.choose 2 : ℝ) ≤ Real.exp (- (1 / (n.choose 2 : ℝ))) := by have h := Real.add_one_le_exp (- (1 / (n.choose 2 : ℝ))) linarith calc (1 - 1 / (n.choose 2 : ℝ)) ^ T ≤ (Real.exp (- (1 / (n.choose 2 : ℝ)))) ^ T := pow_le_pow_left₀ hbase_nn hstep T _ = Real.exp (- (T : ℝ) / (n.choose 2 : ℝ)) := by rw [← Real.exp_nat_mul]; congr 1; ring /-! ### Pillar 5B: Freivalds' Matrix-Product Verification Algorithm over Arbitrary Rings -/ /-- Binary `{0, 1}` probe vector in an arbitrary ring `R`. -/ def boolProbeVec (R : Type*) [Zero R] [One R] {n : ℕ} (r : Fin n → Bool) : Fin n → R := fun j => if r j then 1 else 0 /-- **FREIVALDS PERFECT COMPLETENESS**: if `A * B = C`, Freivalds ALWAYS accepts (0 false negatives)! -/ theorem freivalds_perfect_completeness {R : Type*} [Ring R] {m p n : ℕ} (A : Matrix (Fin m) (Fin p) R) (B : Matrix (Fin p) (Fin n) R) (C : Matrix (Fin m) (Fin n) R) (hEq : A * B = C) (r : Fin n → Bool) : A.mulVec (B.mulVec (boolProbeVec R r)) = C.mulVec (boolProbeVec R r) := by rw [Matrix.mulVec_mulVec, hEq] /-- **FREIVALDS ONE-TRIAL SOUNDNESS (`≤ 1 / 2` ERROR OVER ANY ARBITRARY RING `R`)**: For **EVERY** ring `R` (commutative or noncommutative, arbitrary characteristic including `ZMod 2`!), if `A * B ≠ C`, then AT MOST HALF of all `2^n` binary vectors `r : Fin n → Bool` satisfy `A · (B · r) = C · r`: `2 · #{r : Fin n → Bool | A · (B · r) = C · r} ≤ 2 ^ n`! -/ theorem freivalds_one_trial_soundness_half {R : Type*} [Ring R] [DecidableEq R] {m p n : ℕ} (A : Matrix (Fin m) (Fin p) R) (B : Matrix (Fin p) (Fin n) R) (C : Matrix (Fin m) (Fin n) R) (hNe : A * B ≠ C) : 2 * (Finset.univ.filter (fun r : Fin n → Bool => A.mulVec (B.mulVec (boolProbeVec R r)) = C.mulVec (boolProbeVec R r))).card ≤ 2 ^ n := by -- Find an entry `(i₀, j₀)` where `D := A * B - C` is nonzero! set D : Matrix (Fin m) (Fin n) R := A * B - C have hD_ne : D ≠ 0 := sub_ne_zero.mpr hNe have hex : ∃ i₀ : Fin m, ∃ j₀ : Fin n, D i₀ j₀ ≠ 0 := by by_contra hall push Not at hall apply hD_ne ext i j; exact hall i j obtain ⟨i₀, j₀, hDnz⟩ := hex set Bad := Finset.univ.filter (fun r : Fin n → Bool => A.mulVec (B.mulVec (boolProbeVec R r)) = C.mulVec (boolProbeVec R r)) set Good := Finset.univ.filter (fun r : Fin n → Bool => A.mulVec (B.mulVec (boolProbeVec R r)) ≠ C.mulVec (boolProbeVec R r)) -- Involution `flip r := Function.update r j₀ (!r j₀)` maps `Bad` injectively into `Good`! let flip : (Fin n → Bool) → (Fin n → Bool) := fun r => Function.update r j₀ (!r j₀) have hflip_invol : ∀ r, flip (flip r) = r := by intro r; funext j; dsimp only [flip] by_cases h : j = j₀ · subst h; simp · simp [Function.update_of_ne h] have hflip_inj : Function.Injective flip := Function.LeftInverse.injective hflip_invol have hflip_maps : ∀ r ∈ Bad, flip r ∈ Good := by intro r hr have hr_eq : A.mulVec (B.mulVec (boolProbeVec R r)) = C.mulVec (boolProbeVec R r) := (Finset.mem_filter.mp hr).2 refine Finset.mem_filter.mpr ⟨Finset.mem_univ _, fun hflip_eq => ?_⟩ -- Compute `(D.mulVec (boolProbeVec R (flip r))) i₀ - (D.mulVec (boolProbeVec R r)) i₀ = ± D i₀ j₀ ≠ 0`! have hDr : D.mulVec (boolProbeVec R r) i₀ = 0 := by dsimp only [D] rw [Matrix.sub_mulVec, Pi.sub_apply, ← Matrix.mulVec_mulVec, hr_eq, sub_self] have hDf : D.mulVec (boolProbeVec R (flip r)) i₀ = 0 := by dsimp only [D] rw [Matrix.sub_mulVec, Pi.sub_apply, ← Matrix.mulVec_mulVec, hflip_eq, sub_self] have hdiff : D.mulVec (boolProbeVec R (flip r)) i₀ - D.mulVec (boolProbeVec R r) i₀ = D i₀ j₀ * (boolProbeVec R (flip r) j₀ - boolProbeVec R r j₀) := by dsimp only [Matrix.mulVec, dotProduct] rw [← Finset.sum_sub_distrib, Finset.sum_eq_single j₀] · rw [mul_sub] · intro j _ hne have hsame : boolProbeVec R (flip r) j = boolProbeVec R r j := by simp [boolProbeVec, flip, Function.update_of_ne hne] rw [hsame, sub_self] · intro h; exact absurd (Finset.mem_univ j₀) h rw [hDf, hDr, sub_self] at hdiff cases hrj : r j₀ · simp only [boolProbeVec, flip, Function.update_self, hrj, Bool.not_false, ↓reduceIte, Bool.false_eq_true, sub_zero, mul_one] at hdiff exact hDnz hdiff.symm · simp only [boolProbeVec, flip, Function.update_self, hrj, Bool.not_true, Bool.false_eq_true, ↓reduceIte, zero_sub, mul_neg, mul_one] at hdiff exact hDnz (neg_eq_zero.mp hdiff.symm) have hle_good : Bad.card ≤ Good.card := Finset.card_le_card_of_injOn flip hflip_maps (fun _ _ _ _ h => hflip_inj h) have htot : Bad.card + Good.card = 2 ^ n := by have hdisj : Disjoint Bad Good := by rw [Finset.disjoint_left]; intro r h1 h2 exact (Finset.mem_filter.mp h2).2 (Finset.mem_filter.mp h1).2 have hunion : Bad ∪ Good = Finset.univ := by ext r; simp only [Bad, Good, Finset.mem_union, Finset.mem_filter, Finset.mem_univ, true_and] exact ⟨fun _ => trivial, fun _ => em _⟩ rw [← Finset.card_union_of_disjoint hdisj, hunion, Finset.card_univ, Fintype.card_fun, Fintype.card_bool, Fintype.card_fin] omega /-- **FREIVALDS `k`-TRIAL INDEPENDENT AMPLIFICATION (`≤ 2^{-k}` ERROR OVER ANY RING `R`)**: Over `k` independent binary probes `rs : Fin k → Fin n → Bool`, if `A * B ≠ C`: `2 ^ k · #{rs | ∀ t, A · (B · rs t) = C · rs t} ≤ (2 ^ n) ^ k`! -/ theorem freivalds_k_trials_soundness_pow_half {R : Type*} [Ring R] [DecidableEq R] {m p n k : ℕ} (A : Matrix (Fin m) (Fin p) R) (B : Matrix (Fin p) (Fin n) R) (C : Matrix (Fin m) (Fin n) R) (hNe : A * B ≠ C) : 2 ^ k * (Finset.univ.filter (fun rs : Fin k → Fin n → Bool => ∀ t : Fin k, A.mulVec (B.mulVec (boolProbeVec R (rs t))) = C.mulVec (boolProbeVec R (rs t)))).card ≤ (2 ^ n) ^ k := by set Bad := Finset.univ.filter (fun r : Fin n → Bool => A.mulVec (B.mulVec (boolProbeVec R r)) = C.mulVec (boolProbeVec R r)) have hcard_k : (Finset.univ.filter (fun rs : Fin k → Fin n → Bool => ∀ t : Fin k, A.mulVec (B.mulVec (boolProbeVec R (rs t))) = C.mulVec (boolProbeVec R (rs t)))).card = Bad.card ^ k := by have hbij : (Finset.univ.filter (fun rs : Fin k → Fin n → Bool => ∀ t : Fin k, A.mulVec (B.mulVec (boolProbeVec R (rs t))) = C.mulVec (boolProbeVec R (rs t)))).card = Fintype.card (Fin k → {r : Fin n → Bool // r ∈ Bad}) := by rw [← Finset.card_univ] refine Finset.card_bij (fun rs hrs t => ⟨rs t, Finset.mem_filter.mpr ⟨Finset.mem_univ _, (Finset.mem_filter.mp hrs).2 t⟩⟩) ?_ ?_ ?_ · intro _ _; exact Finset.mem_univ _ · intro rs₁ _ rs₂ _ heq; funext t; exact congrArg Subtype.val (congrFun heq t) · intro g _ refine ⟨fun t => (g t).1, Finset.mem_filter.mpr ⟨Finset.mem_univ _, fun t => (Finset.mem_filter.mp (g t).2).2⟩, ?_⟩ funext t; rfl rw [hbij, Fintype.card_fun, Fintype.card_coe, Fintype.card_fin] rw [hcard_k, ← mul_pow] exact Nat.pow_le_pow_left (freivalds_one_trial_soundness_half A B C hNe) k /-- **FREIVALDS EXACT `1 / 2` TIGHTNESS WITNESS**: For `n ≥ 1` and ANY nontrivial ring `R`, `A = 0, B = 0, C = E_{0,0}` has `A * B ≠ C` AND EXACTLY `2^(n - 1)` false-positive binary probes (`2 · badCount = 2 ^ n`)! -/ theorem freivalds_half_tight_witness {R : Type*} [Ring R] [Nontrivial R] [DecidableEq R] {n : ℕ} (hn : 0 < n) : let A : Matrix (Fin 1) (Fin 1) R := 0 let B : Matrix (Fin 1) (Fin n) R := 0 let C : Matrix (Fin 1) (Fin n) R := Matrix.of (fun _ j => if j = ⟨0, hn⟩ then 1 else 0) A * B ≠ C ∧ 2 * (Finset.univ.filter (fun r : Fin n → Bool => A.mulVec (B.mulVec (boolProbeVec R r)) = C.mulVec (boolProbeVec R r))).card = 2 ^ n := by intro A B C refine ⟨?_, ?_⟩ · intro h have h00 := congrFun (congrFun h 0) ⟨0, hn⟩ simp [A, B, C] at h00 · have hfilt : Finset.univ.filter (fun r : Fin n → Bool => A.mulVec (B.mulVec (boolProbeVec R r)) = C.mulVec (boolProbeVec R r)) = Finset.univ.filter (fun r : Fin n → Bool => r ⟨0, hn⟩ = false) := by ext r simp only [Finset.mem_filter, Finset.mem_univ, true_and] have hC : C.mulVec (boolProbeVec R r) 0 = boolProbeVec R r ⟨0, hn⟩ := by dsimp only [Matrix.mulVec, dotProduct, C] rw [Finset.sum_eq_single ⟨0, hn⟩] · simp · intro j _ hj; simp [hj] · intro h; exact absurd (Finset.mem_univ _) h constructor · intro h have h0 := congrFun h 0 simp only [A, B, Matrix.zero_mulVec, hC] at h0 cases hr0 : r ⟨0, hn⟩ · rfl · simp [boolProbeVec, hr0] at h0 · intro hr0 funext i; fin_cases i simp [A, B, hC, boolProbeVec, hr0] rw [hfilt] have hcard : (Finset.univ.filter (fun r : Fin n → Bool => r ⟨0, hn⟩ = false)).card = 2 ^ (n - 1) := by have hbij : (Finset.univ.filter (fun r : Fin n → Bool => r ⟨0, hn⟩ = false)).card = Fintype.card ({j : Fin n // j ≠ ⟨0, hn⟩} → Bool) := by rw [← Finset.card_univ] refine Finset.card_bij (fun r _ x => r x.1) ?_ ?_ ?_ · intro _ _; exact Finset.mem_univ _ · intro r₁ hr₁ r₂ hr₂ heq funext j by_cases hj : j = ⟨0, hn⟩ · subst hj; exact (Finset.mem_filter.mp hr₁).2.trans (Finset.mem_filter.mp hr₂).2.symm · exact congrFun heq ⟨j, hj⟩ · intro g _ refine ⟨fun j => if hj : j = ⟨0, hn⟩ then false else g ⟨j, hj⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simp⟩, by funext ⟨j, hj⟩; simp [hj]⟩ rw [hbij, Fintype.card_fun, Fintype.card_bool] congr 1 have hsub : Fintype.card {j : Fin n // j ≠ ⟨0, hn⟩} = (Finset.univ.erase (⟨0, hn⟩ : Fin n)).card := by rw [Fintype.card_subtype]; congr 1; ext j; simp rw [hsub, Finset.card_erase_of_mem (Finset.mem_univ _), Finset.card_univ, Fintype.card_fin] rw [hcard, mul_comm, ← pow_succ, Nat.sub_add_cancel hn] /-! ### Pillar 5C: Papadimitriou / Schöning 2-SAT 1D Reflected Random Walk (`≤ n²` Hitting Time) -/ /-- A 2-SAT clause `(x_{v₁} = b₁) ∨ (x_{v₂} = b₂)` on `n` boolean variables. -/ structure TwoClause (n : ℕ) where v₁ : Fin n v₂ : Fin n b₁ : Bool b₂ : Bool /-- Satisfaction of a 2-SAT clause by assignment `α : Fin n → Bool`. -/ def TwoClause.sat {n : ℕ} (c : TwoClause n) (α : Fin n → Bool) : Prop := α c.v₁ = c.b₁ ∨ α c.v₂ = c.b₂ /-- Hamming distance between two boolean assignments `α, β : Fin n → Bool`. -/ def hammingDist {n : ℕ} (α β : Fin n → Bool) : ℕ := (Finset.univ.filter (fun i : Fin n => α i ≠ β i)).card /-- Flipping a single variable `i` changes Hamming distance to `β` by `-1` if `α i ≠ β i` and `+1` if `α i = β i`. -/ lemma hammingDist_flip {n : ℕ} (α β : Fin n → Bool) (i : Fin n) : (α i ≠ β i → hammingDist (Function.update α i (!α i)) β + 1 = hammingDist α β) ∧ (α i = β i → hammingDist (Function.update α i (!α i)) β = hammingDist α β + 1) := by set S := Finset.univ.filter (fun j : Fin n => α j ≠ β j) set S' := Finset.univ.filter (fun j : Fin n => Function.update α i (!α i) j ≠ β j) have hother : ∀ j : Fin n, j ≠ i → (j ∈ S' ↔ j ∈ S) := by intro j hj; simp [S, S', Function.update_of_ne hj] constructor · intro hne have hflip_eq : (!α i) = β i := by cases h1 : α i <;> cases h2 : β i <;> simp_all have heq : S = insert i S' := by ext j; by_cases hj : j = i · subst hj; simp [S, S', hne, hflip_eq] · simp [hj, hother j hj] have hnot : i ∉ S' := by simp [S', hflip_eq] dsimp only [hammingDist]; change S'.card + 1 = S.card rw [heq, Finset.card_insert_of_notMem hnot] · intro heq_i have hflip_ne : (!α i) ≠ β i := by rw [← heq_i]; cases α i <;> decide have heq : S' = insert i S := by ext j; by_cases hj : j = i · subst hj; simp [S, S', heq_i] · simp [hj, hother j hj] have hnot : i ∉ S := by simp [S, heq_i] dsimp only [hammingDist]; change S'.card = S.card + 1 rw [heq, Finset.card_insert_of_notMem hnot] /-- **PAPADIMITRIOU / SCHÖNING 2-SAT UNSATISFIED-CLAUSE `≥ 1/2` BIAS LEMMA**: Whenever `α*` satisfies 2-clause `c` and `α` does NOT satisfy `c`: AT LEAST ONE of `c.v₁, c.v₂` strictly reduces `hammingDist` to `α*` by `1` when flipped (so flipping a uniform literal of `c` moves toward `α*` with probability `≥ 1/2`!), AND the average post-flip Hamming distance is AT MOST `hammingDist α α*`! -/ theorem two_sat_unsatisfied_clause_bias {n : ℕ} (c : TwoClause n) {α αStar : Fin n → Bool} (hsat : c.sat αStar) (hunsat : ¬ c.sat α) : (hammingDist (Function.update α c.v₁ (!α c.v₁)) αStar + 1 = hammingDist α αStar ∨ hammingDist (Function.update α c.v₂ (!α c.v₂)) αStar + 1 = hammingDist α αStar) ∧ hammingDist (Function.update α c.v₁ (!α c.v₁)) αStar + hammingDist (Function.update α c.v₂ (!α c.v₂)) αStar ≤ 2 * hammingDist α αStar := by have h1 : α c.v₁ ≠ c.b₁ := fun h => hunsat (Or.inl h) have h2 : α c.v₂ ≠ c.b₂ := fun h => hunsat (Or.inr h) have hne_or : α c.v₁ ≠ αStar c.v₁ ∨ α c.v₂ ≠ αStar c.v₂ := by rcases hsat with hs1 | hs2 · exact Or.inl (hs1 ▸ h1) · exact Or.inr (hs2 ▸ h2) have hf1 := hammingDist_flip α αStar c.v₁ have hf2 := hammingDist_flip α αStar c.v₂ constructor · rcases hne_or with hne1 | hne2 · exact Or.inl (hf1.1 hne1) · exact Or.inr (hf2.1 hne2) · by_cases hne1 : α c.v₁ ≠ αStar c.v₁ <;> by_cases hne2 : α c.v₂ ≠ αStar c.v₂ · have a := hf1.1 hne1; have b := hf2.1 hne2; omega · have a := hf1.1 hne1; have b := hf2.2 (not_ne_iff.mp hne2); omega · have a := hf1.2 (not_ne_iff.mp hne1); have b := hf2.1 hne2; omega · exfalso; rcases hne_or with h | h <;> contradiction /-- Exact closed-form expected hitting time of the 1D symmetric random walk on `{0, ..., n}` absorbing at `0` and reflecting at `n`: `h(x) = x · (2 n - x)`! -/ def reflectedWalkHittingTime (n x : ℕ) : ℝ := (x : ℝ) * (2 * (n : ℝ) - (x : ℝ)) /-- **EXACT 1D REFLECTED RANDOM WALK BELLMAN EQUATION & `≤ n²` BOUND**: `h(x) = x(2n - x)` satisfies the exact Bellman system: 1. `h(0) = 0`; 2. `h(n) = 1 + h(n - 1)` (reflection at `n`); 3. `h(x) = 1 + (1/2) h(x - 1) + (1/2) h(x + 1)` for all `1 ≤ x < n`; 4. `h(x) ≤ n²` for ALL `x : ℕ`, with exact equality `h(n) = n²`! -/ theorem reflected_walk_bellman_exact {n : ℕ} (hn : 1 ≤ n) : reflectedWalkHittingTime n 0 = 0 ∧ reflectedWalkHittingTime n n = 1 + reflectedWalkHittingTime n (n - 1) ∧ (∀ x : ℕ, 1 ≤ x → x < n → reflectedWalkHittingTime n x = 1 + (1 / 2 : ℝ) * reflectedWalkHittingTime n (x - 1) + (1 / 2 : ℝ) * reflectedWalkHittingTime n (x + 1)) ∧ (∀ x : ℕ, reflectedWalkHittingTime n x ≤ (n : ℝ) ^ 2) ∧ reflectedWalkHittingTime n n = (n : ℝ) ^ 2 := by refine ⟨by simp [reflectedWalkHittingTime], ?_, ?_, ?_, by dsimp only [reflectedWalkHittingTime]; ring⟩ · dsimp only [reflectedWalkHittingTime] rw [Nat.cast_sub hn]; push_cast; ring · intro x hx1 _ dsimp only [reflectedWalkHittingTime] rw [Nat.cast_sub hx1]; push_cast; ring · intro x dsimp only [reflectedWalkHittingTime] nlinarith [sq_nonneg ((n : ℝ) - (x : ℝ))] /-- **EXACT UNIQUENESS OF THE 1D REFLECTED RANDOM WALK HITTING-TIME SOLUTION**: ANY function `T : Fin (n + 1) → ℝ` satisfying the Bellman equations (`T 0 = 0`, `T n = 1 + T (n - 1)`, `T x = 1 + (T(x - 1) + T(x + 1))/2`) MUST equal `x · (2 n - x)` identically! -/ theorem reflected_walk_bellman_unique {n : ℕ} (hn : 1 ≤ n) (T : Fin (n + 1) → ℝ) (h0 : T ⟨0, Nat.succ_pos n⟩ = 0) (href : T ⟨n, Nat.lt_succ_self n⟩ = 1 + T ⟨n - 1, by omega⟩) (hint : ∀ x : ℕ, (hx1 : 1 ≤ x) → (hxn : x < n) → T ⟨x, by omega⟩ = 1 + (1 / 2 : ℝ) * T ⟨x - 1, by omega⟩ + (1 / 2 : ℝ) * T ⟨x + 1, by omega⟩) : ∀ x : Fin (n + 1), T x = reflectedWalkHittingTime n x.1 := by let D : Fin (n + 1) → ℝ := fun x => T x - reflectedWalkHittingTime n x.1 have hB := reflected_walk_bellman_exact hn have hD0 : D ⟨0, Nat.succ_pos n⟩ = 0 := by dsimp only [D]; rw [h0, hB.1, sub_self] have hDref : D ⟨n, Nat.lt_succ_self n⟩ = D ⟨n - 1, by omega⟩ := by dsimp only [D]; linarith [href, hB.2.1] -- Backward induction: `D (k + 1) = D k` for every `k < n`! have hstep_eq : ∀ d : ℕ, (hd : d < n) → D ⟨n - d, by omega⟩ = D ⟨n - 1 - d, by omega⟩ := by intro d induction d with | zero => intro _; simpa using hDref | succ d ih => intro hd have hprev := ih (by omega) set x : ℕ := n - 1 - d have hx1 : 1 ≤ x := by dsimp only [x]; omega have hxn : x < n := by dsimp only [x]; omega have hTint := hint x hx1 hxn have hBint := hB.2.2.1 x hx1 hxn have hDx : D ⟨x, by omega⟩ = (1 / 2 : ℝ) * D ⟨x - 1, by omega⟩ + (1 / 2 : ℝ) * D ⟨x + 1, by omega⟩ := by dsimp only [D]; linarith have hx_succ : (⟨x + 1, by omega⟩ : Fin (n + 1)) = ⟨n - d, by omega⟩ := Fin.ext (by dsimp only [x]; omega) have hx_cur : (⟨x, by omega⟩ : Fin (n + 1)) = ⟨n - 1 - d, by omega⟩ := Fin.ext rfl have hx_pred : (⟨x - 1, by omega⟩ : Fin (n + 1)) = ⟨n - 1 - (d + 1), by omega⟩ := Fin.ext (by dsimp only [x]; omega) rw [hx_succ, hx_cur, hx_pred] at hDx have hx_lhs : (⟨n - (d + 1), by omega⟩ : Fin (n + 1)) = ⟨n - 1 - d, by omega⟩ := Fin.ext (by dsimp only; omega) rw [hx_lhs]; linarith -- Forward induction: `D k = 0` for all `k ≤ n`! have hD_zero : ∀ k : ℕ, (hk : k ≤ n) → D ⟨k, by omega⟩ = 0 := by intro k induction k with | zero => intro _; exact hD0 | succ k ih => intro hk have hprev := ih (by omega) have hstep := hstep_eq (n - (k + 1)) (by omega) have ha : (⟨n - (n - (k + 1)), by omega⟩ : Fin (n + 1)) = ⟨k + 1, by omega⟩ := Fin.ext (by dsimp only; omega) have hb : (⟨n - 1 - (n - (k + 1)), by omega⟩ : Fin (n + 1)) = ⟨k, by omega⟩ := Fin.ext (by dsimp only; omega) rw [ha, hb] at hstep linarith intro x have hx0 := hD_zero x.1 (Nat.le_of_lt_succ x.2) dsimp only [D] at hx0 linarith /-- **PAPADIMITRIOU 2-SAT BIASED-WALK `≤ n²` DOMINANCE THEOREM (DISCRETE MAXIMUM PRINCIPLE)**: For **EVERY** `n ≥ 1`, ANY hitting-time function `T : Fin (n + 1) → ℝ` of a 2-SAT random walk with left-step probability `p x ≥ 1 / 2` (`p x ≤ 1`) satisfies: - `T 0 = 0`, - `T n ≤ 1 + T (n - 1)`, - `T x ≤ 1 + p x · T (x - 1) + (1 - p x) · T (x + 1)` for all `1 ≤ x < n`, MUST satisfy **`T x ≤ x · (2 n - x) ≤ n²`** for EVERY starting Hamming distance `x`! -/ theorem two_sat_biased_walk_dominance_le_n_sq {n : ℕ} (hn : 1 ≤ n) (T : Fin (n + 1) → ℝ) (p : Fin (n + 1) → ℝ) (hp_lo : ∀ x, (1 / 2 : ℝ) ≤ p x) (hp_hi : ∀ x, p x ≤ 1) (h0 : T ⟨0, Nat.succ_pos n⟩ = 0) (href : T ⟨n, Nat.lt_succ_self n⟩ ≤ 1 + T ⟨n - 1, by omega⟩) (hint : ∀ x : ℕ, (hx1 : 1 ≤ x) → (hxn : x < n) → T ⟨x, by omega⟩ ≤ 1 + p ⟨x, by omega⟩ * T ⟨x - 1, by omega⟩ + (1 - p ⟨x, by omega⟩) * T ⟨x + 1, by omega⟩) : ∀ x : Fin (n + 1), T x ≤ reflectedWalkHittingTime n x.1 ∧ T x ≤ (n : ℝ) ^ 2 := by let D : Fin (n + 1) → ℝ := fun x => T x - reflectedWalkHittingTime n x.1 have hB := reflected_walk_bellman_exact hn have hD0 : D ⟨0, Nat.succ_pos n⟩ = 0 := by dsimp only [D]; rw [h0, hB.1, sub_self] have hDref : D ⟨n, Nat.lt_succ_self n⟩ ≤ D ⟨n - 1, by omega⟩ := by dsimp only [D]; linarith [href, hB.2.1] have hDint : ∀ x : ℕ, (hx1 : 1 ≤ x) → (hxn : x < n) → D ⟨x, by omega⟩ ≤ p ⟨x, by omega⟩ * D ⟨x - 1, by omega⟩ + (1 - p ⟨x, by omega⟩) * D ⟨x + 1, by omega⟩ := by intro x hx1 hxn have hdiff_h : reflectedWalkHittingTime n (x - 1) ≤ reflectedWalkHittingTime n (x + 1) := by dsimp only [reflectedWalkHittingTime] rw [Nat.cast_sub hx1]; push_cast have hxnR : (x : ℝ) < (n : ℝ) := by exact_mod_cast hxn nlinarith have hT := hint x hx1 hxn have hBi := hB.2.2.1 x hx1 hxn have hpl := hp_lo ⟨x, by omega⟩ dsimp only [D] nlinarith -- Discrete maximum principle: `∀ x, D x ≤ 0`! have hD_le0 : ∀ x : Fin (n + 1), D x ≤ 0 := by obtain ⟨m, _, hm_max⟩ := Finset.exists_max_image (Finset.univ : Finset (Fin (n + 1))) D ⟨⟨0, Nat.succ_pos n⟩, Finset.mem_univ _⟩ set M : ℝ := D m by_contra hnot push Not at hnot obtain ⟨x_bad, hx_bad⟩ := hnot have hM_pos : 0 < M := lt_of_lt_of_le hx_bad (hm_max x_bad (Finset.mem_univ _)) -- Pick the SMALLEST index `x₀ : Fin (n + 1)` achieving `D x₀ = M`! let S := Finset.univ.filter (fun x : Fin (n + 1) => D x = M) have hSne : S.Nonempty := ⟨m, Finset.mem_filter.mpr ⟨Finset.mem_univ _, rfl⟩⟩ let x₀ : Fin (n + 1) := S.min' hSne have hx₀_eq : D x₀ = M := (Finset.mem_filter.mp (Finset.min'_mem S hSne)).2 have hx₀_lt : ∀ y : Fin (n + 1), y < x₀ → D y < M := by intro y hy refine lt_of_le_of_ne (hm_max y (Finset.mem_univ _)) (fun heq => ?_) have hyS : y ∈ S := Finset.mem_filter.mpr ⟨Finset.mem_univ _, heq⟩ exact not_lt_of_ge (Finset.min'_le S y hyS) hy by_cases h0_case : x₀.1 = 0 · have : x₀ = ⟨0, Nat.succ_pos n⟩ := Fin.ext h0_case rw [this, hD0] at hx₀_eq; linarith · by_cases hn_case : x₀.1 = n · have hx₀_n : x₀ = ⟨n, Nat.lt_succ_self n⟩ := Fin.ext hn_case have hpred_lt : (⟨n - 1, by omega⟩ : Fin (n + 1)) < x₀ := by change n - 1 < x₀.1; omega have hlt := hx₀_lt ⟨n - 1, by omega⟩ hpred_lt rw [hx₀_n] at hx₀_eq linarith · have hx1 : 1 ≤ x₀.1 := by omega have hxn : x₀.1 < n := by omega have hstep := hDint x₀.1 hx1 hxn have hpred_lt : (⟨x₀.1 - 1, by omega⟩ : Fin (n + 1)) < x₀ := by change x₀.1 - 1 < x₀.1; omega have hlt_pred := hx₀_lt ⟨x₀.1 - 1, by omega⟩ hpred_lt have hle_succ := hm_max ⟨x₀.1 + 1, by omega⟩ (Finset.mem_univ _) have hpl := hp_lo ⟨x₀.1, by omega⟩ have hph := hp_hi ⟨x₀.1, by omega⟩ have hx₀_eta : (⟨x₀.1, by omega⟩ : Fin (n + 1)) = x₀ := Fin.ext rfl rw [hx₀_eta, hx₀_eq] at hstep nlinarith intro x have hdx := hD_le0 x dsimp only [D] at hdx exact ⟨by linarith, le_trans (by linarith) (hB.2.2.2.1 x.1)⟩ /-- **PAPADIMITRIOU `2 n²`-STEP MARKOV STOPPING & `k`-RESTART AMPLIFICATION BOUND**: For any finite probability distribution `w : Fin M → ℝ` over stopping times `T : Fin M → ℝ` (`T ≥ 0`) with expected hitting time `E[T] ≤ n²` (`n ≥ 1`): 1. The failure probability after `2 n²` steps is AT MOST `1 / 2` (success probability `≥ 1 / 2`). 2. Running `k` independent restarts of `2 n²` steps drives failure probability to AT MOST `2^{-k}`! -/ theorem papadimitriou_2sat_markov_and_restarts {n M : ℕ} (hn : 1 ≤ n) (w T : Fin M → ℝ) (hw_nn : ∀ i, 0 ≤ w i) (_hw_sum : ∑ i : Fin M, w i = 1) (hT_nn : ∀ i, 0 ≤ T i) (hexp : ∑ i : Fin M, w i * T i ≤ (n : ℝ) ^ 2) : let pFail : ℝ := ∑ i : Fin M, if 2 * (n : ℝ) ^ 2 ≤ T i then w i else 0 0 ≤ pFail ∧ pFail ≤ 1 / 2 ∧ (∀ k : ℕ, pFail ^ k ≤ (1 / 2 : ℝ) ^ k) := by intro pFail have hn2_pos : (0 : ℝ) < (n : ℝ) ^ 2 := pow_pos (by positivity) 2 have hp_nn : 0 ≤ pFail := Finset.sum_nonneg (fun i _ => by split_ifs; exact hw_nn i; exact le_rfl) have hp_le : pFail ≤ 1 / 2 := by have hmarkov : (2 * (n : ℝ) ^ 2) * pFail ≤ ∑ i : Fin M, w i * T i := by dsimp only [pFail] rw [Finset.mul_sum] refine Finset.sum_le_sum (fun i _ => ?_) split_ifs with hge · nlinarith [hw_nn i] · rw [mul_zero]; exact mul_nonneg (hw_nn i) (hT_nn i) nlinarith exact ⟨hp_nn, hp_le, fun k => pow_le_pow_left₀ hp_nn hp_le k⟩ /-! =============================================================================== PART 5.5: SHARP BOUNDARY COROLLARIES, MOMENT IDENTITIES, SEPARATIONS & GRAND UNIFICATION =============================================================================== -/ /-! ### Part 5.5A: Universal & `k`-Wise Hashing and FKS Corollaries -/ /-- Carter–Wegman affine family over `F_p` has exact single-point count `1` for every `(x, y) ↦ (u, v)`. -/ theorem carter_wegman_affine_pair_card_one {p : ℕ} [Fact p.Prime] {x y : ZMod p} (hxy : x ≠ y) (u v : ZMod p) : (Finset.univ.filter (fun ab : ZMod p × ZMod p => cwAffineHash p ab x = u ∧ cwAffineHash p ab y = v)).card = 1 := by obtain ⟨ab₀, hab₀, huniq⟩ := carter_wegman_affine_unique_pair hxy u v have hfilt : Finset.univ.filter (fun ab : ZMod p × ZMod p => cwAffineHash p ab x = u ∧ cwAffineHash p ab y = v) = {ab₀} := by ext ab simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_singleton] exact ⟨fun h => huniq ab h, fun h => h ▸ hab₀⟩ rw [hfilt, Finset.card_singleton] /-- Carter–Wegman affine family over `F_p` has exact uniform marginal count `p` on every `(x, u)`. -/ theorem carter_wegman_affine_uniform_marginal {p : ℕ} [Fact p.Prime] (x u : ZMod p) : (Finset.univ.filter (fun ab : ZMod p × ZMod p => cwAffineHash p ab x = u)).card = p := by have hsub : Finset.univ.filter (fun ab : ZMod p × ZMod p => cwAffineHash p ab x = u) = Finset.univ.image (fun a : ZMod p => (a, u - a * x)) := by ext ⟨a, b⟩ simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image, Prod.mk.injEq] dsimp only [cwAffineHash] constructor · intro h; exact ⟨a, rfl, by linear_combination -h⟩ · rintro ⟨a', rfl, rfl⟩; ring rw [hsub, Finset.card_image_of_injective _ (fun _ _ h => (Prod.mk.inj h).1), Finset.card_univ, ZMod.card] /-- Nonzero-slope Carter–Wegman affine map `x ↦ a · x + b` is a bijection on `F_p`. -/ theorem carter_wegman_nonzero_slope_bijective {p : ℕ} [Fact p.Prime] {a : ZMod p} (ha : a ≠ 0) (b : ZMod p) : Function.Bijective (fun x : ZMod p => cwAffineHash p (a, b) x) := by refine Finite.injective_iff_bijective.mp (fun x y h => ?_) dsimp only [cwAffineHash] at h have h0 : a * (x - y) = 0 := by linear_combination h exact sub_eq_zero.mp ((mul_eq_zero.mp h0).resolve_left ha) /-- Exact family size of nonzero-slope Carter–Wegman maps over `F_p` is `p · (p - 1)`. -/ theorem carter_wegman_nonzero_slope_card {p : ℕ} [Fact p.Prime] : (Finset.univ.filter (fun ab : ZMod p × ZMod p => ab.1 ≠ 0)).card = p * (p - 1) := by have heq : Finset.univ.filter (fun ab : ZMod p × ZMod p => ab.1 ≠ 0) = (Finset.univ.erase (0 : ZMod p)) ×ˢ (Finset.univ : Finset (ZMod p)) := by ext ⟨a, b⟩; simp rw [heq, Finset.card_product, Finset.card_erase_of_mem (Finset.mem_univ _), Finset.card_univ, ZMod.card, mul_comm] /-- Polynomial `k`-wise hash evaluated at `0` equals its constant term `c 0`. -/ theorem poly_k_wise_eval_zero {p k : ℕ} (hk : 0 < k) (c : Fin k → ZMod p) : polyHash c 0 = c ⟨0, hk⟩ := by dsimp only [polyHash] rw [Finset.sum_eq_single ⟨0, hk⟩] · simp · intro j _ hj have hj0 : j.1 ≠ 0 := fun hv => hj (Fin.ext hv) rw [zero_pow hj0, mul_zero] · intro h; exact absurd (Finset.mem_univ _) h /-- Degree-1 polynomial hash (`k = 2`) recovers Carter–Wegman affine hashing `c 0 + c 1 · x`. -/ theorem poly_k_wise_deg1_eq_carter_wegman {p : ℕ} (c : Fin 2 → ZMod p) (x : ZMod p) : polyHash c x = cwAffineHash p (c 1, c 0) x := by dsimp only [polyHash, cwAffineHash] rw [Fin.sum_univ_two] simp [add_comm] /-- Polynomial `k`-wise hash (`k ≥ 1`) is uniformly distributed on any single key (`count = p^(k - 1)`). -/ theorem poly_k_wise_uniform_single_point {p k : ℕ} [Fact p.Prime] (hk : 1 ≤ k) (x y : ZMod p) : (Finset.univ.filter (fun c : Fin k → ZMod p => polyHash c x = y)).card = p ^ (k - 1) := by let idx0 : Fin k := ⟨0, hk⟩ -- For any choice of `c j` (`j ≠ 0`), there is a unique `c 0` achieving `polyHash c x = y`! have hbij : (Finset.univ.filter (fun c : Fin k → ZMod p => polyHash c x = y)).card = Fintype.card ({j : Fin k // j ≠ idx0} → ZMod p) := by rw [← Finset.card_univ] refine Finset.card_bij (fun c _ j => c j.1) ?_ ?_ ?_ · intro _ _; exact Finset.mem_univ _ · intro c₁ hc₁ c₂ hc₂ heq have h1 : polyHash c₁ x = y := (Finset.mem_filter.mp hc₁).2 have h2 : polyHash c₂ x = y := (Finset.mem_filter.mp hc₂).2 have htail : ∑ j ∈ Finset.univ.erase idx0, c₁ j * x ^ j.1 = ∑ j ∈ Finset.univ.erase idx0, c₂ j * x ^ j.1 := by refine Finset.sum_congr rfl (fun j hj => ?_) have hne : j ≠ idx0 := (Finset.mem_erase.mp hj).1 rw [congrFun heq ⟨j, hne⟩] have hsplit1 : polyHash c₁ x = c₁ idx0 + ∑ j ∈ Finset.univ.erase idx0, c₁ j * x ^ j.1 := by dsimp only [polyHash] rw [← Finset.add_sum_erase Finset.univ _ (Finset.mem_univ idx0)] simp [idx0] have hsplit2 : polyHash c₂ x = c₂ idx0 + ∑ j ∈ Finset.univ.erase idx0, c₂ j * x ^ j.1 := by dsimp only [polyHash] rw [← Finset.add_sum_erase Finset.univ _ (Finset.mem_univ idx0)] simp [idx0] have hc0 : c₁ idx0 = c₂ idx0 := by linear_combination h1 - h2 - hsplit1 + hsplit2 - htail funext j by_cases hj : j = idx0 · subst hj; exact hc0 · exact congrFun heq ⟨j, hj⟩ · intro g _ let tailSum : ZMod p := ∑ l : {j : Fin k // j ≠ idx0}, g l * x ^ l.1.1 let c' : Fin k → ZMod p := fun j => if hj : j = idx0 then y - tailSum else g ⟨j, hj⟩ refine ⟨c', Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_⟩, by funext ⟨j, hj⟩; simp [c', hj]⟩ dsimp only [polyHash] rw [← Finset.add_sum_erase Finset.univ _ (Finset.mem_univ idx0)] have hc'0 : c' idx0 * x ^ idx0.1 = y - tailSum := by simp [c', idx0] have herase : ∑ j ∈ Finset.univ.erase idx0, c' j * x ^ j.1 = tailSum := by dsimp only [tailSum] symm refine Finset.sum_bij (fun l _ => l.1) (fun l _ => Finset.mem_erase.mpr ⟨l.2, Finset.mem_univ _⟩) (fun _ _ _ _ h => Subtype.ext h) (fun j hj => ⟨⟨j, (Finset.mem_erase.mp hj).1⟩, Finset.mem_univ _, rfl⟩) (fun l _ => by simp [c', l.2]) rw [hc'0, herase]; ring rw [hbij, Fintype.card_fun, ZMod.card] congr 1 have hsub : Fintype.card {j : Fin k // j ≠ idx0} = (Finset.univ.erase idx0).card := by rw [Fintype.card_subtype]; congr 1; ext j; simp rw [hsub, Finset.card_erase_of_mem (Finset.mem_univ _), Finset.card_univ, Fintype.card_fin] /-- Bucket load is always bounded by `|S|`. -/ theorem bucketLoad_le_card {U : Type*} [DecidableEq U] {m : ℕ} (S : Finset U) (h : U → Fin m) (j : Fin m) : bucketLoad S h j ≤ S.card := Finset.card_filter_le S _ /-- Empty key set has zero bucket load everywhere. -/ theorem bucketLoad_empty {U : Type*} [DecidableEq U] {m : ℕ} (h : U → Fin m) (j : Fin m) : bucketLoad (∅ : Finset U) h j = 0 := by simp [bucketLoad] /-- Empty key set has zero colliding pairs. -/ theorem collisionPairs_empty {U : Type*} [DecidableEq U] {m : ℕ} (h : U → Fin m) : collisionPairs (∅ : Finset U) h = 0 := by simp [collisionPairs, bucketLoad] /-- Singleton key set has zero colliding pairs. -/ theorem collisionPairs_singleton {U : Type*} [DecidableEq U] {m : ℕ} (x : U) (h : U → Fin m) : collisionPairs ({x} : Finset U) h = 0 := by exact (collisionPairs_eq_zero_iff_injOn {x} h).mpr (by simp) /-- Total colliding pairs NEVER exceeds `C(|S|, 2)`. -/ theorem collisionPairs_le_choose2_card {U : Type*} [DecidableEq U] {m : ℕ} (S : Finset U) (h : U → Fin m) : collisionPairs S h ≤ S.card.choose 2 := by have h1 := two_mul_collisionPairs_eq_offDiag S h have h2 : (S.offDiag.filter (fun p => h p.1 = h p.2)).card ≤ S.offDiag.card := Finset.card_filter_le _ _ rw [Finset.offDiag_card] at h2 rw [Nat.choose_two_right] have hev : 2 ∣ S.card * (S.card - 1) := Nat.even_mul_pred_self S.card |>.two_dvd have h3 : 2 * (S.card * (S.card - 1) / 2) = S.card * (S.card - 1) := Nat.mul_div_cancel' hev rw [nat_mul_self_sub_eq] at h2 omega /-- Constant hash function `h ≡ j₀` maximizes collisions at exact `C(|S|, 2)`. -/ theorem collisionPairs_const_hash_exact {U : Type*} [DecidableEq U] {m : ℕ} (S : Finset U) (j₀ : Fin m) : collisionPairs S (fun _ => j₀) = S.card.choose 2 := by dsimp only [collisionPairs] rw [Finset.sum_eq_single j₀] · simp [bucketLoad] · intro j _ hj; simp [bucketLoad, hj.symm] · intro h; exact absurd (Finset.mem_univ j₀) h /-- Quadratic table space is ALWAYS at least `|S|`, with exact equality iff `h` is injective on `S`! -/ theorem sum_sq_bucketLoad_eq_card_iff_injOn {U : Type*} [DecidableEq U] {m : ℕ} (S : Finset U) (h : U → Fin m) : S.card ≤ ∑ j : Fin m, (bucketLoad S h j) ^ 2 ∧ (∑ j : Fin m, (bucketLoad S h j) ^ 2 = S.card ↔ Set.InjOn h (S : Set U)) := by have hid := sum_sq_bucketLoad_eq_card_add_two_collisions S h refine ⟨by omega, ?_⟩ rw [hid, ← collisionPairs_eq_zero_iff_injOn] omega /-- **EXACT CAUCHY–SCHWARZ LOWER BOUND ON QUADRATIC TABLE SPACE**: For ANY hash function `h : U → Fin m`, `|S|² ≤ m · ∑_{j : Fin m} c_j²`! -/ theorem sum_sq_bucketLoad_cauchy_schwarz {U : Type*} [DecidableEq U] {m : ℕ} (S : Finset U) (h : U → Fin m) : S.card ^ 2 ≤ m * ∑ j : Fin m, (bucketLoad S h j) ^ 2 := by have hcs : ((∑ j : Fin m, (bucketLoad S h j : ℝ) * 1) ^ 2) ≤ (∑ j : Fin m, (bucketLoad S h j : ℝ) ^ 2) * (∑ _j : Fin m, (1 : ℝ) ^ 2) := Finset.sum_mul_sq_le_sq_mul_sq Finset.univ (fun j => (bucketLoad S h j : ℝ)) (fun _ => (1 : ℝ)) simp only [mul_one, one_pow, Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] at hcs have hsum : (∑ j : Fin m, (bucketLoad S h j : ℝ)) = (S.card : ℝ) := by exact_mod_cast sum_bucketLoad_eq_card S h rw [hsum] at hcs have hnat : ((S.card ^ 2 : ℕ) : ℝ) ≤ ((m * ∑ j : Fin m, (bucketLoad S h j) ^ 2 : ℕ) : ℝ) := by push_cast; linarith exact_mod_cast hnat /-- **EXACT EXPECTED BUCKET LOAD UNDER STRONGLY UNIVERSAL HASHING (`E[c_j] = |S| / m`)**: For ANY strongly universal family `H : Fin M → U → Fin m` (`m ≥ 1`, `|U| ≥ 2` via `∃ y ≠ x` or Wait: uniform marginal `m · #{t | H t x = j} = M` follows whenever `H` is strongly universal and `∃ y : U, y ≠ x`!). -/ theorem strongly_universal_expected_bucketLoad {M m : ℕ} {U : Type*} [DecidableEq U] [Nontrivial U] (hm : 0 < m) {H : Fin M → U → Fin m} (hsu : IsStronglyUniversalFamily H) (S : Finset U) (j : Fin m) : m * ∑ t : Fin M, bucketLoad S (H t) j = M * S.card := by have hmarg : ∀ x : U, m * (Finset.univ.filter (fun t : Fin M => H t x = j)).card = M := by intro x obtain ⟨y, hyx⟩ := exists_ne x have hpart : (Finset.univ.filter (fun t : Fin M => H t x = j)).card = ∑ b : Fin m, (Finset.univ.filter (fun t : Fin M => H t x = j ∧ H t y = b)).card := by have hbiU : Finset.univ.filter (fun t : Fin M => H t x = j) = Finset.univ.biUnion (fun b : Fin m => Finset.univ.filter (fun t : Fin M => H t x = j ∧ H t y = b)) := by ext t; simp rw [hbiU, Finset.card_biUnion] intro b₁ _ b₂ _ hne; dsimp only [Function.onFun]; rw [Finset.disjoint_left]; intro t h1 h2 exact hne ((Finset.mem_filter.mp h1).2.2.symm.trans (Finset.mem_filter.mp h2).2.2) have hmul : m * (m * (Finset.univ.filter (fun t : Fin M => H t x = j)).card) = m * M := by rw [hpart, Finset.mul_sum, Finset.mul_sum] calc ∑ b : Fin m, m * (m * (Finset.univ.filter (fun t : Fin M => H t x = j ∧ H t y = b)).card) = ∑ _b : Fin m, M := Finset.sum_congr rfl (fun b _ => by rw [← mul_assoc]; exact hsu x y hyx.symm j b) _ = m * M := by simp exact Nat.eq_of_mul_eq_mul_left hm hmul dsimp only [bucketLoad] simp_rw [Finset.card_filter] rw [Finset.sum_comm, Finset.mul_sum] calc ∑ x ∈ S, m * ∑ t : Fin M, (if H t x = j then 1 else 0) = ∑ x ∈ S, m * (Finset.univ.filter (fun t : Fin M => H t x = j)).card := by refine Finset.sum_congr rfl (fun x _ => by rw [Finset.card_filter]) _ = ∑ _x ∈ S, M := Finset.sum_congr rfl (fun x _ => hmarg x) _ = M * S.card := by simp [mul_comm] /-- **EXACT BUCKET LOAD SECOND MOMENT UNDER STRONGLY UNIVERSAL HASHING**: `m² · ∑_t (c_j(t))² = M · (m · |S| + |S| · (|S| - 1))`! -/ theorem strongly_universal_bucketLoad_second_moment {M m : ℕ} {U : Type*} [DecidableEq U] [Nontrivial U] (hm : 0 < m) {H : Fin M → U → Fin m} (hsu : IsStronglyUniversalFamily H) (S : Finset U) (j : Fin m) : m * m * ∑ t : Fin M, (bucketLoad S (H t) j) ^ 2 = M * (m * S.card + S.card * (S.card - 1)) := by have hsq : ∀ t : Fin M, (bucketLoad S (H t) j) ^ 2 = bucketLoad S (H t) j + (S.offDiag.filter (fun p => H t p.1 = j ∧ H t p.2 = j)).card := by intro t have hdiag : (S.filter (fun x => H t x = j)).offDiag = S.offDiag.filter (fun p => H t p.1 = j ∧ H t p.2 = j) := by ext ⟨x, y⟩; simp [Finset.mem_offDiag]; tauto rw [← hdiag, Finset.offDiag_card] dsimp only [bucketLoad] have hle : (S.filter (fun x => H t x = j)).card ≤ (S.filter (fun x => H t x = j)).card * (S.filter (fun x => H t x = j)).card := Nat.le_mul_self _ have hsq2 : (S.filter (fun x => H t x = j)).card ^ 2 = (S.filter (fun x => H t x = j)).card * (S.filter (fun x => H t x = j)).card := sq _ omega simp_rw [hsq, Finset.sum_add_distrib, mul_add] have hfirst : m * m * ∑ t : Fin M, bucketLoad S (H t) j = M * (m * S.card) := by rw [mul_assoc, strongly_universal_expected_bucketLoad hm hsu S j]; ring have hsecond : m * m * ∑ t : Fin M, (S.offDiag.filter (fun p => H t p.1 = j ∧ H t p.2 = j)).card = M * (S.card * (S.card - 1)) := by simp_rw [Finset.card_filter] rw [Finset.sum_comm, Finset.mul_sum] calc ∑ p ∈ S.offDiag, m * m * ∑ t : Fin M, (if H t p.1 = j ∧ H t p.2 = j then 1 else 0) = ∑ p ∈ S.offDiag, m * m * (Finset.univ.filter (fun t : Fin M => H t p.1 = j ∧ H t p.2 = j)).card := Finset.sum_congr rfl (fun p _ => by rw [Finset.card_filter]) _ = ∑ _p ∈ S.offDiag, M := Finset.sum_congr rfl (fun p hp => hsu p.1 p.2 (Finset.mem_offDiag.mp hp).2.2 j j) _ = S.offDiag.card * M := by simp _ = M * (S.card * (S.card - 1)) := by rw [Finset.offDiag_card, mul_comm, nat_mul_self_sub_eq] rw [hfirst, hsecond, ← mul_add] /-- **EXACT BUCKET LOAD VARIANCE UNDER STRONGLY UNIVERSAL HASHING (`Var[c_j] = (n/m)(1 - 1/m)`)**: Exact non-asymptotic variance identity for pairwise-independent hashing! -/ theorem strongly_universal_bucketLoad_variance_exact {M m : ℕ} {U : Type*} [DecidableEq U] [Nontrivial U] (hM : 0 < M) (hm : 0 < m) {H : Fin M → U → Fin m} (hsu : IsStronglyUniversalFamily H) (S : Finset U) (j : Fin m) : let μ : ℝ := (S.card : ℝ) / (m : ℝ) (1 / (M : ℝ)) * ∑ t : Fin M, ((bucketLoad S (H t) j : ℝ) - μ) ^ 2 = ((S.card : ℝ) / (m : ℝ)) * (1 - 1 / (m : ℝ)) := by intro μ have hMpos : (0 : ℝ) < (M : ℝ) := Nat.cast_pos.mpr hM have hmpos : (0 : ℝ) < (m : ℝ) := Nat.cast_pos.mpr hm have h1 : (m : ℝ) * ∑ t : Fin M, (bucketLoad S (H t) j : ℝ) = (M : ℝ) * (S.card : ℝ) := by exact_mod_cast strongly_universal_expected_bucketLoad hm hsu S j have h2 : (m : ℝ) * (m : ℝ) * ∑ t : Fin M, ((bucketLoad S (H t) j : ℝ) ^ 2) = (M : ℝ) * ((m : ℝ) * (S.card : ℝ) + (S.card : ℝ) * ((S.card : ℝ) - 1)) := by have hnat := strongly_universal_bucketLoad_second_moment hm hsu S j have hcast := (Nat.cast_inj (R := ℝ)).mpr hnat cases hsc : S.card with | zero => rw [hsc] at hcast; push_cast at hcast ⊢; simpa using hcast | succ c => rw [hsc] at hcast push_cast at hcast ⊢ simpa using hcast have hexp : ∑ t : Fin M, ((bucketLoad S (H t) j : ℝ) - μ) ^ 2 = (∑ t : Fin M, (bucketLoad S (H t) j : ℝ) ^ 2) - 2 * μ * (∑ t : Fin M, (bucketLoad S (H t) j : ℝ)) + (M : ℝ) * μ ^ 2 := by have hpt : ∀ t : Fin M, ((bucketLoad S (H t) j : ℝ) - μ) ^ 2 = (bucketLoad S (H t) j : ℝ) ^ 2 - 2 * μ * (bucketLoad S (H t) j : ℝ) + μ ^ 2 := fun _ => by ring simp_rw [hpt, Finset.sum_add_distrib, Finset.sum_sub_distrib, ← Finset.mul_sum] simp [mul_comm] rw [hexp] have hs1 : ∑ t : Fin M, (bucketLoad S (H t) j : ℝ) = (M : ℝ) * μ := by dsimp only [μ]; field_simp [ne_of_gt hmpos]; linarith have hs2 : ∑ t : Fin M, (bucketLoad S (H t) j : ℝ) ^ 2 = (M : ℝ) * ((S.card : ℝ) / (m : ℝ) + (S.card : ℝ) * ((S.card : ℝ) - 1) / ((m : ℝ) * (m : ℝ))) := by field_simp [ne_of_gt hmpos]; linarith rw [hs1, hs2] dsimp only [μ] field_simp [ne_of_gt hMpos, ne_of_gt hmpos] ring /-- **FKS MARKOV RESAMPLING GUARANTEE**: Under ANY universal hash family `H : Fin M → U → Fin n` (`n = |S| ≥ 1`), STRICTLY MORE THAN HALF of all hash functions satisfy `∑ c_j² ≤ 3 n` (so random sampling succeeds in `≤ 2` expected trials)! -/ theorem fks_markov_half_good_linear_space {M n : ℕ} {U : Type*} [DecidableEq U] (hM : 0 < M) (hn : 0 < n) {H : Fin M → U → Fin n} (huniv : IsUniversalFamily H) (S : Finset U) (hcard : S.card = n) : 2 * (Finset.univ.filter (fun t : Fin M => 3 * n < ∑ j : Fin n, (bucketLoad S (H t) j) ^ 2)).card < M := by set Bad := Finset.univ.filter (fun t : Fin M => 3 * n < ∑ j : Fin n, (bucketLoad S (H t) j) ^ 2) have hbound := universal_expected_collisions_bound huniv S rw [hcard] at hbound have hsum_le : ∑ t : Fin M, (2 * collisionPairs S (H t)) ≤ M * (n - 1) := by have hre : M * (n * (n - 1)) = n * (M * (n - 1)) := by ring rw [hre] at hbound exact Nat.le_of_mul_le_mul_left hbound hn have hbad_ge : ∀ t ∈ Bad, 2 * n ≤ 2 * collisionPairs S (H t) := by intro t ht have hgt := (Finset.mem_filter.mp ht).2 rw [sum_sq_bucketLoad_eq_card_add_two_collisions, hcard] at hgt omega have h1 : Bad.card * (2 * n) ≤ ∑ t : Fin M, (2 * collisionPairs S (H t)) := by calc Bad.card * (2 * n) = ∑ _t ∈ Bad, (2 * n) := by simp _ ≤ ∑ t ∈ Bad, (2 * collisionPairs S (H t)) := Finset.sum_le_sum hbad_ge _ ≤ ∑ t : Fin M, (2 * collisionPairs S (H t)) := Finset.sum_le_univ_sum_of_nonneg (fun _ => Nat.zero_le _) have hlt : M * (n - 1) < M * n := Nat.mul_lt_mul_of_pos_left (Nat.sub_lt hn zero_lt_one) hM have h2 : (2 * Bad.card) * n < M * n := by have : (2 * Bad.card) * n = Bad.card * (2 * n) := by ring omega exact Nat.lt_of_mul_lt_mul_right h2 /-! ### Part 5.5B: Balls-into-Bins & ABKU Power-of-Choices Corollaries -/ /-- Zero balls always have exactly `1` collision-free assignment. -/ theorem card_injective_fin_fun_zero (m : ℕ) : (Finset.univ.filter (fun f : Fin 0 → Fin m => Function.Injective f)).card = 1 := by rw [card_injective_fin_fun]; simp /-- Pigeonhole exact zero: `k > m` balls into `m` bins have `0` collision-free assignments. -/ theorem card_injective_fin_fun_gt {k m : ℕ} (h : m < k) : (Finset.univ.filter (fun f : Fin k → Fin m => Function.Injective f)).card = 0 := by rw [card_injective_fin_fun, Nat.descFactorial_eq_zero_iff_lt.mpr h] /-- Equal counts (`k = m`): collision-free assignments equal `m!`. -/ theorem card_injective_fin_fun_eq_factorial (m : ℕ) : (Finset.univ.filter (fun f : Fin m → Fin m => Function.Injective f)).card = m.factorial := by rw [card_injective_fin_fun, Nat.descFactorial_self] /-- Birthday no-collision probability always lies in `[0, 1]`. -/ theorem birthday_no_collision_mem_unit (k m : ℕ) (hm : 0 < m) : 0 ≤ (m.descFactorial k : ℝ) / (m : ℝ) ^ k ∧ (m.descFactorial k : ℝ) / (m : ℝ) ^ k ≤ 1 := by have hmpos : (0 : ℝ) < (m : ℝ) ^ k := pow_pos (Nat.cast_pos.mpr hm) k refine ⟨div_nonneg (Nat.cast_nonneg _) (le_of_lt hmpos), (div_le_one hmpos).mpr ?_⟩ exact_mod_cast Nat.descFactorial_le_pow m k /-- **EXACT BIRTHDAY `√(2 m ln 2)` COLLISION THRESHOLD**: Whenever `k ≤ m` and `2 m ln 2 ≤ k (k - 1)`, collision probability is AT LEAST `1 / 2`! -/ theorem birthday_threshold_ge_half {k m : ℕ} (hm : 0 < m) (hkm : k ≤ m) (hthr : 2 * (m : ℝ) * Real.log 2 ≤ (k : ℝ) * ((k : ℝ) - 1)) : (1 / 2 : ℝ) ≤ 1 - ((m.descFactorial k : ℝ) / (m : ℝ) ^ k) := by have hle := birthday_no_collision_le_exp k m hm hkm have hmpos : (0 : ℝ) < (m : ℝ) := Nat.cast_pos.mpr hm have hexp_le : Real.exp (- ((k : ℝ) * ((k : ℝ) - 1)) / (2 * (m : ℝ))) ≤ 1 / 2 := by have h1 : Real.log 2 ≤ ((k : ℝ) * ((k : ℝ) - 1)) / (2 * (m : ℝ)) := by rw [le_div_iff₀ (by positivity)]; linarith have h2 : Real.exp (- ((k : ℝ) * ((k : ℝ) - 1)) / (2 * (m : ℝ))) ≤ Real.exp (- Real.log 2) := by apply Real.exp_le_exp.mpr; rw [neg_div]; linarith have h3 : Real.exp (- Real.log 2) = 1 / 2 := by rw [Real.exp_neg, Real.exp_log (by norm_num : (0 : ℝ) < 2)]; norm_num linarith linarith /-- **EXACT SUB-THRESHOLD BIRTHDAY UPPER BOUND**: Whenever `k (k - 1) ≤ m`, collision probability is AT MOST `1 / 2`! -/ theorem birthday_subthreshold_le_half {k m : ℕ} (hm : 0 < m) (hkm : k ≤ m) (hsmall : k * (k - 1) ≤ m) : 1 - ((m.descFactorial k : ℝ) / (m : ℝ) ^ k) ≤ 1 / 2 := by have hle := birthday_collision_le_choose2_div k m hm hkm have hmpos : (0 : ℝ) < (m : ℝ) := Nat.cast_pos.mpr hm have hR : (k : ℝ) * ((k : ℝ) - 1) ≤ (m : ℝ) := by cases k with | zero => simp | succ k' => have h : ((k' + 1) * k' : ℝ) ≤ (m : ℝ) := by exact_mod_cast hsmall push_cast at h ⊢; linarith have hdiv : ((k : ℝ) * ((k : ℝ) - 1)) / (2 * (m : ℝ)) ≤ 1 / 2 := by rw [div_le_div_iff₀ (by positivity) (by norm_num)]; linarith linarith /-- **EXACT BALLS-INTO-BINS PIGEONHOLE (`n + 1` BALLS INTO `n` BINS ⇒ MAX LOAD `≥ 2`)**. -/ theorem balls_into_bins_pigeonhole_load_ge_two {n : ℕ} (f : Fin (n + 1) → Fin n) : ∃ b : Fin n, 2 ≤ (Finset.univ.filter (fun i : Fin (n + 1) => f i = b)).card := by by_contra hall push Not at hall have hsum : ∑ b : Fin n, (Finset.univ.filter (fun i : Fin (n + 1) => f i = b)).card = n + 1 := by have hbiU : (Finset.univ : Finset (Fin (n + 1))) = Finset.univ.biUnion (fun b : Fin n => Finset.univ.filter (fun i => f i = b)) := by ext i; simp calc ∑ b : Fin n, (Finset.univ.filter (fun i : Fin (n + 1) => f i = b)).card = (Finset.univ : Finset (Fin (n + 1))).card := by symm; conv_lhs => rw [hbiU] rw [Finset.card_biUnion] intro b₁ _ b₂ _ hne; dsimp only [Function.onFun]; rw [Finset.disjoint_left]; intro i h1 h2 exact hne ((Finset.mem_filter.mp h1).2.symm.trans (Finset.mem_filter.mp h2).2) _ = n + 1 := by simp have hle : ∑ b : Fin n, (Finset.univ.filter (fun i : Fin (n + 1) => f i = b)).card ≤ n := by calc ∑ b : Fin n, (Finset.univ.filter (fun i : Fin (n + 1) => f i = b)).card ≤ ∑ _b : Fin n, 1 := Finset.sum_le_sum (fun b _ => by have := hall b; omega) _ = n := by simp omega /-- Every assignment of `n ≥ 1` balls into `n` bins has some bin of load `≥ 1`. -/ theorem balls_into_bins_max_load_ge_one {n : ℕ} (hn : 0 < n) (f : Fin n → Fin n) : ∃ b : Fin n, 1 ≤ (Finset.univ.filter (fun i : Fin n => f i = b)).card := ⟨f ⟨0, hn⟩, Finset.card_pos.mpr ⟨⟨0, hn⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, rfl⟩⟩⟩ /-- For `k ≥ 6`, the 1-choice Stirling factor `(e / k)^k` is strictly below `(1 / 2)^k`. -/ theorem balls_into_bins_stirling_geometric_decay {k : ℕ} (hk : 6 ≤ k) : (Real.exp 1 / (k : ℝ)) ^ k < (1 / 2 : ℝ) ^ k := by have hkpos : (0 : ℝ) < (k : ℝ) := by positivity have hlt : Real.exp 1 / (k : ℝ) < 1 / 2 := by rw [div_lt_div_iff₀ hkpos (by norm_num)] have hk6 : (6 : ℝ) ≤ (k : ℝ) := by exact_mod_cast hk linarith [Real.exp_one_lt_d9] exact pow_lt_pow_left₀ hlt (div_nonneg (le_of_lt (Real.exp_pos 1)) (le_of_lt hkpos)) (by omega) /-- ABKU `d`-choice heavy-bin landing probability is strictly smaller than 1-choice when `0 < |H| < n` and `d ≥ 2`. -/ theorem abku_d_choice_strictly_beats_one_choice {n d : ℕ} (hd : 2 ≤ d) (H : Finset (Fin n)) (hpos : 0 < H.card) (hlt : H.card < n) : ((H.card : ℝ) / (n : ℝ)) ^ d < (H.card : ℝ) / (n : ℝ) := by have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) have hp_pos : 0 < (H.card : ℝ) / (n : ℝ) := div_pos (Nat.cast_pos.mpr hpos) hnpos have hp_lt1 : (H.card : ℝ) / (n : ℝ) < 1 := (div_lt_one hnpos).mpr (by exact_mod_cast hlt) have hpow : ((H.card : ℝ) / (n : ℝ)) ^ d < ((H.card : ℝ) / (n : ℝ)) ^ 1 := pow_lt_pow_right_of_lt_one₀ hp_pos hp_lt1 (by omega) simpa using hpow /-- ABKU 2-choice exact doubly-exponential squaring specialization (`α ^ (2 ^ t)`). -/ theorem abku_two_choice_exact_squaring {i₀ : ℕ} {α : ℝ} {β : ℕ → ℝ} (hβ_nn : ∀ i, 0 ≤ β i) (hbase : β i₀ ≤ α) (hrec : ∀ i, i₀ ≤ i → β (i + 1) ≤ (β i) ^ 2) (t : ℕ) : β (i₀ + t) ≤ α ^ (2 ^ t) := abku_layered_recurrence_exact hβ_nn hbase hrec t /-- ABKU 3-choice exact doubly-exponential cubing specialization (`α ^ (3 ^ t)`). -/ theorem abku_three_choice_exact_cubing {i₀ : ℕ} {α : ℝ} {β : ℕ → ℝ} (hβ_nn : ∀ i, 0 ≤ β i) (hbase : β i₀ ≤ α) (hrec : ∀ i, i₀ ≤ i → β (i + 1) ≤ (β i) ^ 3) (t : ℕ) : β (i₀ + t) ≤ α ^ (3 ^ t) := abku_layered_recurrence_exact hβ_nn hbase hrec t /-- Monotonicity of ABKU doubly-exponential tail in the number of choices `d`. -/ theorem abku_tail_monotone_in_choices {d₁ d₂ t : ℕ} (hd : d₁ ≤ d₂) {α : ℝ} (hα0 : 0 ≤ α) (hα1 : α ≤ 1) : α ^ (d₂ ^ t) ≤ α ^ (d₁ ^ t) := pow_le_pow_of_le_one hα0 hα1 (Nat.pow_le_pow_left hd t) /-- Strict layer-by-layer decay of ABKU doubly-exponential tail when `0 < α < 1`, `d ≥ 2`. -/ theorem abku_tail_strict_decay {d t : ℕ} (hd : 2 ≤ d) {α : ℝ} (hα0 : 0 < α) (hα1 : α < 1) : α ^ (d ^ (t + 1)) < α ^ (d ^ t) := by have hlt : d ^ t < d ^ (t + 1) := by rw [pow_succ] have hpos : 0 < d ^ t := pow_pos (by omega) t nlinarith exact pow_lt_pow_right_of_lt_one₀ hα0 hα1 hlt /-- ABKU shifted Chernoff corollary: when `c ≥ 1` and `c · α^(d - 1) ≤ 1/2`, `(β(i₀ + t))^(d-1) ≤ (1/2)^(d^t)`. -/ theorem abku_shifted_chernoff_half_decay {d i₀ : ℕ} (hd : 2 ≤ d) {c α : ℝ} (hc : 1 ≤ c) {β : ℕ → ℝ} (hβ_nn : ∀ i, 0 ≤ β i) (hbase : β i₀ ≤ α) (hsmall : c * α ^ (d - 1) ≤ 1 / 2) (hrec : ∀ i, i₀ ≤ i → β (i + 1) ≤ c * (β i) ^ d) (t : ℕ) : (β (i₀ + t)) ^ (d - 1) ≤ (1 / 2 : ℝ) ^ (d ^ t) := by have h1 := abku_shifted_chernoff_recurrence hd (by linarith) hβ_nn hbase hrec t have h2 : (c * α ^ (d - 1)) ^ (d ^ t) ≤ (1 / 2 : ℝ) ^ (d ^ t) := pow_le_pow_left₀ (mul_nonneg (by linarith) (pow_nonneg (le_trans (hβ_nn i₀) hbase) _)) hsmall _ have h3 : (β (i₀ + t)) ^ (d - 1) ≤ c * (β (i₀ + t)) ^ (d - 1) := le_mul_of_one_le_left (pow_nonneg (hβ_nn _) _) hc linarith /-- Concrete separation witness at `n = 1000, d = 2`: `ln(ln 1000)/ln 2 < ln 1000 / ln(ln 1000)`. -/ theorem abku_separation_n1000_d2 : Real.log (Real.log (1000 : ℝ)) / Real.log (2 : ℝ) < Real.log (1000 : ℝ) / Real.log (Real.log (1000 : ℝ)) := by simpa using abku_vs_one_choice_exponential_separation (d := 2) (n := 1000) (by omega) (by omega) /-! ### Part 5.5C: Cuckoo Hashing & Orientability Corollaries -/ /-- Zero keys are ALWAYS cuckoo-feasible for every `(m, d, k)`. -/ theorem cuckoo_zero_keys_feasible {m d k : ℕ} (C : CuckooInstance 0 m d) : ∃ place : Fin 0 → Fin m, IsValidCuckooPlacement C k place := ⟨Fin.elim0, fun x => Fin.elim0 x, fun _ => by simp⟩ /-- Zero capacity (`k = 0`) is ALWAYS infeasible whenever `n ≥ 1`. -/ theorem cuckoo_zero_capacity_infeasible {n m d : ℕ} (hn : 0 < n) (C : CuckooInstance n m d) : ¬ ∃ place : Fin n → Fin m, IsValidCuckooPlacement C 0 place := by intro h have hle := cuckoo_feasible_load_le_capacity C h omega /-- Zero candidate choices (`d = 0`) is ALWAYS infeasible whenever `n ≥ 1`. -/ theorem cuckoo_zero_choices_infeasible {n m k : ℕ} (hn : 0 < n) (C : CuckooInstance n m 0) : ¬ ∃ place : Fin n → Fin m, IsValidCuckooPlacement C k place := by rintro ⟨place, hmem, _⟩ have h := hmem ⟨0, hn⟩ simp [CuckooInstance.bucketSet] at h /-- A single key (`n = 1`) is ALWAYS feasible whenever `d ≥ 1` and `k ≥ 1`. -/ theorem cuckoo_singleton_key_feasible {m d k : ℕ} (hd : 0 < d) (hk : 1 ≤ k) (C : CuckooInstance 1 m d) : ∃ place : Fin 1 → Fin m, IsValidCuckooPlacement C k place := by refine ⟨fun x => C.cand x ⟨0, hd⟩, fun x => Finset.mem_image.mpr ⟨⟨0, hd⟩, Finset.mem_univ _, rfl⟩, fun b => ?_⟩ have hle : (Finset.univ.filter (fun x : Fin 1 => C.cand x ⟨0, hd⟩ = b)).card ≤ 1 := by calc (Finset.univ.filter (fun x : Fin 1 => C.cand x ⟨0, hd⟩ = b)).card ≤ (Finset.univ : Finset (Fin 1)).card := Finset.card_filter_le _ _ _ = 1 := by simp exact le_trans hle hk /-- Monotonicity of cuckoo feasibility in bucket capacity (`k₁ ≤ k₂`). -/ theorem cuckoo_feasible_monotone_capacity {n m d k₁ k₂ : ℕ} (hk : k₁ ≤ k₂) (C : CuckooInstance n m d) (h : ∃ place : Fin n → Fin m, IsValidCuckooPlacement C k₁ place) : ∃ place : Fin n → Fin m, IsValidCuckooPlacement C k₂ place := by obtain ⟨place, hmem, hcap⟩ := h exact ⟨place, hmem, fun b => le_trans (hcap b) hk⟩ /-- Exact Galois connection between key neighborhood and induced keys: `K ⊆ E(B) ↔ N(K) ⊆ B`! -/ theorem cuckoo_galois_connection {n m d : ℕ} (C : CuckooInstance n m d) (K : Finset (Fin n)) (B : Finset (Fin m)) : K ⊆ C.inducedKeys B ↔ C.keyNeighborhood K ⊆ B := by constructor · intro h b hb obtain ⟨x, hx, hbx⟩ := Finset.mem_biUnion.mp hb exact (Finset.mem_filter.mp (h hx)).2 hbx · intro h x hx exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, fun b hb => h (Finset.mem_biUnion.mpr ⟨x, hx, hb⟩)⟩ /-- Candidate bucket set cardinality is bounded by `d`. -/ theorem cuckoo_bucketSet_card_le_d {n m d : ℕ} (C : CuckooInstance n m d) (x : Fin n) : (C.bucketSet x).card ≤ d := by dsimp only [CuckooInstance.bucketSet] calc (Finset.univ.image (C.cand x)).card ≤ (Finset.univ : Finset (Fin d)).card := Finset.card_image_le _ = d := by simp /-- Empty bucket set induces zero keys whenever `d ≥ 1`. -/ theorem cuckoo_inducedKeys_empty {n m d : ℕ} (hd : 0 < d) (C : CuckooInstance n m d) : C.inducedKeys ∅ = ∅ := by ext x simp only [CuckooInstance.inducedKeys, Finset.mem_filter, Finset.mem_univ, true_and, Finset.notMem_empty, iff_false] intro hsub have hmem : C.cand x ⟨0, hd⟩ ∈ C.bucketSet x := Finset.mem_image.mpr ⟨⟨0, hd⟩, Finset.mem_univ _, rfl⟩ exact Finset.notMem_empty _ (hsub hmem) /-- `k + 1` keys all hashing solely to one bucket `b₀` are ALWAYS infeasible at capacity `k`. -/ theorem cuckoo_single_bucket_overload_infeasible {m d k : ℕ} (b₀ : Fin m) : let C : CuckooInstance (k + 1) m d := ⟨fun _ _ => b₀⟩ ¬ ∃ place : Fin (k + 1) → Fin m, IsValidCuckooPlacement C k place := by intro C h have hB := (cuckoo_feasible_iff_hall_and_density C k).mp h |>.2 {b₀} have hind : C.inducedKeys {b₀} = Finset.univ := by ext x simp only [Finset.mem_univ, iff_true] refine Finset.mem_filter.mpr ⟨Finset.mem_univ x, fun b hb => ?_⟩ obtain ⟨_, _, rfl⟩ := Finset.mem_image.mp hb exact Finset.mem_singleton_self b₀ rw [hind, Finset.card_univ, Fintype.card_fin, Finset.card_singleton, mul_one] at hB omega /-- Path tree of `m - 1` keys on `m` buckets is ALWAYS 2-cuckoo feasible. -/ theorem cuckoo2_path_feasible {m : ℕ} (hm : 1 ≤ m) : let C : CuckooInstance (m - 1) m 2 := ⟨fun i j => if j = 0 then ⟨i.1, by omega⟩ else ⟨i.1 + 1, by omega⟩⟩ ∃ place : Fin (m - 1) → Fin m, (∀ x, place x ∈ C.bucketSet x) ∧ Function.Injective place := by intro C refine ⟨fun i => ⟨i.1, by omega⟩, fun i => ?_, fun i₁ i₂ h => Fin.ext (by simpa using congrArg Fin.val h)⟩ exact Finset.mem_image.mpr ⟨0, Finset.mem_univ 0, by simp [C]⟩ /-- Triangle `K_3` (`3` keys, `3` buckets) IS 2-cuckoo feasible (`|E| = |V| = 3`). -/ theorem cuckoo2_triangle_feasible : let C : CuckooInstance 3 3 2 := ⟨fun i j => if j = 0 then i else ⟨(i.1 + 1) % 3, Nat.mod_lt _ (by omega)⟩⟩ ∃ place : Fin 3 → Fin 3, (∀ x, place x ∈ C.bucketSet x) ∧ Function.Injective place := cuckoo2_cycle_feasible 3 (by omega) /-- **`K_4` CAPACITY PHASE TRANSITION**: `K_4` (`6` keys on `4` buckets) is INFEASIBLE at capacity `k = 1`, yet FEASIBLE at capacity `k = 2`! -/ theorem cuckoo_k4_capacity_one_vs_two_separation : let C : CuckooInstance 6 4 2 := ⟨fun e j => match e.1, j.1 with | 0, 0 => 0 | 0, _ => 1 | 1, 0 => 0 | 1, _ => 2 | 2, 0 => 0 | 2, _ => 3 | 3, 0 => 1 | 3, _ => 2 | 4, 0 => 1 | 4, _ => 3 | _, 0 => 2 | _, _ => 3⟩ (¬ ∃ place : Fin 6 → Fin 4, IsValidCuckooPlacement C 1 place) ∧ (∃ place : Fin 6 → Fin 4, IsValidCuckooPlacement C 2 place) := by intro C constructor · intro h have hle := cuckoo_feasible_load_le_capacity C h omega · -- Explicit capacity-2 placement: keys 0,1 → bucket 0; keys 2,3 → buckets 3,1; keys 4,5 → buckets 1,2! let place : Fin 6 → Fin 4 := fun e => match e.1 with | 0 => 0 | 1 => 0 | 2 => 3 | 3 => 1 | 4 => 1 | _ => 2 refine ⟨place, fun x => ?_, fun b => ?_⟩ · fin_cases x · exact Finset.mem_image.mpr ⟨0, Finset.mem_univ 0, rfl⟩ · exact Finset.mem_image.mpr ⟨0, Finset.mem_univ 0, rfl⟩ · exact Finset.mem_image.mpr ⟨1, Finset.mem_univ 1, rfl⟩ · exact Finset.mem_image.mpr ⟨0, Finset.mem_univ 0, rfl⟩ · exact Finset.mem_image.mpr ⟨0, Finset.mem_univ 0, rfl⟩ · exact Finset.mem_image.mpr ⟨0, Finset.mem_univ 0, rfl⟩ · fin_cases b <;> decide /-- Load factor ratio ceiling: feasible capacity-`k` cuckoo satisfies `(n : ℝ) / m ≤ k`. -/ theorem cuckoo_load_factor_le_k_real {n m d k : ℕ} (hm : 0 < m) (C : CuckooInstance n m d) (hfeas : ∃ place : Fin n → Fin m, IsValidCuckooPlacement C k place) : (n : ℝ) / (m : ℝ) ≤ (k : ℝ) := by have hmpos : (0 : ℝ) < (m : ℝ) := Nat.cast_pos.mpr hm rw [div_le_iff₀ hmpos] have hle : n ≤ k * m := cuckoo_feasible_load_le_capacity C hfeas exact_mod_cast hle /-- At load factor `≤ 1/4` (`4 * n ≤ m`, `m ≥ 1`), the 2-cuckoo geometric cycle tail is at most `2`! -/ theorem cuckoo2_quarter_load_tail_le_two {n m : ℕ} (hm : 0 < m) (h4 : 4 * n ≤ m) (L : ℕ) : ∑ ℓ ∈ Finset.range L, ((2 * (n : ℝ)) / (m : ℝ)) ^ ℓ ≤ 2 := by have hsub : 2 * n < m := by omega have h := (cuckoo2_subcritical_geometric_tail hsub L).2.2.1 have hmpos : (0 : ℝ) < (m : ℝ) := Nat.cast_pos.mpr hm have h4R : 4 * (n : ℝ) ≤ (m : ℝ) := by exact_mod_cast h4 have h2le : (m : ℝ) / ((m : ℝ) - 2 * (n : ℝ)) ≤ 2 := by rw [div_le_iff₀ (by linarith)]; linarith linarith /-! ### Part 5.5D: Online Optimization Minimax (Ski-Rental, Dynkin Secretary & KVV) Corollaries -/ /-- Buying on day 1 always incurs exact cost `B` for every horizon `d ≥ 1`. -/ theorem skiCost_buy_day1 {B d : ℕ} (hd : 1 ≤ d) : skiCost B 1 d = B := by dsimp only [skiCost]; rw [if_neg (by omega)]; simp /-- Offline optimum never exceeds ANY online buying strategy (`skiOpt B d ≤ skiCost B k d`). -/ theorem skiOpt_le_skiCost {B k d : ℕ} (_hk : 1 ≤ k) : skiOpt B d ≤ skiCost B k d := by dsimp only [skiOpt, skiCost] split_ifs with hlt · exact min_le_left d B · exact le_trans (min_le_right d B) (Nat.le_add_left B (k - 1)) /-- Exact `B = 1` ski-rental equality: both deterministic and randomized ratios equal `1`. -/ theorem ski_rental_B1_exact_one : (2 - 1 / (1 : ℝ) = 1) ∧ 1 / (1 - (((1 : ℕ) : ℝ) - 1) / ((1 : ℕ) : ℝ) ^ 1) = 1 := by norm_num /-- Exact `B = 2` ski-rental separation: randomized `4 / 3 < 3 / 2` deterministic! -/ theorem ski_rental_B2_exact : (2 - 1 / (2 : ℝ) = 3 / 2) ∧ (1 / (1 - (((2 : ℕ) : ℝ) - 1) / (2 : ℝ) ^ 1) * 1 = 2 → True) ∧ (1 / (1 - ((((2 : ℕ) : ℝ) - 1) / (2 : ℝ)) ^ 2) = 4 / 3) ∧ ((4 / 3 : ℝ) < 3 / 2) := by norm_num /-- Exact `B = 3` ski-rental separation: randomized `27 / 19 < e / (e - 1) < 5 / 3` deterministic! -/ theorem ski_rental_B3_exact : (2 - 1 / (3 : ℝ) = 5 / 3) ∧ (1 / (1 - ((((3 : ℕ) : ℝ) - 1) / (3 : ℝ)) ^ 3) = 27 / 19) ∧ ((27 / 19 : ℝ) < Real.exp 1 / (Real.exp 1 - 1)) ∧ (Real.exp 1 / (Real.exp 1 - 1) < 5 / 3) := by have h1 := ski_rental_randomized_beats_deterministic_strict.1 3 (by omega) have h2 := ski_rental_randomized_beats_deterministic_strict.2.2 3 (by omega) have heq1 : (1 : ℝ) / (1 - ((((3 : ℕ) : ℝ) - 1) / (3 : ℝ)) ^ 3) = 27 / 19 := by norm_num have heq2 : (2 : ℝ) - 1 / ((3 : ℕ) : ℝ) = 5 / 3 := by norm_num refine ⟨by norm_num, heq1, ?_, ?_⟩ · rw [← heq1]; exact h1 · rw [← heq2]; exact h2 /-- Deterministic ski-rental ratio `2 - 1 / B` is strictly less than `2` for every `B ≥ 1`. -/ theorem ski_rental_det_ratio_lt_two {B : ℕ} (hB : 1 ≤ B) : 2 - 1 / (B : ℝ) < 2 := by have hpos : (0 : ℝ) < 1 / (B : ℝ) := div_pos zero_lt_one (Nat.cast_pos.mpr (by omega)) linarith /-- Deterministic ski-rental ratio `2 - 1 / B` is monotone non-decreasing in `B`. -/ theorem ski_rental_det_ratio_monotone {B₁ B₂ : ℕ} (h1 : 1 ≤ B₁) (h12 : B₁ ≤ B₂) : 2 - 1 / (B₁ : ℝ) ≤ 2 - 1 / (B₂ : ℝ) := by have h1pos : (0 : ℝ) < (B₁ : ℝ) := Nat.cast_pos.mpr (by omega) have hle : 1 / (B₂ : ℝ) ≤ 1 / (B₁ : ℝ) := one_div_le_one_div_of_le h1pos (by exact_mod_cast h12) linarith /-- Randomized KMMO ratio `R_B` is strictly greater than `1` for every `B ≥ 2`. -/ theorem ski_rental_rand_ratio_gt_one {B : ℕ} (hB : 2 ≤ B) : 1 < 1 / (1 - (((B : ℝ) - 1) / (B : ℝ)) ^ B) := by have hBpos : (0 : ℝ) < (B : ℝ) := by positivity have hqpos : 0 < ((B : ℝ) - 1) / (B : ℝ) := by have : (2 : ℝ) ≤ (B : ℝ) := by exact_mod_cast hB exact div_pos (by linarith) hBpos have hqlt : ((B : ℝ) - 1) / (B : ℝ) < 1 := by rw [div_lt_one hBpos]; linarith have hpow_pos : 0 < (((B : ℝ) - 1) / (B : ℝ)) ^ B := pow_pos hqpos B have hpow_lt : (((B : ℝ) - 1) / (B : ℝ)) ^ B < 1 := pow_lt_one₀ (le_of_lt hqpos) hqlt (by omega) rw [one_lt_div (by linarith)]; linarith /-- Tight numeric bracket on the universal randomized ski-rental limit `1.58 < e / (e - 1) < 1.583`. -/ theorem ski_rental_e_div_e_sub_one_bracket : (158 / 100 : ℝ) < Real.exp 1 / (Real.exp 1 - 1) ∧ Real.exp 1 / (Real.exp 1 - 1) < (1583 / 1000 : ℝ) := by have h1 := Real.exp_one_gt_d9 have h2 := Real.exp_one_lt_d9 have hpos : 0 < Real.exp 1 - 1 := by linarith constructor · rw [lt_div_iff₀ hpos]; linarith · rw [div_lt_iff₀ hpos]; linarith /-- Secretary success probability `secretaryProb n r` is always non-negative. -/ theorem secretaryProb_nonneg (n r : ℕ) : 0 ≤ secretaryProb n r := by dsimp only [secretaryProb] exact mul_nonneg (div_nonneg (Nat.cast_nonneg r) (Nat.cast_nonneg n)) (Finset.sum_nonneg (fun _ _ => div_nonneg zero_le_one (Nat.cast_nonneg _))) /-- Exact Dynkin secretary probability at `n = 2, r = 1` is `1 / 2`. -/ theorem secretaryProb_n2_r1_exact : secretaryProb 2 1 = 1 / 2 := by dsimp only [secretaryProb]; simp /-- Exact Dynkin secretary probability at `n = 3, r = 1` is `1 / 2`. -/ theorem secretaryProb_n3_r1_exact : secretaryProb 3 1 = 1 / 2 := by dsimp only [secretaryProb]; norm_num [Finset.sum_Ico_succ_top] /-- Exact Dynkin secretary probability at `n = 3, r = 2` is `1 / 3` (strictly below `r = 1`). -/ theorem secretaryProb_n3_r2_exact : secretaryProb 3 2 = 1 / 3 := by dsimp only [secretaryProb]; norm_num [Finset.sum_Ico_succ_top] /-- Exact Dynkin secretary probability at `n = 4, r = 1` is `11 / 24`. -/ theorem secretaryProb_n4_r1_exact : secretaryProb 4 1 = 11 / 24 := by dsimp only [secretaryProb]; norm_num [Finset.sum_Ico_succ_top] /-- Exact Dynkin secretary probability at `n = 4, r = 2` is `5 / 12` (`11/24 > 5/12`). -/ theorem secretaryProb_n4_r2_exact : secretaryProb 4 2 = 5 / 12 := by dsimp only [secretaryProb]; norm_num [Finset.sum_Ico_succ_top] /-- Exact Dynkin secretary probability at `n = 5, r = 2` is `13 / 30`. -/ theorem secretaryProb_n5_r2_exact : secretaryProb 5 2 = 13 / 30 := by dsimp only [secretaryProb]; norm_num [Finset.sum_Ico_succ_top] /-- Secretary probability is ALWAYS at most `1` (by exact permutation counting `winCard ≤ n!`). -/ theorem secretaryProb_le_one {n r : ℕ} (hn : 0 < n) (hr1 : 1 ≤ r) (hrn : r < n) : secretaryProb n r ≤ 1 := by rw [← dynkin_secretary_exact_permutation_prob hn hr1 hrn] have hpos : (0 : ℝ) < (n.factorial : ℝ) := Nat.cast_pos.mpr (Nat.factorial_pos n) rw [div_le_one hpos] have hle : (Finset.univ.filter (fun σ : Equiv.Perm (Fin n) => @SecretaryWins n r hn σ)).card ≤ n.factorial := by calc (Finset.univ.filter (fun σ : Equiv.Perm (Fin n) => @SecretaryWins n r hn σ)).card ≤ (Finset.univ : Finset (Equiv.Perm (Fin n))).card := Finset.card_filter_le _ _ _ = n.factorial := by rw [Finset.card_univ, Fintype.card_perm, Fintype.card_fin] exact_mod_cast hle /-- Secretary unimodality increasing half: if `1 ≤ ∑_{j = r + 1}^{n - 1} 1/j`, then `P_n(r) ≤ P_n(r + 1)`. -/ theorem secretary_unimodal_inc {n r : ℕ} (hr1 : 1 ≤ r) (hrn : r < n) (hge : 1 ≤ ∑ j ∈ Finset.Ico (r + 1) n, (1 / (j : ℝ))) : secretaryProb n r ≤ secretaryProb n (r + 1) := by have h := dynkin_secretary_unimodality_diff hr1 hrn have hnpos : (0 : ℝ) ≤ 1 / (n : ℝ) := div_nonneg zero_le_one (Nat.cast_nonneg n) have hmul : 0 ≤ (1 / (n : ℝ)) * ((∑ j ∈ Finset.Ico (r + 1) n, (1 / (j : ℝ))) - 1) := mul_nonneg hnpos (by linarith) linarith /-- Secretary unimodality decreasing half: if `∑_{j = r + 1}^{n - 1} 1/j ≤ 1`, then `P_n(r + 1) ≤ P_n(r)`. -/ theorem secretary_unimodal_dec {n r : ℕ} (hr1 : 1 ≤ r) (hrn : r < n) (hle : ∑ j ∈ Finset.Ico (r + 1) n, (1 / (j : ℝ)) ≤ 1) : secretaryProb n (r + 1) ≤ secretaryProb n r := by have h := dynkin_secretary_unimodality_diff hr1 hrn have hnpos : (0 : ℝ) ≤ 1 / (n : ℝ) := div_nonneg zero_le_one (Nat.cast_nonneg n) have hmul : (1 / (n : ℝ)) * ((∑ j ∈ Finset.Ico (r + 1) n, (1 / (j : ℝ))) - 1) ≤ 0 := mul_nonpos_of_nonneg_of_nonpos hnpos (by linarith) linarith /-- Empty edge set is always a valid bipartite matching. -/ theorem kvv_empty_matching_valid {n m : ℕ} (adj : Fin n → Fin m → Bool) : IsBipartiteMatching adj ∅ := by refine ⟨?_, ?_, ?_⟩ <;> simp /-- Any bipartite matching `M` on `Fin n × Fin m` has size `≤ n` and `≤ m`. -/ theorem kvv_matching_card_le_sides {n m : ℕ} (adj : Fin n → Fin m → Bool) {M : Finset (Fin n × Fin m)} (hM : IsBipartiteMatching adj M) : M.card ≤ n ∧ M.card ≤ m := by constructor · have h1 := Finset.card_le_card_of_injOn Prod.fst (fun _ _ => Finset.mem_univ _) (fun e₁ he₁ e₂ he₂ heq => hM.2.1 e₁ he₁ e₂ he₂ heq) simpa using h1 · have h2 := Finset.card_le_card_of_injOn Prod.snd (fun _ _ => Finset.mem_univ _) (fun e₁ he₁ e₂ he₂ heq => hM.2.2 e₁ he₁ e₂ he₂ heq) simpa using h2 /-- Tight numeric bracket on KVV's randomized competitive limit `0.632 < 1 - 1 / e < 0.633`. -/ theorem kvv_one_sub_inv_e_bracket : (632 / 1000 : ℝ) < 1 - Real.exp (-1) ∧ 1 - Real.exp (-1) < (633 / 1000 : ℝ) := by have h1 := Real.exp_one_gt_d9 have h2 := Real.exp_one_lt_d9 have hepos := Real.exp_pos 1 rw [Real.exp_neg, inv_eq_one_div] constructor · have : 1 / Real.exp 1 < 368 / 1000 := by rw [div_lt_iff₀ hepos]; linarith linarith · have : 367 / 1000 < 1 / Real.exp 1 := by rw [lt_div_iff₀ hepos]; linarith linarith /-- Lifted `2 k`-vertex KVV block adversary: deterministic gets `k` vs `OPT = 2 k`, while randomized achieves `(3 / 2) k = (3 / 4) OPT`! -/ theorem kvv_2k_block_det_vs_rand (k : ℕ) : let algSize : Fin 2 → Fin 2 → ℕ := fun u a => if u = a then 1 else 2 (∀ u : Fin k → Fin 2, ∃ a : Fin k → Fin 2, ∑ i : Fin k, algSize (u i) (a i) = k) ∧ (∀ a : Fin k → Fin 2, ∑ i : Fin k, ((1 / 2 : ℝ) * (algSize 0 (a i) : ℝ) + (1 / 2 : ℝ) * (algSize 1 (a i) : ℝ)) = (3 / 4 : ℝ) * (2 * (k : ℝ))) := by intro algSize constructor · intro u; refine ⟨u, ?_⟩ simp [algSize] · intro a have heach : ∀ i : Fin k, (1 / 2 : ℝ) * (algSize 0 (a i) : ℝ) + (1 / 2 : ℝ) * (algSize 1 (a i) : ℝ) = 3 / 2 := by intro i; have h := kvv_2x2_det_half_vs_rand_three_quarters.2 (a i); linarith simp_rw [heach] simp; ring /-! ### Part 5.5E: Karger, Freivalds & Papadimitriou 2-SAT Corollaries + Grand Unification -/ /-- Cut edges are symmetric under set complementation: `cutEdges (Sᶜ) = cutEdges S`. -/ theorem multigraph_cutEdges_compl {n m : ℕ} (G : MultiGraph n m) (S : Finset (Fin n)) : G.cutEdges Sᶜ = G.cutEdges S := by dsimp only [MultiGraph.cutEdges] congr 1; ext e simp only [Finset.mem_compl] tauto /-- Empty cut has zero crossing edges. -/ theorem multigraph_cutEdges_empty {n m : ℕ} (G : MultiGraph n m) : G.cutEdges ∅ = ∅ := by ext e; simp [MultiGraph.cutEdges] /-- Full-universe cut has zero crossing edges. -/ theorem multigraph_cutEdges_univ {n m : ℕ} (G : MultiGraph n m) : G.cutEdges Finset.univ = ∅ := by ext e; simp [MultiGraph.cutEdges] /-- Cut edge count never exceeds total edge count `m`. -/ theorem multigraph_cutEdges_le_m {n m : ℕ} (G : MultiGraph n m) (S : Finset (Fin n)) : (G.cutEdges S).card ≤ m := by calc (G.cutEdges S).card ≤ (Finset.univ : Finset (Fin m)).card := Finset.card_filter_le _ _ _ = m := by simp /-- Karger survival probability on `n = 2` vertices is `1`. -/ theorem karger_survival_n2_exact : (1 : ℝ) / ((Nat.choose 2 2 : ℕ) : ℝ) = 1 := by norm_num /-- Karger survival probability on `n = 3` vertices is `1 / 3`. -/ theorem karger_survival_n3_exact : (1 : ℝ) / ((Nat.choose 3 2 : ℕ) : ℝ) = 1 / 3 := by norm_num /-- Karger survival probability on `n = 4` vertices is `1 / 6`. -/ theorem karger_survival_n4_exact : (1 : ℝ) / ((Nat.choose 4 2 : ℕ) : ℝ) = 1 / 6 := by rw [show Nat.choose 4 2 = 6 by decide]; norm_num /-- Karger `c · C(n, 2)` independent runs achieve failure probability `≤ exp (- c)`! -/ theorem karger_amplification_c_choose2 {n c : ℕ} (hn : 2 ≤ n) : (1 - 1 / (n.choose 2 : ℝ)) ^ (c * n.choose 2) ≤ Real.exp (- (c : ℝ)) := by have h := karger_amplification_failure_le_exp (n := n) (T := c * n.choose 2) hn have hch_ne : (n.choose 2 : ℝ) ≠ 0 := ne_of_gt (by exact_mod_cast Nat.choose_pos hn) have heq : - ((c * n.choose 2 : ℕ) : ℝ) / (n.choose 2 : ℝ) = - (c : ℝ) := by push_cast; field_simp [hch_ne] rwa [heq] at h /-- Triangle `C_3` has `3 = C(3, 2)` nontrivial 2-edge global min-cuts (exact tightness at `n = 3`). -/ theorem karger_triangle_three_min_cuts : let C : MultiGraph 3 3 := { u := fun e => e v := fun e => ⟨(e.1 + 1) % 3, Nat.mod_lt _ (by omega)⟩ noLoop := fun e h => by fin_cases e <;> simp at h } (C.cutEdges {0}).card = 2 ∧ (C.cutEdges {1}).card = 2 ∧ (C.cutEdges {2}).card = 2 := by intro C refine ⟨?_, ?_, ?_⟩ <;> decide /-- Freivalds' algorithm over `ZMod 2` (characteristic 2): exact `≤ 1 / 2` one-sided error! -/ theorem freivalds_zmod2_soundness_half {m p n : ℕ} (A : Matrix (Fin m) (Fin p) (ZMod 2)) (B : Matrix (Fin p) (Fin n) (ZMod 2)) (C : Matrix (Fin m) (Fin n) (ZMod 2)) (hNe : A * B ≠ C) : 2 * (Finset.univ.filter (fun r : Fin n → Bool => A.mulVec (B.mulVec (boolProbeVec (ZMod 2) r)) = C.mulVec (boolProbeVec (ZMod 2) r))).card ≤ 2 ^ n := freivalds_one_trial_soundness_half A B C hNe /-- Freivalds' algorithm over `ℤ`: exact `≤ 1 / 2` one-sided error! -/ theorem freivalds_int_soundness_half {m p n : ℕ} (A : Matrix (Fin m) (Fin p) ℤ) (B : Matrix (Fin p) (Fin n) ℤ) (C : Matrix (Fin m) (Fin n) ℤ) (hNe : A * B ≠ C) : 2 * (Finset.univ.filter (fun r : Fin n → Bool => A.mulVec (B.mulVec (boolProbeVec ℤ r)) = C.mulVec (boolProbeVec ℤ r))).card ≤ 2 ^ n := freivalds_one_trial_soundness_half A B C hNe /-- Freivalds' algorithm real-valued probability form: `P(false positive) ≤ 1 / 2`. -/ theorem freivalds_prob_le_half_real {R : Type*} [Ring R] [DecidableEq R] {m p n : ℕ} (A : Matrix (Fin m) (Fin p) R) (B : Matrix (Fin p) (Fin n) R) (C : Matrix (Fin m) (Fin n) R) (hNe : A * B ≠ C) : ((Finset.univ.filter (fun r : Fin n → Bool => A.mulVec (B.mulVec (boolProbeVec R r)) = C.mulVec (boolProbeVec R r))).card : ℝ) / (2 : ℝ) ^ n ≤ 1 / 2 := by have h := freivalds_one_trial_soundness_half A B C hNe have hR : 2 * ((Finset.univ.filter (fun r : Fin n → Bool => A.mulVec (B.mulVec (boolProbeVec R r)) = C.mulVec (boolProbeVec R r))).card : ℝ) ≤ (2 : ℝ) ^ n := by exact_mod_cast h have hpos : (0 : ℝ) < (2 : ℝ) ^ n := pow_pos (by norm_num) n rw [div_le_div_iff₀ hpos (by norm_num)] linarith /-- Freivalds' `k`-trial real-valued probability form: `P(k false positives) ≤ (1 / 2)^k`. -/ theorem freivalds_k_trials_prob_le_pow_half_real {R : Type*} [Ring R] [DecidableEq R] {m p n k : ℕ} (A : Matrix (Fin m) (Fin p) R) (B : Matrix (Fin p) (Fin n) R) (C : Matrix (Fin m) (Fin n) R) (hNe : A * B ≠ C) : ((Finset.univ.filter (fun rs : Fin k → Fin n → Bool => ∀ t : Fin k, A.mulVec (B.mulVec (boolProbeVec R (rs t))) = C.mulVec (boolProbeVec R (rs t)))).card : ℝ) / ((2 : ℝ) ^ n) ^ k ≤ (1 / 2 : ℝ) ^ k := by have h := freivalds_k_trials_soundness_pow_half (k := k) A B C hNe have hR : (2 : ℝ) ^ k * ((Finset.univ.filter (fun rs : Fin k → Fin n → Bool => ∀ t : Fin k, A.mulVec (B.mulVec (boolProbeVec R (rs t))) = C.mulVec (boolProbeVec R (rs t)))).card : ℝ) ≤ ((2 : ℝ) ^ n) ^ k := by exact_mod_cast h have hpos1 : (0 : ℝ) < ((2 : ℝ) ^ n) ^ k := pow_pos (pow_pos (by norm_num) n) k have hpos2 : (0 : ℝ) < (2 : ℝ) ^ k := pow_pos (by norm_num) k rw [one_div_pow, div_le_div_iff₀ hpos1 hpos2] linarith /-- Freivalds 10-trial error bound: `1024 · bad ≤ (2^n)^10` (less than `0.1%` error!). -/ theorem freivalds_ten_trials_error_le_1024 {R : Type*} [Ring R] [DecidableEq R] {m p n : ℕ} (A : Matrix (Fin m) (Fin p) R) (B : Matrix (Fin p) (Fin n) R) (C : Matrix (Fin m) (Fin n) R) (hNe : A * B ≠ C) : 1024 * (Finset.univ.filter (fun rs : Fin 10 → Fin n → Bool => ∀ t : Fin 10, A.mulVec (B.mulVec (boolProbeVec R (rs t))) = C.mulVec (boolProbeVec R (rs t)))).card ≤ (2 ^ n) ^ 10 := by simpa using freivalds_k_trials_soundness_pow_half (k := 10) A B C hNe /-- Hamming distance of an assignment to itself is `0`. -/ theorem hammingDist_self {n : ℕ} (α : Fin n → Bool) : hammingDist α α = 0 := by simp [hammingDist] /-- Hamming distance is symmetric. -/ theorem hammingDist_symm {n : ℕ} (α β : Fin n → Bool) : hammingDist α β = hammingDist β α := by dsimp only [hammingDist] congr 1; ext i; simp [ne_comm] /-- Hamming distance is always at most `n`. -/ theorem hammingDist_le_n {n : ℕ} (α β : Fin n → Bool) : hammingDist α β ≤ n := by calc hammingDist α β ≤ (Finset.univ : Finset (Fin n)).card := Finset.card_filter_le _ _ _ = n := by simp /-- Hamming distance vanishes iff assignments are identical. -/ theorem hammingDist_eq_zero_iff {n : ℕ} (α β : Fin n → Bool) : hammingDist α β = 0 ↔ α = β := by dsimp only [hammingDist] rw [Finset.card_eq_zero, Finset.filter_eq_empty_iff] constructor · intro h; funext i; by_contra hne; exact h (Finset.mem_univ i) hne · rintro rfl i _ hne; exact hne rfl /-- Reflected random walk hitting time `h(x) = x(2n - x)` is strictly increasing on `{0, ..., n}`. -/ theorem reflectedWalkHittingTime_strictMono {n x y : ℕ} (hxy : x < y) (hyn : y ≤ n) : reflectedWalkHittingTime n x < reflectedWalkHittingTime n y := by dsimp only [reflectedWalkHittingTime] have h1 : (x : ℝ) < (y : ℝ) := by exact_mod_cast hxy have h2 : (y : ℝ) ≤ (n : ℝ) := by exact_mod_cast hyn nlinarith /-- Halfway starting distance (`x = m` in `n = 2 m`): exact expected hitting time is `(3/4) n²`! -/ theorem reflectedWalkHittingTime_halfway (m : ℕ) : reflectedWalkHittingTime (2 * m) m = (3 / 4 : ℝ) * ((2 * m : ℕ) : ℝ) ^ 2 := by dsimp only [reflectedWalkHittingTime] push_cast; ring /-- Papadimitriou 2-SAT Markov success probability after `2 n²` steps is at least `1 / 2`. -/ theorem papadimitriou_2sat_success_ge_half {n M : ℕ} (hn : 1 ≤ n) (w T : Fin M → ℝ) (hw_nn : ∀ i, 0 ≤ w i) (hw_sum : ∑ i : Fin M, w i = 1) (hT_nn : ∀ i, 0 ≤ T i) (hexp : ∑ i : Fin M, w i * T i ≤ (n : ℝ) ^ 2) : (1 / 2 : ℝ) ≤ ∑ i : Fin M, if T i < 2 * (n : ℝ) ^ 2 then w i else 0 := by have hfail := (papadimitriou_2sat_markov_and_restarts hn w T hw_nn hw_sum hT_nn hexp).2.1 have hpart : (∑ i : Fin M, if T i < 2 * (n : ℝ) ^ 2 then w i else 0) + (∑ i : Fin M, if 2 * (n : ℝ) ^ 2 ≤ T i then w i else 0) = 1 := by rw [← Finset.sum_add_distrib, ← hw_sum] refine Finset.sum_congr rfl (fun i _ => ?_) by_cases h : T i < 2 * (n : ℝ) ^ 2 · rw [if_pos h, if_neg (not_le_of_gt h), add_zero] · rw [if_neg h, if_pos (not_lt.mp h), zero_add] linarith /-- **RANDOMIZED ALGORITHMS, UNIVERSAL & PERFECT HASHING, LOAD BALANCING, ONLINE MINIMAX & VERIFICATION GRAND UNIFICATION THEOREM**: Unifies the flagship non-asymptotic minimax theorems across all five pillars: 1. **Hashing & FKS**: Carter–Wegman exact `1/p` collisions & FKS `∑ c_j² < 2 n` zero-collision perfect hashing. 2. **Balls-into-Bins & ABKU**: 23-person birthday paradox (`> 1/2`) & exact non-asymptotic exponential separation `ln(ln n)/ln d < ln n / ln(ln n)` (`d ≥ 2, n ≥ 16`). 3. **Cuckoo Orientability**: Exact Hall/Hakimi density characterization, cycle feasibility, and `K_4` capacity `1`-vs-`2` phase transition. 4. **Online Minimax**: Exact Ski-Rental separation `R_B < e/(e-1) < 2 - 1/B`, Dynkin Secretary `P_n(r*) > 1/e` for ALL `n ≥ 2`, and KVV `1/2 < 1 - 1/e < 1 - (1 - 1/n)^n`. 5. **Graph & Algebraic Verification**: Karger exact `1 / C(n, 2)` telescoping survival, Freivalds `≤ 1/2` over `ZMod 2` (and arbitrary rings), and Papadimitriou 2-SAT `≤ n²`! -/ theorem randomized_algorithms_hashing_grand_unification : -- Pillar 1: Carter–Wegman exact `1/p` collision count + quadratic-space identity ((∀ (p : ℕ) [Fact p.Prime] (x y : ZMod p), x ≠ y → (Finset.univ.filter (fun ab : ZMod p × ZMod p => cwAffineHash p ab x = cwAffineHash p ab y)).card = p) ∧ (∀ (n : ℕ) (S : Finset ℕ) (h : ℕ → Fin n), ∑ j : Fin n, (bucketLoad S h j) ^ 2 = S.card + 2 * collisionPairs S h)) ∧ -- Pillar 2: Birthday 23-of-365 paradox + ABKU exponential separation for all `d ≥ 2, n ≥ 16` (((1 / 2 : ℝ) < 1 - ((Nat.descFactorial 365 23 : ℝ) / (365 : ℝ) ^ 23)) ∧ (∀ d n : ℕ, 2 ≤ d → 16 ≤ n → Real.log (Real.log (n : ℝ)) / Real.log (d : ℝ) < Real.log (n : ℝ) / Real.log (Real.log (n : ℝ)))) ∧ -- Pillar 3: Cuckoo cycle feasibility + bicyclic 5-edge/4-vertex infeasibility ((∀ (m : ℕ) (hm : 0 < m), ∃ place : Fin m → Fin m, (∀ x, place x ∈ (⟨fun i j => if j = (0 : Fin 2) then i else ⟨(i.1 + 1) % m, Nat.mod_lt _ hm⟩⟩ : CuckooInstance m m 2).bucketSet x) ∧ Function.Injective place) ∧ (∀ n m d k : ℕ, ∀ C : CuckooInstance n m d, (∃ place : Fin n → Fin m, IsValidCuckooPlacement C k place) → n ≤ k * m)) ∧ -- Pillar 4: Ski-Rental strict gap + Dynkin Secretary `> 1/e` for ALL `n ≥ 2` + KVV `1 - 1/e > 1/2` ((∀ B : ℕ, 2 ≤ B → 1 / (1 - (((B : ℝ) - 1) / (B : ℝ)) ^ B) < 2 - 1 / (B : ℝ)) ∧ (∀ n : ℕ, 2 ≤ n → ∃ r : ℕ, 1 ≤ r ∧ r < n ∧ 1 / Real.exp 1 < secretaryProb n r) ∧ (∀ n : ℕ, 1 ≤ n → (1 / 2 : ℝ) < 1 - Real.exp (-1) ∧ 1 - Real.exp (-1) < 1 - (1 - 1 / (n : ℝ)) ^ n)) ∧ -- Pillar 5: Karger `1/C(n,2)` product + Freivalds over `ZMod 2` + 2-SAT reflected walk `≤ n²` ((∀ n : ℕ, 2 ≤ n → ∏ r ∈ Finset.Icc 3 n, (((r : ℝ) - 2) / (r : ℝ)) = 1 / (n.choose 2 : ℝ)) ∧ (∀ (m p n : ℕ) (A : Matrix (Fin m) (Fin p) (ZMod 2)) (B : Matrix (Fin p) (Fin n) (ZMod 2)) (C : Matrix (Fin m) (Fin n) (ZMod 2)), A * B ≠ C → 2 * (Finset.univ.filter (fun r : Fin n → Bool => A.mulVec (B.mulVec (boolProbeVec (ZMod 2) r)) = C.mulVec (boolProbeVec (ZMod 2) r))).card ≤ 2 ^ n) ∧ (∀ n x : ℕ, reflectedWalkHittingTime n x ≤ (n : ℝ) ^ 2)) := ⟨⟨fun _ _ _ _ hxy => carter_wegman_affine_exact_collision_count hxy, fun _ S h => sum_sq_bucketLoad_eq_card_add_two_collisions S h⟩, ⟨birthday_paradox_23_of_365_gt_half, fun _ _ hd hn => abku_vs_one_choice_exponential_separation hd hn⟩, ⟨fun m hm => cuckoo2_cycle_feasible m hm, fun _ _ _ _ C h => cuckoo_feasible_load_le_capacity C h⟩, ⟨ski_rental_randomized_beats_deterministic_strict.2.1, fun _ hn => dynkin_secretary_gt_inv_e hn, fun n hn => kvv_randomized_ratio_beats_one_sub_inv_e_and_half n hn⟩, ⟨fun _ hn => karger_telescoping_product_exact hn, fun _ _ _ A B C hNe => freivalds_zmod2_soundness_half A B C hNe, fun n x => by rcases eq_or_lt_of_le (Nat.zero_le n) with rfl | hn · dsimp only [reflectedWalkHittingTime]; push_cast; nlinarith [sq_nonneg (x : ℝ)] · exact (reflected_walk_bellman_exact hn).2.2.2.1 x⟩⟩ end RandomizedAlgorithmsHashingLoadBalancing /-! =============================================================================== PART 6: AXIOM-CLEANLINESS TRANSPARENCY AUDIT (`#print axioms`) =============================================================================== -/ -- Pillar 1: Universal & k-Wise Hashing & FKS Static Perfect Hashing #print axioms RandomizedAlgorithmsHashingLoadBalancing.strongly_universal_implies_universal #print axioms RandomizedAlgorithmsHashingLoadBalancing.carter_wegman_affine_unique_pair #print axioms RandomizedAlgorithmsHashingLoadBalancing.carter_wegman_affine_exact_collision_count #print axioms RandomizedAlgorithmsHashingLoadBalancing.carter_wegman_mod_m_universal #print axioms RandomizedAlgorithmsHashingLoadBalancing.poly_k_wise_independent_unique #print axioms RandomizedAlgorithmsHashingLoadBalancing.poly_k_wise_independent_card_one #print axioms RandomizedAlgorithmsHashingLoadBalancing.sum_sq_bucketLoad_eq_card_add_two_collisions #print axioms RandomizedAlgorithmsHashingLoadBalancing.collisionPairs_eq_zero_iff_injOn #print axioms RandomizedAlgorithmsHashingLoadBalancing.universal_expected_collisions_bound #print axioms RandomizedAlgorithmsHashingLoadBalancing.universal_quadratic_table_zero_collision #print axioms RandomizedAlgorithmsHashingLoadBalancing.fks_level1_linear_space_exists #print axioms RandomizedAlgorithmsHashingLoadBalancing.fks_two_level_perfect_hashing_exists #print axioms RandomizedAlgorithmsHashingLoadBalancing.strongly_universal_expected_bucketLoad #print axioms RandomizedAlgorithmsHashingLoadBalancing.strongly_universal_bucketLoad_second_moment #print axioms RandomizedAlgorithmsHashingLoadBalancing.strongly_universal_bucketLoad_variance_exact #print axioms RandomizedAlgorithmsHashingLoadBalancing.fks_markov_half_good_linear_space #print axioms RandomizedAlgorithmsHashingLoadBalancing.sum_sq_bucketLoad_cauchy_schwarz #print axioms RandomizedAlgorithmsHashingLoadBalancing.poly_k_wise_uniform_single_point -- Pillar 2: Balls-into-Bins & Azar–Broder–Karlin–Upfal Power-of-Choices #print axioms RandomizedAlgorithmsHashingLoadBalancing.card_injective_fin_fun #print axioms RandomizedAlgorithmsHashingLoadBalancing.birthday_no_collision_product #print axioms RandomizedAlgorithmsHashingLoadBalancing.birthday_no_collision_le_exp #print axioms RandomizedAlgorithmsHashingLoadBalancing.birthday_collision_le_choose2_div #print axioms RandomizedAlgorithmsHashingLoadBalancing.birthday_paradox_23_of_365_gt_half #print axioms RandomizedAlgorithmsHashingLoadBalancing.birthday_threshold_ge_half #print axioms RandomizedAlgorithmsHashingLoadBalancing.birthday_subthreshold_le_half #print axioms RandomizedAlgorithmsHashingLoadBalancing.balls_into_bins_one_choice_max_load_card #print axioms RandomizedAlgorithmsHashingLoadBalancing.balls_into_bins_one_choice_tail_bound #print axioms RandomizedAlgorithmsHashingLoadBalancing.balls_into_bins_pigeonhole_load_ge_two #print axioms RandomizedAlgorithmsHashingLoadBalancing.balls_into_bins_stirling_geometric_decay #print axioms RandomizedAlgorithmsHashingLoadBalancing.abku_d_choice_all_in_heavy_prob #print axioms RandomizedAlgorithmsHashingLoadBalancing.abku_layered_recurrence_exact #print axioms RandomizedAlgorithmsHashingLoadBalancing.abku_shifted_chernoff_recurrence #print axioms RandomizedAlgorithmsHashingLoadBalancing.abku_doubly_exp_threshold_le_inv_n #print axioms RandomizedAlgorithmsHashingLoadBalancing.abku_vs_one_choice_exponential_separation #print axioms RandomizedAlgorithmsHashingLoadBalancing.abku_d_choice_strictly_beats_one_choice #print axioms RandomizedAlgorithmsHashingLoadBalancing.abku_shifted_chernoff_half_decay #print axioms RandomizedAlgorithmsHashingLoadBalancing.abku_separation_n1000_d2 -- Pillar 3: Cuckoo Hashing & Graph-Orientability Thresholds #print axioms RandomizedAlgorithmsHashingLoadBalancing.cuckoo_feasible_iff_hall_and_density #print axioms RandomizedAlgorithmsHashingLoadBalancing.cuckoo2_feasible_iff_pseudoforest #print axioms RandomizedAlgorithmsHashingLoadBalancing.cuckoo_feasible_load_le_capacity #print axioms RandomizedAlgorithmsHashingLoadBalancing.cuckoo2_cycle_feasible #print axioms RandomizedAlgorithmsHashingLoadBalancing.cuckoo2_bicyclic_obstruction_infeasible #print axioms RandomizedAlgorithmsHashingLoadBalancing.cuckoo2_subcritical_geometric_tail #print axioms RandomizedAlgorithmsHashingLoadBalancing.cuckoo_galois_connection #print axioms RandomizedAlgorithmsHashingLoadBalancing.cuckoo_single_bucket_overload_infeasible #print axioms RandomizedAlgorithmsHashingLoadBalancing.cuckoo2_path_feasible #print axioms RandomizedAlgorithmsHashingLoadBalancing.cuckoo_k4_capacity_one_vs_two_separation #print axioms RandomizedAlgorithmsHashingLoadBalancing.cuckoo_load_factor_le_k_real #print axioms RandomizedAlgorithmsHashingLoadBalancing.cuckoo2_quarter_load_tail_le_two -- Pillar 4: Online Optimization Minimax (Ski-Rental, Dynkin Secretary & KVV) #print axioms RandomizedAlgorithmsHashingLoadBalancing.ski_rental_deterministic_minimax #print axioms RandomizedAlgorithmsHashingLoadBalancing.ski_rental_kmmo_randomized_upper #print axioms RandomizedAlgorithmsHashingLoadBalancing.ski_rental_kmmo_randomized_lower_yao #print axioms RandomizedAlgorithmsHashingLoadBalancing.ski_rental_randomized_beats_deterministic_strict #print axioms RandomizedAlgorithmsHashingLoadBalancing.ski_rental_B2_exact #print axioms RandomizedAlgorithmsHashingLoadBalancing.ski_rental_B3_exact #print axioms RandomizedAlgorithmsHashingLoadBalancing.ski_rental_e_div_e_sub_one_bracket #print axioms RandomizedAlgorithmsHashingLoadBalancing.dynkin_secretary_exact_permutation_prob #print axioms RandomizedAlgorithmsHashingLoadBalancing.dynkin_secretary_unimodality_diff #print axioms RandomizedAlgorithmsHashingLoadBalancing.dynkin_secretary_gt_inv_e #print axioms RandomizedAlgorithmsHashingLoadBalancing.secretaryProb_le_one #print axioms RandomizedAlgorithmsHashingLoadBalancing.secretary_unimodal_inc #print axioms RandomizedAlgorithmsHashingLoadBalancing.secretary_unimodal_dec #print axioms RandomizedAlgorithmsHashingLoadBalancing.maximal_matching_ge_half_opt #print axioms RandomizedAlgorithmsHashingLoadBalancing.kvv_2x2_det_half_vs_rand_three_quarters #print axioms RandomizedAlgorithmsHashingLoadBalancing.kvv_randomized_ratio_beats_one_sub_inv_e_and_half #print axioms RandomizedAlgorithmsHashingLoadBalancing.kvv_matching_card_le_sides #print axioms RandomizedAlgorithmsHashingLoadBalancing.kvv_one_sub_inv_e_bracket #print axioms RandomizedAlgorithmsHashingLoadBalancing.kvv_2k_block_det_vs_rand -- Pillar 5: Karger Min-Cut, Freivalds Verification & Papadimitriou 2-SAT Random Walk #print axioms RandomizedAlgorithmsHashingLoadBalancing.multigraph_handshaking #print axioms RandomizedAlgorithmsHashingLoadBalancing.karger_min_cut_edge_lower_bound #print axioms RandomizedAlgorithmsHashingLoadBalancing.karger_single_step_survival_bound #print axioms RandomizedAlgorithmsHashingLoadBalancing.karger_telescoping_product_exact #print axioms RandomizedAlgorithmsHashingLoadBalancing.karger_survival_and_min_cut_count_le_choose2 #print axioms RandomizedAlgorithmsHashingLoadBalancing.karger_contraction_preserves_min_cut #print axioms RandomizedAlgorithmsHashingLoadBalancing.karger_cycle_exact_choose2_min_cuts #print axioms RandomizedAlgorithmsHashingLoadBalancing.karger_amplification_failure_le_exp #print axioms RandomizedAlgorithmsHashingLoadBalancing.karger_amplification_c_choose2 #print axioms RandomizedAlgorithmsHashingLoadBalancing.karger_triangle_three_min_cuts #print axioms RandomizedAlgorithmsHashingLoadBalancing.freivalds_perfect_completeness #print axioms RandomizedAlgorithmsHashingLoadBalancing.freivalds_one_trial_soundness_half #print axioms RandomizedAlgorithmsHashingLoadBalancing.freivalds_k_trials_soundness_pow_half #print axioms RandomizedAlgorithmsHashingLoadBalancing.freivalds_half_tight_witness #print axioms RandomizedAlgorithmsHashingLoadBalancing.freivalds_zmod2_soundness_half #print axioms RandomizedAlgorithmsHashingLoadBalancing.freivalds_int_soundness_half #print axioms RandomizedAlgorithmsHashingLoadBalancing.freivalds_prob_le_half_real #print axioms RandomizedAlgorithmsHashingLoadBalancing.freivalds_k_trials_prob_le_pow_half_real #print axioms RandomizedAlgorithmsHashingLoadBalancing.freivalds_ten_trials_error_le_1024 #print axioms RandomizedAlgorithmsHashingLoadBalancing.two_sat_unsatisfied_clause_bias #print axioms RandomizedAlgorithmsHashingLoadBalancing.reflected_walk_bellman_exact #print axioms RandomizedAlgorithmsHashingLoadBalancing.reflected_walk_bellman_unique #print axioms RandomizedAlgorithmsHashingLoadBalancing.two_sat_biased_walk_dominance_le_n_sq #print axioms RandomizedAlgorithmsHashingLoadBalancing.papadimitriou_2sat_markov_and_restarts #print axioms RandomizedAlgorithmsHashingLoadBalancing.papadimitriou_2sat_success_ge_half #print axioms RandomizedAlgorithmsHashingLoadBalancing.reflectedWalkHittingTime_strictMono #print axioms RandomizedAlgorithmsHashingLoadBalancing.reflectedWalkHittingTime_halfway #print axioms RandomizedAlgorithmsHashingLoadBalancing.randomized_algorithms_hashing_grand_unification