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):
- cell
0holds the top member; - cell
1 + jholds the header of bucketj:emptyfor an empty bucket, otherwise the address of its block together with its secondary member; - the blocks follow, one after the other, and slot
offset j + g_j xholds the keyx.
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 #
PerfectHashing.Family,PerfectHashing.Member: range-indexed hash families and their members.PerfectHashing.Description,PerfectHashing.bucket,PerfectHashing.width,PerfectHashing.offset,PerfectHashing.slot,PerfectHashing.size: the description of a table and its layout.PerfectHashing.Cell,PerfectHashing.memory: the cells of the table for a key list.PerfectHashing.lookup,PerfectHashing.runLookup: membership as a probe program, and its run on a memory.PerfectHashing.SecondaryInjective,PerfectHashing.Valid: injective secondary hashes, and the full FKS conditions (nbuckets,∑ b_j² ≤ 4n, blocks ofb_j²cells).
Main results #
PerfectHashing.runLookup_memory: correctness,lookup xanswersx ∈ keyswhenever the secondary hashes are injective on their buckets.PerfectHashing.isQueryBounded_lookup: worst-case probes, every lookup reads at most3cells, whatever the memory.PerfectHashing.size_eq,PerfectHashing.size_le: space, a valid table occupies exactlyn + 1 + ∑ b_j²cells forn ≥ 1, at most5 n + 2, andPerfectHashing.memory_of_size_lesays every later cell is empty.
References #
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.
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.
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.
One bucket per key (one bucket when there is no key).
The squared bucket sizes sum to at most
4 n.- secondary_range (j : Fin (D.top.range + 1)) : (bucket D keys j).card ≠ 0 → (D.secondary j).range + 1 = (bucket D keys j).card ^ 2
A nonempty bucket of
bkeys has a block ofb²cells. - secondaryInjective : SecondaryInjective D keys
The secondary hashes are injective on their buckets.
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.
The header cell of bucket j.
A block cell holds the key whose slot it is, if any.
Blocks of different buckets do not overlap: the block of i ends before that of a later j
starts.
A stored key's slot lies in the block of its bucket.
The slots of the keys are distinct.
The slot of a stored key holds that key.
Every key cell holds a key of the list.
A lookup run on a memory is the case analysis on the cells it reads.
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.
Every key's slot lies inside the table.
The table uses no cell at or beyond size.
Exact space. With blocks of b_j² cells, the table occupies the top cell, one header per
bucket and ∑ b_j² block cells.
Linear space. A valid table for n keys occupies at most 5 n + 2 cells.