Documentation

CslibFrontier.Foundations.Data.Nat.HalvingRecurrence

Halving recurrences #

A cost T with T n ≤ a T(⌈n/2⌉) + c n for n ≥ 2 grows like a ^ ⌈log₂ n⌉. The substitution method proves this exactly, with a subtracted linear term (Nat.add_mul_two_pow_le_mul_pow_of_halving). Assume

Then T n + β 2 ^ k ≤ α a ^ k for all k and n ≤ 2 ^ k. Because the step only uses a common bound S on all sizes up to ⌈n/2⌉, recurrences that split n into ⌊n/2⌋ and ⌈n/2⌉, or that are not monotone, are covered directly. Nat.pow_clog_two_le_mul_rpow_logb in CslibFrontier.Foundations.Data.Nat.LogTwoRpow converts a ^ ⌈log₂ n⌉ to n ^ log₂ a.

Main results #

References #

theorem Nat.add_mul_two_pow_le_mul_pow_of_halving {T : ℕ → ℕ} {a c α β : ℕ} (ha : 1 ≤ a) (hc : 2 * c + 2 * β ≤ a * β) (base : ∀ n ≤ 1, T n + β ≤ α) (step : ∀ (n : ℕ), 2 ≤ n → ∀ (S : ℕ), (∀ m ≤ (n + 1) / 2, T m ≤ S) → T n ≤ a * S + c * n) (k n : ℕ) :
n ≤ 2 ^ k → T n + β * 2 ^ k ≤ α * a ^ k

Halving recurrence. If T n ≤ a S + c n whenever S bounds T on [0, ⌈n/2⌉] and n ≥ 2, then T n + β 2 ^ k ≤ α a ^ k for n ≤ 2 ^ k, provided the base case T n + β ≤ α for n ≤ 1 and the slack condition 2 c + 2 β ≤ a β.

theorem Nat.add_mul_two_pow_clog_le_of_halving {T : ℕ → ℕ} {a c α β : ℕ} (ha : 1 ≤ a) (hc : 2 * c + 2 * β ≤ a * β) (base : ∀ n ≤ 1, T n + β ≤ α) (step : ∀ (n : ℕ), 2 ≤ n → ∀ (S : ℕ), (∀ m ≤ (n + 1) / 2, T m ≤ S) → T n ≤ a * S + c * n) (n : ℕ) :
T n + β * 2 ^ clog 2 n ≤ α * a ^ clog 2 n

Halving recurrence at k = ⌈log₂ n⌉: T n + β 2 ^ ⌈log₂ n⌉ ≤ α a ^ ⌈log₂ n⌉.

theorem Nat.clog_two_lt_clog_two_of_le_half {m k : ℕ} (hm : 2 ≤ m) (hk : k ≤ (m + 1) / 2) :
clog 2 k < clog 2 m

Halving lowers ⌈log₂⌉. If 2 ≤ m and k ≤ ⌈m / 2⌉, then ⌈log₂ k⌉ < ⌈log₂ m⌉. This is the step of a recursion on ⌈n / 2⌉, such as binary search, that bounds its depth by ⌈log₂ n⌉.

theorem Nat.pow_mul_le_of_step {C : ℕ → ℕ} (hstep : ∀ (k : ℕ), 2 * C (k + 1) ≤ C k) (r : ℕ) :
2 ^ r * C r ≤ C 0

Geometric halving step 2 * C (k + 1) ≤ C k yields 2 ^ r * C r ≤ C 0 for all r.