Documentation

CslibFrontier.DataStructure.Dictionary.PerfectHashing.Construction

Building an FKS table #

The randomized construction of the Fredman–Komlós–Szemerédi table [FKS84], in the form of [CLRS09], Section 11.5, for a duplicate-free list of n keys and a range-indexed universal family F (∀ r, IsTwoUniversal (F.seed r) (F.hash r)):

  1. draw top seeds into n values (range n - 1) until the squared bucket sizes sum to at most 4 n;
  2. for each bucket of b keys, draw seeds into b² values (range b² - 1) until one is injective on the bucket.

Each attempt succeeds with probability at least 1 / 2 (Collisions: Markov's inequality for the first level, and n² ≤ m for the second), so each level takes at most 2 attempts in expectation.

Cost model #

The construction is an OracleComp over the interface PerfectHashing.buildSpec F, whose calls are draw r, a fresh seed of range r, and eval r s x, the value of the function of seed s at the key x. Under the honest answers PerfectHashing.answer F a draw follows the seed law and an evaluation returns the hash value. The model is abstract: it counts seed draws (PerfectHashing.drawCost) and hash evaluations (PerfectHashing.evalCost), not word-RAM instructions. Sorting keys into buckets, checking an attempt and laying out the memory are local computation and are not charged; they use only the hash values the program obtained from its calls.

An oracle program is a finite tree of calls, so the unbounded Las Vegas loop is truncated: each level makes at most fuel attempts (OracleComp.retry), and the construction returns none if some level exhausts its fuel. The expected costs are bounded uniformly in fuel, and the probability of none decays geometrically in fuel.

Main definitions #

Main results #

References #

The interface #

The calls of the construction: draw a seed of range r, or evaluate the function of a seed at a key.

  • draw {X : Type} {F : Family X} (r : ℕ) : Query F

    Draw a fresh seed of range r.

  • eval {X : Type} {F : Family X} (r : ℕ) (s : F.Seed r) (x : X) : Query F

    Evaluate the function of the seed s of range r at the key x.

@[reducible, inline]

The answer type of a call.

@[reducible, inline]

The interface of the construction.

The honest answers: a draw follows the seed law, an evaluation returns the hash value.

@[simp]
theorem Cslib.DataStructure.Dictionary.PerfectHashing.answer_eval {X : Type} (F : Family X) (r : ℕ) (s : F.Seed r) (x : X) :
answer F (Query.eval r s x) = PMF.pure (F.hash r s x)

One attempt #

Evaluate the function of the seed s at each key, in order.

The outcome of one attempt: a seed and the observed values of its function at the keys.

  • seed : F.Seed r

    The drawn seed.

  • values : List (Fin (r + 1))

    The values of its function at the keys, in order.

One attempt: draw a seed of range r and evaluate it at every key.

def Cslib.DataStructure.Dictionary.PerfectHashing.bucketOf {X : Type} {r : ℕ} (keys : List X) (vs : List (Fin (r + 1))) (j : Fin (r + 1)) :

The keys whose observed value is j.

A top-level attempt succeeds when the squared bucket sizes sum to at most 4 n.

Instances For

    A secondary attempt succeeds when the observed values are distinct, that is, when the function is injective on the bucket.

    Instances For

      The construction #

      Build the block of a bucket: at most fuel attempts at a seed into b² values that is injective on the bucket's b keys.

      The table, once every bucket has its block.

      After a successful top-level attempt, build the block of every bucket.

      The FKS construction for the key list keys, with at most fuel attempts per level.

      One attempt under the honest answers #

      @[simp]
      theorem Cslib.DataStructure.Dictionary.PerfectHashing.evalWith_evalAll {X : Type} {F : Family X} (r : ℕ) (s : F.Seed r) (keys : List X) :
      OracleComp.evalWith (answer F) (evalAll F r s keys) = PMF.pure (List.map (F.hash r s) keys)
      theorem Cslib.DataStructure.Dictionary.PerfectHashing.expectedCost_evalAll {X : Type} {F : Family X} (cost : Query F → ENNReal) (r : ℕ) (s : F.Seed r) (keys : List X) :
      OracleComp.expectedCost (answer F) cost (evalAll F r s keys) = (List.map (fun (x : X) => cost (Query.eval r s x)) keys).sum
      theorem Cslib.DataStructure.Dictionary.PerfectHashing.evalWith_trial {X : Type} {F : Family X} (r : ℕ) (keys : List X) :
      OracleComp.evalWith (answer F) (trial F r keys) = PMF.map (fun (s : F.Seed r) => { seed := s, values := List.map (F.hash r s) keys }) (F.seed r)

      One attempt draws a seed and observes the values of its function.

      theorem Cslib.DataStructure.Dictionary.PerfectHashing.expectedCost_trial {X : Type} {F : Family X} (cost : Query F → ENNReal) (r : ℕ) (keys : List X) :
      OracleComp.expectedCost (answer F) cost (trial F r keys) = cost (Query.draw r) + ∑' (s : F.Seed r), (F.seed r) s * (List.map (fun (x : X) => cost (Query.eval r s x)) keys).sum
      @[simp]

      One attempt makes one draw.

      @[simp]

      One attempt makes one evaluation per key.

      theorem Cslib.DataStructure.Dictionary.PerfectHashing.mem_supportWith_trial {X : Type} {F : Family X} {r : ℕ} {keys : List X} {t : Trial F r} (ht : t ∈ OracleComp.supportWith (fun (q : Query F) => (answer F q).support) (trial F r keys)) :
      t.seed ∈ (F.seed r).support ∧ t.values = List.map (F.hash r t.seed) keys

      Buckets from observed values #

      theorem Cslib.DataStructure.Dictionary.PerfectHashing.bucketOf_map {X : Type} {r : ℕ} (f : X → Fin (r + 1)) (keys : List X) (j : Fin (r + 1)) :
      bucketOf keys (List.map f keys) j = List.filter (fun (x : X) => decide (f x = j)) keys
      theorem Cslib.DataStructure.Dictionary.PerfectHashing.bucketOf_sublist {X : Type} {r : ℕ} (keys : List X) (vs : List (Fin (r + 1))) (j : Fin (r + 1)) :
      (bucketOf keys vs j).Sublist keys
      theorem Cslib.DataStructure.Dictionary.PerfectHashing.sum_length_bucketOf_le {X : Type} {r : ℕ} (keys : List X) (vs : List (Fin (r + 1))) :
      ∑ j : Fin (r + 1), (bucketOf keys vs j).length ≤ keys.length
      theorem Cslib.DataStructure.Dictionary.PerfectHashing.card_filter_eq_length {X : Type} [DecidableEq X] {r : ℕ} {keys : List X} (hkeys : keys.Nodup) (f : X → Fin (r + 1)) (j : Fin (r + 1)) :
      {x ∈ keys.toFinset | f x = j}.card = (bucketOf keys (List.map f keys) j).length
      theorem Cslib.DataStructure.Dictionary.PerfectHashing.mem_bucket_iff_mem_bucketOf {X : Type} [DecidableEq X] {F : Family X} {D : Description F} {keys : List X} {vs : List (Fin (D.top.range + 1))} (hvs : vs = List.map D.top.apply keys) {j : Fin (D.top.range + 1)} {x : X} :
      x ∈ bucket D keys j ↔ x ∈ bucketOf keys vs j
      theorem Cslib.DataStructure.Dictionary.PerfectHashing.card_bucket_eq_length {X : Type} [DecidableEq X] {F : Family X} {D : Description F} {keys : List X} (hkeys : keys.Nodup) {vs : List (Fin (D.top.range + 1))} (hvs : vs = List.map D.top.apply keys) (j : Fin (D.top.range + 1)) :
      (bucket D keys j).card = (bucketOf keys vs j).length

      Correctness #

      theorem Cslib.DataStructure.Dictionary.PerfectHashing.assemble_eq_some {X : Type} {F : Family X} {top : Member F} {sec : Fin (top.range + 1) → Option (Member F)} {D : Description F} (h : assemble top sec = some D) :
      ∃ (hs : ∀ (j : Fin (top.range + 1)), (sec j).isSome = true), D = { top := top, secondary := fun (j : Fin (top.range + 1)) => (sec j).get ⋯ }
      theorem Cslib.DataStructure.Dictionary.PerfectHashing.mem_supportWith_buildSecondary {X : Type} {F : Family X} {fuel : ℕ} {bk : List X} {g : Member F} (hg : some g ∈ OracleComp.supportWith (fun (q : Query F) => (answer F q).support) (buildSecondary F fuel bk)) :
      g.range = bk.length ^ 2 - 1 ∧ Set.InjOn g.apply {x : X | x ∈ bk}
      theorem Cslib.DataStructure.Dictionary.PerfectHashing.valid_of_mem_support_build {X : Type} {F : Family X} [DecidableEq X] {fuel : ℕ} {keys : List X} (hkeys : keys.Nodup) {D : Description F} (hD : some D ∈ (OracleComp.evalWith (answer F) (build F fuel keys)).support) :
      Valid D keys

      Correctness of the construction. Under the honest answers, every table the construction returns for a duplicate-free key list satisfies the FKS conditions.

      theorem Cslib.DataStructure.Dictionary.PerfectHashing.runLookup_of_mem_support_build {X : Type} {F : Family X} [DecidableEq X] {fuel : ℕ} {keys : List X} (hkeys : keys.Nodup) {D : Description F} (hD : some D ∈ (OracleComp.evalWith (answer F) (build F fuel keys)).support) (x : X) :
      runLookup F (memory D keys) x = decide (x ∈ keys)

      A table returned by the construction answers every lookup correctly.

      theorem Cslib.DataStructure.Dictionary.PerfectHashing.size_le_of_mem_support_build {X : Type} {F : Family X} [DecidableEq X] {fuel : ℕ} {keys : List X} (hkeys : keys.Nodup) {D : Description F} (hD : some D ∈ (OracleComp.evalWith (answer F) (build F fuel keys)).support) :
      size D keys ≤ 5 * keys.length + 2

      A table returned by the construction for n keys occupies at most 5 n + 2 cells.

      Failure probability and expected cost #

      theorem Cslib.DataStructure.Dictionary.PerfectHashing.toOuterMeasure_not_topGood_le {X : Type} {F : Family X} (hF : ∀ (r : ℕ), Probability.IsTwoUniversal (F.seed r) (F.hash r)) {keys : List X} (hkeys : keys.Nodup) :
      (OracleComp.evalWith (answer F) (trial F (keys.length - 1) keys)).toOuterMeasure {t : Trial F (keys.length - 1) | ¬TopGood keys t} ≤ 2⁻¹

      A top-level attempt into n values fails with probability at most 1 / 2 (Markov's inequality on the expected squared bucket sizes, Collisions).

      A secondary attempt into b² values on a duplicate-free bucket of b keys fails with probability at most 1 / 2 (Collisions).

      The block of a bucket is built except with probability 2^{-fuel} (always, for an empty bucket and positive fuel).

      theorem Cslib.DataStructure.Dictionary.PerfectHashing.evalWith_buildRest_none_le {X : Type} {F : Family X} (hF : ∀ (r : ℕ), Probability.IsTwoUniversal (F.seed r) (F.hash r)) (fuel : ℕ) {keys : List X} (hkeys : keys.Nodup) (t : Trial F (keys.length - 1)) :
      (OracleComp.evalWith (answer F) (buildRest F fuel keys t)) none ≤ ↑(keys.length - 1 + 1) * 2⁻¹ ^ fuel

      After a successful top-level attempt, the blocks are all built except with probability n 2^{-fuel} for n ≥ 1 keys (a union bound over the buckets).

      theorem Cslib.DataStructure.Dictionary.PerfectHashing.evalWith_build_none_le {X : Type} {F : Family X} (hF : ∀ (r : ℕ), Probability.IsTwoUniversal (F.seed r) (F.hash r)) (fuel : ℕ) {keys : List X} (hkeys : keys.Nodup) :
      (OracleComp.evalWith (answer F) (build F fuel keys)) none ≤ ↑(keys.length - 1 + 2) * 2⁻¹ ^ fuel

      Failure probability of the construction. With at most fuel attempts per level, the construction for a duplicate-free list of n ≥ 1 keys fails with probability at most (n + 1) 2^{-fuel}: 2^{-fuel} for the top level and 2^{-fuel} for each of the n buckets.

      theorem Cslib.DataStructure.Dictionary.PerfectHashing.expectedCost_buildSecondary_le {X : Type} {F : Family X} (hF : ∀ (r : ℕ), Probability.IsTwoUniversal (F.seed r) (F.hash r)) (cost : Query F → ENNReal) (fuel : ℕ) {bk : List X} (hbk : bk.Nodup) :

      The block of a bucket costs at most twice one attempt in expectation, whatever the fuel.

      theorem Cslib.DataStructure.Dictionary.PerfectHashing.expectedCost_build_draw_le {X : Type} {F : Family X} (hF : ∀ (r : ℕ), Probability.IsTwoUniversal (F.seed r) (F.hash r)) (fuel : ℕ) {keys : List X} (hkeys : keys.Nodup) :
      OracleComp.expectedCost (answer F) drawCost (build F fuel keys) ≤ 2 * ↑(keys.length - 1 + 2)

      Expected seed draws. The construction for a duplicate-free list of n ≥ 1 keys draws at most 2 (n + 1) seeds in expectation, whatever the fuel: two expected attempts at the top level and at each of the n buckets.

      theorem Cslib.DataStructure.Dictionary.PerfectHashing.expectedCost_build_eval_le {X : Type} {F : Family X} (hF : ∀ (r : ℕ), Probability.IsTwoUniversal (F.seed r) (F.hash r)) (fuel : ℕ) {keys : List X} (hkeys : keys.Nodup) :

      Expected hash evaluations. The construction for a duplicate-free list of n keys makes at most 4 n hash evaluations in expectation, whatever the fuel: two expected attempts of n evaluations at the top level, and two expected attempts of b_j evaluations at each bucket.