Documentation

Complexity.Algorithms.WordRAM.PrefixSums.Tree

Balanced aggregation trees for dynamic prefix sums #

An array of 2 ^ h natural numbers is represented by a complete binary aggregation tree. The logical operations are addition at one position and summation of a prefix. Nodes are laid out in preorder in a contiguous region of 2 * 2 ^ h - 1 words. These definitions do not assign unit cost to a Lean operation: the concrete Word-RAM client supplies the execution and cost proofs.

Main definitions #

Main results #

References #

A complete binary tree with 2 ^ h leaves.

  • leaf (value : ℕ) : Tree 0

    A single array entry.

  • node {h : ℕ} (left right : Tree h) : Tree (h + 1)

    Two equally sized consecutive subarrays.

Instances For
    def Cslib.Algorithms.WordRAM.PrefixSums.instDecidableEqTree.decEq {a✝ : ℕ} (x✝ x✝¹ : Tree a✝) :
    Decidable (x✝ = x✝¹)
    Instances For
      @[instance_reducible]

      Add an increment at a list position; out-of-range positions leave the list unchanged.

      Instances For
        theorem Cslib.Algorithms.WordRAM.PrefixSums.increment_append (as bs : List ℕ) (i δ : ℕ) :
        increment (as ++ bs) i δ = if i < as.length then increment as i δ ++ bs else as ++ increment bs (i - as.length) δ

        Point increments split over concatenation at the left array's length.

        The represented array, in its original order.

        Instances For

          The aggregate stored at the root.

          Instances For

            The sum of the first k entries. Calls beyond the end are clamped.

            Instances For

              Add δ at zero-based position i. The machine interface requires i < 2 ^ h.

              Instances For

                The tree query has the ordinary array-prefix meaning.

                @[simp]
                theorem Cslib.Algorithms.WordRAM.PrefixSums.Tree.total_add {h : ℕ} (t : Tree h) (i δ : ℕ) :
                (t.add i δ).total = t.total + δ

                Point increments raise the total by precisely the increment.

                theorem Cslib.Algorithms.WordRAM.PrefixSums.Tree.leaves_add {h : ℕ} (t : Tree h) (i δ : ℕ) (hi : i < 2 ^ h) :
                (t.add i δ).leaves = increment t.leaves i δ

                The tree update is the ordinary point increment on the represented array.

                Number of words occupied by a complete aggregation tree.

                Instances For

                  The memory stores each subtree's aggregate at its root, followed by its left and right subtrees. No assertion is made about memory outside the occupied interval.

                  Instances For
                    theorem Cslib.Algorithms.WordRAM.PrefixSums.Tree.Rep.root {h b : ℕ} {mem : ℕ → ℕ} {t : Tree h} (ht : t.Rep b mem) :
                    mem b = t.total
                    theorem Cslib.Algorithms.WordRAM.PrefixSums.Tree.Rep.congr {h b : ℕ} {mem mem' : ℕ → ℕ} {t : Tree h} (ht : t.Rep b mem) (he : ∀ (a : ℕ), b ≤ a → a < b + words h → mem' a = mem a) :
                    t.Rep b mem'

                    A frame rule for the tree representation: only its contiguous footprint matters.

                    theorem Cslib.Algorithms.WordRAM.PrefixSums.Tree.Rep.update_of_outside {h a b : ℕ} {mem : ℕ → ℕ} {t : Tree h} (ht : t.Rep b mem) (ha : a < b ∨ b + words h ≤ a) (v : ℕ) :
                    t.Rep b (Function.update mem a v)

                    Writing outside the representation's region preserves it.

                    The all-zero array has a concrete, nonempty representation in zero memory.

                    Instances For