Documentation

CslibFrontier.DataStructure.FenwickTree.Basic

Fenwick Trees (Binary Indexed Trees) #

This module formalizes pure universe-polymorphic Fenwick trees (binary indexed trees, [Fenwick1994], pp. 328–332):

For a bounded-word Word-RAM implementation with checked instruction-count bounds (O(log n) per update and prefix query and O(n) initialization), see Cslib.DataStructure.FenwickTree.WordRAM.

Source Conventions #

Main definitions #

Main results #

References #

structure Cslib.DataStructure.FenwickTree (α : Type u_1) :
Type u_1

Array-backed Fenwick tree (binary indexed tree) over α.

  • tree : Array α

    Internal 0-indexed storage array where entry m - 1 holds node m's interval sum.

Instances For
    def Cslib.DataStructure.instDecidableEqFenwickTree.decEq {α✝ : Type u_1} [DecidableEq α✝] (x✝ x✝¹ : FenwickTree α✝) :
    Decidable (x✝ = x✝¹)
    @[instance_reducible]
    @[instance_reducible]
    instance Cslib.DataStructure.instReprFenwickTree {α✝ : Type u_1} [Repr α✝] :

    Logical length of the represented sequence.

    Fenwick tree representation invariant: t has length s.length and each 1-based node m (1 ≤ m ≤ t.tree.size) stores the interval sum ((s.take m).drop (m - lowbit m)).sum.

    theorem Cslib.DataStructure.FenwickTree.Valid.size_eq {α : Type u_1} [AddCommMonoid α] {t : FenwickTree α} {s : List α} (hv : t.Valid s) :
    theorem Cslib.DataStructure.FenwickTree.Valid.unique {α : Type u_1} [AddCommMonoid α] {t₁ t₂ : FenwickTree α} {s : List α} (h₁ : t₁.Valid s) (h₂ : t₂.Valid s) :
    t₁ = t₂

    Canonical representation uniqueness: Valid t s uniquely determines t.

    On a natural-number Fenwick tree valid for s, every stored cell is at most s.sum.

    Bridging List slice sums and Finset.Ico sums #

    Zero-initialized Fenwick tree #

    All-zero Fenwick tree of length n.

    @[simp]
    theorem Cslib.DataStructure.FenwickTree.size_zeros {α : Type u_1} [Zero α] (n : ℕ) :
    (zeros n).size = n

    Prefix queries (prefixSum) #

    @[irreducible]
    def Cslib.DataStructure.FenwickTree.prefixSumLoop {α : Type u_1} [AddCommMonoid α] (tree : Array α) (m : ℕ) (acc : α) :
    α

    Downward Fenwick query loop accumulating interval sums along m, m - lowbit m, ….

    Prefix sum of the first len elements ([Fenwick1994], "THE CUMULATIVE FREQUENCY", Figure 4, p. 330). Clamps len to t.size.

    theorem Cslib.DataStructure.FenwickTree.prefixSum_eq {α : Type u_1} [AddCommMonoid α] {t : FenwickTree α} {s : List α} (hv : t.Valid s) (len : ℕ) :
    t.prefixSum len = (List.take len s).sum

    Functional correctness of prefixSum ([Fenwick1994], "THE CUMULATIVE FREQUENCY", Figure 4, p. 330): on any valid tree for s, prefixSum t len equals (s.take len).sum for all len : ℕ.

    Point increments (addLoop, add) #

    @[irreducible]
    def Cslib.DataStructure.FenwickTree.addLoop {α : Type u_1} [AddCommMonoid α] (n m : ℕ) (δ : α) (tree : Array α) :

    Upward Fenwick update loop on a backing array: adds δ at 0-based slot m - 1 along m, m + lowbit m, … ≤ n.

    def Cslib.DataStructure.FenwickTree.add {α : Type u_1} [AddCommMonoid α] (t : FenwickTree α) (pos : ℕ) (δ : α) :

    Add δ at 0-based index pos ([Fenwick1994], "UPDATING THE TABLE", pp. 331–332, Figures 5–6 on p. 331). Leaves t unchanged when t.size ≤ pos.

    @[simp]
    theorem Cslib.DataStructure.FenwickTree.addLoop_size {α : Type u_1} [AddCommMonoid α] (n m : ℕ) (δ : α) (tree : Array α) :
    (addLoop n m δ tree).size = tree.size
    theorem Cslib.DataStructure.FenwickTree.valid_add {α : Type u_1} [AddCommMonoid α] {t : FenwickTree α} {s : List α} (hv : t.Valid s) (pos : ℕ) (δ : α) :
    (t.add pos δ).Valid (PrefixSums.addAt s pos δ)

    add t pos δ preserves Valid with respect to PrefixSums.addAt s pos δ ([Fenwick1994], "UPDATING THE TABLE", pp. 331–332, Figures 5–6 on p. 331).

    @[simp]
    theorem Cslib.DataStructure.FenwickTree.size_add {α : Type u_1} [AddCommMonoid α] (t : FenwickTree α) (pos : ℕ) (δ : α) :
    (t.add pos δ).size = t.size
    theorem Cslib.DataStructure.FenwickTree.add_of_size_le {α : Type u_1} [AddCommMonoid α] (t : FenwickTree α) {pos : ℕ} (δ : α) (h : t.size ≤ pos) :
    t.add pos δ = t

    Immediate-parent propagation construction (buildLoop, ofArray, ofList) #

    @[irreducible]
    def Cslib.DataStructure.FenwickTree.buildLoop {α : Type u_1} [AddCommMonoid α] (n m : ℕ) (tree : Array α) :

    Immediate-parent propagation loop: for m = 1 .. n, adds tree[m - 1] into its immediate Fenwick parent m + lowbit m when in range.

    Construct a Fenwick tree from a : Array α by adding each node m once into its immediate Fenwick parent m + lowbit m.

    Construct a Fenwick tree from s : List α.

    structure Cslib.DataStructure.FenwickTree.BuildInv {α : Type u_1} [AddCommMonoid α] (s : List α) (m : ℕ) (tree : Array α) :

    Loop invariant for immediate-parent propagation after processing nodes 1 .. m - 1.

    • size_eq : tree.size = s.length

      Backing array preserves the length of s.

    • done_eq (idx : ℕ) (x✝ : 1 ≤ idx) : idx < m → ∀ (x✝¹ : idx ≤ tree.size), tree[idx - 1] = ∑ i ∈ Finset.Ico (idx - lowbit idx) idx, s.getD i 0

      Nodes < m already hold their final Fenwick interval sums.

    • todo_eq (idx : ℕ) (x✝ : 1 ≤ idx) : m ≤ idx → ∀ (x✝¹ : idx ≤ tree.size), tree[idx - 1] = s.getD (idx - 1) 0 + ∑ c ∈ fenwickChildren idx with c < m, ∑ i ∈ Finset.Ico (c - lowbit c) c, s.getD i 0

      Nodes ≥ m hold s[idx - 1] plus contributions from processed immediate children.

    Before the first propagation step (m = 1), a satisfies BuildInv a.toList 1.

    theorem Cslib.DataStructure.FenwickTree.BuildInv.step {α : Type u_1} [AddCommMonoid α] {s : List α} {m : ℕ} {tree : Array α} (hm1 : 1 ≤ m) (hms : m ≤ s.length) (hinv : BuildInv s m tree) :
    BuildInv s (m + 1) (if m + lowbit m ≤ s.length then tree.modify (m + lowbit m - 1) fun (x : α) => x + tree[m - 1] else tree)

    Single propagation step advancing BuildInv s m to BuildInv s (m + 1).

    theorem Cslib.DataStructure.FenwickTree.BuildInv.to_valid {α : Type u_1} [AddCommMonoid α] {s : List α} {tree : Array α} (hinv : BuildInv s (s.length + 1) tree) :
    { tree := tree }.Valid s

    At completion (m = s.length + 1), BuildInv implies Valid.

    theorem Cslib.DataStructure.FenwickTree.BuildInv.getElem_le_sum {s : List ℕ} {m : ℕ} {tree : Array ℕ} (hinv : BuildInv s m tree) {idx : ℕ} (hidx1 : 1 ≤ idx) (hidx_sz : idx ≤ tree.size) :
    tree[idx - 1] ≤ s.sum

    Over ℕ, every intermediate array entry under BuildInv s m tree is bounded by s.sum.

    theorem Cslib.DataStructure.FenwickTree.BuildInv.parent_add_le_sum {s : List ℕ} {m : ℕ} {tree : Array ℕ} (hm1 : 1 ≤ m) (hms : m ≤ s.length) (hinv : BuildInv s m tree) (hp : m + lowbit m ≤ s.length) :
    tree[m + lowbit m - 1] + tree[m - 1] ≤ s.sum

    Over ℕ, adding tree[m - 1] into its in-range parent p = m + lowbit m never exceeds s.sum.

    ofArray a constructs a valid Fenwick tree representing a.toList.

    Group operations (rangeSum, get, set) #

    Half-open range sum ((s.take r).drop l).sum over [AddCommGroup α], computed as the difference prefixSum t r - prefixSum t l when l < r.

    theorem Cslib.DataStructure.FenwickTree.rangeSum_eq {α : Type u_1} [AddCommGroup α] {t : FenwickTree α} {s : List α} (hv : t.Valid s) (l r : ℕ) :
    t.rangeSum l r = (List.drop l (List.take r s)).sum
    def Cslib.DataStructure.FenwickTree.get {α : Type u_1} [AddCommGroup α] (t : FenwickTree α) (pos : ℕ) :
    α

    Point query at 0-based index pos (0 out of bounds).

    theorem Cslib.DataStructure.FenwickTree.get_eq {α : Type u_1} [AddCommGroup α] {t : FenwickTree α} {s : List α} (hv : t.Valid s) (pos : ℕ) :
    t.get pos = s.getD pos 0
    def Cslib.DataStructure.FenwickTree.set {α : Type u_1} [AddCommGroup α] (t : FenwickTree α) (pos : ℕ) (v : α) :

    Point assignment setting s[pos] := v (no-op when t.size ≤ pos).

    theorem Cslib.DataStructure.FenwickTree.valid_set {α : Type u_1} [AddCommGroup α] {t : FenwickTree α} {s : List α} (hv : t.Valid s) (pos : ℕ) (v : α) :
    (t.set pos v).Valid (s.set pos v)
    @[simp]
    theorem Cslib.DataStructure.FenwickTree.size_set {α : Type u_1} [AddCommGroup α] (t : FenwickTree α) (pos : ℕ) (v : α) :
    (t.set pos v).size = t.size
    theorem Cslib.DataStructure.FenwickTree.set_of_size_le {α : Type u_1} [AddCommGroup α] (t : FenwickTree α) {pos : ℕ} (v : α) (h : t.size ≤ pos) :
    t.set pos v = t