Documentation

CslibFrontier.DataStructure.Dictionary.PerfectHashing.Table

The Fredman–Komlós–Szemerédi two-level table #

The static dictionary of Fredman, Komlós and Szemerédi [FKS84] stores a set of n keys so that membership is decided by a constant number of memory probes, in O(n) cells. This file follows the account of [CLRS09], Section 11.5. A top-level hash h₀ into n values sorts the keys into buckets B j = {x | h₀ x = j}; bucket j gets its own block of b_j² cells, where b_j = |B j|, and a secondary hash g_j into b_j² values that is injective on B j. A key x is stored at cell offset (h₀ x) + g_{h₀ x} x.

The model #

Hash functions come from a range-indexed family PerfectHashing.Family: for each r, seeds of type Seed r describe functions X → Fin (r + 1). A PerfectHashing.Description chooses a top member and one secondary member per bucket. The table occupies the cells 0, 1, 2, … of a memory of typed cells (PerfectHashing.Cell):

A cell holds one key, one seed, or one address with one seed. On a word RAM a seed of a family such as (a x + b) mod p mod m occupies O(1) words, but this file counts typed cells, not words or bits. The bound of 5 n + 2 cells is therefore O(n) words only for families whose seeds fit in O(1) words; such a family is not formalized here, and a seed of the uniformly random function family used in the tests is a whole function table. Likewise the 3 probes of a lookup count memory reads only: evaluating the hash functions is not charged.

PerfectHashing.lookup F x is an OracleComp over the memory interface (PerfectHashing.memSpec, the read of one cell per call). The algorithm depends only on the family and the key; the table is the memory it reads. It reads the top cell, then the header of h₀ x, then the slot of x, and compares the stored key with x.

Main definitions #

Main results #

References #

A range-indexed hash family on X: for each r, seeds of type Seed r, drawn from the law seed r, describe functions hash r s : X → Fin (r + 1).

  • Seed : ℕ → Type

    The seeds of the functions into Fin (r + 1).

  • seed (r : ℕ) : PMF (self.Seed r)

    The law of a fresh seed for range r.

  • hash (r : ℕ) : self.Seed r → X → Fin (r + 1)

    The function described by a seed.

A member of a family: a range r with a seed of the functions into Fin (r + 1).

  • range : ℕ

    The functions take values in Fin (range + 1).

  • seed : F.Seed self.range

    The seed.

The function described by a member.

A two-level table: a top member and, for each of its values, a secondary member.

  • top : Member F

    The top-level hash, whose values index the buckets.

  • secondary : Fin (self.top.range + 1) → Member F

    The secondary hash of each bucket.

One typed memory cell of the table.

  • top {X : Type} {F : Family X} (g : Member F) : Cell F

    The top-level member, stored in cell 0.

  • bucket {X : Type} {F : Family X} (offset : ℕ) (g : Member F) : Cell F

    The header of a nonempty bucket: the first address of its block and its secondary member.

  • key {X : Type} {F : Family X} (x : X) : Cell F

    A stored key.

  • empty {X : Type} {F : Family X} : Cell F

    An unused cell.

@[reducible, inline]

The memory interface: a call a reads cell a.

The keys of the list that the top-level hash sends to j.

The number of cells of the block of bucket j: 0 for an empty bucket, otherwise the number of values of its secondary hash.

The first address of the block of bucket j, after the top cell, the headers and the earlier blocks.

The number of cells used by the table: the top cell, the headers and the blocks.

The cell assigned to a key: its slot in the block of its bucket.

The cells of the table for the key list keys.

Each nonempty bucket's secondary hash is injective on the bucket, so its keys get distinct slots.

The conditions of the FKS scheme: n buckets for n ≥ 1 keys, total squared bucket size at most 4 n, blocks of b_j² cells, and injective secondary hashes.

Membership query of the FKS table over the memory interface: read the top member h₀ in cell 0, the header of bucket h₀ x in cell 1 + h₀ x, and, if the bucket is nonempty, the slot offset + g x given by its header, and compare the stored key with x.

Run a membership query on a memory.

theorem Cslib.DataStructure.Dictionary.PerfectHashing.mem_bucket {X : Type} {F : Family X} [DecidableEq X] {D : Description F} {keys : List X} {j : Fin (D.top.range + 1)} {x : X} :
x ∈ bucket D keys j ↔ x ∈ keys ∧ D.top.apply x = j
theorem Cslib.DataStructure.Dictionary.PerfectHashing.memory_header {X : Type} {F : Family X} [DecidableEq X] (D : Description F) (keys : List X) (j : Fin (D.top.range + 1)) :
memory D keys (1 + ↑j) = if (bucket D keys j).card = 0 then Cell.empty else Cell.bucket (offset D keys j) (D.secondary j)

The header cell of bucket j.

theorem Cslib.DataStructure.Dictionary.PerfectHashing.memory_of_lt {X : Type} {F : Family X} [DecidableEq X] {D : Description F} {keys : List X} {a : ℕ} (ha : D.top.range + 1 < a) :
memory D keys a = match List.find? (fun (x : X) => decide (slot D keys x = a)) keys with | some x => Cell.key x | none => Cell.empty

A block cell holds the key whose slot it is, if any.

theorem Cslib.DataStructure.Dictionary.PerfectHashing.offset_add_width_le {X : Type} {F : Family X} [DecidableEq X] {D : Description F} {keys : List X} {i j : Fin (D.top.range + 1)} (hij : i < j) :
offset D keys i + width D keys i ≤ offset D keys j

Blocks of different buckets do not overlap: the block of i ends before that of a later j starts.

theorem Cslib.DataStructure.Dictionary.PerfectHashing.slot_lt_offset_add_width {X : Type} {F : Family X} [DecidableEq X] {D : Description F} {keys : List X} {x : X} (hx : x ∈ keys) :
slot D keys x < offset D keys (D.top.apply x) + width D keys (D.top.apply x)

A stored key's slot lies in the block of its bucket.

theorem Cslib.DataStructure.Dictionary.PerfectHashing.slot_injOn {X : Type} {F : Family X} [DecidableEq X] {D : Description F} {keys : List X} (hD : SecondaryInjective D keys) :
Set.InjOn (slot D keys) {x : X | x ∈ keys}

The slots of the keys are distinct.

theorem Cslib.DataStructure.Dictionary.PerfectHashing.memory_slot {X : Type} {F : Family X} [DecidableEq X] {D : Description F} {keys : List X} (hD : SecondaryInjective D keys) {x : X} (hx : x ∈ keys) :
memory D keys (slot D keys x) = Cell.key x

The slot of a stored key holds that key.

theorem Cslib.DataStructure.Dictionary.PerfectHashing.mem_of_memory_eq_key {X : Type} {F : Family X} [DecidableEq X] {D : Description F} {keys : List X} {a : ℕ} {y : X} (h : memory D keys a = Cell.key y) :
y ∈ keys

Every key cell holds a key of the list.

theorem Cslib.DataStructure.Dictionary.PerfectHashing.runLookup_eq {X : Type} {F : Family X} [DecidableEq X] (mem : ℕ → Cell F) (x : X) :
runLookup F mem x = match mem 0 with | Cell.top h₀ => match mem (1 + ↑(h₀.apply x)) with | Cell.bucket off g => match mem (off + ↑(g.apply x)) with | Cell.key y => decide (y = x) | x => false | x => false | x => false

A lookup run on a memory is the case analysis on the cells it reads.

theorem Cslib.DataStructure.Dictionary.PerfectHashing.runLookup_memory {X : Type} {F : Family X} [DecidableEq X] {D : Description F} {keys : List X} (hD : SecondaryInjective D keys) (x : X) :
runLookup F (memory D keys) x = decide (x ∈ keys)

Correctness of the FKS lookup. If the secondary hashes are injective on their buckets, the lookup of x in the table of keys answers whether x is a key.

Worst-case probes. A lookup reads at most three cells, whatever the memory holds.

theorem Cslib.DataStructure.Dictionary.PerfectHashing.slot_lt_size {X : Type} {F : Family X} [DecidableEq X] {D : Description F} {keys : List X} {x : X} (hx : x ∈ keys) :
slot D keys x < size D keys

Every key's slot lies inside the table.

theorem Cslib.DataStructure.Dictionary.PerfectHashing.memory_of_size_le {X : Type} {F : Family X} [DecidableEq X] {D : Description F} {keys : List X} {a : ℕ} (ha : size D keys ≤ a) :

The table uses no cell at or beyond size.

theorem Cslib.DataStructure.Dictionary.PerfectHashing.size_eq {X : Type} {F : Family X} [DecidableEq X] {D : Description F} {keys : List X} (h : ∀ (j : Fin (D.top.range + 1)), (bucket D keys j).card ≠ 0 → (D.secondary j).range + 1 = (bucket D keys j).card ^ 2) :
size D keys = D.top.range + 2 + ∑ j : Fin (D.top.range + 1), (bucket D keys j).card ^ 2

Exact space. With blocks of b_j² cells, the table occupies the top cell, one header per bucket and ∑ b_j² block cells.

theorem Cslib.DataStructure.Dictionary.PerfectHashing.size_le {X : Type} {F : Family X} [DecidableEq X] {D : Description F} {keys : List X} (hD : Valid D keys) :
size D keys ≤ 5 * keys.length + 2

Linear space. A valid table for n keys occupies at most 5 n + 2 cells.