Documentation

CslibFrontier.Foundations.Data.BitString.Combination

Ranking and unranking combinations #

The weight-k indicators of length m, ordered lexicographically with false first, are numbered 0, …, C(m, k) - 1. Cslib.BitString.unrankCombination m k r is the indicator of rank r: its first bit is false exactly when r is below the C(m - 1, k) indicators that start with false. Conversely, Cslib.BitString.rankCombination bs recovers the lexicographic rank of bs, and Cslib.BitString.subsetEquiv m k packages this ranking/unranking pair with Cslib.BitString.supportOfList into a computable bijection between k-element subsets of Fin m and Fin (m.choose k).

Main definitions #

Main results #

The combination of rank r among the weight-k indicators of length m, in the lexicographic order with false first. A rank r < C(m, k) gives an indicator of weight k (count_true_unrankCombination), and distinct ranks give distinct indicators (unrankCombination_injective).

A rank below C(m, k) unranks to an indicator of weight k.

theorem Cslib.BitString.unrankCombination_injective {m k r₁ r₂ : ℕ} (hr₁ : r₁ < m.choose k) (hr₂ : r₂ < m.choose k) (heq : unrankCombination m k r₁ = unrankCombination m k r₂) :
r₁ = r₂

Distinct ranks below C(m, k) unrank to distinct indicators.

The lexicographic rank of a Boolean indicator bs among all indicators of length bs.length and weight bs.count true (false before true), inverting unrankCombination.

The rank of bs is strictly below C(bs.length, bs.count true).

@[simp]

Unranking the rank of bs at (bs.length, bs.count true) recovers bs.

@[simp]

Ranking unrankCombination m k r for r < C(m, k) recovers r.

def Cslib.BitString.subsetEquiv (m k : ℕ) :
{ S : Finset (Fin m) // S.card = k } ≃ Fin (m.choose k)

Computable bijection between k-element subsets of Fin m and Fin (m.choose k), obtained by composing rankCombination / unrankCombination with supportOfList.

@[simp]
theorem Cslib.BitString.val_subsetEquiv {m k : ℕ} (S : { S : Finset (Fin m) // S.card = k }) :
↑((subsetEquiv m k) S) = rankCombination (List.ofFn fun (u : Fin m) => decide (u ∈ ↑S))
@[simp]
theorem Cslib.BitString.coe_subsetEquiv_symm {m k : ℕ} (r : Fin (m.choose k)) :