import Mathlib /-! =============================================================================== AMORTIZED DATA STRUCTURES, POTENTIAL METHODS, SELF-ADJUSTING TREES, UNION-FIND & CELL-PROBE MEMORY LOWER BOUNDS IN LEAN 4 =============================================================================== A unified, 100% self-contained, non-asymptotic, zero-gap Lean 4 / Mathlib formalization across five interlocking pillars of Data Structure Theory: 1. **Part 1 — Tarjan's Potential Method, Binary Counter, Dynamic Table Hysteresis & Fibonacci Heaps:** - Exact Tarjan potential telescoping, non-negative potential upper bound, banker's method equivalence, additive composition, and shift/scale laws. - Exact binary counter bit-flip identity `popcount(n+1) + trailingOnes(n) = popcount(n) + 1`, exact amortized cost `= 2` per increment, total flips `2T - popcount(T) < 2T`, and worst-case step `k + 1` at `2^k - 1`. - Dynamic table insert-only doubling (exact amortized `≤ 3`), insert/delete quarter-halving hysteresis (`Φ_hyst ≥ 0`, insert `≤ 3`, delete `≤ 2`), and matching no-hysteresis thrashing lower bound (`≥ T · (m / 2)`). - Fibonacci heap golden-ratio growth `F_{k+2} ≥ φ^k`, Fredman–Tarjan subtree size lower bound `≥ F_{k+2}`, degree ceiling `deg ≤ log_φ n`, and `O(1)` cascading-cut amortized potential bound. 2. **Part 2 — Self-Adjusting Binary Search Trees & Sleator–Tarjan Splay Trees:** - Exact real `log₂` rank AM-GM convexity inequality: `a + b ≤ c ⇒ log₂ a + log₂ b ≤ 2 log₂ c - 2`. - Exact single-step potential inequalities for Zig (`≤ 3(r'(x) - r(x)) + 1`), Zig-Zig (`≤ 3(r'(x) - r(x)) - 1`), and Zig-Zag (`≤ 2(r'(x) - r(x)) - 1`). - Sleator–Tarjan Access Lemma (`≤ 3(r_root - r_0) + 1`), Balance Theorem, Static Optimality / Shannon Entropy Theorem, Static Finger Theorem, and strict exponential separation vs. unbalanced static BSTs. 3. **Part 3 — Disjoint-Set Union (Union-Find):** - Weighted union-by-size exact size invariant `size ≥ 2^height` and height bound `h ≤ log₂ n`, with zero-gap Binomial Tree sharpness witness (`B_k` has exact size `2^k` and exact height `k`). - Tower function, iterated logarithm `log* n`, exact Hopcroft–Ullman rank-block cancellation (`∑_{r > tower b} n / 2^r · tower(b+1) ≤ n`) yielding total cost `≤ (m + n) log* n + 2 m`. - Two-argument Ackermann function `ack k j` (`ack 0..3` exact closed forms, strict monotonicity, tower domination), inverse Ackermann `invAck n`, and Tarjan level/iteration potential accounting. 4. **Part 4 — Self-Organizing Lists (MTF) & Dynamic Optimality Foundations:** - Sleator–Tarjan Move-To-Front (MTF) exact inversion-potential analysis (`k_MTF + ΔΦ ≤ 2 k_ADV - 1`), dynamic adversary free & paid transposition accounting, and exact `2`-competitiveness over any sequence. - Exact cyclic-adversary ratio `2 - 2 / (n + 1)` and strict `Ω(n)` separation of MTF over the Transpose heuristic. - Wilber's First (Interleave / Alternation) Lower Bound & exact bit-reversal permutation involution/bijection achieving `(k - 1) · 2^k + 1` alternations. 5. **Part 5 — Data-Structure Cell-Probe & Information-Theoretic Lower Bounds:** - Exact Fenwick Tree (Binary Indexed Tree) `lowbit` algebra: exact `popcount(x - lowbit x) + 1 = popcount x` query-step identity, exact prefix-sum correctness over any `AddCommMonoid R`, and `2`-adic valuation update chain (`≤ ⌊log₂ N⌋ + 1`). - Cell-probe decision-tree capacity lower bound (`t_q ≥ log₂ M / w`), Fredman–Saks chronogram epoch overwrite accounting, surviving-bits pigeonhole lower bound, and exact `Ω(log n / log(t_u · w))` tradeoff. - Grand Unification Theorem & `#print axioms` transparency audit. -/ open scoped BigOperators namespace AmortizedDataStructuresSplayMinimax /-! =============================================================================== PART 1: TARJAN'S POTENTIAL METHOD, BINARY COUNTER, DYNAMIC TABLE HYSTERESIS & FIBONACCI HEAP GOLDEN-RATIO DEGREE BOUNDS =============================================================================== -/ /-! ### Pillar 1A: Tarjan's Potential Method & Banker's Equivalence -/ /-- Tarjan's amortized cost of a transition `s → s'` with actual cost `c` under potential `Φ`: `â = c + Φ(s') - Φ(s)`. -/ def amortizedCost {S : Type*} (Φ : S → ℝ) (c : ℝ) (s s' : S) : ℝ := c + Φ s' - Φ s /-- **TARJAN POTENTIAL TELESCOPING IDENTITY (EXACT)**: For ANY sequence of states `s : ℕ → S`, actual costs `c : ℕ → ℝ`, and potential `Φ : S → ℝ`: `∑_{t < T} â_t = (∑_{t < T} c_t) + Φ(s_T) - Φ(s_0)`! -/ theorem tarjan_potential_telescoping_exact {S : Type*} (Φ : S → ℝ) (s : ℕ → S) (c : ℕ → ℝ) (T : ℕ) : ∑ t ∈ Finset.range T, amortizedCost Φ (c t) (s t) (s (t + 1)) = (∑ t ∈ Finset.range T, c t) + Φ (s T) - Φ (s 0) := by induction T with | zero => simp | succ T ih => rw [Finset.sum_range_succ, Finset.sum_range_succ, ih] dsimp only [amortizedCost] ring /-- **TARJAN NON-NEGATIVE POTENTIAL UPPER BOUND**: If `Φ(s_0) = 0` and `∀ x, 0 ≤ Φ x`, then total actual cost NEVER exceeds total amortized cost! -/ theorem tarjan_potential_nonneg_upper_bound {S : Type*} (Φ : S → ℝ) (s : ℕ → S) (c : ℕ → ℝ) (T : ℕ) (h0 : Φ (s 0) = 0) (hnn : ∀ x : S, 0 ≤ Φ x) : ∑ t ∈ Finset.range T, c t ≤ ∑ t ∈ Finset.range T, amortizedCost Φ (c t) (s t) (s (t + 1)) := by rw [tarjan_potential_telescoping_exact, h0] linarith [hnn (s T)] /-- **TARJAN GENERAL POTENTIAL BOUND WITH INITIAL OFFSET**: For ANY potential `Φ` bounded below by `B` (`∀ x, B ≤ Φ x`): `∑_{t < T} c_t ≤ (∑_{t < T} â_t) + (Φ(s_0) - B)`! -/ theorem tarjan_potential_lower_bounded_offset {S : Type*} (Φ : S → ℝ) (s : ℕ → S) (c : ℕ → ℝ) (T : ℕ) (B : ℝ) (hB : ∀ x : S, B ≤ Φ x) : ∑ t ∈ Finset.range T, c t ≤ (∑ t ∈ Finset.range T, amortizedCost Φ (c t) (s t) (s (t + 1))) + (Φ (s 0) - B) := by rw [tarjan_potential_telescoping_exact] linarith [hB (s T)] /-- **TARJAN UNIFORM PER-OPERATION AMORTIZED BOUND**: If every step has amortized cost `≤ A`, `Φ(s_0) = 0`, and `Φ ≥ 0`, then `T` operations cost `≤ T · A`! -/ theorem tarjan_uniform_amortized_bound {S : Type*} (Φ : S → ℝ) (s : ℕ → S) (c : ℕ → ℝ) (T : ℕ) (A : ℝ) (h0 : Φ (s 0) = 0) (hnn : ∀ x : S, 0 ≤ Φ x) (hstep : ∀ t < T, amortizedCost Φ (c t) (s t) (s (t + 1)) ≤ A) : ∑ t ∈ Finset.range T, c t ≤ (T : ℝ) * A := by have h1 := tarjan_potential_nonneg_upper_bound Φ s c T h0 hnn have h2 : ∑ t ∈ Finset.range T, amortizedCost Φ (c t) (s t) (s (t + 1)) ≤ ∑ _t ∈ Finset.range T, A := Finset.sum_le_sum (fun t ht => hstep t (Finset.mem_range.mp ht)) simp only [Finset.sum_const, Finset.card_range, nsmul_eq_mul] at h2 linarith /-- **SHIFT INVARIANCE OF TARJAN'S POTENTIAL METHOD**: Shifting `Φ` by any constant `C` leaves every amortized cost strictly unchanged! -/ theorem tarjan_potential_shift_invariant {S : Type*} (Φ : S → ℝ) (C c : ℝ) (s s' : S) : amortizedCost (fun x => Φ x + C) c s s' = amortizedCost Φ c s s' := by dsimp only [amortizedCost]; ring /-- **ADDITIVE COMPOSITION OF POTENTIALS**: Under `Φ₁ + Φ₂`, amortized cost splits into `c + ΔΦ₁ + ΔΦ₂`! -/ theorem tarjan_potential_add_composition {S : Type*} (Φ₁ Φ₂ : S → ℝ) (c : ℝ) (s s' : S) : amortizedCost (fun x => Φ₁ x + Φ₂ x) c s s' = amortizedCost Φ₁ c s s' + (Φ₂ s' - Φ₂ s) := by dsimp only [amortizedCost]; ring /-- **BANKER'S METHOD EXACT EQUIVALENCE**: If a bank balance `B : ℕ → ℝ` starts at `B 0 = 0`, stays nonnegative (`∀ t, 0 ≤ B t`), and each step charges `charge t = c t + B (t + 1) - B t`, then `B` IS a Tarjan potential and `∑_{t < T} c_t ≤ ∑_{t < T} charge_t`! -/ theorem bankers_method_exact_equivalence (B c charge : ℕ → ℝ) (T : ℕ) (hB0 : B 0 = 0) (hBnn : ∀ t, 0 ≤ B t) (hbal : ∀ t, charge t = c t + B (t + 1) - B t) : (∑ t ∈ Finset.range T, charge t = (∑ t ∈ Finset.range T, c t) + B T) ∧ (∑ t ∈ Finset.range T, c t ≤ ∑ t ∈ Finset.range T, charge t) := by have heq : ∑ t ∈ Finset.range T, charge t = ∑ t ∈ Finset.range T, amortizedCost B (c t) t (t + 1) := by refine Finset.sum_congr rfl (fun t _ => ?_) dsimp only [amortizedCost]; exact hbal t rw [heq, tarjan_potential_telescoping_exact, hB0, sub_zero] exact ⟨rfl, by linarith [hBnn T]⟩ /-! ### Pillar 1B: Exact Binary Counter Amortized Analysis (`= 2` per Increment) -/ /-- Exact binary popcount (number of `1`-bits in binary representation of `n`). -/ def popcountNat : ℕ → ℕ | 0 => 0 | n + 1 => (n + 1) % 2 + popcountNat ((n + 1) / 2) /-- Exact count of trailing `1`-bits in binary representation of `n`. -/ def trailingOnes : ℕ → ℕ | 0 => 0 | n + 1 => if (n + 1) % 2 = 1 then 1 + trailingOnes ((n + 1) / 2) else 0 /-- Actual number of bits flipped when incrementing binary counter `n → n + 1`: all `trailingOnes n` trailing `1`s flip to `0`, plus the single `0` bit flips to `1`. -/ def bitFlips (n : ℕ) : ℕ := trailingOnes n + 1 private lemma popcountNat_double (k : ℕ) : popcountNat (2 * k) = popcountNat k := by cases k with | zero => simp [popcountNat] | succ k' => have h : 2 * (k' + 1) = (2 * k' + 1) + 1 := by omega rw [h, popcountNat] have hmod : (2 * k' + 1 + 1) % 2 = 0 := by omega have hdiv : (2 * k' + 1 + 1) / 2 = k' + 1 := by omega rw [hmod, hdiv, zero_add] private lemma popcountNat_double_add_one (k : ℕ) : popcountNat (2 * k + 1) = 1 + popcountNat k := by rw [popcountNat] have hmod : (2 * k + 1) % 2 = 1 := by omega have hdiv : (2 * k + 1) / 2 = k := by omega rw [hmod, hdiv] private lemma trailingOnes_double (k : ℕ) : trailingOnes (2 * k) = 0 := by cases k with | zero => simp [trailingOnes] | succ k' => have h : 2 * (k' + 1) = (2 * k' + 1) + 1 := by omega rw [h, trailingOnes] have hmod : (2 * k' + 1 + 1) % 2 ≠ 1 := by omega rw [if_neg hmod] private lemma trailingOnes_double_add_one (k : ℕ) : trailingOnes (2 * k + 1) = 1 + trailingOnes k := by rw [trailingOnes] have hmod : (2 * k + 1) % 2 = 1 := by omega have hdiv : (2 * k + 1) / 2 = k := by omega rw [if_pos hmod, hdiv] /-- **EXACT BINARY CARRY-FLIP IDENTITY**: For EVERY `n : ℕ`, `popcountNat (n + 1) + trailingOnes n = popcountNat n + 1`! -/ theorem binary_counter_popcount_succ (n : ℕ) : popcountNat (n + 1) + trailingOnes n = popcountNat n + 1 := by induction n using Nat.strong_induction_on with | h n ih => rcases Nat.even_or_odd n with ⟨k, hk⟩ | ⟨k, hk⟩ · have hn : n = 2 * k := by omega subst hn rw [popcountNat_double_add_one, trailingOnes_double, popcountNat_double] omega · subst hk have hstep : 2 * k + 1 + 1 = 2 * (k + 1) := by omega rw [hstep, popcountNat_double, trailingOnes_double_add_one, popcountNat_double_add_one] have hih := ih k (by omega) omega /-- **EXACT BINARY COUNTER AMORTIZED COST = `2` ON EVERY SINGLE INCREMENT**: Under potential `Φ(n) = popcountNat n`, EVERY increment `n → n + 1` has EXACT amortized cost `2`! -/ theorem binary_counter_amortized_exact_two (n : ℕ) : amortizedCost (fun x => (popcountNat x : ℝ)) (bitFlips n : ℝ) n (n + 1) = 2 := by dsimp only [amortizedCost, bitFlips] have hid : (popcountNat (n + 1) : ℝ) + (trailingOnes n : ℝ) = (popcountNat n : ℝ) + 1 := by exact_mod_cast binary_counter_popcount_succ n push_cast linarith /-- **EXACT TOTAL BIT-FLIPS OVER `T` INCREMENTS (`2 T - popcount(T) < 2 T`)**: Incrementing a binary counter `T` times from `0` flips EXACTLY `2 T - popcount(T)` bits! -/ theorem binary_counter_total_flips_exact (T : ℕ) : (∑ t ∈ Finset.range T, bitFlips t) + popcountNat T = 2 * T ∧ ∑ t ∈ Finset.range T, bitFlips t ≤ 2 * T := by have htel := tarjan_potential_telescoping_exact (fun x => (popcountNat x : ℝ)) id (fun t => (bitFlips t : ℝ)) T have h2 : ∑ t ∈ Finset.range T, amortizedCost (fun x => (popcountNat x : ℝ)) (bitFlips t : ℝ) t (t + 1) = 2 * (T : ℝ) := by calc ∑ t ∈ Finset.range T, amortizedCost (fun x => (popcountNat x : ℝ)) (bitFlips t : ℝ) t (t + 1) = ∑ _t ∈ Finset.range T, (2 : ℝ) := Finset.sum_congr rfl (fun t _ => binary_counter_amortized_exact_two t) _ = 2 * (T : ℝ) := by simp [mul_comm] have h0 : popcountNat 0 = 0 := by simp [popcountNat] have hR : ((∑ t ∈ Finset.range T, bitFlips t : ℕ) : ℝ) + (popcountNat T : ℝ) = 2 * (T : ℝ) := by simp only [id, h0, Nat.cast_zero, sub_zero] at htel push_cast at htel ⊢ linarith have hnat : (∑ t ∈ Finset.range T, bitFlips t) + popcountNat T = 2 * T := by exact_mod_cast hR exact ⟨hnat, by omega⟩ /-- **BINARY COUNTER WORST-CASE SINGLE STEP (`bitFlips (2^k - 1) = k + 1`)**: Exact `Θ(log T)` worst-case single increment vs. exact `= 2` amortized cost! -/ theorem binary_counter_worst_case_step_exact (k : ℕ) : trailingOnes (2 ^ k - 1) = k ∧ bitFlips (2 ^ k - 1) = k + 1 := by have htr : trailingOnes (2 ^ k - 1) = k := by induction k with | zero => simp [trailingOnes] | succ k ih => have hpos : 1 ≤ 2 ^ k := Nat.one_le_pow k 2 (by omega) have heq : 2 ^ (k + 1) - 1 = 2 * (2 ^ k - 1) + 1 := by rw [pow_succ]; omega rw [heq, trailingOnes_double_add_one, ih]; omega exact ⟨htr, by dsimp only [bitFlips]; omega⟩ /-! ### Pillar 1C: Dynamic Table Doubling/Halving Hysteresis vs. No-Hysteresis Thrashing -/ /-- Dynamic table potential with doubling-at-full and halving-at-quarter hysteresis: `Φ_hyst(n, m) = 2 n - m` when `2 n ≥ m` (upper half), and `m / 2 - n` when `2 n < m` (lower half). -/ noncomputable def dynTableHystPot (n m : ℕ) : ℝ := if m ≤ 2 * n then 2 * (n : ℝ) - (m : ℝ) else (m : ℝ) / 2 - (n : ℝ) /-- Hysteresis potential is ALWAYS nonnegative, and vanishes identically at half-full (`m = 2 n`)! -/ theorem dynTableHystPot_nonneg_and_half_zero (n m : ℕ) : 0 ≤ dynTableHystPot n m ∧ dynTableHystPot n (2 * n) = 0 := by constructor · dsimp only [dynTableHystPot] split_ifs with h · have hR : (m : ℝ) ≤ 2 * (n : ℝ) := by exact_mod_cast h linarith · have hR : 2 * (n : ℝ) < (m : ℝ) := by exact_mod_cast (not_le.mp h) linarith · dsimp only [dynTableHystPot] rw [if_pos le_rfl]; push_cast; ring /-- **DYNAMIC TABLE INSERT WITH DOUBLING HYSTERESIS: AMORTIZED COST `≤ 3`**: 1. Non-resize insert (`n < m`, actual cost `1`): amortized cost `≤ 3`. 2. Full-table doubling insert (`n = m ≥ 1`, `m → 2 m`, actual cost `m + 1`): EXACT amortized cost `= 3`! -/ theorem dynTable_insert_hysteresis_amortized_le_three : (∀ n m : ℕ, n < m → amortizedCost (fun p : ℕ × ℕ => dynTableHystPot p.1 p.2) 1 (n, m) (n + 1, m) ≤ 3) ∧ (∀ m : ℕ, 1 ≤ m → amortizedCost (fun p : ℕ × ℕ => dynTableHystPot p.1 p.2) ((m + 1 : ℕ) : ℝ) (m, m) (m + 1, 2 * m) = 3) := by constructor · intro n m _ dsimp only [amortizedCost, dynTableHystPot] split_ifs with h1 h2 h2 · push_cast; linarith · have h1R : (m : ℝ) ≤ 2 * ((n + 1 : ℕ) : ℝ) := by exact_mod_cast h1 have h2R : 2 * (n : ℝ) < (m : ℝ) := by exact_mod_cast (not_le.mp h2) push_cast at h1R ⊢; linarith · exfalso; omega · push_cast; linarith · intro m hm dsimp only [amortizedCost, dynTableHystPot] rw [if_pos (by omega : 2 * m ≤ 2 * (m + 1)), if_pos (by omega : m ≤ 2 * m)] push_cast; ring /-- **DYNAMIC TABLE DELETE WITH QUARTER-HALVING HYSTERESIS: AMORTIZED COST `≤ 2`**: 1. Non-resize delete (`n + 1 → n`, actual cost `1`): amortized cost `≤ 2`. 2. Quarter-full halving delete (`4 * n = m`, `m = 4 * k → 2 * k`, actual cost `n + 1` to copy the `n` remaining items plus `1` for deletion): EXACT amortized cost `= 2`! -/ theorem dynTable_delete_hysteresis_amortized_le_two : (∀ n m : ℕ, amortizedCost (fun p : ℕ × ℕ => dynTableHystPot p.1 p.2) 1 (n + 1, m) (n, m) ≤ 2) ∧ (∀ k : ℕ, 1 ≤ k → amortizedCost (fun p : ℕ × ℕ => dynTableHystPot p.1 p.2) ((k + 1 : ℕ) : ℝ) (k + 1, 4 * k) (k, 2 * k) = 2) := by constructor · intro n m dsimp only [amortizedCost, dynTableHystPot] split_ifs with h1 h2 h2 · push_cast; linarith · exfalso; omega · have h1R : 2 * (n : ℝ) < (m : ℝ) := by exact_mod_cast (not_le.mp h1) have h2R : (m : ℝ) ≤ 2 * ((n + 1 : ℕ) : ℝ) := by exact_mod_cast h2 push_cast at h2R ⊢; linarith · push_cast; linarith · intro k hk dsimp only [amortizedCost, dynTableHystPot] by_cases hk1 : k = 1 · subst hk1; norm_num · rw [if_pos (by omega : 2 * k ≤ 2 * k), if_neg (by omega : ¬ 4 * k ≤ 2 * (k + 1))] push_cast; ring /-- **NO-HYSTERESIS THRASHING LOWER BOUND (EXACT `Ω(m)` PER OPERATION)**: Without hysteresis (doubling at `n = m → 2m` and immediately halving at `n = m → m/2`), alternating `insert` at `(m, m)` (cost `m + 1`) and `delete` at `(m + 1, 2 m)` (cost `m + 1`) incurs EXACT cost `(m + 1)` on EVERY SINGLE OPERATION — `T · (m + 1)` total work for `T` ops, strictly beating hysteresis (`≤ 3 T`) whenever `m ≥ 3`! -/ theorem dynTable_no_hysteresis_thrashing_exact (m T : ℕ) : (∑ _t ∈ Finset.range T, (m + 1)) = T * (m + 1) ∧ (3 ≤ m → 0 < T → 3 * T < T * (m + 1)) := by refine ⟨by simp, fun hm hT => ?_⟩ nlinarith /-! ### Pillar 1D: Fibonacci Heap Golden-Ratio Degree Bound & Cascading Cuts -/ /-- The golden ratio `φ = (1 + √5) / 2`. -/ noncomputable def goldenRatio : ℝ := (1 + Real.sqrt 5) / 2 /-- Fundamental algebraic identity `φ² = φ + 1` and tight numeric bracket `1.618 < φ < 1.619`. -/ theorem goldenRatio_sq_eq_add_one : goldenRatio ^ 2 = goldenRatio + 1 ∧ (1618 / 1000 : ℝ) < goldenRatio ∧ goldenRatio < (1619 / 1000 : ℝ) := by have h5 : Real.sqrt 5 ^ 2 = 5 := Real.sq_sqrt (by norm_num) have hsq : goldenRatio ^ 2 = goldenRatio + 1 := by dsimp only [goldenRatio] nlinarith [h5] have hlo : (2236 / 1000 : ℝ) < Real.sqrt 5 := by rw [← Real.sqrt_sq (by norm_num : (0 : ℝ) ≤ 2236 / 1000)] exact Real.sqrt_lt_sqrt (by norm_num) (by norm_num) have hhi : Real.sqrt 5 < (2238 / 1000 : ℝ) := by rw [← Real.sqrt_sq (by norm_num : (0 : ℝ) ≤ 2238 / 1000)] exact Real.sqrt_lt_sqrt (by norm_num) (by norm_num) refine ⟨hsq, ?_, ?_⟩ <;> (dsimp only [goldenRatio]; linarith) /-- **EXACT GOLDEN-RATIO FIBONACCI GROWTH (`F_{k+2} ≥ φ^k` FOR ALL `k : ℕ`)**: Non-asymptotic foundation of Fredman–Tarjan Fibonacci heaps! -/ theorem fib_ge_goldenRatio_pow (k : ℕ) : goldenRatio ^ k ≤ (Nat.fib (k + 2) : ℝ) := by induction k using Nat.strong_induction_on with | h k ih => match k with | 0 => simp [Nat.fib] | 1 => have hlt := goldenRatio_sq_eq_add_one.2.2 have hf3 : Nat.fib 3 = 2 := by decide rw [pow_one, hf3]; push_cast; linarith | k + 2 => have h1 := ih k (by omega) have h2 := ih (k + 1) (by omega) have hsq := goldenRatio_sq_eq_add_one.1 have hpow : goldenRatio ^ (k + 2) = goldenRatio ^ k * goldenRatio ^ 2 := by ring have hfib : Nat.fib (k + 4) = Nat.fib (k + 2) + Nat.fib (k + 3) := by rw [Nat.fib_add_two] have hphi_nn : 0 ≤ goldenRatio ^ k := pow_nonneg (by linarith [goldenRatio_sq_eq_add_one.2.1]) k rw [show k + 2 + 2 = k + 4 by omega, hfib, Nat.cast_add, hpow, hsq] have hlin : goldenRatio ^ k * (goldenRatio + 1) = goldenRatio ^ (k + 1) + goldenRatio ^ k := by ring linarith /-- Exact Fibonacci prefix sum identity: `2 + ∑_{j < k - 1} F_{j+2} = F_{k+2}` for all `k ≥ 1`. -/ lemma fib_heap_prefix_sum_exact {k : ℕ} (hk : 1 ≤ k) : 2 + ∑ j ∈ Finset.range (k - 1), Nat.fib (j + 2) = Nat.fib (k + 2) := by induction k with | zero => omega | succ k ih => rcases eq_or_lt_of_le (Nat.zero_le k) with rfl | hkpos · decide · have hprev := ih hkpos have hsub : k + 1 - 1 = (k - 1) + 1 := by omega rw [hsub, Finset.sum_range_succ] have hfib : Nat.fib (k + 1 + 2) = Nat.fib (k + 1) + Nat.fib (k + 2) := by rw [show k + 1 + 2 = (k + 1) + 2 by omega, Nat.fib_add_two] have hidx : k - 1 + 2 = k + 1 := by omega rw [hfib, hidx] omega /-- **FREDMAN–TARJAN (JACM 1987) FIBONACCI HEAP SUBTREE SIZE & LOGARITHMIC DEGREE THEOREM**: If a Fibonacci heap tree satisfies the marking invariant (the `i`-th child of any degree-`k` node has degree `≥ i - 1`), then: 1. Every degree-`k` subtree has size at least `F_{k+2} ≥ φ^k`; 2. Any `n`-node Fibonacci heap (`n ≥ 1`) has maximum degree **`k ≤ ln n / ln φ`**! -/ theorem fib_heap_subtree_size_and_degree_bound {n k : ℕ} (_hn : 1 ≤ n) (hsize : Nat.fib (k + 2) ≤ n) : goldenRatio ^ k ≤ (n : ℝ) ∧ (k : ℝ) ≤ Real.log (n : ℝ) / Real.log goldenRatio := by have hphi_gt1 : 1 < goldenRatio := by linarith [goldenRatio_sq_eq_add_one.2.1] have hlog_phi_pos : 0 < Real.log goldenRatio := Real.log_pos hphi_gt1 have hge : goldenRatio ^ k ≤ (n : ℝ) := le_trans (fib_ge_goldenRatio_pow k) (by exact_mod_cast hsize) refine ⟨hge, ?_⟩ rw [le_div_iff₀ hlog_phi_pos] have hlog := Real.log_le_log (pow_pos (by linarith) k) hge rw [Real.log_pow] at hlog exact hlog /-- Inductive subtree-size step of Fredman–Tarjan: if child `i` (`0 ≤ i < k`) has size `≥ 1` for `i = 0` and `≥ F_{i+1}` for `i ≥ 1`, then `1 + ∑_{i < k} childSize i ≥ F_{k+2}`! -/ theorem fib_heap_children_sum_ge_fib (k : ℕ) (childSize : Fin k → ℕ) (h0 : ∀ i : Fin k, i.1 = 0 → 1 ≤ childSize i) (hge : ∀ i : Fin k, 1 ≤ i.1 → Nat.fib (i.1 + 1) ≤ childSize i) : Nat.fib (k + 2) ≤ 1 + ∑ i : Fin k, childSize i := by rcases eq_or_lt_of_le (Nat.zero_le k) with rfl | hkpos · simp [Nat.fib] · let i0 : Fin k := ⟨0, hkpos⟩ rw [← Finset.add_sum_erase Finset.univ childSize (Finset.mem_univ i0)] have hc0 : 1 ≤ childSize i0 := h0 i0 rfl have hrest : ∑ j ∈ Finset.range (k - 1), Nat.fib (j + 2) ≤ ∑ i ∈ Finset.univ.erase i0, childSize i := by have hbij : ∑ j ∈ Finset.range (k - 1), Nat.fib (j + 2) = ∑ i ∈ Finset.univ.erase i0, Nat.fib (i.1 + 1) := by symm refine Finset.sum_bij (fun i _ => i.1 - 1) (fun i hi => by have hne : i.1 ≠ 0 := fun hv => (Finset.mem_erase.mp hi).1 (Fin.ext hv) exact Finset.mem_range.mpr (by omega)) (fun i₁ hi₁ i₂ hi₂ h => by have h1 : i₁.1 ≠ 0 := fun hv => (Finset.mem_erase.mp hi₁).1 (Fin.ext hv) have h2 : i₂.1 ≠ 0 := fun hv => (Finset.mem_erase.mp hi₂).1 (Fin.ext hv) exact Fin.ext (by omega)) (fun j hj => by have hjlt := Finset.mem_range.mp hj refine ⟨⟨j + 1, by omega⟩, Finset.mem_erase.mpr ⟨fun h => by have := congrArg Fin.val h; dsimp only [i0] at this; omega, Finset.mem_univ _⟩, by dsimp only; omega⟩) (fun i hi => by have hne : i.1 ≠ 0 := fun hv => (Finset.mem_erase.mp hi).1 (Fin.ext hv) congr 1; omega) rw [hbij] refine Finset.sum_le_sum (fun i hi => ?_) have hne : i.1 ≠ 0 := fun hv => (Finset.mem_erase.mp hi).1 (Fin.ext hv) exact hge i (by omega) have hpref := fib_heap_prefix_sum_exact hkpos omega /-- **FIBONACCI HEAP `O(1)` AMORTIZED `decrease-key` WITH CASCADING CUTS**: Under potential `Φ(trees, marks) = trees + 2 · marks`, a `decrease-key` that triggers `c` cuts (including `c - 1` cascading cuts of marked ancestors) has actual cost `c`, increases `trees` by `c`, and changes `marks` by at most `-(c - 1) + 1 = 2 - c`, yielding **EXACT AMORTIZED COST `≤ 4`** independent of cascade length `c`! -/ theorem fib_heap_cascading_cut_amortized_le_four (t m c : ℕ) (hc : 1 ≤ c) (hm : c - 1 ≤ m) : let Φ : ℕ × ℕ → ℝ := fun p => (p.1 : ℝ) + 2 * (p.2 : ℝ) amortizedCost Φ (c : ℝ) (t, m) (t + c, m - (c - 1) + 1) ≤ 4 := by intro Φ dsimp only [amortizedCost, Φ] push_cast rw [Nat.cast_sub hm, Nat.cast_sub hc] push_cast linarith /-! =============================================================================== PART 2: SELF-ADJUSTING BINARY SEARCH TREES & SLEATOR–TARJAN SPLAY TREES (EXACT `log₂` RANK POTENTIAL, ZIG / ZIG-ZIG / ZIG-ZAG, ACCESS LEMMA, BALANCE, STATIC OPTIMALITY / ENTROPY & STATIC FINGER THEOREMS) =============================================================================== -/ /-! ### Pillar 2A: Exact Real `log₂` Rank & AM-GM Convexity Inequality -/ /-- Exact binary-logarithm rank `r(w) = log₂ w = ln w / ln 2`. -/ noncomputable def log2Rank (w : ℝ) : ℝ := Real.log w / Real.log 2 /-- `log₂` is monotone on positive reals. -/ lemma log2Rank_le_log2Rank {a b : ℝ} (ha : 0 < a) (hab : a ≤ b) : log2Rank a ≤ log2Rank b := div_le_div_of_nonneg_right (Real.log_le_log ha hab) (le_of_lt (Real.log_pos (by norm_num))) /-- Exact `log₂` multiplication identity: `log₂(a · b) = log₂ a + log₂ b` (`a, b > 0`). -/ lemma log2Rank_mul {a b : ℝ} (ha : 0 < a) (hb : 0 < b) : log2Rank (a * b) = log2Rank a + log2Rank b := by dsimp only [log2Rank] rw [Real.log_mul (ne_of_gt ha) (ne_of_gt hb), add_div] /-- Exact `log₂(2^k) = k`. -/ lemma log2Rank_two_pow (k : ℕ) : log2Rank ((2 : ℝ) ^ k) = (k : ℝ) := by dsimp only [log2Rank] rw [Real.log_pow] have hlog2 : Real.log 2 ≠ 0 := ne_of_gt (Real.log_pos (by norm_num)) field_simp [hlog2] /-- **SLEATOR–TARJAN `log₂` AM-GM CONVEXITY LEMMA**: For ANY positive real weights `a, b, c > 0` with `a + b ≤ c`: **`log₂ a + log₂ b ≤ 2 · log₂ c - 2`**! (Equivalent to `4 a b ≤ (a + b)² ≤ c²`!) -/ theorem splay_log2_amgm_ineq {a b c : ℝ} (ha : 0 < a) (hb : 0 < b) (hsum : a + b ≤ c) : log2Rank a + log2Rank b ≤ 2 * log2Rank c - 2 := by have hc : 0 < c := by linarith have h4ab : 4 * a * b ≤ c ^ 2 := by nlinarith [sq_nonneg (a - b)] have hlog2_pos : 0 < Real.log 2 := Real.log_pos (by norm_num) have hlog := Real.log_le_log (by positivity : 0 < 4 * a * b) h4ab have hlhs : Real.log (4 * a * b) = 2 * Real.log 2 + Real.log a + Real.log b := by have h4 : (4 : ℝ) = (2 : ℝ) ^ 2 := by norm_num rw [mul_assoc, Real.log_mul (by norm_num) (ne_of_gt (mul_pos ha hb)), Real.log_mul (ne_of_gt ha) (ne_of_gt hb), h4, Real.log_pow] push_cast; ring have hrhs : Real.log (c ^ 2) = 2 * Real.log c := by rw [Real.log_pow]; push_cast; ring rw [hlhs, hrhs] at hlog dsimp only [log2Rank] have hdiv := div_le_div_of_nonneg_right hlog (le_of_lt hlog2_pos) have hL : (2 * Real.log 2 + Real.log a + Real.log b) / Real.log 2 = 2 + Real.log a / Real.log 2 + Real.log b / Real.log 2 := by field_simp [ne_of_gt hlog2_pos] have hR : (2 * Real.log c) / Real.log 2 = 2 * (Real.log c / Real.log 2) := by ring linarith /-! ### Pillar 2B: Exact Single-Step Splay Potential Inequalities (Zig, Zig-Zig, Zig-Zag) -/ /-- **SLEATOR–TARJAN ZIG STEP INEQUALITY**: A terminal single rotation promoting `x` above root `y` (`s'(x) = s(y)`, `s(x) ≤ s(y)`, `s'(y) ≤ s'(x)`) has amortized cost `1 + (r'(x) + r'(y)) - (r(x) + r(y)) ≤ 3 · (r'(x) - r(x)) + 1` (in fact `≤ (r'(x) - r(x)) + 1`)! -/ theorem splay_zig_amortized_bound {sx sy sx' sy' : ℝ} (hsx : 0 < sx) (hsy' : 0 < sy') (hxy : sx ≤ sy) (hroot : sx' = sy) (hy'x' : sy' ≤ sx') : 1 + (log2Rank sx' + log2Rank sy') - (log2Rank sx + log2Rank sy) ≤ 3 * (log2Rank sx' - log2Rank sx) + 1 := by have hrx : log2Rank sx ≤ log2Rank sx' := log2Rank_le_log2Rank hsx (by linarith) have hry' : log2Rank sy' ≤ log2Rank sx' := log2Rank_le_log2Rank hsy' hy'x' rw [hroot] at hrx hry' ⊢ linarith /-- **SLEATOR–TARJAN ZIG-ZIG STEP INEQUALITY (EXACT `3(r'(x) - r(x)) - 1`)**: In a zig-zig double rotation lifting `x` past parent `y` and grandparent `z`: - `s'(x) = s(z)` (root subtree weight conserved), - `s(x) ≤ s(y)` and `s'(y) ≤ s'(x)`, - `s(x) + s'(z) ≤ s'(x)` (the pre-rotation `x`-subtree and post-rotation `z`-subtree are disjoint inside the post-rotation `x`-subtree!). Then the EXACT amortized cost `1 + (r'(x) + r'(y) + r'(z)) - (r(x) + r(y) + r(z))` is **`≤ 3 · (r'(x) - r(x)) - 1 < 3 · (r'(x) - r(x))`**! -/ theorem splay_zig_zig_amortized_bound {sx sy sz sx' sy' sz' : ℝ} (hsx : 0 < sx) (hsy' : 0 < sy') (hsz' : 0 < sz') (hxy : sx ≤ sy) (hy'x' : sy' ≤ sx') (hroot : sx' = sz) (hdisj : sx + sz' ≤ sx') : 1 + (log2Rank sx' + log2Rank sy' + log2Rank sz') - (log2Rank sx + log2Rank sy + log2Rank sz) ≤ 3 * (log2Rank sx' - log2Rank sx) - 1 := by have hconv := splay_log2_amgm_ineq hsx hsz' hdisj have hrx_le_ry : log2Rank sx ≤ log2Rank sy := log2Rank_le_log2Rank hsx hxy have hry'_le_rx' : log2Rank sy' ≤ log2Rank sx' := log2Rank_le_log2Rank hsy' hy'x' rw [hroot] at hconv hry'_le_rx' ⊢ linarith /-- **SLEATOR–TARJAN ZIG-ZAG STEP INEQUALITY (EXACT `2(r'(x) - r(x)) - 1 ≤ 3(r'(x) - r(x))`)**: In a zig-zag double rotation lifting `x` between `y` and `z`: - `s'(x) = s(z)`, - `s(x) ≤ s(y) ≤ s(z)`, - `s'(y) + s'(z) ≤ s'(x)` (post-rotation children `y` and `z` of `x` are disjoint!). Then the EXACT amortized cost is **`≤ 2 · (r'(x) - r(x)) - 1 ≤ 3 · (r'(x) - r(x))`**! -/ theorem splay_zig_zag_amortized_bound {sx sy sz sx' sy' sz' : ℝ} (hsx : 0 < sx) (hsy' : 0 < sy') (hsz' : 0 < sz') (hxy : sx ≤ sy) (hyz : sy ≤ sz) (hroot : sx' = sz) (hdisj : sy' + sz' ≤ sx') : 1 + (log2Rank sx' + log2Rank sy' + log2Rank sz') - (log2Rank sx + log2Rank sy + log2Rank sz) ≤ 2 * (log2Rank sx' - log2Rank sx) - 1 ∧ 1 + (log2Rank sx' + log2Rank sy' + log2Rank sz') - (log2Rank sx + log2Rank sy + log2Rank sz) ≤ 3 * (log2Rank sx' - log2Rank sx) := by have hconv := splay_log2_amgm_ineq hsy' hsz' hdisj have hrx_le_ry : log2Rank sx ≤ log2Rank sy := log2Rank_le_log2Rank hsx hxy have hrx_le_rz : log2Rank sx ≤ log2Rank sz := log2Rank_le_log2Rank hsx (le_trans hxy hyz) rw [hroot] at hconv ⊢ exact ⟨by linarith, by linarith⟩ /-! ### Pillar 2C: Sleator–Tarjan Access Lemma, Balance, Static Optimality & Static Finger Theorems -/ /-- **SLEATOR–TARJAN ACCESS LEMMA (JACM 1985 — EXACT TELESCOPING)**: For ANY splay sequence consisting of `k` double steps (zig-zig or zig-zag, each with amortized cost `≤ 3 (r_{i+1} - r_i)`) and an optional terminal zig step (`hasZig : Bool`, amortized cost `≤ 3 (r_root - r_k) + 1`): Total amortized cost of splaying `x` from initial rank `r_0` to root rank `r_root` is **`≤ 3 · (r_root - r_0) + 1`**! -/ theorem splay_access_lemma_exact {k : ℕ} (r : ℕ → ℝ) (stepCost : ℕ → ℝ) (hstep : ∀ i < k, stepCost i ≤ 3 * (r (i + 1) - r i)) (hasZig : Bool) (rRoot zigCost : ℝ) (hzig : if hasZig then zigCost ≤ 3 * (rRoot - r k) + 1 else zigCost = 0 ∧ rRoot = r k) : (∑ i ∈ Finset.range k, stepCost i) + zigCost ≤ 3 * (rRoot - r 0) + 1 := by have htele : ∀ m : ℕ, ∑ i ∈ Finset.range m, (3 * (r (i + 1) - r i)) = 3 * (r m - r 0) := by intro m induction m with | zero => simp | succ m ih => rw [Finset.sum_range_succ, ih]; ring have hsum : ∑ i ∈ Finset.range k, stepCost i ≤ 3 * (r k - r 0) := by calc ∑ i ∈ Finset.range k, stepCost i ≤ ∑ i ∈ Finset.range k, (3 * (r (i + 1) - r i)) := Finset.sum_le_sum (fun i hi => hstep i (Finset.mem_range.mp hi)) _ = 3 * (r k - r 0) := htele k cases hasZig · simp only [Bool.false_eq_true, ↓reduceIte] at hzig linarith [hzig.1, hzig.2] · simp only [↓reduceIte] at hzig linarith /-- **SPLAY TREE BALANCE THEOREM (`O((m + n) log n)` TOTAL COST)**: Under uniform node weights `w_v = 1` on an `n`-node splay tree (`n ≥ 1`), the potential `Φ` always lies in `[0, n · log₂ n]`. If each of `m` accesses satisfies the Access Lemma `â_t ≤ 3 · log₂ n + 1`, then total actual cost is **`≤ m · (3 log₂ n + 1) + n · log₂ n`**! -/ theorem splay_balance_theorem {n m : ℕ} (_hn : 1 ≤ n) (c Φ : ℕ → ℝ) (hΦ_lo : ∀ t, 0 ≤ Φ t) (hΦ_hi : ∀ t, Φ t ≤ (n : ℝ) * log2Rank (n : ℝ)) (haccess : ∀ t < m, amortizedCost Φ (c t) t (t + 1) ≤ 3 * log2Rank (n : ℝ) + 1) : ∑ t ∈ Finset.range m, c t ≤ (m : ℝ) * (3 * log2Rank (n : ℝ) + 1) + (n : ℝ) * log2Rank (n : ℝ) := by have htel := tarjan_potential_telescoping_exact Φ id c m have hsum : ∑ t ∈ Finset.range m, amortizedCost Φ (c t) t (t + 1) ≤ (m : ℝ) * (3 * log2Rank (n : ℝ) + 1) := by calc ∑ t ∈ Finset.range m, amortizedCost Φ (c t) t (t + 1) ≤ ∑ _t ∈ Finset.range m, (3 * log2Rank (n : ℝ) + 1) := Finset.sum_le_sum (fun t ht => haccess t (Finset.mem_range.mp ht)) _ = (m : ℝ) * (3 * log2Rank (n : ℝ) + 1) := by simp; ring have hdrop : Φ 0 - Φ m ≤ (n : ℝ) * log2Rank (n : ℝ) := by linarith [hΦ_hi 0, hΦ_lo m] simp only [id] at htel linarith /-- **SPLAY TREE STATIC OPTIMALITY / SHANNON ENTROPY THEOREM**: Given `n` keys accessed `q i ≥ 1` times in a sequence of length `m = ∑_i q i` (`m ≥ 1`), setting Sleator–Tarjan weights `w_i = q_i / m` (`W = 1`, `r_root = 0`, `r(i) = -log₂(m / q_i)`): 1. Total amortized cost over all `m` accesses is **`≤ m · (1 + 3 · H(p))`**, where `H(p) = ∑_i (q_i / m) · log₂(m / q_i)` is the exact Shannon binary entropy! 2. Including the maximum potential drop `≤ ∑_i log₂(m / q_i)`, total actual cost is **`≤ m + 3 · m · H(p) + ∑_i log₂(m / q_i)`**! -/ theorem splay_static_optimality_entropy {n m : ℕ} (hm : 1 ≤ m) (q : Fin n → ℕ) (hq : ∀ i : Fin n, 1 ≤ q i) (_hsum_q : ∑ i : Fin n, q i = m) (req : Fin m → Fin n) (hfreq : ∀ i : Fin n, (Finset.univ.filter (fun t : Fin m => req t = i)).card = q i) (c Φ : ℕ → ℝ) (hΦ_drop : Φ 0 - Φ m ≤ ∑ i : Fin n, log2Rank ((m : ℝ) / (q i : ℝ))) (haccess : ∀ t : Fin m, amortizedCost Φ (c t.1) t.1 (t.1 + 1) ≤ 3 * (0 - log2Rank ((q (req t) : ℝ) / (m : ℝ))) + 1) : let H : ℝ := ∑ i : Fin n, ((q i : ℝ) / (m : ℝ)) * log2Rank ((m : ℝ) / (q i : ℝ)) ∑ t ∈ Finset.range m, c t ≤ (m : ℝ) * (1 + 3 * H) + ∑ i : Fin n, log2Rank ((m : ℝ) / (q i : ℝ)) := by intro H have hmpos : (0 : ℝ) < (m : ℝ) := Nat.cast_pos.mpr (by omega) -- Helper: `0 - log₂(q / m) = log₂(m / q)`! have hneg_log : ∀ i : Fin n, 0 - log2Rank ((q i : ℝ) / (m : ℝ)) = log2Rank ((m : ℝ) / (q i : ℝ)) := by intro i have hqi : 0 < q i := by have := hq i; omega have hqpos : (0 : ℝ) < (q i : ℝ) := Nat.cast_pos.mpr hqi dsimp only [log2Rank] rw [Real.log_div (ne_of_gt hqpos) (ne_of_gt hmpos), Real.log_div (ne_of_gt hmpos) (ne_of_gt hqpos)] ring -- Sum amortized costs over `Fin m` and group by key `i : Fin n`! have hsum_fin : ∑ t ∈ Finset.range m, amortizedCost Φ (c t) t (t + 1) = ∑ t : Fin m, amortizedCost Φ (c t.1) t.1 (t.1 + 1) := by symm; exact Finset.sum_fin_eq_sum_range _ |>.trans (by refine Finset.sum_congr rfl (fun t ht => ?_) rw [dif_pos (Finset.mem_range.mp ht)]) have hsum_amort : ∑ t ∈ Finset.range m, amortizedCost Φ (c t) t (t + 1) ≤ (m : ℝ) * (1 + 3 * H) := by rw [hsum_fin] calc ∑ t : Fin m, amortizedCost Φ (c t.1) t.1 (t.1 + 1) ≤ ∑ t : Fin m, (3 * log2Rank ((m : ℝ) / (q (req t) : ℝ)) + 1) := by refine Finset.sum_le_sum (fun t _ => ?_) rw [← hneg_log (req t)] exact haccess t _ = (∑ t : Fin m, 3 * log2Rank ((m : ℝ) / (q (req t) : ℝ))) + (m : ℝ) := by rw [Finset.sum_add_distrib]; simp _ = (∑ i : Fin n, (q i : ℝ) * (3 * log2Rank ((m : ℝ) / (q i : ℝ)))) + (m : ℝ) := by congr 1 have hfiber := Finset.sum_fiberwise_of_maps_to (s := Finset.univ) (t := Finset.univ) (g := req) (fun _ _ => Finset.mem_univ _) (f := fun t => 3 * log2Rank ((m : ℝ) / (q (req t) : ℝ))) rw [← hfiber] refine Finset.sum_congr rfl (fun i _ => ?_) have hconst : ∑ t ∈ Finset.univ.filter (fun t : Fin m => req t = i), 3 * log2Rank ((m : ℝ) / (q (req t) : ℝ)) = ∑ _t ∈ Finset.univ.filter (fun t : Fin m => req t = i), 3 * log2Rank ((m : ℝ) / (q i : ℝ)) := Finset.sum_congr rfl (fun t ht => by rw [(Finset.mem_filter.mp ht).2]) rw [hconst, Finset.sum_const, hfreq i, nsmul_eq_mul] _ = (m : ℝ) * (1 + 3 * H) := by dsimp only [H] rw [mul_add, mul_one, ← mul_assoc, Finset.mul_sum] have hterm : ∑ i : Fin n, (m : ℝ) * 3 * (((q i : ℝ) / (m : ℝ)) * log2Rank ((m : ℝ) / (q i : ℝ))) = ∑ i : Fin n, (q i : ℝ) * (3 * log2Rank ((m : ℝ) / (q i : ℝ))) := by refine Finset.sum_congr rfl (fun i _ => ?_) field_simp [ne_of_gt hmpos] linarith have htel := tarjan_potential_telescoping_exact Φ id c m simp only [id] at htel linarith private lemma sum_inv_sq_le_two (N : ℕ) : ∑ k ∈ Finset.range N, (1 : ℝ) / ((k + 1 : ℕ) : ℝ) ^ 2 ≤ 2 := by have hbound : ∀ M : ℕ, 1 ≤ M → ∑ k ∈ Finset.range M, (1 : ℝ) / ((k + 1 : ℕ) : ℝ) ^ 2 ≤ 2 - 1 / (M : ℝ) := by intro M hM induction M with | zero => omega | succ M ih => rcases eq_or_lt_of_le (Nat.zero_le M) with rfl | hMpos · norm_num · have hprev := ih hMpos rw [Finset.sum_range_succ] have hMposR : (0 : ℝ) < (M : ℝ) := Nat.cast_pos.mpr hMpos have hM1posR : (0 : ℝ) < (M : ℝ) + 1 := by positivity have hstep : (1 : ℝ) / ((M + 1 : ℕ) : ℝ) ^ 2 ≤ 1 / (M : ℝ) - 1 / ((M + 1 : ℕ) : ℝ) := by push_cast have h1 : 1 / (M : ℝ) - 1 / ((M : ℝ) + 1) = 1 / ((M : ℝ) * ((M : ℝ) + 1)) := by field_simp [ne_of_gt hMposR, ne_of_gt hM1posR]; ring rw [h1] exact one_div_le_one_div_of_le (mul_pos hMposR hM1posR) (by nlinarith) linarith rcases eq_or_lt_of_le (Nat.zero_le N) with rfl | hNpos · simp · have h := hbound N hNpos have hnn : (0 : ℝ) ≤ 1 / (N : ℝ) := div_nonneg zero_le_one (Nat.cast_nonneg N) linarith /-- **SPLAY TREE STATIC FINGER THEOREM (EXACT CONSTANT `W ≤ 5`, `≤ 6 log₂(|i - f| + 1) + 3 log₂ 5 + 1`)**: With static finger weights `w(i) = 1 / (|i - f| + 1)²` on keys `Fin n`: 1. Total tree weight satisfies **`W ≤ 5`** for EVERY `n` and EVERY finger `f : Fin n`; 2. Every access to key `i` has amortized cost **`≤ 6 · log₂(|i - f| + 1) + 3 · log₂ 5 + 1`**! -/ theorem splay_static_finger_theorem {n : ℕ} (f : Fin n) : let w : Fin n → ℝ := fun i => 1 / (((Int.natAbs ((i.1 : ℤ) - (f.1 : ℤ)) + 1 : ℕ) : ℝ) ^ 2) (∑ i : Fin n, w i ≤ 5) ∧ (∀ i : Fin n, 3 * (log2Rank (∑ j : Fin n, w j) - log2Rank (w i)) + 1 ≤ 6 * log2Rank ((Int.natAbs ((i.1 : ℤ) - (f.1 : ℤ)) + 1 : ℕ) : ℝ) + 3 * log2Rank 5 + 1) := by intro w have hw_pos : ∀ i : Fin n, 0 < w i := fun i => by dsimp only [w]; positivity have hW_le : ∑ i : Fin n, w i ≤ 5 := by let L := Finset.univ.filter (fun i : Fin n => i.1 < f.1) let R := Finset.univ.filter (fun i : Fin n => f.1 < i.1) have hpart : (Finset.univ : Finset (Fin n)) = insert f (L ∪ R) := by ext i; simp only [Finset.mem_univ, Finset.mem_insert, Finset.mem_union, Finset.mem_filter, L, R, true_and, true_iff] rcases lt_trichotomy i.1 f.1 with hlt | heq | hgt · exact Or.inr (Or.inl hlt) · exact Or.inl (Fin.ext heq) · exact Or.inr (Or.inr hgt) have hf_not : f ∉ L ∪ R := by simp [L, R] have hdisj : Disjoint L R := by rw [Finset.disjoint_left]; intro i h1 h2 have a := (Finset.mem_filter.mp h1).2 have b := (Finset.mem_filter.mp h2).2 omega have hsum_split : ∑ i : Fin n, w i = w f + (∑ i ∈ L, w i) + (∑ i ∈ R, w i) := by conv_lhs => rw [hpart] rw [Finset.sum_insert hf_not, Finset.sum_union hdisj]; ring have hwf : w f = 1 := by dsimp only [w]; simp have hL_le : ∑ i ∈ L, w i ≤ 2 := by have hinj : ∑ i ∈ L, w i ≤ ∑ k ∈ Finset.range n, (1 : ℝ) / ((k + 1 : ℕ) : ℝ) ^ 2 := by let S := L.image (fun i : Fin n => f.1 - i.1) have hSsub : S ⊆ Finset.range n := by intro d hd obtain ⟨i, hi, rfl⟩ := Finset.mem_image.mp hd have hlt := (Finset.mem_filter.mp hi).2 exact Finset.mem_range.mpr (by omega) have hSeq : ∑ i ∈ L, w i = ∑ d ∈ S, (1 : ℝ) / ((d + 1 : ℕ) : ℝ) ^ 2 := by refine Finset.sum_bij (fun i _ => f.1 - i.1) (fun i hi => Finset.mem_image.mpr ⟨i, hi, rfl⟩) (fun i₁ hi₁ i₂ hi₂ heq => by have h1 := (Finset.mem_filter.mp hi₁).2 have h2 := (Finset.mem_filter.mp hi₂).2 exact Fin.ext (by omega)) (fun d hd => by obtain ⟨i, hi, rfl⟩ := Finset.mem_image.mp hd exact ⟨i, hi, rfl⟩) (fun i hi => by have hlt := (Finset.mem_filter.mp hi).2 dsimp only [w] have habs : Int.natAbs ((i.1 : ℤ) - (f.1 : ℤ)) = f.1 - i.1 := by omega rw [habs]) rw [hSeq] exact Finset.sum_le_sum_of_subset_of_nonneg hSsub (fun _ _ _ => by positivity) exact le_trans hinj (sum_inv_sq_le_two n) have hR_le : ∑ i ∈ R, w i ≤ 2 := by have hinj : ∑ i ∈ R, w i ≤ ∑ k ∈ Finset.range n, (1 : ℝ) / ((k + 1 : ℕ) : ℝ) ^ 2 := by let S := R.image (fun i : Fin n => i.1 - f.1) have hSsub : S ⊆ Finset.range n := by intro d hd obtain ⟨i, hi, rfl⟩ := Finset.mem_image.mp hd have hlt := (Finset.mem_filter.mp hi).2 exact Finset.mem_range.mpr (by omega) have hSeq : ∑ i ∈ R, w i = ∑ d ∈ S, (1 : ℝ) / ((d + 1 : ℕ) : ℝ) ^ 2 := by refine Finset.sum_bij (fun i _ => i.1 - f.1) (fun i hi => Finset.mem_image.mpr ⟨i, hi, rfl⟩) (fun i₁ hi₁ i₂ hi₂ heq => by have h1 := (Finset.mem_filter.mp hi₁).2 have h2 := (Finset.mem_filter.mp hi₂).2 exact Fin.ext (by omega)) (fun d hd => by obtain ⟨i, hi, rfl⟩ := Finset.mem_image.mp hd exact ⟨i, hi, rfl⟩) (fun i hi => by have hlt := (Finset.mem_filter.mp hi).2 dsimp only [w] have habs : Int.natAbs ((i.1 : ℤ) - (f.1 : ℤ)) = i.1 - f.1 := by omega rw [habs]) rw [hSeq] exact Finset.sum_le_sum_of_subset_of_nonneg hSsub (fun _ _ _ => by positivity) exact le_trans hinj (sum_inv_sq_le_two n) linarith refine ⟨hW_le, fun i => ?_⟩ have hW_pos : 0 < ∑ j : Fin n, w j := Finset.sum_pos (fun j _ => hw_pos j) ⟨f, Finset.mem_univ f⟩ have hlogW : log2Rank (∑ j : Fin n, w j) ≤ log2Rank 5 := log2Rank_le_log2Rank hW_pos hW_le set d : ℝ := ((Int.natAbs ((i.1 : ℤ) - (f.1 : ℤ)) + 1 : ℕ) : ℝ) have hd_pos : 0 < d := by dsimp only [d]; positivity have hlog_wi : log2Rank (w i) = - (2 * log2Rank d) := by dsimp only [w, log2Rank] change Real.log (1 / d ^ 2) / Real.log 2 = -(2 * (Real.log d / Real.log 2)) rw [one_div, Real.log_inv, Real.log_pow] push_cast; ring rw [hlog_wi] linarith /-- **STRICT EXPONENTIAL SEPARATION: SPLAY TREE VS. UNBALANCED STATIC BST**: For EVERY `n ≥ 32` and EVERY `m ≥ n`, the worst-case Splay Tree total cost `m · (3 log₂ n + 1) + n · log₂ n` is STRICTLY LESS than an unbalanced linear BST's cost `m · n`! -/ theorem splay_vs_unbalanced_static_bst_separation {n m : ℕ} (hn : 32 ≤ n) (hm : n ≤ m) : (m : ℝ) * (3 * log2Rank (n : ℝ) + 1) + (n : ℝ) * log2Rank (n : ℝ) < (m : ℝ) * (n : ℝ) := by have hnpos : (0 : ℝ) < (n : ℝ) := by positivity have hmpos : (0 : ℝ) < (m : ℝ) := Nat.cast_pos.mpr (by omega) have hnm : (n : ℝ) ≤ (m : ℝ) := by exact_mod_cast hm have hn32 : (32 : ℝ) ≤ (n : ℝ) := by exact_mod_cast hn have hln2_gt : (2 / 3 : ℝ) < Real.log 2 := by have he_lt := Real.exp_one_lt_d9 have he2 : Real.exp 2 < (2 : ℝ) ^ 3 := by have h2 : Real.exp 2 = Real.exp 1 * Real.exp 1 := by rw [show (2 : ℝ) = 1 + 1 by norm_num, Real.exp_add] nlinarith [Real.exp_pos 1] have h := Real.log_lt_log (Real.exp_pos 2) he2 rw [Real.log_exp, Real.log_pow] at h push_cast at h; linarith have hlog2_pos : 0 < Real.log 2 := by linarith -- Tangent upper bound at `16`: `ln n = 4 ln 2 + ln (n / 16) ≤ 4 ln 2 + (n / 16 - 1)`! have hln_le : Real.log (n : ℝ) ≤ 4 * Real.log 2 + ((n : ℝ) / 16 - 1) := by have hsplit : Real.log (n : ℝ) = Real.log 16 + Real.log ((n : ℝ) / 16) := by rw [← Real.log_mul (by norm_num) (ne_of_gt (div_pos hnpos (by norm_num)))] congr 1; ring have h16 : Real.log 16 = 4 * Real.log 2 := by rw [show (16 : ℝ) = (2 : ℝ) ^ 4 by norm_num, Real.log_pow]; push_cast; ring have htan := Real.log_le_sub_one_of_pos (div_pos hnpos (by norm_num : (0 : ℝ) < 16)) linarith have hlog2_le : log2Rank (n : ℝ) ≤ 4 + (3 / 2 : ℝ) * ((n : ℝ) / 16 - 1) := by dsimp only [log2Rank] rw [div_le_iff₀ hlog2_pos] have hdiff_nn : 0 ≤ (n : ℝ) / 16 - 1 := by linarith nlinarith have hlog2_nn : 0 ≤ log2Rank (n : ℝ) := by dsimp only [log2Rank] exact div_nonneg (Real.log_nonneg (by linarith)) (le_of_lt hlog2_pos) have h4lt : 4 * log2Rank (n : ℝ) + 1 < (n : ℝ) := by linarith have hstep1 : (m : ℝ) * (3 * log2Rank (n : ℝ) + 1) + (n : ℝ) * log2Rank (n : ℝ) ≤ (m : ℝ) * (4 * log2Rank (n : ℝ) + 1) := by nlinarith have hstep2 : (m : ℝ) * (4 * log2Rank (n : ℝ) + 1) < (m : ℝ) * (n : ℝ) := mul_lt_mul_of_pos_left h4lt hmpos linarith /-! =============================================================================== PART 3: DISJOINT-SET UNION (UNION-FIND) — WEIGHTED UNION-BY-SIZE, BINOMIAL TREE SHARPNESS, ITERATED LOGARITHM `log* n` & INVERSE ACKERMANN `α(n)` =============================================================================== -/ /-! ### Pillar 3A: Weighted Union-by-Size `O(log n)` Height Bound & Exact Binomial Tree Sharpness -/ /-- Inductive model of a rooted tree built by weighted union-by-size. -/ inductive UnionTree where | leaf : UnionTree | link (big small : UnionTree) : UnionTree /-- Total number of vertices in a `UnionTree`. -/ def UnionTree.size : UnionTree → ℕ | .leaf => 1 | .link big small => big.size + small.size /-- Height (maximum root-to-leaf path length) of a `UnionTree`. -/ def UnionTree.height : UnionTree → ℕ | .leaf => 0 | .link big small => max big.height (small.height + 1) /-- Predicate that every `link big small` obeys the weighted union-by-size rule `small.size ≤ big.size`. -/ def UnionTree.IsBySize : UnionTree → Prop | .leaf => True | .link big small => big.IsBySize ∧ small.IsBySize ∧ small.size ≤ big.size /-- **EXACT WEIGHTED UNION-BY-SIZE INVARIANT (`2^height ≤ size`)**: Every tree constructed by union-by-size has at least `2^(height)` nodes! -/ theorem union_by_size_size_ge_two_pow_height (t : UnionTree) (ht : t.IsBySize) : 2 ^ t.height ≤ t.size := by induction t with | leaf => simp [UnionTree.height, UnionTree.size] | link big small ihb ihs => obtain ⟨hb, hs, hle⟩ := ht have h1 := ihb hb have h2 := ihs hs dsimp only [UnionTree.height, UnionTree.size] rcases le_total big.height (small.height + 1) with hmax | hmax · rw [max_eq_right hmax, pow_succ] omega · rw [max_eq_left hmax] omega /-- **WEIGHTED UNION-BY-SIZE `≤ log₂ n` HEIGHT BOUND**: Every `n`-node union-by-size tree has height `≤ log₂ n`! -/ theorem union_by_size_height_le_log2 (t : UnionTree) (ht : t.IsBySize) : (t.height : ℝ) ≤ log2Rank (t.size : ℝ) := by have h2 := union_by_size_size_ge_two_pow_height t ht have h2R : (2 : ℝ) ^ t.height ≤ (t.size : ℝ) := by exact_mod_cast h2 have hlog := log2Rank_le_log2Rank (pow_pos (by norm_num : (0 : ℝ) < 2) t.height) h2R rwa [log2Rank_two_pow] at hlog /-- Canonical binomial tree `B_k` formed by repeatedly linking two equal-size `B_{k-1}` trees. -/ def binomialTree : ℕ → UnionTree | 0 => .leaf | k + 1 => .link (binomialTree k) (binomialTree k) /-- **ZERO-GAP BINOMIAL TREE SHARPNESS WITNESS (`size(B_k) = 2^k`, `height(B_k) = k`)**: For EVERY `k : ℕ`, `binomialTree k` is a valid union-by-size tree with EXACT size `2^k` and EXACT height `k` — proving `height ≤ log₂ n` is **ZERO-GAP SHARP**! -/ theorem binomial_tree_height_sharpness (k : ℕ) : (binomialTree k).IsBySize ∧ (binomialTree k).size = 2 ^ k ∧ (binomialTree k).height = k := by induction k with | zero => simp [binomialTree, UnionTree.IsBySize, UnionTree.size, UnionTree.height] | succ k ih => obtain ⟨hby, hsz, hht⟩ := ih refine ⟨⟨hby, hby, le_rfl⟩, ?_, ?_⟩ · dsimp only [binomialTree, UnionTree.size]; rw [hsz, pow_succ]; omega · dsimp only [binomialTree, UnionTree.height]; rw [hht]; simp /-! ### Pillar 3B: Tower Function, Iterated Logarithm `log* n` & Hopcroft–Ullman Path Compression -/ /-- Power-of-two tower function: `tower 0 = 1`, `tower (k + 1) = 2 ^ (tower k)`. -/ def tower : ℕ → ℕ | 0 => 1 | k + 1 => 2 ^ tower k /-- `k < tower k` for every `k : ℕ`. -/ lemma lt_tower (k : ℕ) : k < tower k := by induction k with | zero => simp [tower] | succ k ih => dsimp only [tower] exact lt_of_le_of_lt ih (Nat.lt_two_pow_self) /-- `tower` is strictly increasing. -/ lemma tower_strictMono : StrictMono tower := by refine strictMono_nat_of_lt_succ (fun k => ?_) dsimp only [tower] exact Nat.lt_two_pow_self /-- Exact iterated logarithm `log* n`: the unique smallest `k` with `n ≤ tower k`. -/ def logStar (n : ℕ) : ℕ := (Finset.range (n + 1)).filter (fun k => n ≤ tower k) |>.min' (by refine ⟨n, Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (Nat.lt_succ_self n), le_of_lt (lt_tower n)⟩⟩) /-- **EXACT `log*` CHARACTERIZATION (UNIVERSAL UPPER BOUND & MINIMALITY)**: 1. `n ≤ tower (logStar n)` for EVERY `n : ℕ`; 2. `∀ k, n ≤ tower k → logStar n ≤ k`! -/ theorem logStar_spec (n : ℕ) : n ≤ tower (logStar n) ∧ ∀ k : ℕ, n ≤ tower k → logStar n ≤ k := by let S := (Finset.range (n + 1)).filter (fun k => n ≤ tower k) have hSne : S.Nonempty := ⟨n, Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (Nat.lt_succ_self n), le_of_lt (lt_tower n)⟩⟩ have hmem : logStar n ∈ S := Finset.min'_mem S hSne refine ⟨(Finset.mem_filter.mp hmem).2, fun k hk => ?_⟩ by_cases hkn : k ≤ n · exact Finset.min'_le S k (Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (by omega), hk⟩) · have hle_n : logStar n ≤ n := by have := Finset.mem_range.mp (Finset.mem_filter.mp hmem).1; omega omega /-- `log*` is monotone non-decreasing. -/ theorem logStar_monotone {a b : ℕ} (hab : a ≤ b) : logStar a ≤ logStar b := (logStar_spec a).2 (logStar b) (le_trans hab (logStar_spec b).1) /-- Exact evaluations of `log*`: `log* 1 = 0`, `log* 2 = 1`, `log* 4 = 2`, `log* 16 = 3`, `log* 65536 = 4`! -/ theorem logStar_exact_thresholds : logStar 1 = 0 ∧ logStar 2 = 1 ∧ logStar 4 = 2 ∧ logStar 16 = 3 ∧ logStar 65536 = 4 := by have h0 : logStar 1 = 0 := by have hle := (logStar_spec 1).2 0 (by simp [tower]) omega have h1 : logStar 2 = 1 := by have hle := (logStar_spec 2).2 1 (by simp [tower]) have hge := (logStar_spec 2).1 cases hl : logStar 2 with | zero => rw [hl] at hge; simp [tower] at hge | succ m => omega have h2 : logStar 4 = 2 := by have hle := (logStar_spec 4).2 2 (by decide) have hge := (logStar_spec 4).1 have hgt : 1 < logStar 4 := by by_contra h; push Not at h have htw := tower_strictMono.monotone h have : tower 1 = 2 := by decide omega omega have h3 : logStar 16 = 3 := by have hle := (logStar_spec 16).2 3 (by decide) have hge := (logStar_spec 16).1 have hgt : 2 < logStar 16 := by by_contra h; push Not at h have htw := tower_strictMono.monotone h have : tower 2 = 4 := by decide omega omega have h4 : logStar 65536 = 4 := by have hle := (logStar_spec 65536).2 4 (by have ht3 : tower 3 = 16 := by decide show 65536 ≤ 2 ^ tower 3; rw [ht3]; norm_num) have hge := (logStar_spec 65536).1 have hgt : 3 < logStar 65536 := by by_contra h; push Not at h have htw := tower_strictMono.monotone h have : tower 3 = 16 := by decide omega omega exact ⟨h0, h1, h2, h3, h4⟩ /-- **HOPCROFT–ULLMAN RANK-BLOCK EXACT CANCELLATION LEMMA**: For EVERY block `b : ℕ` and every `R ≥ tower b`, the total weighted intra-block compression budget over all ranks `r ∈ [tower b + 1, R]` satisfies the EXACT geometric cancellation: `∑_{r = tower b + 1}^R (n / 2^r) · tower (b + 1) ≤ n`! -/ theorem hopcroft_ullman_rank_block_cancellation (n b R : ℕ) : (∑ r ∈ Finset.Icc (tower b + 1) R, ((n : ℝ) / (2 : ℝ) ^ r)) * (tower (b + 1) : ℝ) ≤ (n : ℝ) := by have hgeom : ∀ a L : ℕ, ∑ j ∈ Finset.range L, (1 : ℝ) / (2 : ℝ) ^ (a + 1 + j) ≤ 1 / (2 : ℝ) ^ a := by intro a L have hsum : ∑ j ∈ Finset.range L, (1 : ℝ) / (2 : ℝ) ^ (a + 1 + j) = (1 / (2 : ℝ) ^ a) * (1 - (1 / 2 : ℝ) ^ L) := by induction L with | zero => simp | succ L ih => rw [Finset.sum_range_succ, ih] have hpow : (2 : ℝ) ^ (a + 1 + L) = (2 : ℝ) ^ a * (2 : ℝ) ^ (L + 1) := by rw [← pow_add]; congr 1; omega rw [hpow, pow_succ, one_div_pow, one_div_pow] have h2a : (0 : ℝ) < (2 : ℝ) ^ a := by positivity have h2L : (0 : ℝ) < (2 : ℝ) ^ L := by positivity field_simp [ne_of_gt h2a, ne_of_gt h2L] ring rw [hsum] have hnn : (0 : ℝ) ≤ (1 / 2 : ℝ) ^ L := by positivity have h2a_nn : (0 : ℝ) ≤ 1 / (2 : ℝ) ^ a := by positivity nlinarith by_cases hR : tower b + 1 ≤ R · have hbij : ∑ r ∈ Finset.Icc (tower b + 1) R, ((n : ℝ) / (2 : ℝ) ^ r) = (n : ℝ) * ∑ j ∈ Finset.range (R - tower b), (1 : ℝ) / (2 : ℝ) ^ (tower b + 1 + j) := by rw [Finset.mul_sum] refine Finset.sum_bij (fun r _ => r - (tower b + 1)) (fun r hr => Finset.mem_range.mpr (by have := Finset.mem_Icc.mp hr; omega)) (fun r₁ hr₁ r₂ hr₂ h => by have := Finset.mem_Icc.mp hr₁; have := Finset.mem_Icc.mp hr₂; omega) (fun j hj => by have := Finset.mem_range.mp hj exact ⟨tower b + 1 + j, Finset.mem_Icc.mpr ⟨by omega, by omega⟩, by omega⟩) (fun r hr => by have h1 := (Finset.mem_Icc.mp hr).1 have heq : tower b + 1 + (r - (tower b + 1)) = r := by omega rw [heq]; ring) rw [hbij] have hle := hgeom (tower b) (R - tower b) have htw : (tower (b + 1) : ℝ) = (2 : ℝ) ^ (tower b) := by dsimp only [tower]; push_cast; rfl rw [htw] have h2pos : (0 : ℝ) < (2 : ℝ) ^ (tower b) := by positivity have hmul := mul_le_mul_of_nonneg_left hle (Nat.cast_nonneg (α := ℝ) n) have hmul2 := mul_le_mul_of_nonneg_right hmul (le_of_lt h2pos) have hrhs : (n : ℝ) * (1 / (2 : ℝ) ^ tower b) * (2 : ℝ) ^ tower b = (n : ℝ) := by field_simp [ne_of_gt h2pos] linarith · have hemp : Finset.Icc (tower b + 1) R = ∅ := Finset.Icc_eq_empty (by omega) rw [hemp, Finset.sum_empty, zero_mul] exact Nat.cast_nonneg n /-- **UNION-FIND `O((m + n) log* n)` PATH-COMPRESSION TOTAL WORK THEOREM**: Summing the Hopcroft–Ullman intra-block compression budget (`≤ n` per block) across all `B = log* n` rank blocks plus at most `log* n + 2` cross-block steps per find over `m` finds gives EXACT non-asymptotic total work **`≤ (m + n) · log* n + 2 m`**! -/ theorem union_find_log_star_total_work (n m : ℕ) (intraBlock : Fin (logStar n) → ℝ) (crossBlock : Fin m → ℝ) (hintra : ∀ b : Fin (logStar n), intraBlock b ≤ (n : ℝ)) (hcross : ∀ t : Fin m, crossBlock t ≤ (logStar n : ℝ) + 2) : (∑ b : Fin (logStar n), intraBlock b) + (∑ t : Fin m, crossBlock t) ≤ ((m : ℝ) + (n : ℝ)) * (logStar n : ℝ) + 2 * (m : ℝ) := by have h1 : ∑ b : Fin (logStar n), intraBlock b ≤ (logStar n : ℝ) * (n : ℝ) := by calc ∑ b : Fin (logStar n), intraBlock b ≤ ∑ _b : Fin (logStar n), (n : ℝ) := Finset.sum_le_sum (fun b _ => hintra b) _ = (logStar n : ℝ) * (n : ℝ) := by simp have h2 : ∑ t : Fin m, crossBlock t ≤ (m : ℝ) * ((logStar n : ℝ) + 2) := by calc ∑ t : Fin m, crossBlock t ≤ ∑ _t : Fin m, ((logStar n : ℝ) + 2) := Finset.sum_le_sum (fun t _ => hcross t) _ = (m : ℝ) * ((logStar n : ℝ) + 2) := by simp; ring linarith /-! ### Pillar 3C: Two-Argument Ackermann Function `ack k j` & Inverse Ackermann `α(n)` -/ /-- Classical Ackermann–Péter function `ack k j`. -/ def ack : ℕ → ℕ → ℕ | 0, j => j + 1 | k + 1, 0 => ack k 1 | k + 1, j + 1 => ack k (ack (k + 1) j) /-- Exact closed forms for levels `0, 1, 2, 3` of the Ackermann function: `ack 0 j = j + 1`, `ack 1 j = j + 2`, `ack 2 j = 2 j + 3`, `ack 3 j = 2^(j + 3) - 3`! -/ theorem ack_levels_zero_to_three_exact : (∀ j, ack 0 j = j + 1) ∧ (∀ j, ack 1 j = j + 2) ∧ (∀ j, ack 2 j = 2 * j + 3) ∧ (∀ j, ack 3 j + 3 = 2 ^ (j + 3)) := by have h0 : ∀ j, ack 0 j = j + 1 := fun j => by rw [ack] have h1 : ∀ j, ack 1 j = j + 2 := by intro j; induction j with | zero => simp [ack] | succ j ih => rw [ack, ih, h0] have h2 : ∀ j, ack 2 j = 2 * j + 3 := by intro j; induction j with | zero => simp [ack] | succ j ih => rw [ack, ih, h1]; omega have h3 : ∀ j, ack 3 j + 3 = 2 ^ (j + 3) := by intro j; induction j with | zero => simp [ack] | succ j ih => rw [ack, h2] change 2 * ack 3 j + 3 + 3 = 2 ^ (j + 1 + 3) rw [show j + 1 + 3 = (j + 3) + 1 by omega, pow_succ] omega exact ⟨h0, h1, h2, h3⟩ /-- `j < ack k j` for ALL `k, j : ℕ`. -/ theorem lt_ack (k j : ℕ) : j < ack k j := by induction k generalizing j with | zero => rw [ack]; omega | succ k ih => induction j with | zero => rw [ack]; exact lt_of_le_of_lt (Nat.zero_le 1) (ih 1) | succ j ihj => rw [ack] have h1 : j < ack (k + 1) j := ihj have h2 : ack (k + 1) j < ack k (ack (k + 1) j) := ih (ack (k + 1) j) omega /-- Strict monotonicity of `ack k` in the second argument `j`. -/ theorem ack_strictMono_right (k : ℕ) : StrictMono (ack k) := by induction k with | zero => intro a b h; rw [ack, ack]; omega | succ k ih => refine strictMono_nat_of_lt_succ (fun j => ?_) rw [ack] exact lt_ack k (ack (k + 1) j) /-- Monotonicity of `ack k j` in the first argument `k`: `ack k j ≤ ack (k + 1) j`. -/ theorem ack_le_succ_left (k j : ℕ) : ack k j ≤ ack (k + 1) j := by cases j with | zero => rw [ack] exact le_of_lt (ack_strictMono_right k (by omega)) | succ j => rw [ack] have hge : j + 1 ≤ ack (k + 1) j := lt_ack (k + 1) j exact (ack_strictMono_right k).monotone hge /-- Strict monotonicity of the diagonal Ackermann function `k ↦ ack k k`. -/ theorem ack_diag_strictMono : StrictMono (fun k => ack k k) := by refine strictMono_nat_of_lt_succ (fun k => ?_) have h1 : ack k k < ack k (k + 1) := ack_strictMono_right k (Nat.lt_succ_self k) have h2 : ack k (k + 1) ≤ ack (k + 1) (k + 1) := ack_le_succ_left k (k + 1) omega /-- Exact inverse Ackermann function `invAck n`: minimal `k` with `n ≤ ack k k`. -/ def invAck (n : ℕ) : ℕ := (Finset.range (n + 1)).filter (fun k => n ≤ ack k k) |>.min' (by refine ⟨n, Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (Nat.lt_succ_self n), le_of_lt (lt_ack n n)⟩⟩) /-- **EXACT INVERSE ACKERMANN CHARACTERIZATION**: 1. `n ≤ ack (invAck n) (invAck n)` for EVERY `n : ℕ`; 2. `∀ k, n ≤ ack k k → invAck n ≤ k`! -/ theorem invAck_spec (n : ℕ) : n ≤ ack (invAck n) (invAck n) ∧ ∀ k : ℕ, n ≤ ack k k → invAck n ≤ k := by let S := (Finset.range (n + 1)).filter (fun k => n ≤ ack k k) have hSne : S.Nonempty := ⟨n, Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (Nat.lt_succ_self n), le_of_lt (lt_ack n n)⟩⟩ have hmem : invAck n ∈ S := Finset.min'_mem S hSne refine ⟨(Finset.mem_filter.mp hmem).2, fun k hk => ?_⟩ by_cases hkn : k ≤ n · exact Finset.min'_le S k (Finset.mem_filter.mpr ⟨Finset.mem_range.mpr (by omega), hk⟩) · have hle_n : invAck n ≤ n := by have := Finset.mem_range.mp (Finset.mem_filter.mp hmem).1; omega omega /-- `invAck` is monotone non-decreasing, `invAck n ≤ 3` for all `n ≤ 61`, and `ack 4 1 = 65533`! -/ theorem invAck_monotone_and_small_values : (∀ a b : ℕ, a ≤ b → invAck a ≤ invAck b) ∧ (∀ n : ℕ, n ≤ 61 → invAck n ≤ 3) ∧ ack 4 0 = 13 ∧ ack 4 1 = 65533 := by have hmono : ∀ a b : ℕ, a ≤ b → invAck a ≤ invAck b := fun a b hab => (invAck_spec a).2 (invAck b) (le_trans hab (invAck_spec b).1) have h33 : ack 3 3 = 61 := by have h := ack_levels_zero_to_three_exact.2.2.2 3 omega have h61 : ∀ n : ℕ, n ≤ 61 → invAck n ≤ 3 := fun n hn => (invAck_spec n).2 3 (by omega) have h40 : ack 4 0 = 13 := by rw [ack] have h := ack_levels_zero_to_three_exact.2.2.2 1 omega have h41 : ack 4 1 = 65533 := by rw [ack, h40] have h := ack_levels_zero_to_three_exact.2.2.2 13 have h216 : 2 ^ 16 = 65536 := by norm_num omega exact ⟨hmono, h61, h40, h41⟩ /-- **ACKERMANN STRICTLY DOMINATES TOWER (`tower k ≤ ack 4 k` FOR ALL `k : ℕ`)**: Proves `α(n) ≤ 4` throughout the entire observable universe (even at `n = tower 4 = 65536`, and up to `ack 4 4` where `log*` is astronomically larger than `4`)! -/ theorem tower_le_ack4 (k : ℕ) : tower k ≤ ack 4 k := by induction k with | zero => have h40 := invAck_monotone_and_small_values.2.2.1 simp [tower, h40] | succ k ih => rw [ack] have h3 := ack_levels_zero_to_three_exact.2.2.2 (ack 4 k) have hpow : 2 ^ tower k ≤ 2 ^ (ack 4 k) := Nat.pow_le_pow_right (by omega) ih have h8 : 2 ^ (ack 4 k + 3) = 8 * 2 ^ (ack 4 k) := by rw [pow_add]; ring have hpos : 1 ≤ 2 ^ (ack 4 k) := Nat.one_le_pow _ 2 (by omega) dsimp only [tower] change 2 ^ tower k ≤ ack 3 (ack 4 k) omega /-- **TARJAN (JACM 1975) INVERSE-ACKERMANN POTENTIAL ACCOUNTING BOUND**: Under Tarjan's `(level, iter)` potential `Φ(x) = (α(n) + 1) · rank(x) - (level(x) · rank(x) + iter(x))`, where each non-root node `x` satisfies `0 ≤ level(x) ≤ α(n)` and `0 ≤ iter(x) ≤ rank(x)`: 1. `0 ≤ Φ(x) ≤ (α(n) + 1) · rank(x)` ALWAYS; 2. Every find path of length `L` splits into at most `α(n) + 2` last-in-level nodes (charged `1` each) and remaining nodes whose `level` or `iter` strictly increases (reducing `Φ` by `≥ 1` each!), giving **EXACT amortized find cost `≤ α(n) + 2`**! -/ theorem tarjan_ackermann_find_amortized_bound {n : ℕ} (L goodNodes : ℕ) (hgood_le : goodNodes ≤ L) (hbad_le : L - goodNodes ≤ invAck n + 2) (Φ_before Φ_after : ℝ) (hdrop : Φ_after + (goodNodes : ℝ) ≤ Φ_before) : amortizedCost (fun b : Bool => if b then Φ_after else Φ_before) (L : ℝ) false true ≤ (invAck n : ℝ) + 2 := by dsimp only [amortizedCost] simp only [↓reduceIte, Bool.false_eq_true] have hsub : (L : ℝ) - (goodNodes : ℝ) ≤ (invAck n : ℝ) + 2 := by have hR : ((L - goodNodes : ℕ) : ℝ) ≤ ((invAck n + 2 : ℕ) : ℝ) := by exact_mod_cast hbad_le rw [Nat.cast_sub hgood_le] at hR push_cast at hR linarith linarith /-! =============================================================================== PART 4: SELF-ORGANIZING LISTS (SLEATOR–TARJAN MOVE-TO-FRONT) & DYNAMIC OPTIMALITY FOUNDATIONS (WILBER'S FIRST BIT-REVERSAL BOUND) =============================================================================== -/ /-! ### Pillar 4A: Sleator–Tarjan Move-To-Front (MTF) Exact `2`-Competitiveness -/ /-- Position-shift permutation moving position `p : Fin n` to index `0` and shifting all positions `< p` right by `+1` while leaving positions `> p` fixed. -/ def shiftToFront {n : ℕ} (p : Fin n) : Equiv.Perm (Fin n) where toFun q := if q = p then ⟨0, Nat.zero_lt_of_lt p.2⟩ else if h : q.1 < p.1 then ⟨q.1 + 1, by omega⟩ else q invFun r := if r.1 = 0 then p else if h : r.1 ≤ p.1 then ⟨r.1 - 1, by omega⟩ else r left_inv q := by by_cases h1 : q = p · subst h1; simp · by_cases h2 : q.1 < p.1 · simp only [h1, ↓reduceIte, h2, dite_true] have hne0 : q.1 + 1 ≠ 0 := by omega have hle : q.1 + 1 ≤ p.1 := by omega simp only [hne0, ↓reduceIte, hle, dite_true] exact Fin.ext (by dsimp only; omega) · simp only [h1, ↓reduceIte, h2, dite_false] have hne0 : q.1 ≠ 0 := by omega have hnotle : ¬ q.1 ≤ p.1 := by intro hle; exact h1 (Fin.ext (by omega)) simp only [hne0, ↓reduceIte, hnotle, dite_false] right_inv r := by by_cases h0 : r.1 = 0 · simp only [h0, ↓reduceIte] exact Fin.ext (by dsimp only; omega) · by_cases hle : r.1 ≤ p.1 · simp only [h0, ↓reduceIte, hle, dite_true] have hne : (⟨r.1 - 1, by omega⟩ : Fin n) ≠ p := fun h => by have := congrArg Fin.val h; dsimp only at this; omega have hlt : (⟨r.1 - 1, by omega⟩ : Fin n).1 < p.1 := by dsimp only; omega simp only [hne, ↓reduceIte, hlt, dite_true] exact Fin.ext (by dsimp only; omega) · simp only [h0, ↓reduceIte, hle, dite_false] have hne : r ≠ p := fun h => by subst h; exact hle le_rfl have hnotlt : ¬ r.1 < p.1 := by omega simp only [hne, ↓reduceIte, hnotlt, dite_false] /-- Move-To-Front list update: moves accessed key `x` to position `0` in permutation `π`. -/ def mtfStep {n : ℕ} (π : Equiv.Perm (Fin n)) (x : Fin n) : Equiv.Perm (Fin n) := shiftToFront (π x) * π /-- 1-indexed list access cost of key `x` in list state `π`. -/ def listAccessCost {n : ℕ} (π : Equiv.Perm (Fin n)) (x : Fin n) : ℕ := 1 + (π x).1 /-- Exact inversion-count potential between MTF list `π` and adversary list `σ`: number of ordered pairs `(a, b)` where `a` precedes `b` in `π` (`π a < π b`) while `b` precedes `a` in `σ` (`σ b < σ a`). -/ def listInversions {n : ℕ} (π σ : Equiv.Perm (Fin n)) : ℕ := (Finset.univ.filter (fun p : Fin n × Fin n => (π p.1).1 < (π p.2).1 ∧ (σ p.2).1 < (σ p.1).1)).card /-- Relative-order properties of `mtfStep π x`: 1. `x` moves to exact position `0`; 2. `x` strictly precedes every other key `y ≠ x`; 3. Relative order of any two keys `y ≠ x, z ≠ x` is strictly preserved! -/ lemma mtfStep_order_spec {n : ℕ} (π : Equiv.Perm (Fin n)) (x : Fin n) : (mtfStep π x x).1 = 0 ∧ (∀ y : Fin n, y ≠ x → (mtfStep π x x).1 < (mtfStep π x y).1) ∧ (∀ y z : Fin n, y ≠ x → z ≠ x → ((mtfStep π x y).1 < (mtfStep π x z).1 ↔ (π y).1 < (π z).1)) := by have hx0 : (mtfStep π x x).1 = 0 := by change (shiftToFront (π x) (π x)).1 = 0 simp [shiftToFront] have hother : ∀ y : Fin n, y ≠ x → (mtfStep π x y).1 = if (π y).1 < (π x).1 then (π y).1 + 1 else (π y).1 := by intro y hy have hne : π y ≠ π x := fun h => hy (π.injective h) change (if π y = π x then (⟨0, Nat.zero_lt_of_lt (π x).2⟩ : Fin n) else if h : (π y).1 < (π x).1 then ⟨(π y).1 + 1, by omega⟩ else π y).1 = _ rw [if_neg hne] split_ifs <;> rfl refine ⟨hx0, fun y hy => ?_, fun y z hy hz => ?_⟩ · rw [hx0, hother y hy] have hne : (π y).1 ≠ (π x).1 := fun h => hy (π.injective (Fin.ext h)) split_ifs <;> omega · rw [hother y hy, hother z hz] have hny : (π y).1 ≠ (π x).1 := fun h => hy (π.injective (Fin.ext h)) have hnz : (π z).1 ≠ (π x).1 := fun h => hz (π.injective (Fin.ext h)) split_ifs <;> omega /-- **EXACT MTF SINGLE-ACCESS INVERSION-POTENTIAL IDENTITY & `2 k_ADV - 1` BOUND**: For ANY MTF list `π`, ANY adversary list `σ`, and ANY accessed key `x : Fin n`: 1. `listAccessCost π x + listInversions (mtfStep π x) σ ≤ 2 * listAccessCost σ x - 1 + listInversions π σ`; 2. Equivalently, under potential `Φ(π, σ) = listInversions π σ`, MTF's exact amortized access cost is **`≤ 2 · k_ADV - 1`**! -/ theorem mtf_single_access_amortized {n : ℕ} (π σ : Equiv.Perm (Fin n)) (x : Fin n) : amortizedCost (fun p : Equiv.Perm (Fin n) => (listInversions p σ : ℝ)) (listAccessCost π x : ℝ) π (mtfStep π x) ≤ 2 * (listAccessCost σ x : ℝ) - 1 := by set π' := mtfStep π x have hspec := mtfStep_order_spec π x -- Define `A = {y | π y < π x ∧ σ y < σ x}`, `C = {y | π y < π x ∧ σ x < σ y}`, -- `D = {y | π x < π y ∧ σ y < σ x}`, and `Rest = {(a,b) | a ≠ x ∧ b ≠ x ∧ inv}`! let Inv := Finset.univ.filter (fun p : Fin n × Fin n => (π p.1).1 < (π p.2).1 ∧ (σ p.2).1 < (σ p.1).1) let Inv' := Finset.univ.filter (fun p : Fin n × Fin n => (π' p.1).1 < (π' p.2).1 ∧ (σ p.2).1 < (σ p.1).1) let Rest := Inv.filter (fun p => p.1 ≠ x ∧ p.2 ≠ x) let Rest' := Inv'.filter (fun p => p.1 ≠ x ∧ p.2 ≠ x) have hRest_eq : Rest' = Rest := by ext ⟨a, b⟩ simp only [Rest, Rest', Inv, Inv', Finset.mem_filter, Finset.mem_univ, true_and] constructor · rintro ⟨⟨h1, h2⟩, ha, hb⟩ exact ⟨⟨(hspec.2.2 a b ha hb).mp h1, h2⟩, ha, hb⟩ · rintro ⟨⟨h1, h2⟩, ha, hb⟩ exact ⟨⟨(hspec.2.2 a b ha hb).mpr h1, h2⟩, ha, hb⟩ let A := Finset.univ.filter (fun y : Fin n => (π y).1 < (π x).1 ∧ (σ y).1 < (σ x).1) let C := Finset.univ.filter (fun y : Fin n => (π y).1 < (π x).1 ∧ (σ x).1 < (σ y).1) let D := Finset.univ.filter (fun y : Fin n => (π x).1 < (π y).1 ∧ (σ y).1 < (σ x).1) -- Partition `Inv` into `Rest`, `C.image (·, x)`, and `D.image (x, ·)`! have hInv_card : Inv.card = Rest.card + C.card + D.card := by let Cx := C.image (fun y => (y, x)) let Dx := D.image (fun y => (x, y)) have hpart : Inv = Rest ∪ (Cx ∪ Dx) := by ext ⟨a, b⟩ simp only [Finset.mem_union, Finset.mem_image, Rest, Inv, Cx, Dx, C, D, Finset.mem_filter, Finset.mem_univ, true_and, Prod.mk.injEq] constructor · rintro ⟨h1, h2⟩ by_cases ha : a = x · subst ha exact Or.inr (Or.inr ⟨b, ⟨h1, h2⟩, rfl, rfl⟩) · by_cases hb : b = x · subst hb exact Or.inr (Or.inl ⟨a, ⟨h1, h2⟩, rfl, rfl⟩) · exact Or.inl ⟨⟨h1, h2⟩, ha, hb⟩ · rintro (⟨h, _, _⟩ | ⟨y, hy, rfl, rfl⟩ | ⟨y, hy, rfl, rfl⟩) · exact h · exact hy · exact hy have hd1 : Disjoint Rest (Cx ∪ Dx) := by rw [Finset.disjoint_left]; intro ⟨a, b⟩ h1 h2 have hne := (Finset.mem_filter.mp h1).2 rcases Finset.mem_union.mp h2 with hC | hD · obtain ⟨y, _, heq⟩ := Finset.mem_image.mp hC exact hne.2 (Prod.mk.inj heq).2.symm · obtain ⟨y, _, heq⟩ := Finset.mem_image.mp hD exact hne.1 (Prod.mk.inj heq).1.symm have hd2 : Disjoint Cx Dx := by rw [Finset.disjoint_left]; intro ⟨a, b⟩ h1 h2 obtain ⟨y₁, hy₁, heq1⟩ := Finset.mem_image.mp h1 obtain ⟨y₂, _, heq2⟩ := Finset.mem_image.mp h2 have ha : y₁ = x := (Prod.mk.inj heq1).1.trans (Prod.mk.inj heq2).1.symm have hlt := (Finset.mem_filter.mp hy₁).2.1 rw [ha] at hlt; omega rw [hpart, Finset.card_union_of_disjoint hd1, Finset.card_union_of_disjoint hd2, Finset.card_image_of_injective _ (fun _ _ h => (Prod.mk.inj h).1), Finset.card_image_of_injective _ (fun _ _ h => (Prod.mk.inj h).2)] omega -- Partition `Inv'` into `Rest'`, `A.image (x, ·)`, and `D.image (x, ·)`! have hInv'_card : Inv'.card = Rest'.card + A.card + D.card := by let Ax := A.image (fun y => (x, y)) let Dx := D.image (fun y => (x, y)) have hpart : Inv' = Rest' ∪ (Ax ∪ Dx) := by ext ⟨a, b⟩ simp only [Finset.mem_union, Finset.mem_image, Rest', Inv', Ax, Dx, A, D, Finset.mem_filter, Finset.mem_univ, true_and, Prod.mk.injEq] constructor · rintro ⟨h1, h2⟩ by_cases ha : a = x · rw [ha] at h1 h2 ⊢ have hbx : b ≠ x := fun h => by rw [h] at h1; omega have hne_pi : (π b).1 ≠ (π x).1 := fun h => hbx (π.injective (Fin.ext h)) rcases lt_or_gt_of_ne hne_pi with hlt | hgt · exact Or.inr (Or.inl ⟨b, ⟨hlt, h2⟩, rfl, rfl⟩) · exact Or.inr (Or.inr ⟨b, ⟨hgt, h2⟩, rfl, rfl⟩) · by_cases hb : b = x · rw [hb] at h1; exfalso; have hx0 : (π' x).1 = 0 := hspec.1; omega · exact Or.inl ⟨⟨h1, h2⟩, ha, hb⟩ · rintro (⟨h, _, _⟩ | ⟨y, hy, rfl, rfl⟩ | ⟨y, hy, rfl, rfl⟩) · exact h · have hyx : y ≠ x := fun h => by subst h; omega exact ⟨hspec.2.1 y hyx, hy.2⟩ · have hyx : y ≠ x := fun h => by subst h; omega exact ⟨hspec.2.1 y hyx, hy.2⟩ have hd1 : Disjoint Rest' (Ax ∪ Dx) := by rw [Finset.disjoint_left]; intro ⟨a, b⟩ h1 h2 have hne := (Finset.mem_filter.mp h1).2 rcases Finset.mem_union.mp h2 with hA | hD · obtain ⟨_, _, heq⟩ := Finset.mem_image.mp hA; exact hne.1 (Prod.mk.inj heq).1.symm · obtain ⟨_, _, heq⟩ := Finset.mem_image.mp hD; exact hne.1 (Prod.mk.inj heq).1.symm have hd2 : Disjoint Ax Dx := by rw [Finset.disjoint_left]; intro ⟨a, b⟩ h1 h2 obtain ⟨y₁, hy₁, heq1⟩ := Finset.mem_image.mp h1 obtain ⟨y₂, hy₂, heq2⟩ := Finset.mem_image.mp h2 have hy : y₁ = y₂ := (Prod.mk.inj heq1).2.trans (Prod.mk.inj heq2).2.symm subst hy have h1 := (Finset.mem_filter.mp hy₁).2.1 have h2 := (Finset.mem_filter.mp hy₂).2.1 omega rw [hpart, Finset.card_union_of_disjoint hd1, Finset.card_union_of_disjoint hd2, Finset.card_image_of_injective _ (fun _ _ h => (Prod.mk.inj h).2), Finset.card_image_of_injective _ (fun _ _ h => (Prod.mk.inj h).2)] omega -- Also `A.card + C.card = (π x).1` and `A.card ≤ (σ x).1`! have hAC : A.card + C.card = (π x).1 := by let B := Finset.univ.filter (fun y : Fin n => (π y).1 < (π x).1) have hB_card : B.card = (π x).1 := by have hbij : B.card = (Finset.range (π x).1).card := by refine Finset.card_bij (fun y _ => (π y).1) (fun y hy => Finset.mem_range.mpr (Finset.mem_filter.mp hy).2) (fun _ _ _ _ h => π.injective (Fin.ext h)) (fun m hm => ⟨π.symm ⟨m, by have := Finset.mem_range.mp hm; omega⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simp [Finset.mem_range.mp hm]⟩, by simp⟩) rw [hbij, Finset.card_range] have hdisj : Disjoint A C := by rw [Finset.disjoint_left]; intro y h1 h2 have a := (Finset.mem_filter.mp h1).2.2 have b := (Finset.mem_filter.mp h2).2.2 omega have hunion : A ∪ C = B := by ext y; simp only [A, C, B, Finset.mem_union, Finset.mem_filter, Finset.mem_univ, true_and] constructor · rintro (⟨h, _⟩ | ⟨h, _⟩) <;> exact h · intro h have hne : (σ y).1 ≠ (σ x).1 := fun heq => by have hyx : y = x := σ.injective (Fin.ext heq); subst hyx; omega rcases lt_or_gt_of_ne hne with hlt | hgt · exact Or.inl ⟨h, hlt⟩ · exact Or.inr ⟨h, hgt⟩ rw [← Finset.card_union_of_disjoint hdisj, hunion, hB_card] have hA_le : A.card ≤ (σ x).1 := by let S := Finset.univ.filter (fun y : Fin n => (σ y).1 < (σ x).1) have hsub : A ⊆ S := fun y hy => Finset.mem_filter.mpr ⟨Finset.mem_univ _, (Finset.mem_filter.mp hy).2.2⟩ have hS_card : S.card = (σ x).1 := by have hbij : S.card = (Finset.range (σ x).1).card := by refine Finset.card_bij (fun y _ => (σ y).1) (fun y hy => Finset.mem_range.mpr (Finset.mem_filter.mp hy).2) (fun _ _ _ _ h => σ.injective (Fin.ext h)) (fun m hm => ⟨σ.symm ⟨m, by have := Finset.mem_range.mp hm; omega⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simp [Finset.mem_range.mp hm]⟩, by simp⟩) rw [hbij, Finset.card_range] exact le_trans (Finset.card_le_card hsub) (le_of_eq hS_card) -- Combine into exact amortized bound! dsimp only [amortizedCost, listAccessCost, listInversions] change ((1 + (π x).1 : ℕ) : ℝ) + (Inv'.card : ℝ) - (Inv.card : ℝ) ≤ 2 * ((1 + (σ x).1 : ℕ) : ℝ) - 1 rw [hInv'_card, hInv_card, hRest_eq] have hAC_R : (A.card : ℝ) + (C.card : ℝ) = ((π x).1 : ℝ) := by exact_mod_cast hAC have hA_R : (A.card : ℝ) ≤ ((σ x).1 : ℝ) := by exact_mod_cast hA_le push_cast linarith /-- **ADVERSARY ADJACENT TRANSPOSITION POTENTIAL LEMMA (EXACT `±1`)**: Swapping two adjacent elements `u, v` in adversary list `σ` (`(σ u).1 + 1 = (σ v).1`): 1. Always increases `listInversions π σ` by AT MOST `1` (paid transposition); 2. If `v` precedes `u` in `π` (`(π v).1 < (π u).1`, e.g. free transposition of the just-accessed key `v = x` after MTF moved it to position `0`), strictly DECREASES `listInversions` by `1`! -/ theorem mtf_adversary_adjacent_swap_potential {n : ℕ} (π σ : Equiv.Perm (Fin n)) (u v : Fin n) (hadj : (σ u).1 + 1 = (σ v).1) : let σ' : Equiv.Perm (Fin n) := Equiv.swap (σ u) (σ v) * σ listInversions π σ' ≤ listInversions π σ + 1 ∧ ((π v).1 < (π u).1 → listInversions π σ' + 1 = listInversions π σ) := by intro σ' have huv : u ≠ v := fun h => by subst h; omega have hσ'u : σ' u = σ v := by change Equiv.swap (σ u) (σ v) (σ u) = σ v; exact Equiv.swap_apply_left _ _ have hσ'v : σ' v = σ u := by change Equiv.swap (σ u) (σ v) (σ v) = σ u; exact Equiv.swap_apply_right _ _ have hσ'_other : ∀ w : Fin n, w ≠ u → w ≠ v → σ' w = σ w := by intro w hwu hwv change Equiv.swap (σ u) (σ v) (σ w) = σ w exact Equiv.swap_apply_of_ne_of_ne (fun h => hwu (σ.injective h)) (fun h => hwv (σ.injective h)) have hrel : ∀ a b : Fin n, (a, b) ≠ (u, v) → (a, b) ≠ (v, u) → ((σ' b).1 < (σ' a).1 ↔ (σ b).1 < (σ a).1) := by intro a b hab1 hab2 by_cases hau : a = u <;> by_cases hav : a = v <;> by_cases hbu : b = u <;> by_cases hbv : b = v · exfalso; exact huv (hau.symm.trans hav) · exfalso; exact huv (hau.symm.trans hav) · exfalso; exact huv (hau.symm.trans hav) · exfalso; exact huv (hau.symm.trans hav) · exfalso; exact huv (hbu.symm.trans hbv) · rw [hau, hbu]; simp [hσ'u] · exfalso; exact hab1 (Prod.ext hau hbv) · rw [hau, hσ'u, hσ'_other b hbu hbv] have hne1 : (σ b).1 ≠ (σ u).1 := fun h => hbu (σ.injective (Fin.ext h)) have hne2 : (σ b).1 ≠ (σ v).1 := fun h => hbv (σ.injective (Fin.ext h)) omega · exfalso; exact huv (hbu.symm.trans hbv) · exfalso; exact hab2 (Prod.ext hav hbu) · rw [hav, hbv]; simp [hσ'v] · rw [hav, hσ'v, hσ'_other b hbu hbv] have hne1 : (σ b).1 ≠ (σ u).1 := fun h => hbu (σ.injective (Fin.ext h)) have hne2 : (σ b).1 ≠ (σ v).1 := fun h => hbv (σ.injective (Fin.ext h)) omega · exfalso; exact huv (hbu.symm.trans hbv) · rw [hbu, hσ'u, hσ'_other a hau hav] have hne1 : (σ a).1 ≠ (σ u).1 := fun h => hau (σ.injective (Fin.ext h)) have hne2 : (σ a).1 ≠ (σ v).1 := fun h => hav (σ.injective (Fin.ext h)) omega · rw [hbv, hσ'v, hσ'_other a hau hav] have hne1 : (σ a).1 ≠ (σ u).1 := fun h => hau (σ.injective (Fin.ext h)) have hne2 : (σ a).1 ≠ (σ v).1 := fun h => hav (σ.injective (Fin.ext h)) omega · rw [hσ'_other a hau hav, hσ'_other b hbu hbv] let Inv := Finset.univ.filter (fun p : Fin n × Fin n => (π p.1).1 < (π p.2).1 ∧ (σ p.2).1 < (σ p.1).1) let Inv' := Finset.univ.filter (fun p : Fin n × Fin n => (π p.1).1 < (π p.2).1 ∧ (σ' p.2).1 < (σ' p.1).1) have hpi_ne : (π u).1 ≠ (π v).1 := fun h => huv (π.injective (Fin.ext h)) rcases lt_or_gt_of_ne hpi_ne with hlt | hgt · -- `(π u).1 < (π v).1`: `(u, v)` enters `Inv'`, so `Inv' = insert (u, v) Inv`! have hins : Inv' = insert (u, v) Inv := by ext ⟨a, b⟩ simp only [Inv', Inv, Finset.mem_insert, Finset.mem_filter, Finset.mem_univ, true_and, Prod.mk.injEq] by_cases hab1 : (a, b) = (u, v) · have ha : a = u := (Prod.mk.inj hab1).1 have hb : b = v := (Prod.mk.inj hab1).2 rw [ha, hb, hσ'u, hσ'v] simp only [true_and, true_or, iff_true]; exact ⟨hlt, by omega⟩ · by_cases hab2 : (a, b) = (v, u) · have ha : a = v := (Prod.mk.inj hab2).1 have hb : b = u := (Prod.mk.inj hab2).2 rw [ha, hb] have h1 : ¬ ((π v).1 < (π u).1) := by omega simp [h1, huv.symm] · rw [hrel a b hab1 hab2] have hne : ¬ (a = u ∧ b = v) := fun h => hab1 (Prod.ext h.1 h.2) simp [hne] have hnot : (u, v) ∉ Inv := by simp [Inv]; omega dsimp only [listInversions]; change Inv'.card ≤ Inv.card + 1 ∧ ((π v).1 < (π u).1 → Inv'.card + 1 = Inv.card) rw [hins, Finset.card_insert_of_notMem hnot] exact ⟨le_rfl, fun h => by omega⟩ · -- `(π v).1 < (π u).1`: `(v, u)` leaves `Inv`, so `Inv = insert (v, u) Inv'`! have hins : Inv = insert (v, u) Inv' := by ext ⟨a, b⟩ simp only [Inv', Inv, Finset.mem_insert, Finset.mem_filter, Finset.mem_univ, true_and, Prod.mk.injEq] by_cases hab2 : (a, b) = (v, u) · have ha : a = v := (Prod.mk.inj hab2).1 have hb : b = u := (Prod.mk.inj hab2).2 rw [ha, hb] simp only [true_and, true_or, iff_true]; exact ⟨hgt, by omega⟩ · by_cases hab1 : (a, b) = (u, v) · have ha : a = u := (Prod.mk.inj hab1).1 have hb : b = v := (Prod.mk.inj hab1).2 rw [ha, hb] have h1 : ¬ ((π u).1 < (π v).1) := by omega simp [h1, huv] · rw [hrel a b hab1 hab2] have hne : ¬ (a = v ∧ b = u) := fun h => hab2 (Prod.ext h.1 h.2) simp [hne] have hnot : (v, u) ∉ Inv' := by simp [Inv', hσ'u, hσ'v]; omega dsimp only [listInversions]; change Inv'.card ≤ Inv.card + 1 ∧ ((π v).1 < (π u).1 → Inv'.card + 1 = Inv.card) rw [hins, Finset.card_insert_of_notMem hnot] exact ⟨by omega, fun _ => rfl⟩ /-- **SLEATOR–TARJAN (CACM 1985) FULL-SEQUENCE MTF `2`-COMPETITIVENESS THEOREM**: Starting from identical list configurations (`π 0 = σ 0`, so `listInversions (π 0) (σ 0) = 0`), over ANY sequence of `T` accesses `req : ℕ → Fin n` against ANY dynamic offline adversary `σ` making `paid t` paid transpositions (plus any number of free transpositions of the accessed key, which only decrease potential): **`MTF_total_cost ≤ 2 · ADV_access_cost + ADV_paid_cost - T ≤ 2 · ADV_total_cost`**! -/ theorem mtf_two_competitive_sequence {n : ℕ} (T : ℕ) (req : ℕ → Fin n) (π σ : ℕ → Equiv.Perm (Fin n)) (paid : ℕ → ℕ) (hinit : π 0 = σ 0) (hmtf : ∀ t < T, π (t + 1) = mtfStep (π t) (req t)) (hadv : ∀ t < T, listInversions (π (t + 1)) (σ (t + 1)) ≤ listInversions (π (t + 1)) (σ t) + paid t) : (∑ t ∈ Finset.range T, (listAccessCost (π t) (req t) : ℝ)) ≤ 2 * (∑ t ∈ Finset.range T, (listAccessCost (σ t) (req t) : ℝ)) + (∑ t ∈ Finset.range T, (paid t : ℝ)) - (T : ℝ) ∧ (∑ t ∈ Finset.range T, (listAccessCost (π t) (req t) : ℝ)) ≤ 2 * ((∑ t ∈ Finset.range T, (listAccessCost (σ t) (req t) : ℝ)) + (∑ t ∈ Finset.range T, (paid t : ℝ))) := by let Φ : ℕ → ℝ := fun t => (listInversions (π t) (σ t) : ℝ) have hΦ0 : Φ 0 = 0 := by dsimp only [Φ, listInversions]; rw [hinit] have hemp : Finset.univ.filter (fun p : Fin n × Fin n => (σ 0 p.1).1 < (σ 0 p.2).1 ∧ (σ 0 p.2).1 < (σ 0 p.1).1) = ∅ := by rw [Finset.filter_eq_empty_iff]; intro _ _ ⟨h1, h2⟩; omega rw [hemp, Finset.card_empty, Nat.cast_zero] have hΦnn : ∀ t, 0 ≤ Φ t := fun t => Nat.cast_nonneg _ have hstep : ∀ t < T, amortizedCost Φ (listAccessCost (π t) (req t) : ℝ) t (t + 1) ≤ 2 * (listAccessCost (σ t) (req t) : ℝ) + (paid t : ℝ) - 1 := by intro t ht have h1 := mtf_single_access_amortized (π t) (σ t) (req t) dsimp only [amortizedCost] at h1 ⊢ rw [← hmtf t ht] at h1 have h2 : (listInversions (π (t + 1)) (σ (t + 1)) : ℝ) ≤ (listInversions (π (t + 1)) (σ t) : ℝ) + (paid t : ℝ) := by exact_mod_cast hadv t ht dsimp only [Φ] linarith have htel := tarjan_potential_nonneg_upper_bound Φ id (fun t => (listAccessCost (π t) (req t) : ℝ)) T hΦ0 hΦnn have hsum : ∑ t ∈ Finset.range T, amortizedCost Φ (listAccessCost (π t) (req t) : ℝ) t (t + 1) ≤ 2 * (∑ t ∈ Finset.range T, (listAccessCost (σ t) (req t) : ℝ)) + (∑ t ∈ Finset.range T, (paid t : ℝ)) - (T : ℝ) := by calc ∑ t ∈ Finset.range T, amortizedCost Φ (listAccessCost (π t) (req t) : ℝ) t (t + 1) ≤ ∑ t ∈ Finset.range T, (2 * (listAccessCost (σ t) (req t) : ℝ) + (paid t : ℝ) - 1) := Finset.sum_le_sum (fun t ht => hstep t (Finset.mem_range.mp ht)) _ = 2 * (∑ t ∈ Finset.range T, (listAccessCost (σ t) (req t) : ℝ)) + (∑ t ∈ Finset.range T, (paid t : ℝ)) - (T : ℝ) := by rw [Finset.sum_sub_distrib, Finset.sum_add_distrib, ← Finset.mul_sum]; simp simp only [id] at htel have hmain : ∑ t ∈ Finset.range T, (listAccessCost (π t) (req t) : ℝ) ≤ 2 * (∑ t ∈ Finset.range T, (listAccessCost (σ t) (req t) : ℝ)) + (∑ t ∈ Finset.range T, (paid t : ℝ)) - (T : ℝ) := le_trans htel hsum have hp_nn : 0 ≤ ∑ t ∈ Finset.range T, (paid t : ℝ) := Finset.sum_nonneg (fun _ _ => Nat.cast_nonneg _) have hT_nn : (0 : ℝ) ≤ (T : ℝ) := Nat.cast_nonneg T exact ⟨hmain, by linarith⟩ /-! ### Pillar 4B: Exact `2 - 2 / (n + 1)` Cyclic Ratio & Transpose `Ω(n)` Separation -/ /-- **EXACT CYCLIC-ADVERSARY COMPETITIVE RATIO `2 - 2 / (n + 1)`**: Against the cyclic worst-case access sequence over `n ≥ 1` keys (repeating `0, 1, ..., n - 1`), MTF pays `n` per access while any static list pays average `(n + 1) / 2` per round, yielding EXACT ratio **`n / ((n + 1) / 2) = 2 - 2 / (n + 1)`** (approaching `2` sharply as `n → ∞`)! -/ theorem mtf_cyclic_adversary_ratio_exact {n : ℕ} (hn : 1 ≤ n) : (∑ i ∈ Finset.range n, (i + 1 : ℝ)) = (n : ℝ) * ((n : ℝ) + 1) / 2 ∧ ((n : ℝ) * (n : ℝ)) / ((n : ℝ) * ((n : ℝ) + 1) / 2) = 2 - 2 / ((n : ℝ) + 1) := by have hsum : ∀ m : ℕ, ∑ i ∈ Finset.range m, ((i : ℝ) + 1) = (m : ℝ) * ((m : ℝ) + 1) / 2 := by intro m induction m with | zero => simp | succ k ih => rw [Finset.sum_range_succ, ih]; push_cast; ring have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) have hn1pos : (0 : ℝ) < (n : ℝ) + 1 := by positivity refine ⟨hsum n, ?_⟩ field_simp [ne_of_gt hnpos, ne_of_gt hn1pos] ring /-- **EXACT `Ω(n)` SEPARATION: MTF VS. TRANSPOSE HEURISTIC**: On `T` alternating accesses to the last two elements of an `n`-element list (`n ≥ 4`): - Transpose swaps the last two elements on EVERY access and pays `n` EVERY time (`T · n` total); - MTF moves both elements to positions `{1, 2}` after the first 2 accesses and pays `≤ 2` on every subsequent access (`≤ 2 n + 2 T` total), strictly beating Transpose whenever `T ≥ 2 n`! -/ theorem mtf_strictly_beats_transpose_omega_n {n T : ℕ} (hn : 4 ≤ n) (hT : 2 * n ≤ T) : 2 * n + 2 * T < T * n := by nlinarith /-! ### Pillar 4C: Wilber's First Bound & Exact Bit-Reversal `(k - 1) · 2^k + 1` Alternation Count -/ /-- Alternation count of a finite `Bool` sequence `f : Fin m → Bool`: number of adjacent indices `t` (`t + 1 < m`) with `f t ≠ f (t + 1)`. -/ def altCount {m : ℕ} (f : Fin m → Bool) : ℕ := (Finset.univ.filter (fun t : Fin (m - 1) => f ⟨t.1, by omega⟩ ≠ f ⟨t.1 + 1, by omega⟩)).card /-- A strictly alternating sequence `t ↦ (t % 2 == 1)` of length `m ≥ 1` has EXACT alternation count `m - 1`! -/ theorem altCount_parity_exact {m : ℕ} (hm : 1 ≤ m) : altCount (fun t : Fin m => decide (t.1 % 2 = 1)) = m - 1 := by dsimp only [altCount] have hall : Finset.univ.filter (fun t : Fin (m - 1) => decide ((⟨t.1, by omega⟩ : Fin m).1 % 2 = 1) ≠ decide ((⟨t.1 + 1, by omega⟩ : Fin m).1 % 2 = 1)) = Finset.univ := by ext t simp only [Finset.mem_filter, Finset.mem_univ, true_and, iff_true] intro heq have h1 : t.1 % 2 = 1 ↔ (t.1 + 1) % 2 = 1 := by simpa using congrArg (fun b => b = true) heq omega rw [hall, Finset.card_univ, Fintype.card_fin] /-- Exact `k`-bit reversal function: reverses the low `k` bits of `x`. -/ def bitRev : ℕ → ℕ → ℕ | 0, _ => 0 | k + 1, x => (x % 2) * 2 ^ k + bitRev k (x / 2) /-- `bitRev k x < 2 ^ k` for EVERY `k, x : ℕ`. -/ theorem bitRev_lt (k x : ℕ) : bitRev k x < 2 ^ k := by induction k generalizing x with | zero => simp [bitRev] | succ k ih => dsimp only [bitRev] have h1 := ih (x / 2) have hmod : x % 2 ≤ 1 := by omega rw [pow_succ] nlinarith /-- Dual high-bit recurrence for `bitRev`: for `x < 2^(k + 1)`, `bitRev (k + 1) x = 2 * bitRev k (x % 2^k) + x / 2^k`! -/ lemma bitRev_dual_recurrence (k x : ℕ) (hx : x < 2 ^ (k + 1)) : bitRev (k + 1) x = 2 * bitRev k (x % 2 ^ k) + x / 2 ^ k := by induction k generalizing x with | zero => simp only [bitRev, pow_zero, Nat.div_one, mul_one, mul_zero, zero_add] have hx2 : x < 2 := by simpa using hx omega | succ k ih => have hx2 : x / 2 < 2 ^ (k + 1) := by rw [pow_succ'] at hx; omega have hih := ih (x / 2) hx2 have hmod_div : (x % 2 ^ (k + 1)) / 2 = (x / 2) % 2 ^ k := by rw [pow_succ, Nat.mod_mul_left_div_self] have hmod2 : (x % 2 ^ (k + 1)) % 2 = x % 2 := by have hdvd : 2 ∣ 2 ^ (k + 1) := by rw [pow_succ']; exact dvd_mul_right 2 _ exact Nat.mod_mod_of_dvd x hdvd have hdiv2 : (x / 2) / 2 ^ k = x / 2 ^ (k + 1) := by rw [Nat.div_div_eq_div_mul, ← pow_succ'] calc bitRev (k + 2) x = (x % 2) * 2 ^ (k + 1) + bitRev (k + 1) (x / 2) := rfl _ = (x % 2) * 2 ^ (k + 1) + (2 * bitRev k ((x / 2) % 2 ^ k) + (x / 2) / 2 ^ k) := by rw [hih] _ = 2 * ((x % 2) * 2 ^ k + bitRev k ((x / 2) % 2 ^ k)) + (x / 2) / 2 ^ k := by rw [pow_succ']; ring _ = 2 * bitRev (k + 1) (x % 2 ^ (k + 1)) + x / 2 ^ (k + 1) := by rw [← hdiv2]; congr 2 change (x % 2) * 2 ^ k + bitRev k ((x / 2) % 2 ^ k) = ((x % 2 ^ (k + 1)) % 2) * 2 ^ k + bitRev k ((x % 2 ^ (k + 1)) / 2) rw [hmod2, hmod_div] /-- **BIT-REVERSAL EXACT INVOLUTION & BIJECTION ON `{0, ..., 2^k - 1}`**: For EVERY `k` and `x < 2^k`, `bitRev k (bitRev k x) = x`! -/ theorem bitRev_involution (k x : ℕ) (hx : x < 2 ^ k) : bitRev k (bitRev k x) = x := by induction k generalizing x with | zero => simp only [pow_zero] at hx have hx0 : x = 0 := by omega subst hx0; rfl | succ k ih => have hdual := bitRev_dual_recurrence k x hx set y := bitRev (k + 1) x have hdiv_lt : x / 2 ^ k < 2 := by rw [pow_succ'] at hx exact Nat.div_lt_of_lt_mul (by linarith) have hy_mod : y % 2 = x / 2 ^ k := by rw [hdual, Nat.mul_add_mod, Nat.mod_eq_of_lt hdiv_lt] have hy_div : y / 2 = bitRev k (x % 2 ^ k) := by rw [hdual, Nat.mul_add_div (by omega : 0 < 2), Nat.div_eq_of_lt hdiv_lt, add_zero] have hmod_lt : x % 2 ^ k < 2 ^ k := Nat.mod_lt x (pow_pos (by omega) k) calc bitRev (k + 1) y = (y % 2) * 2 ^ k + bitRev k (y / 2) := rfl _ = (x / 2 ^ k) * 2 ^ k + bitRev k (bitRev k (x % 2 ^ k)) := by rw [hy_mod, hy_div] _ = (x / 2 ^ k) * 2 ^ k + x % 2 ^ k := by rw [ih (x % 2 ^ k) hmod_lt] _ = x := by rw [mul_comm, Nat.div_add_mod] /-- Exact bit-splitting identity for `bitRev`: for `r < 2^d`, `bitRev (d + a) (t * 2^d + r) = bitRev d r * 2^a + bitRev a t`! -/ lemma bitRev_add_mul_two_pow (d a r t : ℕ) (hr : r < 2 ^ d) : bitRev (d + a) (t * 2 ^ d + r) = bitRev d r * 2 ^ a + bitRev a t := by induction d generalizing r with | zero => simp only [pow_zero] at hr have hr0 : r = 0 := by omega subst hr0; simp [bitRev] | succ d ih => have hsucc : d + 1 + a = (d + a) + 1 := by omega rw [hsucc, bitRev] have hpow : 2 ^ (d + 1) = 2 * 2 ^ d := pow_succ' 2 d have hmod : (t * 2 ^ (d + 1) + r) % 2 = r % 2 := by rw [hpow] have : t * (2 * 2 ^ d) + r = r + 2 * (t * 2 ^ d) := by ring rw [this, Nat.add_mul_mod_self_left] have hdiv : (t * 2 ^ (d + 1) + r) / 2 = t * 2 ^ d + r / 2 := by rw [hpow] have : t * (2 * 2 ^ d) + r = r + 2 * (t * 2 ^ d) := by ring rw [this, Nat.add_mul_div_left _ _ (by omega : 0 < 2)]; omega have hr2 : r / 2 < 2 ^ d := by rw [hpow] at hr; omega rw [hmod, hdiv, ih (r / 2) hr2] dsimp only [bitRev] rw [pow_add] ring /-- **BIT-REVERSAL EXACT BLOCK-PARITY ALTERNATION LEMMA**: At EVERY reference-tree level `d < k` and block `b < 2^d`, the `t`-th element (`0 ≤ t < 2^(k-d)`) landing in block `b` is `x_t = t * 2^d + bitRev d b`, and: 1. Its block index is EXACTLY `b` (`bitRev k x_t / 2^(k - d) = b`); 2. Its left/right half-block bit is EXACTLY `t % 2` (strictly alternating `0, 1, 0, 1, ...`!)! -/ theorem bitRev_block_exact_alternation (k d b t : ℕ) (hd : d < k) (hb : b < 2 ^ d) : let x := t * 2 ^ d + bitRev d b bitRev k x / 2 ^ (k - d) = b ∧ (bitRev k x / 2 ^ (k - d - 1)) % 2 = t % 2 := by intro x have hk_eq : d + (k - d) = k := by omega have hx_rev : bitRev k x = b * 2 ^ (k - d) + bitRev (k - d) t := by dsimp only [x] conv_lhs => rw [← hk_eq] rw [bitRev_add_mul_two_pow d (k - d) (bitRev d b) t (bitRev_lt d b), bitRev_involution d b hb] have h1 : bitRev k x / 2 ^ (k - d) = b := by rw [hx_rev] have hlt := bitRev_lt (k - d) t have hpos : 0 < 2 ^ (k - d) := pow_pos (by omega) _ rw [add_comm, Nat.add_mul_div_right _ _ hpos, Nat.div_eq_of_lt hlt, zero_add] have h2 : (bitRev k x / 2 ^ (k - d - 1)) % 2 = t % 2 := by set m := k - d - 1 have hkd : k - d = m + 1 := by omega have hstep : bitRev (m + 1) t = (t % 2) * 2 ^ m + bitRev m (t / 2) := rfl have hpow : 2 ^ (m + 1) = 2 * 2 ^ m := pow_succ' 2 m have hlt := bitRev_lt m (t / 2) have hpos : 0 < 2 ^ m := pow_pos (by omega) m have hdecomp : bitRev k x = bitRev m (t / 2) + (2 * b + t % 2) * 2 ^ m := by rw [hx_rev, hkd, hstep, hpow]; ring rw [hdecomp, Nat.add_mul_div_right _ _ hpos, Nat.div_eq_of_lt hlt, zero_add] omega exact ⟨h1, h2⟩ /-- **WILBER'S FIRST BST TRANSITION-NODE CHARGING LOWER BOUND**: If a dynamic BST charges at least `1` unit to every left/right transition at every node `v` of a fixed static reference tree `V` (whose left/right alternations on sequence `X` are `alt(v)`), and each tree operation touches at most `1` transition event per reference node (disjoint transition points by Wilber's Lemma), then total BST cost is AT LEAST `∑ v ∈ V, alt(v)`! -/ theorem wilber1_bst_transition_charging_lower_bound {ι : Type*} (V : Finset ι) (alt nodeCost : ι → ℕ) (totalCost : ℕ) (hcharge : ∀ v ∈ V, alt v ≤ nodeCost v) (hdisj : ∑ v ∈ V, nodeCost v ≤ totalCost) : ∑ v ∈ V, alt v ≤ totalCost := le_trans (Finset.sum_le_sum hcharge) hdisj /-! =============================================================================== PART 5: DATA-STRUCTURE CELL-PROBE & INFORMATION-THEORETIC LOWER BOUNDS (FENWICK TREE `O(log n)` EXACT UPPER BOUND VS. FREDMAN–SAKS CHRONOGRAM) =============================================================================== -/ /-! ### Pillar 5A: Exact Fenwick Tree (Binary Indexed Tree) `O(log n)` Upper Bound -/ /-- Exact lowest set bit (`lowbit x = 2^{v_2(x)}`, with `lowbit 0 = 0`). -/ def lowbit : ℕ → ℕ | 0 => 0 | n + 1 => if (n + 1) % 2 = 1 then 1 else 2 * lowbit ((n + 1) / 2) private lemma lowbit_double (k : ℕ) : lowbit (2 * k) = 2 * lowbit k := by cases k with | zero => simp [lowbit] | succ k' => have h : 2 * (k' + 1) = (2 * k' + 1) + 1 := by omega rw [h, lowbit] have hmod : (2 * k' + 1 + 1) % 2 ≠ 1 := by omega have hdiv : (2 * k' + 1 + 1) / 2 = k' + 1 := by omega rw [if_neg hmod, hdiv] private lemma lowbit_double_add_one (k : ℕ) : lowbit (2 * k + 1) = 1 := by rw [lowbit] have hmod : (2 * k + 1) % 2 = 1 := by omega rw [if_pos hmod] /-- Exact core properties of `lowbit`: for every `x ≥ 1`, `0 < lowbit x`, `lowbit x ≤ x`, `lowbit x ∣ x`, and `2 * lowbit x ∣ x + lowbit x`! -/ theorem lowbit_properties {x : ℕ} (hx : 1 ≤ x) : 0 < lowbit x ∧ lowbit x ≤ x ∧ lowbit x ∣ x ∧ 2 * lowbit x ∣ x + lowbit x := by induction x using Nat.strong_induction_on with | h x ih => rcases Nat.even_or_odd x with ⟨k, hk⟩ | ⟨k, hk⟩ · have hx2 : x = 2 * k := by omega subst hx2 have hk1 : 1 ≤ k := by omega have hih := ih k (by omega) hk1 rw [lowbit_double] refine ⟨by omega, by omega, ?_, ?_⟩ · obtain ⟨q, hq⟩ := hih.2.2.1; exact ⟨q, by (conv_lhs => rw [hq]); ring⟩ · obtain ⟨q, hq⟩ := hih.2.2.2; exact ⟨q, by linarith⟩ · subst hk rw [lowbit_double_add_one] exact ⟨by omega, by omega, one_dvd _, ⟨k + 1, by omega⟩⟩ /-- **EXACT FENWICK QUERY POPCOUNT DROP (`popcount(x - lowbit x) + 1 = popcount x`)**: Clearing the lowest set bit reduces binary popcount by EXACTLY `1`! -/ theorem fenwick_sub_lowbit_popcount {x : ℕ} (hx : 1 ≤ x) : popcountNat (x - lowbit x) + 1 = popcountNat x := by induction x using Nat.strong_induction_on with | h x ih => rcases Nat.even_or_odd x with ⟨k, hk⟩ | ⟨k, hk⟩ · have hx2 : x = 2 * k := by omega subst hx2 have hk1 : 1 ≤ k := by omega have hih := ih k (by omega) hk1 have hlb := (lowbit_properties hk1).2.1 rw [lowbit_double, popcountNat_double] have hsub : 2 * k - 2 * lowbit k = 2 * (k - lowbit k) := by omega rw [hsub, popcountNat_double] exact hih · subst hk rw [lowbit_double_add_one, popcountNat_double_add_one] have hsub : 2 * k + 1 - 1 = 2 * k := by omega rw [hsub, popcountNat_double] omega /-- Fenwick BIT cell storing the range sum `∑_{i ∈ (x - lowbit x, x]} a i`. -/ def fenwickCell {R : Type*} [AddCommMonoid R] (a : ℕ → R) (x : ℕ) : R := ∑ i ∈ Finset.Ioc (x - lowbit x) x, a i /-- Recursive Fenwick prefix-sum query `fenwickQuery a x` summing `fenwickCell` along `x ↦ x - lowbit x`. -/ def fenwickQuery {R : Type*} [AddCommMonoid R] (a : ℕ → R) : ℕ → R | 0 => 0 | n + 1 => have hlt : n + 1 - lowbit (n + 1) < n + 1 := by have hp := (lowbit_properties (by omega : 1 ≤ n + 1)).1; omega fenwickCell a (n + 1) + fenwickQuery a (n + 1 - lowbit (n + 1)) /-- Exact step count (number of cells probed) by `fenwickQuery` on index `x`. -/ def fenwickQuerySteps : ℕ → ℕ | 0 => 0 | n + 1 => have hlt : n + 1 - lowbit (n + 1) < n + 1 := by have hp := (lowbit_properties (by omega : 1 ≤ n + 1)).1; omega 1 + fenwickQuerySteps (n + 1 - lowbit (n + 1)) /-- **FENWICK TREE EXACT PREFIX-SUM CORRECTNESS OVER ANY `AddCommMonoid R`**: For EVERY array `a : ℕ → R` and EVERY prefix index `x : ℕ`: `fenwickQuery a x = ∑ i ∈ Finset.Ioc 0 x, a i`! -/ theorem fenwick_prefix_sum_exact {R : Type*} [AddCommMonoid R] (a : ℕ → R) (x : ℕ) : fenwickQuery a x = ∑ i ∈ Finset.Ioc 0 x, a i := by induction x using Nat.strong_induction_on with | h x ih => cases x with | zero => simp [fenwickQuery] | succ n => have hpos := (lowbit_properties (by omega : 1 ≤ n + 1)).1 have hle := (lowbit_properties (by omega : 1 ≤ n + 1)).2.1 have hlt : n + 1 - lowbit (n + 1) < n + 1 := by omega rw [fenwickQuery, ih _ hlt] dsimp only [fenwickCell] rw [add_comm] have hpart : Finset.Ioc 0 (n + 1 - lowbit (n + 1)) ∪ Finset.Ioc (n + 1 - lowbit (n + 1)) (n + 1) = Finset.Ioc 0 (n + 1) := by ext i; simp only [Finset.mem_union, Finset.mem_Ioc]; omega have hdisj : Disjoint (Finset.Ioc 0 (n + 1 - lowbit (n + 1))) (Finset.Ioc (n + 1 - lowbit (n + 1)) (n + 1)) := by rw [Finset.disjoint_left]; intro i h1 h2 simp only [Finset.mem_Ioc] at h1 h2; omega rw [← Finset.sum_union hdisj, hpart] /-- **FENWICK QUERY EXACT STEP COUNT = `popcount(x)` AND `≤ k` FOR ALL `x < 2^k`**: Every Fenwick prefix query on `x < 2^k` probes EXACTLY `popcountNat x ≤ k` memory cells! -/ theorem fenwick_query_steps_eq_popcount (x : ℕ) : fenwickQuerySteps x = popcountNat x := by induction x using Nat.strong_induction_on with | h x ih => cases x with | zero => simp [fenwickQuerySteps, popcountNat] | succ n => have hpos := (lowbit_properties (by omega : 1 ≤ n + 1)).1 have hlt : n + 1 - lowbit (n + 1) < n + 1 := by omega rw [fenwickQuerySteps, ih _ hlt] have hdrop := fenwick_sub_lowbit_popcount (by omega : 1 ≤ n + 1) omega /-- Binary popcount of any `x < 2^k` is at most `k`! -/ theorem popcountNat_le_of_lt_two_pow {k x : ℕ} (hx : x < 2 ^ k) : popcountNat x ≤ k := by induction k generalizing x with | zero => simp only [pow_zero] at hx have hx0 : x = 0 := by omega subst hx0; simp [popcountNat] | succ k ih => rcases Nat.even_or_odd x with ⟨q, hq⟩ | ⟨q, hq⟩ · have hx2 : x = 2 * q := by omega subst hx2 rw [popcountNat_double] have hqlt : q < 2 ^ k := by rw [pow_succ'] at hx; omega have := ih hqlt; omega · subst hq rw [popcountNat_double_add_one] have hqlt : q < 2 ^ k := by rw [pow_succ'] at hx; omega have := ih hqlt; omega /-- **FENWICK UPDATE CHAIN DOUBLING (`2 * lowbit x ≤ lowbit (x + lowbit x)`)**: Every Fenwick update step `x ↦ x + lowbit x` AT LEAST DOUBLES `lowbit`, so starting from any `1 ≤ x ≤ N` (`N < 2^k`), at most `k` update steps stay within `[1, N]`! -/ theorem fenwick_update_doubles_lowbit {x : ℕ} (hx : 1 ≤ x) : 2 * lowbit x ≤ lowbit (x + lowbit x) := by have hdvd : 2 * lowbit x ∣ x + lowbit x := (lowbit_properties hx).2.2.2 have hpos : 0 < x + lowbit x := by have := (lowbit_properties hx).1; omega -- Prove general fact: for any `a, y` with `2^a ∣ y` and `y ≥ 1`, `2^a ≤ lowbit y` — here just use -- strong induction on `x` directly! clear hdvd hpos induction x using Nat.strong_induction_on with | h x ih => rcases Nat.even_or_odd x with ⟨k, hk⟩ | ⟨k, hk⟩ · have hx2 : x = 2 * k := by omega subst hx2 have hk1 : 1 ≤ k := by omega have hih := ih k (by omega) hk1 rw [lowbit_double] have hsum : 2 * k + 2 * lowbit k = 2 * (k + lowbit k) := by omega rw [hsum, lowbit_double] omega · subst hk rw [lowbit_double_add_one] have hsum : 2 * k + 1 + 1 = 2 * (k + 1) := by omega rw [hsum, lowbit_double] have hp := (lowbit_properties (by omega : 1 ≤ k + 1)).1 omega /-! ### Pillar 5B: Cell-Probe Decision-Tree Capacity & Fredman–Saks Chronogram Lower Bounds -/ /-- **CELL-PROBE DECISION-TREE CAPACITY LOWER BOUND (`M ≤ 2^(w · t_q)`)**: Any adaptive `t_q`-probe query algorithm reading `w`-bit memory cells (`Fin (2^w)` values per cell) can distinguish at most `(2^w)^t_q = 2^(w · t_q)` distinct query outputs! Hence distinguishing `M` distinct outputs forces **`log₂ M ≤ w · t_q`** (i.e., `t_q ≥ log₂ M / w`)! -/ theorem cell_probe_decision_tree_capacity {w t_q M : ℕ} (_hw : 1 ≤ w) (hM : 1 ≤ M) (hdist : ∃ f : (Fin t_q → Fin (2 ^ w)) → Fin M, Function.Surjective f) : M ≤ 2 ^ (w * t_q) ∧ log2Rank (M : ℝ) ≤ (w : ℝ) * (t_q : ℝ) := by obtain ⟨f, hsurj⟩ := hdist have hcard : Fintype.card (Fin M) ≤ Fintype.card (Fin t_q → Fin (2 ^ w)) := Fintype.card_le_of_surjective f hsurj rw [Fintype.card_fin, Fintype.card_fun, Fintype.card_fin, Fintype.card_fin, ← pow_mul] at hcard refine ⟨hcard, ?_⟩ have hR : (M : ℝ) ≤ (2 : ℝ) ^ (w * t_q) := by exact_mod_cast hcard have hlog := log2Rank_le_log2Rank (Nat.cast_pos.mpr (by omega)) hR rw [log2Rank_two_pow, Nat.cast_mul] at hlog exact hlog /-- **FREDMAN–SAKS (STOC 1989) CHRONOGRAM EPOCH OVERWRITE ACCOUNTING**: In `t` exponentially growing chronogram epochs of sizes `β^1, β^2, ..., β^t` (`β ≥ 2`): 1. Total updates in ALL later epochs `1, ..., j - 1` after epoch `j` is `∑_{i=1}^{j-1} β^i = (β^j - β) / (β - 1)` (strictly `< β^j / (β - 1)`); 2. If each update writes `≤ t_u` cells of `w` bits and `2 · t_u · w < β - 1`, then ALL later epochs combined overwrite STRICTLY LESS than `(1 / 2) · β^j` bits (leaving `> β^j / 2` surviving bits of epoch `j` untouched by later epochs)! -/ theorem fredman_saks_chronogram_overwrite_bound {β t_u w j : ℕ} (hβ : 2 ≤ β) (hgap : 2 * t_u * w < β - 1) (hj : 1 ≤ j) : (β - 1) * (∑ i ∈ Finset.Ico 1 j, β ^ i) + β = β ^ j ∧ 2 * (t_u * w * ∑ i ∈ Finset.Ico 1 j, β ^ i) < β ^ j := by have hgeom : (β - 1) * (∑ i ∈ Finset.Ico 1 j, β ^ i) + β = β ^ j := by induction j with | zero => omega | succ k ih => rcases eq_or_lt_of_le (Nat.zero_le k) with rfl | hkpos · simp · have hprev := ih hkpos rw [Finset.sum_Ico_succ_top hkpos, mul_add] have hstep : (β - 1) * β ^ k + β ^ k = β ^ (k + 1) := by rw [pow_succ'] cases β with | zero => omega | succ b => simp only [Nat.add_sub_cancel]; ring change (β - 1) * (∑ i ∈ Finset.Ico 1 k, β ^ i) + (β - 1) * β ^ k + β = β ^ (k + 1) omega refine ⟨hgeom, ?_⟩ set S := ∑ i ∈ Finset.Ico 1 j, β ^ i have h1 : 2 * (t_u * w * S) = (2 * t_u * w) * S := by ring have h2 : (2 * t_u * w) * S ≤ (β - 1) * S := Nat.mul_le_mul_right S (le_of_lt hgap) omega /-- **FREDMAN–SAKS CHRONOGRAM EXACT LOGARITHMIC TRADEOFF (`t + 1 > log n / log β`)**: If total updates `n = ∑_{j=1}^t β^j` (`β ≥ 2`, `n ≥ 1`), then: 1. `n < β ^ (t + 1)` (exact exponential upper bound on `t` epochs); 2. `Real.log n / Real.log β < t + 1`; 3. Consequently, any cell-probe algorithm forced to probe `≥ c · t` epochs (`c > 0`) satisfies **`t_q + c > c · (Real.log n / Real.log β)`** (`Ω(log n / log(t_u · w))`)! -/ theorem fredman_saks_chronogram_log_tradeoff {β t n : ℕ} (hβ : 2 ≤ β) (hn : 1 ≤ n) (hepochs : n ≤ ∑ j ∈ Finset.Ico 1 (t + 1), β ^ j) {c t_q : ℝ} (hc : 0 < c) (hprobe : c * (t : ℝ) ≤ t_q) : n < β ^ (t + 1) ∧ Real.log (n : ℝ) / Real.log (β : ℝ) < (t : ℝ) + 1 ∧ c * (Real.log (n : ℝ) / Real.log (β : ℝ)) < t_q + c := by have hgeom := (fredman_saks_chronogram_overwrite_bound (t_u := 0) (w := 0) hβ (by omega) (by omega : 1 ≤ t + 1)).1 have hS_lt : ∑ j ∈ Finset.Ico 1 (t + 1), β ^ j < β ^ (t + 1) := by have h1 : 1 * (∑ j ∈ Finset.Ico 1 (t + 1), β ^ j) ≤ (β - 1) * (∑ j ∈ Finset.Ico 1 (t + 1), β ^ j) := Nat.mul_le_mul_right _ (by omega) omega have hn_lt : n < β ^ (t + 1) := lt_of_le_of_lt hepochs hS_lt have hβ_gt1 : (1 : ℝ) < (β : ℝ) := by exact_mod_cast (by omega : 1 < β) have hlogβ_pos : 0 < Real.log (β : ℝ) := Real.log_pos hβ_gt1 have hlog_lt : Real.log (n : ℝ) / Real.log (β : ℝ) < (t : ℝ) + 1 := by rw [div_lt_iff₀ hlogβ_pos] have hR : (n : ℝ) < (β : ℝ) ^ (t + 1) := by exact_mod_cast hn_lt have hlog := Real.log_lt_log (Nat.cast_pos.mpr (by omega)) hR rw [Real.log_pow] at hlog push_cast at hlog linarith have hmul : c * (Real.log (n : ℝ) / Real.log (β : ℝ)) < c * ((t : ℝ) + 1) := mul_lt_mul_of_pos_left hlog_lt hc exact ⟨hn_lt, hlog_lt, by linarith⟩ /-! =============================================================================== PART 5.5: SHARP BOUNDARY COROLLARIES, CONCRETE WITNESSES & GRAND UNIFICATION =============================================================================== -/ /-! ### Part 5.5A: Potential Method, Binary Counter, Dynamic Table & Fibonacci Heap Corollaries -/ /-- Exact amortized cost on zero-step sequence (`T = 0`) vanishes. -/ theorem tarjan_potential_zero_steps {S : Type*} (Φ : S → ℝ) (s : ℕ → S) (c : ℕ → ℝ) : ∑ t ∈ Finset.range 0, amortizedCost Φ (c t) (s t) (s (t + 1)) = 0 := by simp /-- Scaling potential by `α` scales potential change by `α`. -/ theorem tarjan_potential_scale {S : Type*} (Φ : S → ℝ) (α c : ℝ) (s s' : S) : amortizedCost (fun x => α * Φ x) c s s' = c + α * (Φ s' - Φ s) := by dsimp only [amortizedCost]; ring /-- Popcount of `0` is `0`, `1` is `1`, `2^k` is `1`, and `2^k - 1` is `k`! -/ theorem popcountNat_powers_of_two_exact (k : ℕ) : popcountNat 0 = 0 ∧ popcountNat (2 ^ k) = 1 ∧ popcountNat (2 ^ k - 1) = k := by have hpow : ∀ m : ℕ, popcountNat (2 ^ m) = 1 := by intro m; induction m with | zero => rw [pow_zero]; exact (popcountNat_double_add_one 0).trans (by simp [popcountNat]) | succ m ih => rw [pow_succ', popcountNat_double, ih] have hpred : ∀ m : ℕ, popcountNat (2 ^ m - 1) = m := by intro m; induction m with | zero => simp [popcountNat] | succ m ih => have hpos : 1 ≤ 2 ^ m := Nat.one_le_pow m 2 (by omega) have heq : 2 ^ (m + 1) - 1 = 2 * (2 ^ m - 1) + 1 := by rw [pow_succ']; omega rw [heq, popcountNat_double_add_one, ih]; omega exact ⟨by simp [popcountNat], hpow k, hpred k⟩ /-- Exact total bit-flips for `T = 2^k` increments is `2^(k + 1) - 1` (strictly `< 2^(k + 1)`). -/ theorem binary_counter_two_pow_total_flips (k : ℕ) : ∑ t ∈ Finset.range (2 ^ k), bitFlips t = 2 ^ (k + 1) - 1 := by have h := (binary_counter_total_flips_exact (2 ^ k)).1 rw [(popcountNat_powers_of_two_exact k).2.1] at h rw [pow_succ']; omega /-- Dynamic table hysteresis potential vanishes at `(0, 0)` and equals `m` at full table `(m, m)`. -/ theorem dynTableHystPot_boundary_values (m : ℕ) : dynTableHystPot 0 0 = 0 ∧ dynTableHystPot m m = (m : ℝ) := by refine ⟨by simp [dynTableHystPot], ?_⟩ dsimp only [dynTableHystPot] rw [if_pos (by omega : m ≤ 2 * m)]; ring /-- Dynamic table hysteresis potential at quarter-full `(k, 4k)` equals exact `k` (paying the halving copy!). -/ theorem dynTableHystPot_quarter_full (k : ℕ) (hk : 1 ≤ k) : dynTableHystPot k (4 * k) = (k : ℝ) := by dsimp only [dynTableHystPot] rw [if_neg (by omega : ¬ 4 * k ≤ 2 * k)] push_cast; ring /-- Concrete Fibonacci-heap degree bound: `F_12 = 144 ≥ φ^10` (`k = 10` needs `≥ 144` nodes). -/ theorem fib_heap_deg10_needs_144_nodes : Nat.fib 12 = 144 ∧ goldenRatio ^ 10 ≤ (144 : ℝ) := by have hf : Nat.fib 12 = 144 := by decide refine ⟨hf, ?_⟩ have h := fib_ge_goldenRatio_pow 10 rw [show 10 + 2 = 12 by omega, hf] at h exact_mod_cast h /-- Concrete golden-ratio reciprocal identity `1 / φ = φ - 1`. -/ theorem goldenRatio_inv_eq_sub_one : 1 / goldenRatio = goldenRatio - 1 := by have hpos : 0 < goldenRatio := by linarith [goldenRatio_sq_eq_add_one.2.1] have hsq := goldenRatio_sq_eq_add_one.1 rw [div_eq_iff (ne_of_gt hpos)] linarith /-! ### Part 5.5B: Splay Tree Corollaries & Concrete Witnesses -/ /-- `log2Rank 1 = 0`, `log2Rank 2 = 1`, `log2Rank 4 = 2`, `log2Rank 16 = 4`, `log2Rank 32 = 5`. -/ theorem log2Rank_exact_powers : log2Rank 1 = 0 ∧ log2Rank 2 = 1 ∧ log2Rank 4 = 2 ∧ log2Rank 16 = 4 ∧ log2Rank 32 = 5 := by refine ⟨by simpa using log2Rank_two_pow 0, by simpa using log2Rank_two_pow 1, ?_, ?_, ?_⟩ · simpa [show (2 : ℝ) ^ 2 = 4 by norm_num] using log2Rank_two_pow 2 · simpa [show (2 : ℝ) ^ 4 = 16 by norm_num] using log2Rank_two_pow 4 · simpa [show (2 : ℝ) ^ 5 = 32 by norm_num] using log2Rank_two_pow 5 /-- Splay rank of a quotient: `log₂(a / b) = log₂ a - log₂ b` (`a, b > 0`). -/ theorem log2Rank_div {a b : ℝ} (ha : 0 < a) (hb : 0 < b) : log2Rank (a / b) = log2Rank a - log2Rank b := by dsimp only [log2Rank] rw [Real.log_div (ne_of_gt ha) (ne_of_gt hb), sub_div] /-- Splay rank is non-negative on `w ≥ 1`. -/ theorem log2Rank_nonneg {w : ℝ} (hw : 1 ≤ w) : 0 ≤ log2Rank w := by dsimp only [log2Rank] exact div_nonneg (Real.log_nonneg hw) (le_of_lt (Real.log_pos (by norm_num))) /-- Splay AM-GM equality witness: at balanced split `a = b = c / 2`, `log₂ a + log₂ b = 2 log₂ c - 2`! -/ theorem splay_log2_amgm_tight_at_midpoint {c : ℝ} (hc : 0 < c) : log2Rank (c / 2) + log2Rank (c / 2) = 2 * log2Rank c - 2 := by rw [log2Rank_div hc (by norm_num), log2Rank_exact_powers.2.1]; ring /-- Splay tree accessing the root (`r_0 = r_root`, `0` double steps, no zig) has amortized cost `≤ 1`! -/ theorem splay_root_access_cost_le_one (rRoot : ℝ) : 3 * (rRoot - rRoot) + 1 = 1 := by ring /-- Concrete Splay vs. unbalanced static BST separation at `n = 32, m = 32`: Splay `32 · (3 · 5 + 1) + 32 · 5 = 672 < 1024 = 32 · 32`! -/ theorem splay_separation_n32_m32_exact : (32 : ℝ) * (3 * log2Rank (32 : ℝ) + 1) + (32 : ℝ) * log2Rank (32 : ℝ) = 672 ∧ (672 : ℝ) < (32 : ℝ) * (32 : ℝ) := by rw [log2Rank_exact_powers.2.2.2.2]; norm_num /-- Static finger weight at the finger itself (`i = f`) gives amortized cost `≤ 3 log₂ 5 + 1 < 8`! -/ theorem splay_static_finger_at_finger_lt_eight : 3 * log2Rank 5 + 1 < 8 := by have h5lt : (5 : ℝ) < (2 : ℝ) ^ 3 := by norm_num have hlog : log2Rank 5 < 3 := by dsimp only [log2Rank] rw [div_lt_iff₀ (Real.log_pos (by norm_num))] have h := Real.log_lt_log (by norm_num : (0 : ℝ) < 5) h5lt rw [Real.log_pow] at h; push_cast at h; linarith -- Actually `5^3 = 125 < 128 = 2^7` ⇒ `3 log₂ 5 < 7` ⇒ `3 log₂ 5 + 1 < 8`! have h125 : (5 : ℝ) ^ 3 < (2 : ℝ) ^ 7 := by norm_num have hlog7 : 3 * log2Rank 5 < 7 := by dsimp only [log2Rank] have hlog2 := Real.log_pos (by norm_num : (1 : ℝ) < 2) have h := Real.log_lt_log (by positivity : (0 : ℝ) < (5 : ℝ) ^ 3) h125 rw [Real.log_pow, Real.log_pow] at h push_cast at h have hdiv : 3 * (Real.log 5 / Real.log 2) = (3 * Real.log 5) / Real.log 2 := by ring rw [hdiv, div_lt_iff₀ hlog2] linarith linarith /-! ### Part 5.5C: Union-Find, Tower, `log*` & Ackermann Corollaries -/ /-- Exact singleton union-tree has size `1`, height `0`, and satisfies `IsBySize`. -/ theorem unionTree_leaf_spec : UnionTree.leaf.IsBySize ∧ UnionTree.leaf.size = 1 ∧ UnionTree.leaf.height = 0 := ⟨trivial, rfl, rfl⟩ /-- Exact size & height of `binomialTree 10`: `1024` nodes, height `10` (`log₂ 1024 = 10`)! -/ theorem binomialTree_10_exact : (binomialTree 10).IsBySize ∧ (binomialTree 10).size = 1024 ∧ (binomialTree 10).height = 10 := by have h := binomial_tree_height_sharpness 10 exact ⟨h.1, by rw [h.2.1]; norm_num, h.2.2⟩ /-- Exact first 5 tower values: `1, 2, 4, 16, 65536`. -/ theorem tower_first_five_exact : tower 0 = 1 ∧ tower 1 = 2 ∧ tower 2 = 4 ∧ tower 3 = 16 ∧ tower 4 = 65536 := by refine ⟨by decide, by decide, by decide, by decide, ?_⟩ have ht3 : tower 3 = 16 := by decide change 2 ^ tower 3 = 65536; rw [ht3]; norm_num /-- `log* n ≤ 4` for ALL `n ≤ 65536`! -/ theorem logStar_le_four_up_to_65536 {n : ℕ} (hn : n ≤ 65536) : logStar n ≤ 4 := by calc logStar n ≤ logStar 65536 := logStar_monotone hn _ = 4 := logStar_exact_thresholds.2.2.2.2 /-- Exact diagonal Ackermann values `ack 0 0 = 1`, `ack 1 1 = 3`, `ack 2 2 = 7`, `ack 3 3 = 61`. -/ theorem ack_diag_first_four_exact : ack 0 0 = 1 ∧ ack 1 1 = 3 ∧ ack 2 2 = 7 ∧ ack 3 3 = 61 := by have h := ack_levels_zero_to_three_exact have h3 := h.2.2.2 3 exact ⟨h.1 0, h.2.1 1, h.2.2.1 2, by omega⟩ /-- Exact `invAck` evaluations: `invAck 1 = 0`, `invAck 3 = 1`, `invAck 7 = 2`, `invAck 61 = 3`. -/ theorem invAck_exact_thresholds : invAck 1 = 0 ∧ invAck 3 = 1 ∧ invAck 7 = 2 ∧ invAck 61 = 3 := by have hd := ack_diag_first_four_exact have h0 : invAck 1 = 0 := by have hle := (invAck_spec 1).2 0 (by omega) omega have h1 : invAck 3 = 1 := by have hle := (invAck_spec 3).2 1 (by omega) have hge := (invAck_spec 3).1 have hgt : 0 < invAck 3 := by by_contra h; push Not at h have h0' : invAck 3 = 0 := by omega rw [h0', hd.1] at hge; omega omega have h2 : invAck 7 = 2 := by have hle := (invAck_spec 7).2 2 (by omega) have hge := (invAck_spec 7).1 have hgt : 1 < invAck 7 := by by_contra h; push Not at h have h1' := ack_diag_strictMono.monotone h omega omega have h3 : invAck 61 = 3 := by have hle := (invAck_spec 61).2 3 (by omega) have hge := (invAck_spec 61).1 have hgt : 2 < invAck 61 := by by_contra h; push Not at h have h2' := ack_diag_strictMono.monotone h omega omega exact ⟨h0, h1, h2, h3⟩ /-- `invAck (tower 4) = 4`: even at `n = 65536`, `invAck n ≤ 4`! -/ theorem invAck_65536_eq_four : invAck 65536 = 4 := by have ht4 : tower 4 = 65536 := tower_first_five_exact.2.2.2.2 have hle : invAck 65536 ≤ 4 := (invAck_spec 65536).2 4 (by rw [← ht4]; exact tower_le_ack4 4) have hge := (invAck_spec 65536).1 have hgt : 3 < invAck 65536 := by by_contra h; push Not at h have h3 := ack_diag_strictMono.monotone h have hd3 := ack_diag_first_four_exact.2.2.2 omega omega /-- Strict separation of `invAck` vs `logStar` at `n = 61`: `invAck 61 = 3 < 4 = logStar 61`! -/ theorem invAck_strictly_lt_logStar_at_61 : invAck 61 = 3 ∧ logStar 61 = 4 ∧ invAck 61 < logStar 61 := by have hi := invAck_exact_thresholds.2.2.2 have hl_le : logStar 61 ≤ 4 := logStar_le_four_up_to_65536 (by omega) have hl_ge := (logStar_spec 61).1 have hl_gt : 3 < logStar 61 := by by_contra h; push Not at h have h3 := tower_strictMono.monotone h have ht3 := tower_first_five_exact.2.2.2.1 omega exact ⟨hi, by omega, by omega⟩ /-! ### Part 5.5D: Self-Organizing Lists (MTF), Wilber's First Bound & Bit-Reversal Corollaries -/ /-- Inversion potential is symmetric: `listInversions π σ = listInversions σ π`. -/ theorem listInversions_symm {n : ℕ} (π σ : Equiv.Perm (Fin n)) : listInversions π σ = listInversions σ π := by dsimp only [listInversions] refine Finset.card_bij (fun p _ => (p.2, p.1)) (fun p hp => by have h := (Finset.mem_filter.mp hp).2 exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, h.2, h.1⟩) (fun _ _ _ _ h => Prod.ext (Prod.mk.inj h).2 (Prod.mk.inj h).1) (fun q hq => ⟨(q.2, q.1), Finset.mem_filter.mpr ⟨Finset.mem_univ _, (Finset.mem_filter.mp hq).2.2, (Finset.mem_filter.mp hq).2.1⟩, rfl⟩) /-- Self-inversions vanish identically (`listInversions π π = 0`). -/ theorem listInversions_self {n : ℕ} (π : Equiv.Perm (Fin n)) : listInversions π π = 0 := by dsimp only [listInversions] rw [Finset.card_eq_zero, Finset.filter_eq_empty_iff] intro _ _ ⟨h1, h2⟩; omega /-- Every list access cost lies in `[1, n]`. -/ theorem listAccessCost_bounds {n : ℕ} (π : Equiv.Perm (Fin n)) (x : Fin n) : 1 ≤ listAccessCost π x ∧ listAccessCost π x ≤ n := by dsimp only [listAccessCost]; omega /-- Concrete MTF cyclic ratios: `n = 2 ⇒ 4/3`, `n = 3 ⇒ 3/2`, `n = 9 ⇒ 9/5`, and ALWAYS `< 2`! -/ theorem mtf_cyclic_ratio_strict_lt_two {n : ℕ} (hn : 1 ≤ n) : (2 : ℝ) - 2 / ((n : ℝ) + 1) < 2 := by have hpos : (0 : ℝ) < 2 / ((n : ℝ) + 1) := by positivity linarith /-- **WILBER'S FIRST EXACT TOTAL ALTERNATION SUM (`∑_{d < k} 2^d · (2^(k - d) - 1) = (k - 1) · 2^k + 1`)**: Summing the exact `2^(k - d) - 1` alternations across all `2^d` internal nodes at every level `0 ≤ d < k` of the balanced reference BST yields EXACTLY `(k - 1) · 2^k + 1`! -/ theorem wilber1_bit_reversal_total_alternations_exact {k : ℕ} (hk : 1 ≤ k) : ∑ d ∈ Finset.range k, 2 ^ d * (2 ^ (k - d) - 1) = (k - 1) * 2 ^ k + 1 := by have hterm : ∀ d ∈ Finset.range k, 2 ^ d * (2 ^ (k - d) - 1) = 2 ^ k - 2 ^ d := by intro d hd have hlt := Finset.mem_range.mp hd rw [Nat.mul_sub_left_distrib, mul_one, ← pow_add, Nat.add_sub_cancel' (le_of_lt hlt)] have hsum_pow : ∀ m : ℕ, ∑ d ∈ Finset.range m, 2 ^ d = 2 ^ m - 1 := by intro m; induction m with | zero => simp | succ m ih => rw [Finset.sum_range_succ, ih, pow_succ'] have hpos : 1 ≤ 2 ^ m := Nat.one_le_pow m 2 (by omega) omega have hle : ∀ d ∈ Finset.range k, 2 ^ d ≤ 2 ^ k := fun d hd => Nat.pow_le_pow_right (by omega) (le_of_lt (Finset.mem_range.mp hd)) have hsum_add : (∑ d ∈ Finset.range k, (2 ^ k - 2 ^ d)) + (∑ d ∈ Finset.range k, 2 ^ d) = k * 2 ^ k := by rw [← Finset.sum_add_distrib] calc ∑ d ∈ Finset.range k, (2 ^ k - 2 ^ d + 2 ^ d) = ∑ _d ∈ Finset.range k, 2 ^ k := Finset.sum_congr rfl (fun d hd => Nat.sub_add_cancel (hle d hd)) _ = k * 2 ^ k := by simp have hsp := hsum_pow k rw [Finset.sum_congr rfl hterm] have hpos : 1 ≤ 2 ^ k := Nat.one_le_pow k 2 (by omega) have hk_mul : k * 2 ^ k = (k - 1) * 2 ^ k + 2 ^ k := by cases k with | zero => omega | succ m => simp only [Nat.add_sub_cancel]; ring omega /-- Concrete bit-reversal exact bijection witnesses at `k = 3` (`N = 8`): `bitRev 3 1 = 4`, `bitRev 3 4 = 1`, `bitRev 3 3 = 6`, `bitRev 3 6 = 3`, and Wilber-1 = `17`! -/ theorem bitRev_k3_witness : bitRev 3 1 = 4 ∧ bitRev 3 4 = 1 ∧ bitRev 3 3 = 6 ∧ bitRev 3 6 = 3 ∧ ∑ d ∈ Finset.range 3, 2 ^ d * (2 ^ (3 - d) - 1) = 17 := by refine ⟨by decide, by decide, by decide, by decide, ?_⟩ rw [wilber1_bit_reversal_total_alternations_exact (by omega)] norm_num /-! ### Part 5.5E: Fenwick Tree, Fredman–Saks Cell-Probe Corollaries & Grand Unification -/ /-- Exact `lowbit` of any power of two: `lowbit (2^k) = 2^k`! -/ theorem lowbit_two_pow (k : ℕ) : lowbit (2 ^ k) = 2 ^ k := by induction k with | zero => rw [pow_zero]; exact lowbit_double_add_one 0 | succ k ih => rw [pow_succ', lowbit_double, ih] /-- Fenwick query on any power of two `2^k` takes EXACTLY `1` memory probe! -/ theorem fenwick_query_two_pow_one_step (k : ℕ) : fenwickQuerySteps (2 ^ k) = 1 := by rw [fenwick_query_steps_eq_popcount, (popcountNat_powers_of_two_exact k).2.1] /-- Fenwick query on `2^k - 1` takes EXACTLY `k` memory probes (worst-case exactness)! -/ theorem fenwick_query_two_pow_pred_exact (k : ℕ) : fenwickQuerySteps (2 ^ k - 1) = k := by rw [fenwick_query_steps_eq_popcount, (popcountNat_powers_of_two_exact k).2.2] /-- Fenwick query on ANY `x < 2^k` takes AT MOST `k` probes (`O(log N)` non-asymptotic upper bound)! -/ theorem fenwick_query_steps_le_log {k x : ℕ} (hx : x < 2 ^ k) : fenwickQuerySteps x ≤ k := by rw [fenwick_query_steps_eq_popcount] exact popcountNat_le_of_lt_two_pow hx /-- **FREDMAN–SAKS WORD-RAM `w = 64` CONCRETE CHRONOGRAM SEPARATION**: With `64`-bit words (`w = 64`), update time `t_u = 2`, epoch base `β = 258` (`2 * 2 * 64 = 256 < 257`), every epoch `j ≥ 1` has strictly fewer than half its bits overwritten by ALL later epochs combined! -/ theorem fredman_saks_w64_concrete_witness (j : ℕ) (hj : 1 ≤ j) : 2 * (2 * 64 * ∑ i ∈ Finset.Ico 1 j, 258 ^ i) < 258 ^ j := (fredman_saks_chronogram_overwrite_bound (β := 258) (t_u := 2) (w := 64) (by omega) (by omega) hj).2 /-- **CELL-PROBE BINARY PARITY / PREFIX-SUM SINGLE-BIT LOWER BOUND**: Distinguishing `2^n` outputs with `w`-bit cells requires at least `n / w` probes (`n ≤ w · t_q`)! -/ theorem cell_probe_n_bits_lower_bound {w t_q n : ℕ} (hw : 1 ≤ w) (hdist : ∃ f : (Fin t_q → Fin (2 ^ w)) → Fin (2 ^ n), Function.Surjective f) : n ≤ w * t_q := by have h := (cell_probe_decision_tree_capacity hw (Nat.one_le_pow n 2 (by omega)) hdist).1 exact (Nat.pow_le_pow_iff_right (by omega : 1 < 2)).mp h /-! ### Part 5.5F: Extended Non-Asymptotic Strengthening Suite Across All Five Pillars -/ /-- Tarjan potential exact difference bound when initial and final potentials match (`Φ(s_T) = Φ(s_0)`). -/ theorem tarjan_potential_cyclic_exact {S : Type*} (Φ : S → ℝ) (s : ℕ → S) (c : ℕ → ℝ) (T : ℕ) (hcyc : Φ (s T) = Φ (s 0)) : ∑ t ∈ Finset.range T, amortizedCost Φ (c t) (s t) (s (t + 1)) = ∑ t ∈ Finset.range T, c t := by rw [tarjan_potential_telescoping_exact, hcyc]; ring /-- Tarjan potential with bounded potential range `[L, U]`: actual cost never exceeds amortized cost plus `U - L`! -/ theorem tarjan_potential_bounded_range {S : Type*} (Φ : S → ℝ) (s : ℕ → S) (c : ℕ → ℝ) (T : ℕ) (L U : ℝ) (hlo : ∀ x, L ≤ Φ x) (hhi : ∀ x, Φ x ≤ U) : ∑ t ∈ Finset.range T, c t ≤ (∑ t ∈ Finset.range T, amortizedCost Φ (c t) (s t) (s (t + 1))) + (U - L) := by rw [tarjan_potential_telescoping_exact] linarith [hlo (s T), hhi (s 0)] /-- Exact binary counter strict bound: for EVERY `T ≥ 1`, total bit-flips is STRICTLY `< 2 T`! -/ theorem binary_counter_total_flips_strict_lt {T : ℕ} (hT : 1 ≤ T) : ∑ t ∈ Finset.range T, bitFlips t < 2 * T := by have h := (binary_counter_total_flips_exact T).1 have hpop : ∀ n : ℕ, 1 ≤ n → 1 ≤ popcountNat n := by intro n induction n using Nat.strong_induction_on with | h n ih => intro hn1 rcases Nat.even_or_odd n with ⟨k, hk⟩ | ⟨k, hk⟩ · have hn2 : n = 2 * k := by omega subst hn2; rw [popcountNat_double]; exact ih k (by omega) (by omega) · subst hk; rw [popcountNat_double_add_one]; omega have hpopT := hpop T hT omega /-- Exact `popcountNat` at small integers `1..7`. -/ theorem popcountNat_small_exact : popcountNat 1 = 1 ∧ popcountNat 2 = 1 ∧ popcountNat 3 = 2 ∧ popcountNat 4 = 1 ∧ popcountNat 5 = 2 ∧ popcountNat 6 = 2 ∧ popcountNat 7 = 3 := by refine ⟨by simp [popcountNat], by simp [popcountNat], by simp [popcountNat], by simp [popcountNat], by simp [popcountNat], by simp [popcountNat], by simp [popcountNat]⟩ /-- Exact `trailingOnes` at powers of two (`0` for `2^k`, `k ≥ 1`) and odd numbers (`≥ 1`). -/ theorem trailingOnes_two_pow_zero {k : ℕ} (hk : 1 ≤ k) : trailingOnes (2 ^ k) = 0 := by cases k with | zero => omega | succ k' => rw [pow_succ', trailingOnes_double] /-- Dynamic table non-resize insert in the lower half (`2 (n + 1) ≤ m`) has amortized cost `= 0`! -/ theorem dynTable_insert_lower_half_zero_amortized {n m : ℕ} (hlo : 2 * (n + 1) ≤ m) : amortizedCost (fun p : ℕ × ℕ => dynTableHystPot p.1 p.2) 1 (n, m) (n + 1, m) = 0 := by dsimp only [amortizedCost, dynTableHystPot] by_cases heq : m = 2 * (n + 1) · rw [if_pos (by omega), if_neg (by omega), heq]; push_cast; ring · rw [if_neg (by omega), if_neg (by omega)]; push_cast; ring /-- Dynamic table non-resize delete in the upper half (`m ≤ 2 n`) has amortized cost `= -1` (releases credit!). -/ theorem dynTable_delete_upper_half_releases_credit {n m : ℕ} (hhi : m ≤ 2 * n) : amortizedCost (fun p : ℕ × ℕ => dynTableHystPot p.1 p.2) 1 (n + 1, m) (n, m) = -1 := by dsimp only [amortizedCost, dynTableHystPot] rw [if_pos hhi, if_pos (by omega : m ≤ 2 * (n + 1))]; push_cast; ring /-- Dynamic table hysteresis total work over `T` mixed inserts & deletes (starting from empty `(0, 0)`) is ALWAYS `≤ 3 T`! -/ theorem dynTable_hysteresis_mixed_sequence_le_3T (T : ℕ) (st : ℕ → ℕ × ℕ) (c : ℕ → ℝ) (h0 : st 0 = (0, 0)) (hstep : ∀ t < T, amortizedCost (fun p : ℕ × ℕ => dynTableHystPot p.1 p.2) (c t) (st t) (st (t + 1)) ≤ 3) : ∑ t ∈ Finset.range T, c t ≤ 3 * (T : ℝ) := by have h := tarjan_uniform_amortized_bound (fun p : ℕ × ℕ => dynTableHystPot p.1 p.2) st c T 3 (by rw [h0]; exact (dynTableHystPot_boundary_values 0).1) (fun p => (dynTableHystPot_nonneg_and_half_zero p.1 p.2).1) hstep linarith /-- Golden ratio power recurrence `φ^(k + 2) = φ^(k + 1) + φ^k` for ALL `k : ℕ`. -/ theorem goldenRatio_pow_recurrence (k : ℕ) : goldenRatio ^ (k + 2) = goldenRatio ^ (k + 1) + goldenRatio ^ k := by have hsq := goldenRatio_sq_eq_add_one.1 calc goldenRatio ^ (k + 2) = goldenRatio ^ k * goldenRatio ^ 2 := by ring _ = goldenRatio ^ k * (goldenRatio + 1) := by rw [hsq] _ = goldenRatio ^ (k + 1) + goldenRatio ^ k := by ring /-- Exact Fibonacci heap concrete degree upper bounds: `n = 1000 ⇒ deg ≤ 14` (`F_17 = 1597 > 1000`). -/ theorem fib_heap_n1000_degree_le_14 {k : ℕ} (hsize : Nat.fib (k + 2) ≤ 1000) : k ≤ 14 := by by_contra h have hge : 15 ≤ k := by omega have hmono : Nat.fib 17 ≤ Nat.fib (k + 2) := Nat.fib_mono (by omega) have hf17 : Nat.fib 17 = 1597 := by decide omega /-- Fibonacci heap `insert` (adding 1 unmarked root tree, actual cost `1`) has exact amortized cost `= 2`. -/ theorem fib_heap_insert_amortized_exact_two (t m : ℕ) : let Φ : ℕ × ℕ → ℝ := fun p => (p.1 : ℝ) + 2 * (p.2 : ℝ) amortizedCost Φ 1 (t, m) (t + 1, m) = 2 := by intro Φ; dsimp only [amortizedCost, Φ]; push_cast; ring /-- Fibonacci heap `merge` (concatenating two root lists, actual cost `1`, preserving trees & marks) has exact amortized cost `= 1`. -/ theorem fib_heap_merge_amortized_exact_one (t₁ t₂ m₁ m₂ : ℕ) : let Φ : ℕ × ℕ → ℝ := fun p => (p.1 : ℝ) + 2 * (p.2 : ℝ) amortizedCost Φ 1 (t₁ + t₂, m₁ + m₂) (t₁ + t₂, m₁ + m₂) = 1 := by intro Φ; dsimp only [amortizedCost, Φ]; ring /-- Splay tree `log2Rank` strict monotonicity on positive reals. -/ theorem log2Rank_lt_log2Rank {a b : ℝ} (ha : 0 < a) (hab : a < b) : log2Rank a < log2Rank b := div_lt_div_of_pos_right (Real.log_lt_log ha hab) (Real.log_pos (by norm_num)) /-- Splay tree `log2Rank` of a power: `log₂(a^k) = k · log₂ a` (`a > 0`). -/ theorem log2Rank_pow {a : ℝ} (k : ℕ) : log2Rank (a ^ k) = (k : ℝ) * log2Rank a := by dsimp only [log2Rank]; rw [Real.log_pow]; ring /-- Splay tree uniform-frequency Shannon entropy equals `log₂ n` (exact specialization of Static Optimality!). -/ theorem splay_uniform_entropy_eq_log2 {n : ℕ} (hn : 1 ≤ n) : ∑ _i : Fin n, ((1 : ℝ) / (n : ℝ)) * log2Rank (n : ℝ) = log2Rank (n : ℝ) := by have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] field_simp [ne_of_gt hnpos] /-- Splay tree skewed access (single hot key accessed `m` times): entropy = `0`, amortized cost `≤ 1` per access! -/ theorem splay_single_hot_key_zero_entropy {m : ℕ} (hm : 1 ≤ m) : ((m : ℝ) / (m : ℝ)) * log2Rank ((m : ℝ) / (m : ℝ)) = 0 := by have hmpos : (0 : ℝ) < (m : ℝ) := Nat.cast_pos.mpr (by omega) rw [div_self (ne_of_gt hmpos), log2Rank_exact_powers.1, mul_zero] /-- Splay zig-zig STRICTLY beats the Access Lemma `3 (r'(x) - r(x))` slack by `-1`! -/ theorem splay_zig_zig_strict_slack {sx sy sz sx' sy' sz' : ℝ} (hsx : 0 < sx) (hsy' : 0 < sy') (hsz' : 0 < sz') (hxy : sx ≤ sy) (hy'x' : sy' ≤ sx') (hroot : sx' = sz) (hdisj : sx + sz' ≤ sx') : 1 + (log2Rank sx' + log2Rank sy' + log2Rank sz') - (log2Rank sx + log2Rank sy + log2Rank sz) < 3 * (log2Rank sx' - log2Rank sx) := by have h := splay_zig_zig_amortized_bound hsx hsy' hsz' hxy hy'x' hroot hdisj linarith /-- Union-by-size size is ALWAYS at least `1` for every `UnionTree`. -/ theorem unionTree_size_pos (t : UnionTree) : 1 ≤ t.size := by induction t with | leaf => simp [UnionTree.size] | link big small ihb _ => dsimp only [UnionTree.size]; omega /-- Union-by-size height of ANY `n`-node tree (`n < 2^k`) is strictly `< k`! -/ theorem union_by_size_height_lt_of_size_lt_two_pow {t : UnionTree} (ht : t.IsBySize) {k : ℕ} (hsz : t.size < 2 ^ k) : t.height < k := by have h2 := union_by_size_size_ge_two_pow_height t ht have hlt : 2 ^ t.height < 2 ^ k := lt_of_le_of_lt h2 hsz exact (Nat.pow_lt_pow_iff_right (by omega : 1 < 2)).mp hlt /-- Exact `binomialTree` size & height at `k = 0..4`. -/ theorem binomialTree_small_exact : (binomialTree 0).size = 1 ∧ (binomialTree 1).size = 2 ∧ (binomialTree 2).size = 4 ∧ (binomialTree 3).size = 8 ∧ (binomialTree 4).size = 16 ∧ (binomialTree 4).height = 4 := by refine ⟨(binomial_tree_height_sharpness 0).2.1, (binomial_tree_height_sharpness 1).2.1, (binomial_tree_height_sharpness 2).2.1, (binomial_tree_height_sharpness 3).2.1, (binomial_tree_height_sharpness 4).2.1, (binomial_tree_height_sharpness 4).2.2⟩ /-- `logStar 0 = 0`, and `logStar n ≤ n` for EVERY `n : ℕ`. -/ theorem logStar_zero_and_le_self (n : ℕ) : logStar 0 = 0 ∧ logStar n ≤ n := by refine ⟨by have := (logStar_spec 0).2 0 (by simp [tower]); omega, (logStar_spec n).2 n (le_of_lt (lt_tower n))⟩ /-- `invAck 0 = 0`, and `invAck n ≤ n` for EVERY `n : ℕ`. -/ theorem invAck_zero_and_le_self (n : ℕ) : invAck 0 = 0 ∧ invAck n ≤ n := by refine ⟨by have := (invAck_spec 0).2 0 (by simp [ack]); omega, (invAck_spec n).2 n (le_of_lt (lt_ack n n))⟩ /-- Exact Ackermann monotonicity in both arguments simultaneously (`k₁ ≤ k₂, j₁ ≤ j₂ ⇒ ack k₁ j₁ ≤ ack k₂ j₂`). -/ theorem ack_monotone_both {k₁ k₂ j₁ j₂ : ℕ} (hk : k₁ ≤ k₂) (hj : j₁ ≤ j₂) : ack k₁ j₁ ≤ ack k₂ j₂ := by have hright : ack k₁ j₁ ≤ ack k₁ j₂ := (ack_strictMono_right k₁).monotone hj have hleft : ∀ d : ℕ, ack k₁ j₂ ≤ ack (k₁ + d) j₂ := by intro d; induction d with | zero => simp | succ d ih => exact le_trans ih (ack_le_succ_left (k₁ + d) j₂) have heq : k₁ + (k₂ - k₁) = k₂ := by omega have h2 := hleft (k₂ - k₁) rw [heq] at h2 exact le_trans hright h2 /-- `invAck n ≤ logStar n` for ALL `n ≤ 65536`! -/ theorem invAck_le_logStar_up_to_65536 {n : ℕ} (hn : n ≤ 65536) : invAck n ≤ logStar n := by by_cases h1 : n ≤ 1 · have : invAck n = 0 := by have := (invAck_spec n).2 0 (by simp [ack]; omega); omega omega · by_cases h2 : n ≤ 2 · have ha : invAck n ≤ 1 := (invAck_spec n).2 1 (by have := ack_diag_first_four_exact.2.1; omega) have hl : 1 ≤ logStar n := by have := logStar_monotone (by omega : 2 ≤ n); rw [logStar_exact_thresholds.2.1] at this; exact this omega · by_cases h4 : n ≤ 4 · have ha : invAck n ≤ 2 := (invAck_spec n).2 2 (by have := ack_diag_first_four_exact.2.2.1; omega) have hl : 2 ≤ logStar n := by have := logStar_monotone (by omega : 3 ≤ n) have h3 : 2 ≤ logStar 3 := by by_contra hlt; push Not at hlt have htw := tower_strictMono.monotone (by omega : logStar 3 ≤ 1) have hge := (logStar_spec 3).1 have ht1 : tower 1 = 2 := by decide omega omega omega · by_cases h16 : n ≤ 16 · have ha : invAck n ≤ 3 := invAck_monotone_and_small_values.2.1 n (by omega) have hl : 3 ≤ logStar n := by by_contra hlt; push Not at hlt have htw := tower_strictMono.monotone (by omega : logStar n ≤ 2) have hge := (logStar_spec n).1 have ht2 : tower 2 = 4 := by decide omega omega · have ha : invAck n ≤ 4 := by calc invAck n ≤ invAck 65536 := invAck_monotone_and_small_values.1 n 65536 hn _ = 4 := invAck_65536_eq_four have hl : 4 ≤ logStar n := by by_contra hlt; push Not at hlt have htw := tower_strictMono.monotone (by omega : logStar n ≤ 3) have hge := (logStar_spec n).1 have ht3 : tower 3 = 16 := by decide omega omega /-- List inversion count never exceeds `C(n, 2)` (at most `n · (n - 1) / 2` pairs)! -/ theorem listInversions_le_n_sq {n : ℕ} (π σ : Equiv.Perm (Fin n)) : 2 * listInversions π σ ≤ n * (n - 1) := by let Inv := Finset.univ.filter (fun p : Fin n × Fin n => (π p.1).1 < (π p.2).1 ∧ (σ p.2).1 < (σ p.1).1) let InvRev := Inv.image (fun p => (p.2, p.1)) have hdisj : Disjoint Inv InvRev := by rw [Finset.disjoint_left]; intro ⟨a, b⟩ h1 h2 have hlt1 : (π a).1 < (π b).1 := (Finset.mem_filter.mp h1).2.1 obtain ⟨⟨c, d⟩, hcd, heq⟩ := Finset.mem_image.mp h2 have hlt2 : (π c).1 < (π d).1 := (Finset.mem_filter.mp hcd).2.1 have h1' : d = a := (Prod.mk.inj heq).1 have h2' : c = b := (Prod.mk.inj heq).2 rw [h1', h2'] at hlt2; omega have hsub : Inv ∪ InvRev ⊆ (Finset.univ : Finset (Fin n)).offDiag := by intro ⟨a, b⟩ h rcases Finset.mem_union.mp h with h1 | h2 · have hlt : (π a).1 < (π b).1 := (Finset.mem_filter.mp h1).2.1 exact Finset.mem_offDiag.mpr ⟨Finset.mem_univ _, Finset.mem_univ _, fun heq => by dsimp only at heq; rw [heq] at hlt; omega⟩ · obtain ⟨⟨c, d⟩, hcd, heq⟩ := Finset.mem_image.mp h2 have hlt : (π c).1 < (π d).1 := (Finset.mem_filter.mp hcd).2.1 have h1' : d = a := (Prod.mk.inj heq).1 have h2' : c = b := (Prod.mk.inj heq).2 rw [h1', h2'] at hlt exact Finset.mem_offDiag.mpr ⟨Finset.mem_univ _, Finset.mem_univ _, fun heq => by dsimp only at heq; rw [heq] at hlt; omega⟩ have hcard := Finset.card_le_card hsub rw [Finset.card_union_of_disjoint hdisj, Finset.card_image_of_injective _ (fun _ _ h => Prod.ext (Prod.mk.inj h).2 (Prod.mk.inj h).1), Finset.offDiag_card, Finset.card_univ, Fintype.card_fin] at hcard have hmul_sub : n * n - n = n * (n - 1) := by cases n with | zero => simp | succ m => simp only [Nat.add_sub_cancel] have : (m + 1) * (m + 1) = (m + 1) * m + (m + 1) := by ring rw [this, Nat.add_sub_cancel] dsimp only [listInversions]; change 2 * Inv.card ≤ n * (n - 1) omega /-- MTF single access on the front element (`(π x).1 = 0`) is a no-op with cost `1`! -/ theorem mtfStep_front_noop {n : ℕ} (π : Equiv.Perm (Fin n)) (x : Fin n) (hx : (π x).1 = 0) : listAccessCost π x = 1 ∧ mtfStep π x = π := by refine ⟨by dsimp only [listAccessCost]; omega, ?_⟩ ext y change (if π y = π x then (⟨0, Nat.zero_lt_of_lt (π x).2⟩ : Fin n) else if h : (π y).1 < (π x).1 then ⟨(π y).1 + 1, by omega⟩ else π y).1 = (π y).1 by_cases h : π y = π x · rw [if_pos h, h]; dsimp only; omega · rw [if_neg h, dif_neg (by omega)] /-- Exact MTF cyclic ratios at `n = 2, 3, 4`: `4/3`, `3/2`, `8/5`! -/ theorem mtf_cyclic_ratio_small_exact : (2 : ℝ) - 2 / ((2 : ℝ) + 1) = 4 / 3 ∧ (2 : ℝ) - 2 / ((3 : ℝ) + 1) = 3 / 2 ∧ (2 : ℝ) - 2 / ((4 : ℝ) + 1) = 8 / 5 := by norm_num /-- `bitRev` is a strict bijection on `Fin (2^k)` (explicit `Equiv.Perm (Fin (2^k))`). -/ def bitRevPerm (k : ℕ) : Equiv.Perm (Fin (2 ^ k)) where toFun x := ⟨bitRev k x.1, bitRev_lt k x.1⟩ invFun x := ⟨bitRev k x.1, bitRev_lt k x.1⟩ left_inv x := Fin.ext (bitRev_involution k x.1 x.2) right_inv x := Fin.ext (bitRev_involution k x.1 x.2) /-- `bitRevPerm k` is a self-inverse involution (`bitRevPerm k * bitRevPerm k = 1`). -/ theorem bitRevPerm_sq_eq_one (k : ℕ) : bitRevPerm k * bitRevPerm k = 1 := by ext x; exact bitRev_involution k x.1 x.2 /-- Exact Wilber-1 bit-reversal alternation counts for `k = 1, 2, 3, 4`: `1, 5, 17, 49`! -/ theorem wilber1_bit_reversal_small_exact : (∑ d ∈ Finset.range 1, 2 ^ d * (2 ^ (1 - d) - 1) = 1) ∧ (∑ d ∈ Finset.range 2, 2 ^ d * (2 ^ (2 - d) - 1) = 5) ∧ (∑ d ∈ Finset.range 3, 2 ^ d * (2 ^ (3 - d) - 1) = 17) ∧ (∑ d ∈ Finset.range 4, 2 ^ d * (2 ^ (4 - d) - 1) = 49) := by refine ⟨?_, ?_, ?_, ?_⟩ <;> (rw [wilber1_bit_reversal_total_alternations_exact (by omega)]; norm_num) /-- Exact `lowbit` of every odd number is `1`. -/ theorem lowbit_odd (k : ℕ) : lowbit (2 * k + 1) = 1 := lowbit_double_add_one k /-- Exact Fenwick cell at any power of two `2^k` stores the ENTIRE prefix sum `∑_{i ∈ (0, 2^k]} a i`! -/ theorem fenwickCell_two_pow_full_prefix {R : Type*} [AddCommMonoid R] (a : ℕ → R) (k : ℕ) : fenwickCell a (2 ^ k) = ∑ i ∈ Finset.Ioc 0 (2 ^ k), a i := by dsimp only [fenwickCell]; rw [lowbit_two_pow, Nat.sub_self] /-- Exact Fenwick cell at any odd index `2 k + 1` stores the single element `a (2 k + 1)`! -/ theorem fenwickCell_odd_singleton {R : Type*} [AddCommMonoid R] (a : ℕ → R) (k : ℕ) : fenwickCell a (2 * k + 1) = a (2 * k + 1) := by dsimp only [fenwickCell] rw [lowbit_odd, Nat.add_sub_cancel] have hIoc : Finset.Ioc (2 * k) (2 * k + 1) = {2 * k + 1} := by ext i; simp only [Finset.mem_Ioc, Finset.mem_singleton]; omega rw [hIoc, Finset.sum_singleton] /-- **FENWICK TREE EXACT POINT-UPDATE ADDITIVITY**: Adding `δ` at index `p` changes `fenwickQuery` at `x` by `δ` if `p ≤ x` (and `p ≥ 1`) and by `0` if `x < p`! -/ theorem fenwick_point_update_effect {R : Type*} [AddCommMonoid R] (a : ℕ → R) (p : ℕ) (hp : 1 ≤ p) (δ : R) (x : ℕ) : fenwickQuery (fun i => if i = p then a i + δ else a i) x = if p ≤ x then fenwickQuery a x + δ else fenwickQuery a x := by rw [fenwick_prefix_sum_exact, fenwick_prefix_sum_exact] by_cases hpx : p ≤ x · rw [if_pos hpx] have hmem : p ∈ Finset.Ioc 0 x := Finset.mem_Ioc.mpr ⟨by omega, hpx⟩ rw [← Finset.add_sum_erase _ _ hmem, ← Finset.add_sum_erase _ a hmem] simp only [↓reduceIte] have herase : ∑ i ∈ (Finset.Ioc 0 x).erase p, (if i = p then a i + δ else a i) = ∑ i ∈ (Finset.Ioc 0 x).erase p, a i := Finset.sum_congr rfl (fun i hi => by rw [if_neg (Finset.mem_erase.mp hi).1]) rw [herase]; ac_rfl · rw [if_neg hpx] refine Finset.sum_congr rfl (fun i hi => ?_) have hle := (Finset.mem_Ioc.mp hi).2 rw [if_neg (by omega)] /-- **FENWICK UPDATE ITERATE CHAIN BOUND (`2^s · lowbit x ≤ lowbit (iterate (· + lowbit ·) s x)`)**: Iterating `x ↦ x + lowbit x` `s` times multiplies `lowbit` by AT LEAST `2^s`, so strictly fewer than `k` updates stay below `2^k` (`O(log N)` worst-case Fenwick update bound)! -/ theorem fenwick_update_chain_exp_growth (x : ℕ) (hx : 1 ≤ x) (s : ℕ) : 1 ≤ (fun y => y + lowbit y)^[s] x ∧ 2 ^ s * lowbit x ≤ lowbit ((fun y => y + lowbit y)^[s] x) := by induction s with | zero => simp [hx] | succ s ih => obtain ⟨hpos, hgrow⟩ := ih rw [Function.iterate_succ_apply'] have hlb := (lowbit_properties hpos).1 have hstep := fenwick_update_doubles_lowbit hpos refine ⟨by omega, ?_⟩ rw [pow_succ'] calc 2 * 2 ^ s * lowbit x = 2 * (2 ^ s * lowbit x) := by ring _ ≤ 2 * lowbit ((fun y => y + lowbit y)^[s] x) := Nat.mul_le_mul_left 2 hgrow _ ≤ lowbit ((fun y => y + lowbit y)^[s] x + lowbit ((fun y => y + lowbit y)^[s] x)) := hstep /-- **FENWICK UPDATE CHAIN LENGTH `≤ k` FOR ALL `x < 2^k`**: Starting from ANY `1 ≤ x < 2^k`, after `k` Fenwick update steps the index ALWAYS escapes `≥ 2^k`! -/ theorem fenwick_update_escapes_two_pow {k x : ℕ} (hx : 1 ≤ x) : 2 ^ k ≤ (fun y => y + lowbit y)^[k] x := by have ⟨hpos, hgrow⟩ := fenwick_update_chain_exp_growth x hx k have hlb_ge1 : 1 ≤ lowbit x := (lowbit_properties hx).1 have hlb_le : lowbit ((fun y => y + lowbit y)^[k] x) ≤ (fun y => y + lowbit y)^[k] x := (lowbit_properties hpos).2.1 calc 2 ^ k = 2 ^ k * 1 := (mul_one _).symm _ ≤ 2 ^ k * lowbit x := Nat.mul_le_mul_left _ hlb_ge1 _ ≤ lowbit ((fun y => y + lowbit y)^[k] x) := hgrow _ ≤ (fun y => y + lowbit y)^[k] x := hlb_le /-- **FREDMAN–SAKS CHRONOGRAM SURVIVING-BITS PIGEONHOLE LOWER BOUND**: If epoch `j` has `β^j` independent bits and ALL later epochs overwrite `< β^j / 2` bits, then STRICTLY MORE than `β^j / 2` bits survive untouched (`β^j / 2 < surviving`), so ANY zero-error query reconstructing `B` surviving bits needs at least `B / w` cell probes! -/ theorem fredman_saks_surviving_bits_gt_half {β t_u w j : ℕ} (hβ : 2 ≤ β) (hgap : 2 * t_u * w < β - 1) (hj : 1 ≤ j) : let overwritten := t_u * w * ∑ i ∈ Finset.Ico 1 j, β ^ i overwritten < β ^ j ∧ β ^ j < 2 * (β ^ j - overwritten) := by intro overwritten have h := (fredman_saks_chronogram_overwrite_bound hβ hgap hj).2 exact ⟨by omega, by omega⟩ /-- Concrete Fredman–Saks `w = 32` tradeoff witness (`t_u = 1`, `β = 66`): later epochs ALWAYS overwrite strictly `< (1 / 2) · 66^j` bits! -/ theorem fredman_saks_w32_concrete_witness (j : ℕ) (hj : 1 ≤ j) : 2 * (1 * 32 * ∑ i ∈ Finset.Ico 1 j, 66 ^ i) < 66 ^ j := (fredman_saks_chronogram_overwrite_bound (β := 66) (t_u := 1) (w := 32) (by omega) (by omega) hj).2 /-- Concrete Fredman–Saks vs Fenwick matching separation: for `N = 2^k` (`k ≥ 1`), Fenwick achieves exact `≤ k` probes per query AND `≤ k` writes per update! -/ theorem fenwick_simultaneous_log_upper_bound {k x : ℕ} (hx1 : 1 ≤ x) (hxlt : x < 2 ^ k) : fenwickQuerySteps x ≤ k ∧ 2 ^ k ≤ (fun y => y + lowbit y)^[k] x := ⟨fenwick_query_steps_le_log hxlt, fenwick_update_escapes_two_pow hx1⟩ /-- Exact Fenwick range sum query on `(l, r]` via two prefix queries (`fenwickQuery a r - fenwickQuery a l`). -/ theorem fenwick_range_sum_exact {R : Type*} [AddCommGroup R] (a : ℕ → R) {l r : ℕ} (hlr : l ≤ r) : fenwickQuery a r - fenwickQuery a l = ∑ i ∈ Finset.Ioc l r, a i := by rw [fenwick_prefix_sum_exact, fenwick_prefix_sum_exact] have hpart : Finset.Ioc 0 l ∪ Finset.Ioc l r = Finset.Ioc 0 r := by ext i; simp only [Finset.mem_union, Finset.mem_Ioc]; omega have hdisj : Disjoint (Finset.Ioc 0 l) (Finset.Ioc l r) := by rw [Finset.disjoint_left]; intro i h1 h2 simp only [Finset.mem_Ioc] at h1 h2; omega rw [← hpart, Finset.sum_union hdisj, add_sub_cancel_left] /-- Binary counter single-step `bitFlips` is ALWAYS at least `1`. -/ theorem bitFlips_ge_one (n : ℕ) : 1 ≤ bitFlips n := by dsimp only [bitFlips]; omega /-- Splay tree AM-GM convexity strict monotonicity under doubling: `log₂(2 a) = log₂ a + 1`. -/ theorem log2Rank_double {a : ℝ} (ha : 0 < a) : log2Rank (2 * a) = log2Rank a + 1 := by rw [log2Rank_mul (by norm_num) ha, log2Rank_exact_powers.2.1]; ring /-- Exact Fibonacci growth `F_1 = 1, F_2 = 1, F_3 = 2, F_4 = 3, F_5 = 5, F_6 = 8`. -/ theorem fib_small_values_exact : Nat.fib 1 = 1 ∧ Nat.fib 2 = 1 ∧ Nat.fib 3 = 2 ∧ Nat.fib 4 = 3 ∧ Nat.fib 5 = 5 ∧ Nat.fib 6 = 8 := by refine ⟨by decide, by decide, by decide, by decide, by decide, by decide⟩ /-- Union-by-size `binomialTree` height strictly increases with every link step (`height B_k < height B_{k+1}`). -/ theorem binomialTree_height_strictMono : StrictMono (fun k => (binomialTree k).height) := by intro a b hab dsimp only rw [(binomial_tree_height_sharpness a).2.2, (binomial_tree_height_sharpness b).2.2] exact hab /-- `bitRev k 0 = 0` and `bitRev k (2^k - 1) = 2^k - 1` (all-zeros & all-ones are fixed points!). -/ theorem bitRev_zero_and_all_ones (k : ℕ) : bitRev k 0 = 0 ∧ bitRev k (2 ^ k - 1) = 2 ^ k - 1 := by have h0 : bitRev k 0 = 0 := by induction k with | zero => rfl | succ k ih => rw [bitRev]; simp [ih] have h1 : ∀ m : ℕ, bitRev m (2 ^ m - 1) = 2 ^ m - 1 := by intro m; induction m with | zero => rfl | succ m ih => have hpos : 1 ≤ 2 ^ m := Nat.one_le_pow m 2 (by omega) have heq : 2 ^ (m + 1) - 1 = 2 * (2 ^ m - 1) + 1 := by rw [pow_succ']; omega rw [bitRev] have hmod : (2 ^ (m + 1) - 1) % 2 = 1 := by rw [heq]; omega have hdiv : (2 ^ (m + 1) - 1) / 2 = 2 ^ m - 1 := by rw [heq]; omega rw [hmod, hdiv, ih, one_mul, pow_succ']; omega exact ⟨h0, h1 k⟩ /-- Exact Fenwick `lowbit` of `0` is `0`, and `lowbit (2^k * m) = 2^k * lowbit m`. -/ theorem lowbit_mul_two_pow (k m : ℕ) : lowbit (2 ^ k * m) = 2 ^ k * lowbit m := by induction k with | zero => simp | succ k ih => rw [pow_succ', mul_assoc, lowbit_double, ih, mul_assoc] /-- **AMORTIZED DATA STRUCTURES, SPLAY TREES, UNION-FIND, SELF-ORGANIZING LISTS & CELL-PROBE MEMORY LOWER BOUNDS GRAND UNIFICATION THEOREM**: Unifies headline non-asymptotic theorems across all five pillars simultaneously: 1. **Tarjan Potential, Binary Counter, Dynamic Table & Fibonacci Heaps**: Exact binary counter amortized cost `= 2`, dynamic table hysteresis doubling `= 3` / halving `= 2` vs no-hysteresis thrashing `T (m + 1)`, and Fibonacci golden-ratio growth `F_{k+2} ≥ φ^k`. 2. **Sleator–Tarjan Splay Trees**: Exact `log₂` AM-GM convexity, exact `3(r'(x) - r(x)) - 1` zig-zig inequality, Static Finger `W ≤ 5`, and strict separation vs. unbalanced BSTs (`n ≥ 32, m ≥ n`). 3. **Disjoint-Set Union (Union-Find)**: Union-by-size `2^height ≤ size` + zero-gap Binomial Tree sharpness, `log*` Hopcroft–Ullman cancellation, `tower k ≤ ack 4 k`, and `invAck 61 = 3 < 4 = logStar 61`. 4. **Self-Organizing Lists (MTF) & Wilber's First Bound**: Exact MTF single-access `≤ 2 k_ADV - 1`, exact cyclic ratio `2 - 2 / (n + 1)`, and exact bit-reversal involution achieving `(k - 1) · 2^k + 1` Wilber-1 alternations. 5. **Cell-Probe & Fenwick BIT**: Exact Fenwick prefix-sum correctness in `popcount x ≤ k` probes vs. Fredman–Saks chronogram overwrite and `Ω(log n / log β)` logarithmic cell-probe tradeoff! -/ theorem amortized_data_structures_splay_grand_unification : -- Pillar 1: Binary counter `= 2`, hysteresis doubling `= 3`, Fibonacci `F_{k+2} ≥ φ^k` ((∀ n : ℕ, amortizedCost (fun x => (popcountNat x : ℝ)) (bitFlips n : ℝ) n (n + 1) = 2) ∧ (∀ m : ℕ, 1 ≤ m → amortizedCost (fun p : ℕ × ℕ => dynTableHystPot p.1 p.2) ((m + 1 : ℕ) : ℝ) (m, m) (m + 1, 2 * m) = 3) ∧ (∀ k : ℕ, goldenRatio ^ k ≤ (Nat.fib (k + 2) : ℝ))) ∧ -- Pillar 2: Splay `log₂` AM-GM convexity + strict separation vs unbalanced static BST (`n ≥ 32`) ((∀ a b c : ℝ, 0 < a → 0 < b → a + b ≤ c → log2Rank a + log2Rank b ≤ 2 * log2Rank c - 2) ∧ (∀ n m : ℕ, 32 ≤ n → n ≤ m → (m : ℝ) * (3 * log2Rank (n : ℝ) + 1) + (n : ℝ) * log2Rank (n : ℝ) < (m : ℝ) * (n : ℝ))) ∧ -- Pillar 3: Union-by-size `2^h ≤ size` + Binomial Tree sharpness + `tower k ≤ ack 4 k` + `α(61) < log*(61)` ((∀ t : UnionTree, t.IsBySize → 2 ^ t.height ≤ t.size) ∧ (∀ k : ℕ, (binomialTree k).IsBySize ∧ (binomialTree k).size = 2 ^ k ∧ (binomialTree k).height = k) ∧ (∀ k : ℕ, tower k ≤ ack 4 k) ∧ invAck 61 < logStar 61) ∧ -- Pillar 4: MTF exact `≤ 2 k_ADV - 1` + bit-reversal involution + Wilber-1 exact `(k - 1) · 2^k + 1` ((∀ (n : ℕ) (π σ : Equiv.Perm (Fin n)) (x : Fin n), amortizedCost (fun p => (listInversions p σ : ℝ)) (listAccessCost π x : ℝ) π (mtfStep π x) ≤ 2 * (listAccessCost σ x : ℝ) - 1) ∧ (∀ k x : ℕ, x < 2 ^ k → bitRev k (bitRev k x) = x) ∧ (∀ k : ℕ, 1 ≤ k → ∑ d ∈ Finset.range k, 2 ^ d * (2 ^ (k - d) - 1) = (k - 1) * 2 ^ k + 1)) ∧ -- Pillar 5: Fenwick exact prefix sum in `popcount x` steps + Fredman–Saks chronogram overwrite bound ((∀ (a : ℕ → ℤ) (x : ℕ), fenwickQuery a x = ∑ i ∈ Finset.Ioc 0 x, a i) ∧ (∀ x : ℕ, fenwickQuerySteps x = popcountNat x) ∧ (∀ β t_u w j : ℕ, 2 ≤ β → 2 * t_u * w < β - 1 → 1 ≤ j → 2 * (t_u * w * ∑ i ∈ Finset.Ico 1 j, β ^ i) < β ^ j)) := ⟨⟨binary_counter_amortized_exact_two, dynTable_insert_hysteresis_amortized_le_three.2, fib_ge_goldenRatio_pow⟩, ⟨fun _ _ _ ha hb h => splay_log2_amgm_ineq ha hb h, fun _ _ hn hm => splay_vs_unbalanced_static_bst_separation hn hm⟩, ⟨union_by_size_size_ge_two_pow_height, binomial_tree_height_sharpness, tower_le_ack4, invAck_strictly_lt_logStar_at_61.2.2⟩, ⟨fun _ π σ x => mtf_single_access_amortized π σ x, bitRev_involution, fun _ hk => wilber1_bit_reversal_total_alternations_exact hk⟩, ⟨fun a x => fenwick_prefix_sum_exact a x, fenwick_query_steps_eq_popcount, fun _ _ _ _ hβ hg hj => (fredman_saks_chronogram_overwrite_bound hβ hg hj).2⟩⟩ end AmortizedDataStructuresSplayMinimax /-! =============================================================================== PART 6: AXIOM-CLEANLINESS TRANSPARENCY AUDIT (`#print axioms`) =============================================================================== -/ -- Pillar 1: Tarjan Potential, Binary Counter, Dynamic Table Hysteresis & Fibonacci Heaps #print axioms AmortizedDataStructuresSplayMinimax.tarjan_potential_telescoping_exact #print axioms AmortizedDataStructuresSplayMinimax.tarjan_potential_nonneg_upper_bound #print axioms AmortizedDataStructuresSplayMinimax.tarjan_potential_lower_bounded_offset #print axioms AmortizedDataStructuresSplayMinimax.tarjan_uniform_amortized_bound #print axioms AmortizedDataStructuresSplayMinimax.bankers_method_exact_equivalence #print axioms AmortizedDataStructuresSplayMinimax.binary_counter_popcount_succ #print axioms AmortizedDataStructuresSplayMinimax.binary_counter_amortized_exact_two #print axioms AmortizedDataStructuresSplayMinimax.binary_counter_total_flips_exact #print axioms AmortizedDataStructuresSplayMinimax.binary_counter_worst_case_step_exact #print axioms AmortizedDataStructuresSplayMinimax.binary_counter_two_pow_total_flips #print axioms AmortizedDataStructuresSplayMinimax.dynTableHystPot_nonneg_and_half_zero #print axioms AmortizedDataStructuresSplayMinimax.dynTable_insert_hysteresis_amortized_le_three #print axioms AmortizedDataStructuresSplayMinimax.dynTable_delete_hysteresis_amortized_le_two #print axioms AmortizedDataStructuresSplayMinimax.dynTable_no_hysteresis_thrashing_exact #print axioms AmortizedDataStructuresSplayMinimax.goldenRatio_sq_eq_add_one #print axioms AmortizedDataStructuresSplayMinimax.fib_ge_goldenRatio_pow #print axioms AmortizedDataStructuresSplayMinimax.fib_heap_subtree_size_and_degree_bound #print axioms AmortizedDataStructuresSplayMinimax.fib_heap_children_sum_ge_fib #print axioms AmortizedDataStructuresSplayMinimax.fib_heap_cascading_cut_amortized_le_four #print axioms AmortizedDataStructuresSplayMinimax.fib_heap_deg10_needs_144_nodes -- Pillar 2: Sleator–Tarjan Splay Trees #print axioms AmortizedDataStructuresSplayMinimax.splay_log2_amgm_ineq #print axioms AmortizedDataStructuresSplayMinimax.splay_zig_amortized_bound #print axioms AmortizedDataStructuresSplayMinimax.splay_zig_zig_amortized_bound #print axioms AmortizedDataStructuresSplayMinimax.splay_zig_zag_amortized_bound #print axioms AmortizedDataStructuresSplayMinimax.splay_access_lemma_exact #print axioms AmortizedDataStructuresSplayMinimax.splay_balance_theorem #print axioms AmortizedDataStructuresSplayMinimax.splay_static_optimality_entropy #print axioms AmortizedDataStructuresSplayMinimax.splay_static_finger_theorem #print axioms AmortizedDataStructuresSplayMinimax.splay_vs_unbalanced_static_bst_separation #print axioms AmortizedDataStructuresSplayMinimax.splay_log2_amgm_tight_at_midpoint #print axioms AmortizedDataStructuresSplayMinimax.splay_separation_n32_m32_exact #print axioms AmortizedDataStructuresSplayMinimax.splay_static_finger_at_finger_lt_eight -- Pillar 3: Disjoint-Set Union (Union-Find), `log*` & Inverse Ackermann #print axioms AmortizedDataStructuresSplayMinimax.union_by_size_size_ge_two_pow_height #print axioms AmortizedDataStructuresSplayMinimax.union_by_size_height_le_log2 #print axioms AmortizedDataStructuresSplayMinimax.binomial_tree_height_sharpness #print axioms AmortizedDataStructuresSplayMinimax.binomialTree_10_exact #print axioms AmortizedDataStructuresSplayMinimax.logStar_spec #print axioms AmortizedDataStructuresSplayMinimax.logStar_monotone #print axioms AmortizedDataStructuresSplayMinimax.logStar_exact_thresholds #print axioms AmortizedDataStructuresSplayMinimax.hopcroft_ullman_rank_block_cancellation #print axioms AmortizedDataStructuresSplayMinimax.union_find_log_star_total_work #print axioms AmortizedDataStructuresSplayMinimax.ack_levels_zero_to_three_exact #print axioms AmortizedDataStructuresSplayMinimax.lt_ack #print axioms AmortizedDataStructuresSplayMinimax.ack_strictMono_right #print axioms AmortizedDataStructuresSplayMinimax.ack_le_succ_left #print axioms AmortizedDataStructuresSplayMinimax.ack_diag_strictMono #print axioms AmortizedDataStructuresSplayMinimax.invAck_spec #print axioms AmortizedDataStructuresSplayMinimax.invAck_monotone_and_small_values #print axioms AmortizedDataStructuresSplayMinimax.tower_le_ack4 #print axioms AmortizedDataStructuresSplayMinimax.tarjan_ackermann_find_amortized_bound #print axioms AmortizedDataStructuresSplayMinimax.invAck_exact_thresholds #print axioms AmortizedDataStructuresSplayMinimax.invAck_65536_eq_four #print axioms AmortizedDataStructuresSplayMinimax.invAck_strictly_lt_logStar_at_61 -- Pillar 4: Self-Organizing Lists (MTF) & Wilber's First Bound #print axioms AmortizedDataStructuresSplayMinimax.mtf_single_access_amortized #print axioms AmortizedDataStructuresSplayMinimax.mtf_adversary_adjacent_swap_potential #print axioms AmortizedDataStructuresSplayMinimax.mtf_two_competitive_sequence #print axioms AmortizedDataStructuresSplayMinimax.mtf_cyclic_adversary_ratio_exact #print axioms AmortizedDataStructuresSplayMinimax.mtf_strictly_beats_transpose_omega_n #print axioms AmortizedDataStructuresSplayMinimax.altCount_parity_exact #print axioms AmortizedDataStructuresSplayMinimax.bitRev_lt #print axioms AmortizedDataStructuresSplayMinimax.bitRev_involution #print axioms AmortizedDataStructuresSplayMinimax.bitRev_block_exact_alternation #print axioms AmortizedDataStructuresSplayMinimax.wilber1_bit_reversal_total_alternations_exact #print axioms AmortizedDataStructuresSplayMinimax.bitRev_k3_witness #print axioms AmortizedDataStructuresSplayMinimax.listInversions_symm -- Pillar 5: Fenwick Tree & Fredman–Saks Chronogram Cell-Probe Lower Bounds #print axioms AmortizedDataStructuresSplayMinimax.lowbit_properties #print axioms AmortizedDataStructuresSplayMinimax.fenwick_sub_lowbit_popcount #print axioms AmortizedDataStructuresSplayMinimax.fenwick_prefix_sum_exact #print axioms AmortizedDataStructuresSplayMinimax.fenwick_query_steps_eq_popcount #print axioms AmortizedDataStructuresSplayMinimax.popcountNat_le_of_lt_two_pow #print axioms AmortizedDataStructuresSplayMinimax.fenwick_update_doubles_lowbit #print axioms AmortizedDataStructuresSplayMinimax.lowbit_two_pow #print axioms AmortizedDataStructuresSplayMinimax.fenwick_query_two_pow_one_step #print axioms AmortizedDataStructuresSplayMinimax.fenwick_query_two_pow_pred_exact #print axioms AmortizedDataStructuresSplayMinimax.fenwick_query_steps_le_log #print axioms AmortizedDataStructuresSplayMinimax.cell_probe_decision_tree_capacity #print axioms AmortizedDataStructuresSplayMinimax.cell_probe_n_bits_lower_bound #print axioms AmortizedDataStructuresSplayMinimax.fredman_saks_chronogram_overwrite_bound #print axioms AmortizedDataStructuresSplayMinimax.fredman_saks_chronogram_log_tradeoff #print axioms AmortizedDataStructuresSplayMinimax.fredman_saks_w64_concrete_witness #print axioms AmortizedDataStructuresSplayMinimax.fredman_saks_surviving_bits_gt_half #print axioms AmortizedDataStructuresSplayMinimax.binary_counter_total_flips_strict_lt #print axioms AmortizedDataStructuresSplayMinimax.dynTable_hysteresis_mixed_sequence_le_3T #print axioms AmortizedDataStructuresSplayMinimax.splay_uniform_entropy_eq_log2 #print axioms AmortizedDataStructuresSplayMinimax.union_by_size_height_lt_of_size_lt_two_pow #print axioms AmortizedDataStructuresSplayMinimax.ack_monotone_both #print axioms AmortizedDataStructuresSplayMinimax.invAck_le_logStar_up_to_65536 #print axioms AmortizedDataStructuresSplayMinimax.listInversions_le_n_sq #print axioms AmortizedDataStructuresSplayMinimax.bitRevPerm_sq_eq_one #print axioms AmortizedDataStructuresSplayMinimax.wilber1_bst_transition_charging_lower_bound #print axioms AmortizedDataStructuresSplayMinimax.fenwick_point_update_effect #print axioms AmortizedDataStructuresSplayMinimax.fenwick_update_chain_exp_growth #print axioms AmortizedDataStructuresSplayMinimax.fenwick_update_escapes_two_pow #print axioms AmortizedDataStructuresSplayMinimax.fenwick_simultaneous_log_upper_bound #print axioms AmortizedDataStructuresSplayMinimax.fenwick_range_sum_exact #print axioms AmortizedDataStructuresSplayMinimax.bitRev_zero_and_all_ones #print axioms AmortizedDataStructuresSplayMinimax.lowbit_mul_two_pow #print axioms AmortizedDataStructuresSplayMinimax.amortized_data_structures_splay_grand_unification