Documentation

CslibFrontier.Computability.Streaming.Basic

One-pass streaming algorithms #

A deterministic one-pass streaming algorithm (Cslib.Streaming.Algorithm σ α β) reads a stream s : List α of items from left to right. It starts in a memory state init : σ, replaces its state q by update q a on reading each item a, and finally reports output q : β. Updates are arbitrary functions, so the model is non-uniform: it charges only the memory that survives between items, which is the resource bounded by communication arguments [AlonMatiasSzegedy1999, Section 3].

Space. A.SpaceLE m b says that the memory states A reaches on streams of at most m items (Cslib.Streaming.Algorithm.reachable) have an injective encoding by b-bit strings, that is, there are at most 2 ^ b of them (Cslib.Streaming.Algorithm.spaceLE_of_subset_finset). The space of an algorithm is thus log₂ of the number of its reachable states, rounded up; it does not count states that no short stream reaches.

Randomness. A randomized streaming algorithm (Cslib.Streaming.Randomized σ α β) is a distribution over deterministic ones: its random choices are drawn before the stream is read, as for public-coin protocols (Cslib.Communication.PublicCoin). Its memory bound (Cslib.Streaming.Randomized.SpaceLE) is checked separately for each algorithm in the support. Since the update function of a drawn algorithm may depend on the random choices, the random bits are free: they are not charged to the memory. This is the convention under which communication lower bounds transfer. An algorithm that tosses fresh coins at each item becomes such a distribution, for example after it counts the items it has read, which costs ⌈log₂ (m + 1)⌉ more bits on streams of at most m items; so lower bounds of order n still apply to it, up to that additive term. μ.Solves ε R m says that on every stream of at most m items, the answer of a random algorithm drawn from μ is related to the stream by R except with probability at most ε.

Cslib.Streaming.Algorithm.reduce prefixes fixed items to the stream, translates the items and post-processes the answer; it preserves memory bounds, which is how streaming lower bounds pass between problems.

Main definitions #

Main results #

References #

structure Cslib.Streaming.Algorithm (σ : Type u_1) (α : Type u_2) (β : Type u_3) :
Type (max (max u_1 u_2) u_3)

A deterministic one-pass streaming algorithm over items of type α with outputs in β: memory states σ, an initial state, an update reading one item, and an output function.

  • init : σ

    The initial memory state.

  • update : σ → α → σ

    The memory state after reading one more item.

  • output : σ → β

    The answer reported in a memory state.

Instances For
    def Cslib.Streaming.Algorithm.stateFrom {σ : Type u_1} {α : Type u_2} {β : Type u_3} (A : Algorithm σ α β) (q : σ) (s : List α) :
    σ

    The memory state after reading the stream s from q.

    Instances For
      def Cslib.Streaming.Algorithm.state {σ : Type u_1} {α : Type u_2} {β : Type u_3} (A : Algorithm σ α β) (s : List α) :
      σ

      The memory state after reading the stream s.

      Instances For
        def Cslib.Streaming.Algorithm.run {σ : Type u_1} {α : Type u_2} {β : Type u_3} (A : Algorithm σ α β) (s : List α) :
        β

        The answer after reading the stream s.

        Instances For
          @[simp]
          theorem Cslib.Streaming.Algorithm.stateFrom_nil {σ : Type u_1} {α : Type u_2} {β : Type u_3} (A : Algorithm σ α β) (q : σ) :
          A.stateFrom q [] = q
          @[simp]
          theorem Cslib.Streaming.Algorithm.stateFrom_cons {σ : Type u_1} {α : Type u_2} {β : Type u_3} (A : Algorithm σ α β) (q : σ) (a : α) (s : List α) :
          A.stateFrom q (a :: s) = A.stateFrom (A.update q a) s
          theorem Cslib.Streaming.Algorithm.stateFrom_append {σ : Type u_1} {α : Type u_2} {β : Type u_3} (A : Algorithm σ α β) (q : σ) (s t : List α) :
          A.stateFrom q (s ++ t) = A.stateFrom (A.stateFrom q s) t
          theorem Cslib.Streaming.Algorithm.state_append {σ : Type u_1} {α : Type u_2} {β : Type u_3} (A : Algorithm σ α β) (s t : List α) :
          A.state (s ++ t) = A.stateFrom (A.state s) t
          def Cslib.Streaming.Algorithm.reachable {σ : Type u_1} {α : Type u_2} {β : Type u_3} (A : Algorithm σ α β) (m : ℕ) :
          Set σ

          The memory states reachable on streams of length at most m.

          Instances For
            theorem Cslib.Streaming.Algorithm.state_mem_reachable {σ : Type u_1} {α : Type u_2} {β : Type u_3} (A : Algorithm σ α β) {m : ℕ} {s : List α} (hs : s.length ≤ m) :
            theorem Cslib.Streaming.Algorithm.reachable_mono {σ : Type u_1} {α : Type u_2} {β : Type u_3} (A : Algorithm σ α β) {m m' : ℕ} (h : m ≤ m') :
            A.reachable m ⊆ A.reachable m'
            def Cslib.Streaming.Algorithm.SpaceLE {σ : Type u_1} {α : Type u_2} {β : Type u_3} (A : Algorithm σ α β) (m b : ℕ) :

            A uses at most b bits of memory on streams of length at most m: the memory states it reaches on such streams have an injective encoding by b-bit strings, that is, there are at most 2 ^ b of them.

            Instances For
              theorem Cslib.Streaming.Algorithm.SpaceLE.mono {σ : Type u_1} {α : Type u_2} {β : Type u_3} {A : Algorithm σ α β} {m m' b : ℕ} (h : A.SpaceLE m' b) (hm : m ≤ m') :
              A.SpaceLE m b
              theorem Cslib.Streaming.Algorithm.exists_injOn_of_subset_finset {σ : Type u_1} {S : Set σ} {T : Finset σ} (hST : S ⊆ ↑T) {b : ℕ} (hT : T.card ≤ 2 ^ b) :
              ∃ (enc : σ → BitString b), Set.InjOn enc S

              A set of states contained in a finite set of at most 2 ^ b states has an injective b-bit encoding.

              theorem Cslib.Streaming.Algorithm.spaceLE_of_subset_finset {σ : Type u_1} {α : Type u_2} {β : Type u_3} {A : Algorithm σ α β} {m b : ℕ} {T : Finset σ} (hT : A.reachable m ⊆ ↑T) (hcard : T.card ≤ 2 ^ b) :
              A.SpaceLE m b

              An algorithm whose reachable states lie in a finite set of at most 2 ^ b states uses at most b bits of memory.

              theorem Cslib.Streaming.Algorithm.spaceLE_of_card_le {σ : Type u_1} {α : Type u_2} {β : Type u_3} {A : Algorithm σ α β} [Fintype σ] {m b : ℕ} (h : Fintype.card σ ≤ 2 ^ b) :
              A.SpaceLE m b

              An algorithm with at most 2 ^ b memory states uses at most b bits of memory.

              def Cslib.Streaming.Algorithm.reduce {σ : Type u_1} {α : Type u_2} {β : Type u_3} (A : Algorithm σ α β) {α' : Type u_4} {β' : Type u_5} (p : List α) (g : α' → α) (d : β → β') :
              Algorithm σ α' β'

              The algorithm that first reads the items p, then reads each item a as g a, and answers d of the answer of A.

              Instances For
                theorem Cslib.Streaming.Algorithm.state_reduce {σ : Type u_1} {α : Type u_2} {β : Type u_3} {A : Algorithm σ α β} {α' : Type u_4} {β' : Type u_5} (p : List α) (g : α' → α) (d : β → β') (s : List α') :
                (A.reduce p g d).state s = A.state (p ++ List.map g s)
                theorem Cslib.Streaming.Algorithm.run_reduce {σ : Type u_1} {α : Type u_2} {β : Type u_3} {A : Algorithm σ α β} {α' : Type u_4} {β' : Type u_5} (p : List α) (g : α' → α) (d : β → β') (s : List α') :
                (A.reduce p g d).run s = d (A.run (p ++ List.map g s))
                theorem Cslib.Streaming.Algorithm.SpaceLE.reduce {σ : Type u_1} {α : Type u_2} {β : Type u_3} {A : Algorithm σ α β} {α' : Type u_4} {β' : Type u_5} {m b : ℕ} (p : List α) (h : A.SpaceLE (p.length + m) b) (g : α' → α) (d : β → β') :
                (A.reduce p g d).SpaceLE m b

                Reductions keep memory bounds, with the prefix counted in the stream length.

                Randomized streaming algorithms #

                @[reducible, inline]
                abbrev Cslib.Streaming.Randomized (σ : Type u_1) (α : Type u_2) (β : Type u_3) :
                Type (max (max u_3 u_2) u_1)

                A randomized streaming algorithm with public randomness: a distribution over deterministic streaming algorithms.

                Instances For
                  noncomputable def Cslib.Streaming.Randomized.errorProb {σ : Type u_1} {α : Type u_2} {β : Type u_3} (μ : Randomized σ α β) (R : List α → β → Prop) (s : List α) :

                  The probability that the algorithm drawn from μ gives an answer on the stream s that is not related to s by R.

                  Instances For
                    def Cslib.Streaming.Randomized.Solves {σ : Type u_1} {α : Type u_2} {β : Type u_3} (μ : Randomized σ α β) (ε : ℝ) (R : List α → β → Prop) (m : ℕ) :

                    μ answers R with error at most ε on streams of length at most m.

                    Instances For
                      def Cslib.Streaming.Randomized.SpaceLE {σ : Type u_1} {α : Type u_2} {β : Type u_3} (μ : Randomized σ α β) (m b : ℕ) :

                      Every algorithm that μ can draw uses at most b bits of memory on streams of length at most m.

                      Instances For
                        theorem Cslib.Streaming.Randomized.Solves.mono {σ : Type u_1} {α : Type u_2} {β : Type u_3} {μ : Randomized σ α β} {ε ε' : ℝ} {R : List α → β → Prop} {m m' : ℕ} (h : μ.Solves ε R m) (hε : ε ≤ ε') (hm : m' ≤ m) :
                        μ.Solves ε' R m'
                        theorem Cslib.Streaming.Randomized.SpaceLE.mono {σ : Type u_1} {α : Type u_2} {β : Type u_3} {μ : Randomized σ α β} {m m' b : ℕ} (h : μ.SpaceLE m b) (hm : m' ≤ m) :
                        μ.SpaceLE m' b
                        theorem Cslib.Streaming.Randomized.solves_pure {σ : Type u_1} {α : Type u_2} {β : Type u_3} (A : Algorithm σ α β) {R : List α → β → Prop} {m : ℕ} (hA : ∀ (s : List α), s.length ≤ m → R s (A.run s)) :
                        Solves (PMF.pure A) 0 R m

                        A deterministic algorithm that is always correct solves with error zero.

                        theorem Cslib.Streaming.Randomized.spaceLE_pure {σ : Type u_1} {α : Type u_2} {β : Type u_3} {A : Algorithm σ α β} {m b : ℕ} (hA : A.SpaceLE m b) :

                        A deterministic algorithm keeps its memory bound as a randomized one.

                        theorem Cslib.Streaming.Randomized.errorProb_map_uniformOfFintype {σ : Type u_1} {α : Type u_2} {β : Type u_3} {Ω : Type u_4} [Fintype Ω] [Nonempty Ω] (F : Ω → Algorithm σ α β) (R : List α → β → Prop) (s : List α) [DecidablePred fun (ω : Ω) => ¬R s ((F ω).run s)] :
                        errorProb (PMF.map F (Probability.PMF.uniformOfFintype Ω)) R s = ↑{ω : Ω | ¬R s ((F ω).run s)}.dens

                        The error of an algorithm drawn with a uniformly random seed is the fraction of the seeds on which it errs.

                        theorem Cslib.Streaming.Randomized.errorProb_map_reduce_le {σ : Type u_1} {α : Type u_2} {β : Type u_3} {α' : Type u_4} {β' : Type u_5} (μ : Randomized σ α β) (p : List α) (g : α' → α) (d : β → β') {R : List α → β → Prop} {R' : List α' → β' → Prop} (s : List α') (hR : ∀ (c : β), R (p ++ List.map g s) c → R' s (d c)) :
                        errorProb (PMF.map (fun (A : Algorithm σ α β) => A.reduce p g d) μ) R' s ≤ μ.errorProb R (p ++ List.map g s)

                        Reducing every algorithm drawn from μ to R' by Algorithm.reduce keeps the error, when the answer of A related to the reduced stream by R is mapped by d to an answer related to the original stream by R'.

                        theorem Cslib.Streaming.Randomized.SpaceLE.map_reduce {σ : Type u_1} {α : Type u_2} {β : Type u_3} {α' : Type u_4} {β' : Type u_5} {μ : Randomized σ α β} {m b : ℕ} (p : List α) (h : μ.SpaceLE (p.length + m) b) (g : α' → α) (d : β → β') :
                        SpaceLE (PMF.map (fun (A : Algorithm σ α β) => A.reduce p g d) μ) m b

                        Reducing every algorithm drawn from μ keeps the memory bound, with the prefix counted in the stream length.