Imports
open Finsetopen Filteropen scoped BigOperators

5.1. The Hiring Problem

This file proves the finite symmetry calculation behind the CLRS hiring problem. At step n+1, the new candidate is hired exactly when the best among the first n+1 candidates is in the new candidate's position. Under the uniform rank model, that event has probability 1/(n+1). Summing these indicator expectations gives the harmonic number.

Main result:

  • Theorem CLRS.Chapter05.uniformAverage_indicator_singleton: a singleton event in a finite uniform space has probability 1/m.

  • Theorem CLRS.Chapter05.hireProbability_eq: the hire probability at step n+1 is 1/(n+1).

  • Theorem CLRS.Chapter05.expectedHiresByIndicators_eq_harmonic: summing the indicator expectations gives the harmonic number.

  • Theorem CLRS.Chapter05.expectedHires_eq_harmonic: the equivalent recurrence solution equals the harmonic number.

  • Theorem CLRS.Chapter05.expectedHires_isBigTheta_log: expected hires grow logarithmically.

Current gaps:

  • None for the current finite rank-symmetry model. A lower-level model of random permutations and pseudocode execution remains a future refinement.

namespace CLRSnamespace Chapter05

Finite uniform expectation model

Uniform average over the finite sample space {0, ..., m-1}.

noncomputable def uniformAverageRange (m : ) (X : ) : := ( i range m, X i) / (m : )

A 0/1 indicator as a real-valued random variable.

def indicator (P : Prop) [Decidable P] : := if P then 1 else 0

In a finite uniform space of size m, a singleton event has probability 1/m.

Hiring probabilities from symmetry

At step n+1, index n is the new candidate's position in a rank-symmetry sample space of size n+1.

def newCandidateIsBest (n rankOfBest : ) : Prop := rankOfBest = n
instance newCandidateIsBestDecidable (n rankOfBest : ) : Decidable (newCandidateIsBest n rankOfBest) := inferInstanceAs (Decidable (rankOfBest = n))

The probability that the new candidate is the best among the first n+1.

noncomputable def hireProbability (n : ) : := uniformAverageRange (n + 1) (fun rankOfBest => indicator (rankOfBest = n))

The single-step hiring probability is 1/(n+1) by finite symmetry.

Harmonic numbers

The n-th harmonic number, written as Σ_{i=0}^{n-1} 1/(i+1).

noncomputable def harmonic (n : ) : := i range n, 1 / ((i : ) + 1)
@[simp] lemma harmonic_zero : harmonic 0 = 0 :=

Successor recurrence for harmonic numbers.

lemma harmonic_succ (n : ) : harmonic (n + 1) = harmonic n + 1 / ((n : ) + 1) :=

Harmonic numbers are positive once the index is positive.

Expected number of hires

Expected hires as a sum of indicator expectations.

noncomputable def expectedHiresByIndicators (n : ) : := i range n, hireProbability i

Linearity of expectation reduces the hiring problem to the harmonic sum.

theorem expectedHiresByIndicators_eq_harmonic (n : ) : expectedHiresByIndicators n = harmonic n :=

Expected number of hires from n candidates, assuming the CLRS recurrence obtained from the finite rank-symmetry argument.

noncomputable def expectedHires : | 0 => 0 | n + 1 => expectedHires n + 1 / ((n : ) + 1)

The expected-hire recurrence has the harmonic-number closed form.

The recurrence and indicator-sum views of the expected hires agree.

Asymptotic growth

The real harmonic sum used in this section agrees with Mathlib's rational harmonic numbers after casting to .

theorem harmonic_eq_mathlib_harmonic (n : ) : harmonic n = (_root_.harmonic n : ) :=

The real harmonic sum in this section has logarithmic growth.

The CLRS expected number of hires is logarithmic: E[X] = Θ(log n).

end Chapter05end CLRS