The canonical rounded balanced recurrence
DefinitionCLRS_Chapter4_SubstitutionExamplesThe principal constructed natural cost has B(0)=0, B(1)=1 and B(n)=B(floor(n/2))+B(ceil(n/2))+n for n at least two. The bundle includes its finite child-list and recurrence definitions and the lightweight facts needed by their interface. Subsequent nodes give monotonicity and all-size or eventual bounds. It is a numerical recurrence, with no claim that a separate source program realizes it.
Formalization Note. This content is transcribed from Algo2Lean development snapshot fd33cf959b39f9e4a9fcfda4ef31d5a36b784d41, with explicit platform import and namespace adaptation. It is not attributed to public compiler v0.1.4. The submitted definition bundle must be checked without unfinished-proof assumptions.
Catalog organization (2026-10-11). This record was retired from the active Chapter 4 catalog to focus the presentation on three algorithms and four reusable recurrence results. Its mathematical statement and proof status are unchanged, and existing imports and links remain valid. The retained results are CLRS.Chapter4Substitution.upper_comparison, CLRS.Chapter4Substitution.lower_comparison. This example-only definition module is not required by the seven retained proofs.
import Definitions.Def_CLRS_Chapter4_Substitution
import Std
set_option autoImplicit false
set_option backward.isDefEq.respectTransparency false
set_option backward.defeqAttrib.useBackward true
namespace CLRS
/-!
A genuinely arbitrary-size example of substitution. This is a numerical cost
recurrence, not a claim that an unverified source program realizes it.
The two child sizes are floor(n/2) and n-floor(n/2), so odd sizes have unequal
children. A first quadratic candidate illustrates general substitution.
Monotonicity and exact values at powers of two then give tight logarithmic
bounds for every natural size and an eventual Theta(n log₂ n) certificate.
-/
namespace Chapter4SubstitutionExamples
open Chapter4Substitution
/-- Balanced floor/ceiling splitting, including the unequal split for odd n. -/
def balancedChildren (n : Nat) : List Nat := [n / 2, n - n / 2]
/-- Both children are positive and strictly smaller above the base range. -/
theorem balanced_split_bounds (n : Nat) (large : 2 ≤ n) :
0 < n / 2 ∧ n / 2 < n ∧ 0 < n - n / 2 ∧ n - n / 2 < n := by
omega
/-- A concrete total recurrence, defined by well-founded recursion on size.
Its finite base range includes zero and one. -/
def balancedCost (n : Nat) : Nat :=
if _small : n < 2 then n
else balancedCost (n/2) + balancedCost (n-n/2) + n
termination_by n
decreasing_by
all_goals
have bounds := balanced_split_bounds n (by omega)
omega
-- Kernel-checked boundary and unequal-split instances complement the general theorem.
end Chapter4SubstitutionExamples
end CLRS