Documentation

CslibFrontier.Foundations.Data.Nat.MasterTheorem

The master theorem for divide-and-conquer recurrences #

Nat.DivideConquerBound T a b n₀ T₀ f says that T satisfies the divide-and-conquer inequality T n ≤ a T(⌈n/b⌉) + f n above the threshold n₀, with T n ≤ T₀ up to n₀. The recurrence is stated in a form that cost functions satisfy directly. For every n > n₀, every S that bounds T on [0, ⌈n/b⌉] gives T n ≤ a S + f n. Floors, ceilings and several subproblems of different sizes up to ⌈n/b⌉ are therefore covered. T need not be monotone or satisfy an equality.

The general bound unrolls the recursion: for a ≥ 1, monotone f and n ≤ n₀ b ^ k,

T n ≤ a ^ k T₀ + ∑ i < k, a ^ i f (n₀ b ^ (k - i))

(Nat.DivideConquerBound.le_sum). For f n = c n ^ d the sum is geometric with ratio a / b ^ d, and the three cases of the master theorem follow with explicit constants.

DivideConquerBound itself imposes no condition on a, b or n₀. Every bound below assumes a ≥ 1, and the bounds in terms of n also assume b ≥ 2 and n₀ ≥ 1. Without them the conclusions can fail. For example, T n = n with a = b = n₀ = T₀ = 1 and c = d = 0 satisfies DivideConquerBound, because ⌈n/1⌉ = n, yet the second bound would give T 2 ≤ 1. Write K = ⌈log_b n⌉.

The first and third bounds hold for all a ≥ 1. They are trivial outside their case, where the left factor is zero. The n-forms use b ^ ⌈log_b n⌉ ≤ b n (Nat.pow_clog_le_mul). CslibFrontier.Foundations.Data.Nat.MasterTheoremAsymptotics converts them into O(n ^ log_b a), O(n ^ d log n) and O(n ^ d), and also proves matching Θ bounds across all three cases (including case 3 under the general regularity condition).

For case 3 with a general monotone driving function f, Nat.RegularityCondition f a b n₀ p q states the integer regularity condition p < q and q a f(⌊n/b⌋) ≤ p f(n) for n > n₀. Unrolling along the exact ceiling chain ⌈n / b ^ i⌉ avoids overshooting n to n₀ b ^ k (which would fail for super-polynomial f such as 2 ^ n), yielding the explicit bound (q - p) f(n₀ + 1) T n ≤ ((q - p) a ^ 2 T₀ + (q - p + a q) f(n₀ + 1)) f n for n > n₀ (Nat.DivideConquerBound.sub_mul_le_of_regular).

Nat.DivideConquerLower T a b n₀ T₀ f is the dual lower recurrence a T(⌊n/b⌋) + f n ≤ T n for n > n₀ with T₀ ≤ T n on [1, n₀], allowing subproblems of any size in [⌊n/b⌋, ⌈n/b⌉]. Nat.DivideConquerEq T a b n₀ Tmin Tmax f bundles matching upper and lower recurrences, with constructors of_div and of_ceilDiv for exact floor and ceiling recurrences.

Nat.add_mul_two_pow_le_mul_pow_of_halving (CslibFrontier.Foundations.Data.Nat.HalvingRecurrence) remains the sharper tool for b = 2: it carries a subtracted lower-order term through the induction, which some clients need.

Main definitions #

Main results #

References #

structure Nat.DivideConquerBound (T : ℕ → ℕ) (a b n₀ T₀ : ℕ) (f : ℕ → ℕ) :

T satisfies the divide-and-conquer inequality T n ≤ a T(⌈n/b⌉) + f n above n₀, with T n ≤ T₀ up to n₀. The recursive step quantifies over every bound S of T on [0, ⌈n/b⌉], so subproblems of any size up to ⌈n/b⌉ may be used.

  • base (n : ℕ) : n ≤ n₀ → T n ≤ T₀

    The base case: T n ≤ T₀ for n ≤ n₀.

  • step (n : ℕ) : n₀ < n → ∀ (S : ℕ), (∀ m ≤ (n + b - 1) / b, T m ≤ S) → T n ≤ a * S + f n

    The recursive step: T n ≤ a S + f n whenever S bounds T up to ⌈n/b⌉.

theorem Nat.DivideConquerBound.le_sum {T f : ℕ → ℕ} {a b n₀ T₀ : ℕ} (h : DivideConquerBound T a b n₀ T₀ f) (ha : 1 ≤ a) (hf : Monotone f) (k n : ℕ) :
n ≤ n₀ * b ^ k → T n ≤ a ^ k * T₀ + ∑ i ∈ Finset.range k, a ^ i * f (n₀ * b ^ (k - i))

Unrolling the recursion. For monotone f and n ≤ n₀ b ^ k, T n ≤ a ^ k T₀ + ∑ i < k, a ^ i f (n₀ b ^ (k - i)).

theorem Nat.DivideConquerBound.le_geom_sum {T : ℕ → ℕ} {a b n₀ T₀ c d : ℕ} (h : DivideConquerBound T a b n₀ T₀ fun (n : ℕ) => c * n ^ d) (ha : 1 ≤ a) (k n : ℕ) (hn : n ≤ n₀ * b ^ k) :
T n ≤ a ^ k * T₀ + c * n₀ ^ d * ∑ i ∈ Finset.range k, a ^ i * (b ^ d) ^ (k - i)

The unrolled recursion for f n = c n ^ d.

Geometric sums #

The three cases #

theorem Nat.DivideConquerBound.sub_mul_le_pow {T : ℕ → ℕ} {a b n₀ T₀ c d : ℕ} (h : DivideConquerBound T a b n₀ T₀ fun (n : ℕ) => c * n ^ d) (ha : 1 ≤ a) (k n : ℕ) (hn : n ≤ n₀ * b ^ k) :
(a - b ^ d) * T n ≤ ((a - b ^ d) * T₀ + c * n₀ ^ d * b ^ d) * a ^ k

Master theorem, case 1 (a > b ^ d), for n ≤ n₀ b ^ k: (a - b ^ d) T n ≤ ((a - b ^ d) T₀ + c n₀ ^ d b ^ d) a ^ k. The bound holds for every a ≥ 1 and is trivial when a ≤ b ^ d.

theorem Nat.DivideConquerBound.le_pow_mul {T : ℕ → ℕ} {a b n₀ T₀ c d : ℕ} (h : DivideConquerBound T a b n₀ T₀ fun (n : ℕ) => c * n ^ d) (ha : 1 ≤ a) (had : a = b ^ d) (k n : ℕ) (hn : n ≤ n₀ * b ^ k) :
T n ≤ a ^ k * (T₀ + c * n₀ ^ d * k)

Master theorem, case 2 (a = b ^ d), for n ≤ n₀ b ^ k: T n ≤ a ^ k (T₀ + c n₀ ^ d k).

theorem Nat.DivideConquerBound.pow_sub_mul_le_pow {T : ℕ → ℕ} {a b n₀ T₀ c d : ℕ} (h : DivideConquerBound T a b n₀ T₀ fun (n : ℕ) => c * n ^ d) (ha : 1 ≤ a) (k n : ℕ) (hn : n ≤ n₀ * b ^ k) :
(b ^ d - a) * T n ≤ ((b ^ d - a) * T₀ + c * n₀ ^ d * b ^ d) * (b ^ d) ^ k

Master theorem, case 3 (a < b ^ d), for n ≤ n₀ b ^ k: (b ^ d - a) T n ≤ ((b ^ d - a) T₀ + c n₀ ^ d b ^ d) (b ^ d) ^ k. The bound holds for every a ≥ 1 and is trivial when a ≥ b ^ d.

Bounds in terms of n #

theorem Nat.DivideConquerBound.sub_mul_le_pow_clog {T : ℕ → ℕ} {a b n₀ T₀ c d : ℕ} (h : DivideConquerBound T a b n₀ T₀ fun (n : ℕ) => c * n ^ d) (ha : 1 ≤ a) (hb : 1 < b) (hn₀ : 1 ≤ n₀) (n : ℕ) :
(a - b ^ d) * T n ≤ ((a - b ^ d) * T₀ + c * n₀ ^ d * b ^ d) * a ^ clog b n

Master theorem, case 1 with K = ⌈log_b n⌉: (a - b ^ d) T n ≤ ((a - b ^ d) T₀ + c n₀ ^ d b ^ d) a ^ K.

theorem Nat.DivideConquerBound.le_mul_pow_mul_clog {T : ℕ → ℕ} {a b n₀ T₀ c d : ℕ} (h : DivideConquerBound T a b n₀ T₀ fun (n : ℕ) => c * n ^ d) (ha : 1 ≤ a) (hb : 1 < b) (hn₀ : 1 ≤ n₀) (had : a = b ^ d) {n : ℕ} (hn : n ≠ 0) :
T n ≤ (b * n) ^ d * (T₀ + c * n₀ ^ d * clog b n)

Master theorem, case 2 with K = ⌈log_b n⌉: if a = b ^ d and n ≠ 0, then T n ≤ (b n) ^ d (T₀ + c n₀ ^ d K).

theorem Nat.DivideConquerBound.pow_sub_mul_le_mul_pow {T : ℕ → ℕ} {a b n₀ T₀ c d : ℕ} (h : DivideConquerBound T a b n₀ T₀ fun (n : ℕ) => c * n ^ d) (ha : 1 ≤ a) (hb : 1 < b) (hn₀ : 1 ≤ n₀) {n : ℕ} (hn : n ≠ 0) :
(b ^ d - a) * T n ≤ ((b ^ d - a) * T₀ + c * n₀ ^ d * b ^ d) * (b * n) ^ d

Master theorem, case 3 in terms of n: if n ≠ 0, then (b ^ d - a) T n ≤ ((b ^ d - a) T₀ + c n₀ ^ d b ^ d) (b n) ^ d.

theorem Nat.DivideConquerBound.exists_mono_threshold {T : ℕ → ℕ} {a b n₀ T₀ : ℕ} {f : ℕ → ℕ} (h : DivideConquerBound T a b n₀ T₀ f) {n₁ : ℕ} (hn : n₀ ≤ n₁) :
∃ (T₁ : ℕ), DivideConquerBound T a b n₁ T₁ f

Raising the base threshold of DivideConquerBound from n₀ to any n₁ ≥ n₀.

Case 3 under the general regularity condition #

structure Nat.RegularityCondition (f : ℕ → ℕ) (a b n₀ p q : ℕ) :

The regularity condition for case 3 of the master theorem, in integer form: p < q, f is monotone, and q * a * f (n / b) ≤ p * f n for every n > n₀ (expressing a f(⌊n/b⌋) ≤ (p / q) f(n) with contraction ratio p / q < 1).

  • lt : p < q

    The contraction ratio p / q is strictly less than 1.

  • mono : Monotone f

    The driving function f is monotone.

  • step (n : ℕ) : n₀ < n → q * a * f (n / b) ≤ p * f n

    Regularity inequality q * a * f (n / b) ≤ p * f n above n₀.

theorem Nat.RegularityCondition.of_pow {a b d : ℕ} (hlt : a < b ^ d) (c n₀ : ℕ) :
RegularityCondition (fun (n : ℕ) => c * n ^ d) a b n₀ a (b ^ d)

A polynomial driving function f n = c * n ^ d with a < b ^ d satisfies the regularity condition with ratio a / b ^ d.

theorem Nat.DivideConquerBound.sub_mul_le_of_regular {T f : ℕ → ℕ} {a b n₀ T₀ p q : ℕ} (h : DivideConquerBound T a b n₀ T₀ f) (hreg : RegularityCondition f a b n₀ p q) (ha : 1 ≤ a) (hb : 1 < b) (hn₀ : 1 ≤ n₀) {n : ℕ} (hn : n₀ < n) :
(q - p) * f (n₀ + 1) * T n ≤ ((q - p) * a ^ 2 * T₀ + (q - p + a * q) * f (n₀ + 1)) * f n

Master theorem, case 3 (general regularity condition): under RegularityCondition f a b n₀ p q, for every n > n₀, (q - p) f(n₀ + 1) T n ≤ ((q - p) a ^ 2 T₀ + (q - p + a q) f(n₀ + 1)) f n.

Lower bounds and matching recurrences #

structure Nat.DivideConquerLower (T : ℕ → ℕ) (a b n₀ T₀ : ℕ) (f : ℕ → ℕ) :

T satisfies the divide-and-conquer lower inequality a T(⌊n/b⌋) + f n ≤ T n above n₀, with T₀ ≤ T n for 1 ≤ n ≤ n₀. The recursive step quantifies over every lower bound S of T on [⌊n/b⌋, ⌈n/b⌉], so subproblems of any size between ⌊n/b⌋ and ⌈n/b⌉ are covered.

  • base (n : ℕ) : 1 ≤ n → n ≤ n₀ → T₀ ≤ T n

    The base case: T₀ ≤ T n for 1 ≤ n ≤ n₀.

  • step (n : ℕ) : n₀ < n → ∀ (S : ℕ), (∀ (m : ℕ), n / b ≤ m → m ≤ (n + b - 1) / b → S ≤ T m) → a * S + f n ≤ T n

    The recursive step: a S + f n ≤ T n whenever S bounds T below on [⌊n/b⌋, ⌈n/b⌉].

theorem Nat.DivideConquerLower.le_of_pos {T f : ℕ → ℕ} {a b n₀ T₀ : ℕ} (h : DivideConquerLower T a b n₀ T₀ f) (ha : 1 ≤ a) (hb : 1 < b) (hnb : b ≤ n₀ + 1) (n : ℕ) :
1 ≤ n → T₀ ≤ T n

Every positive input satisfies T₀ ≤ T n once b ≤ n₀ + 1 (so no recursive call above n₀ drops to 0).

theorem Nat.DivideConquerLower.sum_le {T f : ℕ → ℕ} {a b n₀ T₀ : ℕ} (h : DivideConquerLower T a b n₀ T₀ f) (ha : 1 ≤ a) (hb : 1 < b) (hnb : b ≤ n₀ + 1) (hf : Monotone f) (k n : ℕ) (hn : n₀ * b ^ k ≤ n) :
a ^ k * T₀ + ∑ i ∈ Finset.range k, a ^ i * f (n₀ * b ^ (k - i)) ≤ T n

Unrolling the lower recursion. For monotone f, b ≤ n₀ + 1, and n₀ b ^ k ≤ n, a ^ k T₀ + ∑ i < k, a ^ i f (n₀ b ^ (k - i)) ≤ T n.

theorem Nat.DivideConquerLower.pow_mul_le {T f : ℕ → ℕ} {a b n₀ T₀ : ℕ} (h : DivideConquerLower T a b n₀ T₀ f) (ha : 1 ≤ a) (hb : 1 < b) (hnb : b ≤ n₀ + 1) (k n : ℕ) (hn : n₀ * b ^ k ≤ n) :
a ^ k * T₀ ≤ T n

Master theorem lower bound, case 1, for n₀ b ^ k ≤ n: a ^ k T₀ ≤ T n. Requires no monotonicity or polynomial assumption on f.

theorem Nat.DivideConquerLower.pow_log_mul_le {T f : ℕ → ℕ} {a b n₀ T₀ : ℕ} (h : DivideConquerLower T a b n₀ T₀ f) (ha : 1 ≤ a) (hb : 1 < b) (hnb : b ≤ n₀ + 1) {n : ℕ} (hn : n₀ ≤ n) :
a ^ log b (n / n₀) * T₀ ≤ T n

Master theorem lower bound, case 1, in terms of n: for n₀ ≤ n, a ^ ⌊log_b (n / n₀)⌋ * T₀ ≤ T n.

theorem Nat.DivideConquerLower.mul_pow_mul_le {T : ℕ → ℕ} {a b n₀ T₀ c d : ℕ} (h : DivideConquerLower T a b n₀ T₀ fun (n : ℕ) => c * n ^ d) (hb : 1 < b) (hn₀ : 1 ≤ n₀) (had : a = b ^ d) (k n : ℕ) (hn : n₀ * b ^ k ≤ n) :
c * k * (n₀ * b ^ k) ^ d ≤ T n

Master theorem lower bound, case 2 (a = b ^ d), for n₀ b ^ k ≤ n: c * k * (n₀ * b ^ k) ^ d ≤ T n.

theorem Nat.DivideConquerLower.le_of_lt {T f : ℕ → ℕ} {a b n₀ T₀ : ℕ} (h : DivideConquerLower T a b n₀ T₀ f) {n : ℕ} (hn : n₀ < n) :
f n ≤ T n

Master theorem lower bound, case 3: f n ≤ T n for every n > n₀.

structure Nat.DivideConquerEq (T : ℕ → ℕ) (a b n₀ Tmin Tmax : ℕ) (f : ℕ → ℕ) :

Matching upper and lower divide-and-conquer recurrences above n₀, with base bounds Tmin ≤ T n (for 1 ≤ n ≤ n₀) and T n ≤ Tmax (for n ≤ n₀).

  • upper : DivideConquerBound T a b n₀ Tmax f

    The upper recurrence bound T n ≤ a T(⌈n/b⌉) + f n.

  • lower : DivideConquerLower T a b n₀ Tmin f

    The lower recurrence bound a T(⌊n/b⌋) + f n ≤ T n.

theorem Nat.DivideConquerEq.of_div {T f : ℕ → ℕ} {a b n₀ Tmin Tmax : ℕ} (hb : 0 < b) (hmax : ∀ n ≤ n₀, T n ≤ Tmax) (hmin : ∀ (n : ℕ), 1 ≤ n → n ≤ n₀ → Tmin ≤ T n) (hrec : ∀ (n : ℕ), n₀ < n → T n = a * T (n / b) + f n) :
DivideConquerEq T a b n₀ Tmin Tmax f

Build DivideConquerEq from an exact floor recurrence T n = a * T (n / b) + f n.

theorem Nat.DivideConquerEq.of_ceilDiv {T f : ℕ → ℕ} {a b n₀ Tmin Tmax : ℕ} (hb : 0 < b) (hmax : ∀ n ≤ n₀, T n ≤ Tmax) (hmin : ∀ (n : ℕ), 1 ≤ n → n ≤ n₀ → Tmin ≤ T n) (hrec : ∀ (n : ℕ), n₀ < n → T n = a * T ((n + b - 1) / b) + f n) :
DivideConquerEq T a b n₀ Tmin Tmax f

Build DivideConquerEq from an exact ceiling recurrence T n = a * T ((n + b - 1) / b) + f n.