Documentation

CslibFrontier.Foundations.Data.Finset.LowestInsert

Maintaining the lowest elements of a growing set #

Finset.lowest k S keeps the k smallest elements of S. This file shows that it can be maintained online: inserting a new element into the current selection and selecting again gives the selection of the enlarged set (Finset.lowest_insert_lowest). A one-pass algorithm can therefore keep only k elements, as the k-minimum-values sketch for distinct elements does (CslibFrontier.Computability.Streaming.DistinctElements). It also characterizes when all selected elements satisfy a property closed downwards (Finset.forall_mem_lowest_iff); for the property · < x, this says that the k-th smallest element is below x exactly when at least k elements are.

Main results #

theorem Finset.lowest_eq_self {α : Type u_1} [LinearOrder α] {k : ℕ} {S : Finset α} (h : S.card ≤ k) :
lowest k S = S

A set with at most k elements is its own selection of k lowest elements.

theorem Finset.lowest_eq_lowest_of_subset {α : Type u_1} [LinearOrder α] {k : ℕ} {S T : Finset α} (hTS : T ⊆ S) (hT : lowest k S ⊆ T) :
lowest k T = lowest k S

If T ⊆ S contains the k lowest elements of S, then they are also the k lowest elements of T.

theorem Finset.lowest_insert_lowest {α : Type u_1} [LinearOrder α] [DecidableEq α] (k : ℕ) (x : α) (S : Finset α) :
lowest k (insert x (lowest k S)) = lowest k (insert x S)

Online selection: inserting x into the k lowest elements of S and selecting the k lowest again gives the k lowest elements of insert x S.

theorem Finset.forall_mem_lowest_iff {α : Type u_1} [LinearOrder α] {k : ℕ} {S : Finset α} (hk : k ≤ S.card) (P : α → Prop) [DecidablePred P] (hP : ∀ (x y : α), x ≤ y → P y → P x) :
(∀ x ∈ lowest k S, P x) ↔ k ≤ (filter P S).card

Order statistics of a downward-closed property: when k ≤ #S, the k lowest elements of S all satisfy a property P closed downwards exactly when at least k elements of S do.