Documentation

CslibFrontier.DataStructure.FenwickTree.Lowbit

Lowest-Set-Bit Combinatorics for Fenwick Trees #

This module develops the binary arithmetic and interval combinatorics underlying Fenwick trees (binary indexed trees, [Fenwick1994], "PRINCIPLES", pp. 328–329, and "OPERATIONS AND CODE TO HANDLE THE STRUCTURE", pp. 329–330).

For a 1-based index m ≥ 1, lowbit m is the value 2 ^ k of the least significant 1-bit of m (conventionally written m & -m in two's-complement arithmetic), and node m aggregates the half-open 0-based index interval [m - lowbit m, m):

Main definitions #

Main results #

References #

Binary lowest set bit (lowbit) #

@[irreducible]

Value 2 ^ k of the least significant 1-bit of n (0 when n = 0), defined by binary recursion on ℕ (modeling two's-complement n & -n on fixed-width words).

theorem Cslib.DataStructure.FenwickTree.exists_odd_mul_lowbit {n : ℕ} (hn : 0 < n) :
∃ (q : ℕ) (k : ℕ), lowbit n = 2 ^ k ∧ n = (2 * q + 1) * 2 ^ k

Odd-quotient power-of-two factorization of a positive natural via lowbit.

theorem Cslib.DataStructure.FenwickTree.two_pow_le_lowbit_of_dvd {n k : ℕ} (hn : 0 < n) (hdvd : 2 ^ k ∣ n) :
2 ^ k ≤ lowbit n

Any power of two dividing n > 0 is bounded by lowbit n.

Stepping downward m ↦ m - lowbit m at least doubles lowbit whenever m - lowbit m > 0.

Stepping upward m ↦ m + lowbit m at least doubles lowbit for all m > 0.

Parent-interval bridge #

theorem Cslib.DataStructure.FenwickTree.fenwick_parent_bridge {m idx : ℕ} (hm : 0 < m) (hlt : m < idx) :
idx - lowbit idx < m ↔ idx - lowbit idx < m + lowbit m ∧ m + lowbit m ≤ idx

Fundamental Fenwick parent-interval bridge: for 0 < m < idx, idx's interval [idx - lowbit idx, idx) reaches below m iff it reaches below m's Fenwick parent m + lowbit m and m + lowbit m ≤ idx.

Downward and upward traversal chains #

@[irreducible]

Downward Fenwick query chain m, m - lowbit m, … down to 0.

@[irreducible]

Upward Fenwick update chain m, m + lowbit m, … within [1, n].

Every downward Fenwick query chain from m > 0 has length at most ⌊log₂ m⌋ + 1.

Uniform ⌈log₂ (n + 1)⌉ bound on downChain m for all m ≤ n (0 when n = 0).

theorem Cslib.DataStructure.FenwickTree.length_upChain_le_log {n m : ℕ} (hm : 0 < m) (hmn : m ≤ n) :
(upChain n m).length ≤ Nat.log 2 n + 1

Every upward Fenwick update chain from 1 ≤ m ≤ n has length at most ⌊log₂ n⌋ + 1.

Uniform ⌈log₂ (n + 1)⌉ bound on upChain n m (0 when n = 0).

theorem Cslib.DataStructure.FenwickTree.mem_upChain_iff {n m idx : ℕ} (hm : 0 < m) :
idx ∈ upChain n m ↔ m ≤ idx ∧ idx ≤ n ∧ idx - lowbit idx < m

Exact Fenwick ancestor characterization: starting from m > 0, idx is visited by upChain n m iff m ≤ idx ≤ n and idx's Fenwick interval [idx - lowbit idx, idx) contains m - 1 (i.e. idx - lowbit idx < m).

Immediate-children partition of [idx - lowbit idx, idx) #

Immediate Fenwick children of idx: positive indices c < idx with c + lowbit c = idx.

Intervals [c - lowbit c, c) of distinct immediate Fenwick children of idx are disjoint.

{idx - 1} is disjoint from all immediate-child intervals [c - lowbit c, c).

theorem Cslib.DataStructure.FenwickTree.insert_biUnion_fenwickChildren {idx : ℕ} (hidx : 0 < idx) :
insert (idx - 1) ((fenwickChildren idx).biUnion fun (c : ℕ) => Finset.Ico (c - lowbit c) c) = Finset.Ico (idx - lowbit idx) idx

For idx > 0, {idx - 1} and the intervals [c - lowbit c, c) of c ∈ fenwickChildren idx partition [idx - lowbit idx, idx).