Imports
import Mathlib.Tactic

4.3. The Substitution Method

This file packages the CLRS substitution method as reusable induction principles for one-step recurrence bounds. The core idea is intentionally small: a guessed bound is a loop invariant over the recurrence index.

Main results:

  • Theorem CLRS.Chapter04.substitution_upper_bound: base plus step preservation proves an upper bound.

  • Theorem CLRS.Chapter04.substitution_lower_bound: the corresponding lower-bound principle.

  • Theorem CLRS.Chapter04.substitution_sandwich: simultaneous lower and upper bounds.

  • Theorems CLRS.Chapter04.linear_substitution_upper_bound and CLRS.Chapter04.geometric_substitution_upper_bound: ready-to-use CLRS-style templates for common recurrence guesses.

Current gaps:

  • These lemmas model the substitution proof method, not a full recurrence parser. Later divide-and-conquer sections can instantiate them after deriving the appropriate one-step inequality from floors, ceilings, or exact-power reductions.

namespace CLRSnamespace Chapter04

A proposed upper bound for a cost function.

def IsUpperBound (T B : ) : Prop := n, T n B n

A proposed lower bound for a cost function.

def IsLowerBound (T B : ) : Prop := n, B n T n

The basic substitution-method upper-bound principle: prove the proposed bound at the base case, then prove that one recurrence step preserves it.

theorem substitution_upper_bound (T B : ) (h0 : T 0 B 0) (hstep : n, T n B n T (n + 1) B (n + 1)) : IsUpperBound T B := induction n with

The lower-bound form of the substitution method. It has the same proof shape as the upper-bound theorem, but the guessed function is below the recurrence.

theorem substitution_lower_bound (T B : ) (h0 : B 0 T 0) (hstep : n, B n T n B (n + 1) T (n + 1)) : IsLowerBound T B := induction n with

If the lower and upper guesses are both preserved by the recurrence step, then the true cost function is sandwiched between them for every index.

theorem substitution_sandwich (T L U : ) (hL0 : L 0 T 0) (hU0 : T 0 U 0) (hLstep : n, L n T n L (n + 1) T (n + 1)) (hUstep : n, T n U n T (n + 1) U (n + 1)) : IsLowerBound T L IsUpperBound T U :=

Common recurrence templates

Linear additive upper bounds: if each step adds at most inc, then the cost is at most the base bound plus inc * n.

theorem linear_substitution_upper_bound (T : ) {base inc : } (h0 : T 0 base) (hstep : n, T (n + 1) T n + inc) : n : , T n base + inc * (n : ) := induction n with calc T (n + 1) T n + inc := hstep n _ (base + inc * (n : )) + inc := _ = base + inc * ((n + 1 : ) : ) :=

Linear additive lower bounds: if each step adds at least inc, then the cost is at least the base bound plus inc * n.

theorem linear_substitution_lower_bound (T : ) {base inc : } (h0 : base T 0) (hstep : n, T n + inc T (n + 1)) : n : , base + inc * (n : ) T n := induction n with calc base + inc * ((n + 1 : ) : ) = (base + inc * (n : )) + inc := _ T n + inc := _ T (n + 1) := hstep n

Geometric upper bounds: a nonnegative multiplicative step preserves a guessed bound of the form base * a^n.

Geometric lower bounds, dual to CLRS.Chapter04.geometric_substitution_upper_bound.

_ a * T n := _ T (n + 1) := hstep n
end Chapter04end CLRS