ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Theory of sets

The headers of include/ADS/set realize the SET of chapter 8 of the book (on a boolean vector, on an unsorted and on a sorted list, on any dictionary) with its operations of two sets, and, beyond the book, three families that the engineering of the sets has added to it: the compressed bitmaps of Roaring, the intersections of sorted sequences of chapter 6, Set Intersection, of the book of Ferragina, and the filters, i.e., the sets that may answer yes for an item they do not hold. This page collects what the headers take for granted: the definitions and the notation (see Definitions and notation), the realizations and the costs of their operations with the reason of each (see Realizations and their costs), the lower bounds and the algorithms of the operations of two sets (see Operations of two sets and Intersections of sorted sequences), and the analysis of the filters, from the lower bound on their space to the false positives of each of them (see Filters), and of the filters of ranges (see Range filters), and the space-efficient sorted set of Morin (see The space-efficient sorted set). The pseudocode of each algorithm is instead on the page of its header, in the comment of its function, together with the section of the book, or the paper, that it follows.

Definitions and notation

A set \( A \) is a finite collection of distinct items of a universe \( U \); its operations are those of the concept Set of ADT.h, i.e., contains(), insert() and remove(), with forEach(), which visits the items, and the four operations of two sets, the union \( A \cup B \), the intersection \( A \cap B \), the difference \( A \setminus B \) and the symmetric difference \( A \triangle B = ( A \setminus B ) \cup ( B \setminus A ) \), called setUnion(), setIntersection(), setDifference() and setSymmetricDifference(), since union is a keyword of C++ (chapter 8 of the book). A multiset may hold an item more than once, and its multiplicity is the number of its occurrences.

A filter of a set \( S \) of \( n \) keys answers a query \( \mathrm{maybe}( x ) \) with no false negative, i.e., \( \mathrm{maybe}( x ) \) is true for every \( x \in S \), and with a false positive, i.e., true for an \( x \notin S \), with a probability \( \varepsilon \) over the choice of its hash functions, the false positive rate. A filter is static when it is built once from \( S \), incremental when it takes insertions one at a time, and dynamic when it takes removals too.

The symbols of the headers of the area
SymbolMeaning
\( n \), \( m \)the sizes of the two sets \( A \) and \( B \) of an operation, \( m \le n \) for the intersections; in a filter, \( n \) is the number of keys and \( m \) the bits, the places or the rows of its array, and in a hash table \( m \) is the size of the table
\( N \)the dimension of a boolean vector, whose items are the integers of \( [ 0 , N ) \)
\( \varepsilon \)the false positive rate of a filter
\( k \), \( r \)the number of hash functions of a filter of Bloom (\( k \) in BloomFilter, \( r \) in BloomVariants.h); in a Ribbon filter, \( r \) is the bits of a result
\( f \), \( b \)the bits of a fingerprint of a cuckoo filter and of a binary fuse filter
\( \alpha \)the load of a table, the fraction of its places that are taken
\( \Delta_j \)the gap between the places of two consecutive items of the shorter list in the longer one, in a search by doubling
\( L \)the length of a block of the two-level storage, of a bucket, or of a segment of a binary fuse filter

Realizations and their costs

The library has six realizations of the SET, which differ in the universe they accept, in the space and in the time of the operations; the bounds of the hash tables are expected, over the choice of the hash function, and those of the operations of two sets are written for \( | A | = n \) and \( | B | = m \).

The realizations of the SET and the cost of their operations (chapter 8 of the book)
RealizationSpacecontains(), insert(), remove()min(), max()forEach()operations of two sets
BoolSet\( \Theta( N ) \) bits\( O( 1 ) \)\( O( N ) \)\( O( N ) \)\( \Theta( N ) \), \( \lceil N / 64 \rceil \) operations on words
ListSet\( \Theta( n ) \)\( O( n ) \) (\( O( 1 ) \) for insertNew())\( O( n ) \)\( O( n ) \)\( O( n m ) \)
SortedListSet\( \Theta( n ) \)\( O( n ) \)\( O( 1 ) \)\( O( n ) \)\( O( n + m ) \)
ChainedHashSet, OpenHashSet, LinkedHashSet\( \Theta( n + m ) \)\( O( 1 ) \) expected\( O( n + m ) \)\( O( n + m ) \), \( O( m ) \), \( O( n ) \)\( O( \max( m_A , n_A ) + \max( m_B , n_B ) ) \) expected
TreeSet\( \Theta( n ) \)\( O( \log n ) \)\( O( \log n ) \)\( O( n ) \), in order\( O( n + m \log( n + m ) ) \)
SpaceEfficientSortedSet\( n + O( n / b + b ) \) items\( O( \log n + b ) \) amortized expected\( O( \log n ) \) expected\( O( n ) \), in ordernone
RoaringBitmapat most \( 2 n \) bytes and \( | U | / 8 \) bytes, about\( O( \log c + 4096 ) \) for \( c \) containers\( O( 1 ) \) to reach\( O( n + c ) \)a merge of the containers, each pair in \( O( 4096 ) \)

The boolean vector is the only realization whose operations do not depend on the items held but on the universe: it is preferable when \( N \) is not much larger than the sets, and its scans go 64 items at a time, a word per step, which leaves the bounds as they are and divides the time by up to 64. The unsorted list accepts any type with an equality, and its operations of two sets cost \( O( n m ) \), which is optimal under that only assumption (see Operations of two sets); insertNew() costs \( O( 1 ) \) since it skips the search of a duplicate, which the operations of two sets can do whenever the item is known to be new. The sorted list needs a total order, keeps its minimum and maximum at its ends, and merges two sets in \( O( n + m ) \) time. The hash tables give \( O( 1 ) \) expected time to the three operations on single items, and pay the size \( m \) of the table in every scan (all the \( m \) places are read, plus the \( n \) cells of the chains with chaining); LinkedHashSet threads a list through the items, so that its scans cost \( O( n ) \) in the order of insertion (exercise 8.9). The Red-Black tree keeps the order, and its operations of two sets insert the items of one set into a copy of the other, \( m \) insertions of \( O( \log( n + m ) ) \) (exercise 8.7).

The Roaring bitmap cuts the universe of the integers of 32 bits into chunks of \( 2^{16} \) by their top 16 bits, and keeps each chunk that holds an item as a sorted array of the low 16 bits of its items when it holds at most 4096 of them, and as a vector of \( 2^{16} \) bits otherwise. The two forms cost the same, 8 KB, exactly at 4096 items ( \( 4096 \cdot 16 = 2^{16} \) bits), and the threshold takes the smaller one in either case; hence no container takes more than 8 KB, an item costs at most 2 bytes in an array, and a set takes at most about \( \min( 2 n , | U | / 8 ) \) bytes, the space of a sorted array on the sparse chunks and that of a boolean vector on the dense ones.

The sets of Morin's book

Chapter 1 of Morin's Open Data Structures has two interfaces of sets, stated as concepts by SetInterfaces.h: the USET, whose find( x ) returns the item equal to \( x \) (an item may carry more than the key that the equality reads), and the SSET, whose find( x ) is the successor search, the least item not smaller than \( x \), here lowerBound( x ); insert() and remove() return whether they changed the set. ArrayUSet and ArraySSet are their realizations on a vector that exercise 1.6 asks for, the oracles of the faster ones: find() scans the unsorted vector in \( O( n ) \), while the sorted one answers find() and lowerBound() by binary search in \( O( \log n ) \) and moves the items after the place of a change by one block move, which is what exercise 1.7 asks to improve first.

The dictionaries of the library are sets of Morin through one adaptor, DictionarySet, rather than each by members of its own: their lowerBound() gives a pair or an iterator, as a map does, while the interfaces of Morin return the item. Each hash table gives findKey( k ), the address of the key \( k \) it holds (a KeyedDictionary), and each search tree, skip list, B-tree and the sorted array give also lowerBoundKey( k ), the address of the smallest key not smaller than \( k \) (an OrderedDictionary), both at the cost of a search; over the first DictionarySet is a USET, over the second an SSET, and the tries of the integers are SSETs by themselves. Its insert() and remove() take the answer of the dictionary when it gives one, and otherwise search once more, a constant factor; a splay tree, whose searches splay the node found, sits in the adaptor as a mutable member, so that the constant find() of a USET keeps the amortized bound of the splay.

The bag of exercise 1.5 keeps items equal to one another, all of them: Bag is a USET of groups, a hash table from an item to the vector of the items equal to it in the order of their insertions, so that insert(), remove() (of the last one added), find() and count() take \( O( 1 ) \) expected time and findAll() the time of the group it returns. It differs from HashMultiset, which counts the copies of an item and keeps one, in that the equal items need not be identical, e.g., records equal by their key.

Operations of two sets

Each operation of two sets is a scan of one set with a query of the other: the union inserts the items of \( B \) into a copy of \( A \), the intersection removes from a copy of the smaller set its items not in the other, the difference removes from a copy of \( A \) its items in \( B \), and the symmetric difference adds to \( A \setminus B \) the items of \( B \) not in \( A \) (SetOperations.h, on any set with a copy and a forEach()). Its cost is that of the copy, of the scan and of \( n \) or \( m \) queries, i.e., on a hash table of \( m_X \) places and \( n_X \) items, \( O( \max( m_A , n_A ) + \max( m_B , n_B ) ) \) expected (exercise 8.6).

Lemma 1 (the sets with an equality only). If the items can only be compared for equality, every algorithm that computes \( A \cap B \) makes \( n m \) comparisons in the worst case.

Indeed, let an adversary answer "different" to every comparison. If the algorithm stops without comparing some \( a \in A \) with some \( b \in B \), the input in which \( a = b \), all the other items being distinct, agrees with every answer given, so that the algorithm returns the same result on it, while the intersection of the two inputs differs by \( a \). The same argument holds for the union, the difference and the symmetric difference; hence the \( O( n m ) \) of ListSet is optimal (exercises 8.1 and 8.2).

With a total order and the sets sorted, the four operations are merges: the two lists are scanned together, the smaller of their two current items is examined and passed, and an item of \( A \) only, of \( B \) only, or of both goes to the result if the operation takes it (merge() of SortedListSet.h, section 8.3). Each comparison passes at least one item, hence at most \( n + m \) comparisons; on lists, which give no access but by a scan, this is optimal, and the result is sorted as it is written, each item appended at the end in \( O( 1 ) \). On sorted arrays the intersection can do better, as Intersections of sorted sequences shows.

Intersections of sorted sequences

The intersection of two sorted sequences is the core of the answer to a query of several words by an inverted index, each word having the sorted list of the documents that contain it (Ferragina, chapter 6, Set Intersection); there \( m \le n \) and the two lists may differ in length by orders of magnitude, and SortedIntersection.h gives the algorithms of that chapter.

Lemma 2 (the lower bound of the intersection). An algorithm that intersects a sorted array of \( n \) items with one of \( m \le n \) items by comparisons makes, in the worst case,

\[ \log_2 \binom{n}{m} \ge m \log_2 \frac{n}{m} \tag{1} \]

comparisons.

Indeed, when \( B \subseteq A \) the algorithm must tell which of the \( \binom{n}{m} \) subsets of \( A \) of \( m \) items it is, and a tree of binary comparisons with that many leaves has depth at least the logarithm of their number; the inequality is that of \( \binom{n}{m} \ge ( n / m )^m \). The merge is optimal when \( m \) and \( n \) are close, since then (1) is of the order of \( n + m \), and is far from it when \( m \ll n \).

A binary search in \( A \) of each item of \( B \) costs \( O( m \log n ) \), which is better than the merge when \( m = o( n / \log n ) \). The mutual partition of Baeza-Yates searches the median of the shorter list in the longer one, which splits both lists into two pairs of shorter lists, and recurs on each pair with the roles swapped when the part of the longer list is the shorter; its cost is \( O( m ( 1 + \log( n / m ) ) ) \), which meets (1). The search by doubling reaches the same bound with no recursion: each item of \( B \) is searched in \( A \) from the place of the previous one, by probes at the distances \( 1 , 2 , 4 , \ldots \) and then a binary search in the last gap (Bentley and Yao), which costs \( O( 1 + \log \Delta_j ) \) for an item found \( \Delta_j \) places after the previous one. Since the gaps sum to at most \( n \) and the logarithm is concave,

\[ \sum_{j=1}^m ( 1 + \log_2 \Delta_j ) \le m + m \log_2 \frac{n}{m} , \tag{2} \]

the sum of the logarithms of \( m \) numbers of given sum being largest when they are equal. The two-level storage keeps in an array \( A' \) the first item of each block of \( L \) items of \( A \); a merge of \( A' \) with \( B \) tells the block where each item of \( B \) may be, and each block met is merged with its items of \( B \), in \( O( n / L + m L ) \) time, which is \( O( \sqrt{n m} ) \) for \( L = \sqrt{n / m} \); on the disk, with blocks of \( B \) items, a block of \( A \) met costs at most \( \lceil L / B \rceil + 1 \) reads. Finally, the permuted buckets of Sanders and Transier map the universe \( [ 0 , 2^w ) \) by the permutation \( \pi( x ) = ( a x + c ) \bmod 2^w \), \( a \) odd so that \( \pi \) is invertible, and split each list by the top \( \ell = \lceil \log_2( n / L ) \rceil \) bits of the images into buckets of about \( L \) items; an image of the shorter list is looked for in the bucket of the longer one named by its own top bits, so that a bucket of \( A \) is read only if \( B \) has an item in it, at most once, and the time is \( O( m + \min\{ n , m L \} ) \) expected.

The intersections of a sorted list of \( n \) items and one of \( m \le n \) items (Ferragina, chapter 6)
AlgorithmFunctionTimePreferable
mergeintersectMerge()\( O( n + m ) \)\( m \) close to \( n \)
binary searchesintersectBinarySearch()\( O( m \log n ) \)\( m = o( n / \log n ) \)
mutual partitionintersectMutualPartition()\( O( m ( 1 + \log( n / m ) ) ) \)optimal by (1), with the cost of a recursion
doublingintersectDoubling()\( O( m ( 1 + \log( n / m ) ) ) \) by (2)optimal, with no recursion
two levelsintersectTwoLevel()\( O( n / L + m L ) \)on the disk, few reads
permuted bucketsPermutedBuckets\( O( m + \min\{ n , m L \} ) \) expectedlists of similar length, not sorted beforehand

The bounds count comparisons, and on a modern processor the branches that the comparisons decide cost more than the comparisons, since those of a merge of two lists that interleave at random are mispredicted half of the time. VectorIntersection.h therefore merges with no branch, the indices advanced by the results of the comparisons, or compares a block of 8 items of each list all against all with the vector instructions; on lists of very different lengths it searches by doubling, as above, and on the integers of 32 bits through a window of 64 items, whose moves the processor predicts. The bounds stay those of the table, and the time falls by a constant factor (Schlegel, Willhalm and Lehner; Lemire, Boytsov and Kurz).

Filters

A filter trades exactness for space: it does not store the keys, and can therefore take fewer bits than any exact set, but no fewer than the following bound allows.

Lemma 3 (the space of a filter). A filter of any \( n \) keys of a universe \( U \), with \( n \ll | U | \) and false positive rate \( \varepsilon \), takes at least

\[ n \log_2 \frac{1}{\varepsilon} \tag{3} \]

bits (Carter, Floyd, Gill, Markowsky and Wegman).

Indeed, a filter of \( s \) bits has at most \( 2^s \) states, and a state answers yes on a set of about \( n + \varepsilon | U | \) keys, which contains at most \( \binom{n + \varepsilon | U |}{n} \) sets of \( n \) keys; since each of the \( \binom{| U |}{n} \) sets of \( n \) keys must be contained in the answers of some state,

\[ 2^s \ge \binom{| U |}{n} \Big/ \binom{n + \varepsilon | U |}{n} \approx \Bigl( \frac{| U |}{\varepsilon | U |} \Bigr)^n = \varepsilon^{-n} \]

for \( n \ll \varepsilon | U | \), which is (3). The filters below differ by how close they come to (3), by the operations they take, and by the lines of cache that a query reads.

Lemma 4 (the filter of Bloom). Let a filter of \( m \) bits set, for each of \( n \) keys, the bits of \( k \) hash functions taken as independent and uniform. A bit is still 0 with probability \( p = ( 1 - 1 / m )^{k n} \approx e^{-k n / m} \), and the false positive rate is

\[ \varepsilon = ( 1 - p )^k \approx \bigl( 1 - e^{-k n / m} \bigr)^k , \tag{4} \]

least for \( k = ( m / n ) \ln 2 \), where \( \varepsilon = 2^{-k} \approx 0.6185^{m / n} \); the filter then takes \( m = n \log_2( 1 / \varepsilon ) / \ln 2 \approx 1.44 \, n \log_2( 1 / \varepsilon ) \) bits, i.e., 1.44 times (3).

Indeed, a key out of \( S \) is a false positive when its \( k \) bits are all set, each with probability \( 1 - p \), which gives (4) up to the dependence of the bits, negligible for large \( m \). With \( k = - ( m / n ) \ln p \) one has \( \ln \varepsilon = - ( m / n ) \ln p \, \ln( 1 - p ) \), which is symmetric in \( p \) and \( 1 - p \) and hence least at \( p = 1 / 2 \), i.e., when half of the bits are set; then \( \varepsilon = 2^{-k} \) with \( k = ( m / n ) \ln 2 \), and solving for \( m \) gives the space. For \( \varepsilon = 0.01 \) the filter takes about 9.6 bits per key. The \( k \) places of BloomFilter are \( h_1 + i h_2 \bmod m \), \( i = 0 , \ldots , k - 1 \), of two hashes only, which Kirsch and Mitzenmacher show to leave (4) unchanged asymptotically.

The variants of BloomVariants.h and BlockedBloomFilter.h change one assumption at a time. The partitioned filter gives each hash a part of \( m / k \) bits of its own, so that the \( k \) bits of a key are distinct: a bit of a part is 0 with probability \( ( 1 - k / m )^n \approx e^{-k n / m} \), and (4) holds with a slightly smaller variance (Grandi). The spectral filter of Cohen and Matias keeps \( m \) counters instead of bits, adds the multiplicity of a key to its counters and estimates it by the least of them: each counter of \( x \) has received all the occurrences of \( x \), so that the estimate is never below the multiplicity, and it is above it only if every counter of \( x \) is shared with another key, i.e., with the probability (4); a counter that reaches its largest value sticks there, so that a removal never makes a false negative (the counting filter of Fan, Cao, Almeida and Broder). The compressed filter of Mitzenmacher is sent through a channel compressed by an arithmetic coder, in \( m \, H( p ) \) bits, \( H \) the binary entropy; for a given number of bits sent, a larger filter with fewer hashes, whose bits are sparser, gives fewer false positives than the filter of the optimal \( k \). Two machines intersect \( A \) and \( B \) by sending the filter of \( A \) and receiving back the keys \( Q \) of \( B \) that it accepts, \( A \cap B \subseteq Q \), about \( | A \cap B | + \varepsilon | B | \) keys instead of the whole of \( A \) (bloomIntersection()).

The blocked filter of Putze, Sanders and Singler puts the bits of a key in one block of 256 bits, one line of cache, instead of \( k \) lines anywhere. The blocks then get a Poisson number of keys of mean \( \lambda = 256 n / m \), and a block of \( j \) keys, which sets one bit of each of its 8 words of 32 bits per key, answers yes with probability \( ( 1 - ( 1 - 1 / 32 )^j )^8 \), so that

\[ \varepsilon = \sum_{j \ge 0} e^{-\lambda} \frac{\lambda^j}{j!} \Bigl( 1 - \Bigl( 1 - \frac{1}{32} \Bigr)^j \Bigr)^8 ; \tag{5} \]

the crowded blocks answer yes more often than the sparse ones save, and (5) is above (4) with the same bits, e.g., 1.26 per cent against 0.82 at 10 bits per key, the price of one miss of the cache per query.

The filters of fingerprints keep, for each key, a fingerprint of \( f \) bits of its hash, and a key out of \( S \) is a false positive when its fingerprint equals one of those it is compared with. In the cuckoo filter of Fan, Andersen, Kaminsky and Mitzenmacher a key has two buckets of 4 places, the second being the first XOR a hash of the fingerprint, so that a fingerprint can be moved to its other bucket with no key at hand; a query compares at most 8 fingerprints, each equal to that of the key with probability \( 1 / ( 2^f - 1 ) \), and at the load \( \alpha \)

\[ \varepsilon = 1 - \Bigl( 1 - \frac{1}{2^f - 1} \Bigr)^{8 \alpha} \le \frac{8 \alpha}{2^f - 1} , \tag{6} \]

in \( f / \alpha \) bits per key; insertions by evictions reach a load of about 0.95, and the filter takes removals. The prefix filter of Even, Even and Morrison keeps the smallest 25 fingerprints of each bin of 32 bytes, in a pocket dictionary of Bercea and Even, and sends the others to a small blocked filter, the spare, which a query reads only when its fingerprint is above the largest one of a full bin; with \( m \) bins at the load \( \alpha \), the load of a bin is binomial of mean \( 25 \alpha \), the spare gets at most about \( n / \sqrt{2 \pi \cdot 25} \), i.e., 8 per cent, of the keys at \( \alpha = 1 \) (by the bound of Stirling on the binomial at its mean), and a query reads one line of cache in almost every case.

The static filters solve a linear system. The binary fuse filter of Graf and Lemire gives each key 3 places of an array \( F \) of fingerprints of \( b \) bits, in 3 consecutive segments, and asks that \( F[ h_0 ] \oplus F[ h_1 ] \oplus F[ h_2 ] = f( h ) \); the system is solved by peeling, i.e., by repeatedly taking out a place reached by one key only, which is the last to be assigned for that key, and the peeling succeeds with high probability when the array has about \( 1.125 n \) places for large \( n \) (\( 1.23 n \) for 3 places anywhere, as in the xor filters), the spatial coupling of the segments lowering the threshold (Dietzfelbinger and Walzer). The Ribbon filter of Dillinger and Walzer gives each key one equation over \( \mathbb{F}_2 \) with 64 coefficients from a random start, solved by the Gauss elimination of a banded matrix in the order of the starts, in \( r ( 1 + \varepsilon' ) \) bits per key for a small overhead \( \varepsilon' \). In both, a key out of \( S \) meets its equation when a fixed value equals its fingerprint, which is uniform and independent of it; hence \( \varepsilon = 2^{-b} \), or \( 2^{-r} \), and the space is within a factor 1.125, or \( 1 + \varepsilon' \), of (3).

The filters of the library, at about 8 bits of fingerprint, and their space with respect to (3)
FilterOperationsBits per keyLines of cache per query
BloomFilterinsertions\( 1.44 \log_2( 1 / \varepsilon ) \)\( k \)
BlockedBloomFilterinsertions, unionabove that of BloomFilter by (5)1
SpectralBloomFilterinsertions, removals, multiplicitiesthat of BloomFilter, a counter per bit\( r \)
CuckooFilterinsertions, removals\( f / \alpha \), \( \varepsilon \) by (6)2
PrefixFilterinsertions, up to \( n \) keysabout 11.55 for \( \varepsilon \approx 0.39 \) per cent1, rarely 2
BinaryFuseFilterstatic\( 1.125 b \), \( \varepsilon = 2^{-b} \)3
RibbonFilterstatic\( r ( 1 + \varepsilon' ) \), \( \varepsilon = 2^{-r} \)2 for a key out of the set

Range filters

A range filter of a set \( S \) of \( n \) keys of the universe \( [ 0 , 2^{64} ) \) tells whether a range \( [ a , b ] \) may hold a key of \( S \): it answers true for every range that holds one, and for an empty range of length \( \ell = b - a + 1 \) with probability \( \varepsilon( \ell ) \). A filter of points answers a range by asking its \( \ell \) points, i.e., with \( \ell \) queries and up to \( \ell \) times their false positives, while the filters of ranges built on tries (SuRF), on filters of prefixes (Rosetta) or on learned models (SNARF, Proteus) answer in fewer steps but fail on the correlated queries, the empty ranges close to a key, where their false positives go to 1. GrafiteFilter of GrafiteFilter.h has a bound that holds for every set and every query, the only randomness being that of a hash.

With \( B \) bits per key, Grafite reduces the universe to \( [ 0 , r ) \), \( r = n \, 2^{B-2} \), by the hash

\[ h( x ) = \bigl( q( \lfloor x / r \rfloor ) + x \bigr) \bmod r , \tag{7} \]

\( q \) a pairwise independent hash of the blocks of \( r \) consecutive keys into \( [ 0 , r ) \), and keeps the codes \( h( S ) \) by Elias-Fano, in \( n B \) bits. Inside a block, (7) is a rotation, so that a range of \( \ell \le r \) keys, which meets at most two blocks, has for image at most two cyclic ranges of \( [ 0 , r ) \) of \( \ell \) codes in all, which a successor query of Elias-Fano answers in \( O( 1 ) \) each.

Lemma 5 (the range filter of Grafite, Costa, Ferragina and Vinciguerra). For every set \( S \) and every range of \( \ell \) keys,

\[ \varepsilon( \ell ) \le \min\Bigl( 1 , \frac{ n \ell }{ r } \Bigr) = \min\Bigl( 1 , \frac{ \ell }{ 2^{B-2} } \Bigr) . \tag{8} \]

Indeed, a key of \( S \) in the range has its code in the image of the range, and there is no false negative. A key \( x \) of \( S \) out of the range, in a block that the range meets, has its code out of the image of the part of the range in its block, since (7) is one to one on a block; with respect to the part of the range in the other block, and to all of it when \( x \) is in a block that the range does not meet, the code of \( x \) is uniform on \( [ 0 , r ) \) given \( q \) of the other block, \( q \) being pairwise independent, and falls in a stretch of at most \( \ell \) codes with probability at most \( \ell / r \). The union bound over the \( n \) keys gives (8), which generalizes the construction of Goswami, Grønlund, Larsen and Pagh to the ranges of any length; the lower bound of the same paper, \( n \log_2( \ell / \varepsilon ) - O( n ) \) bits, is within 2 bits per key of Grafite.

BucketingFilter, the heuristic of the same paper, keeps instead the buckets of \( s = \lceil 2^{64} / ( n \, 2^{B-2} ) \rceil \) keys that hold a key of \( S \), and answers true when a kept bucket meets the range. It needs no hash and is faster, and it is exact on queries drawn at random independently of the keys; but an empty range inside the bucket of a key, at distance below \( s \) from it, is always a false positive, so that no bound like (8) holds for every query.

The space-efficient sorted set

SpaceEfficientSortedSet is the SESSet of exercise 4.13 of Morin, Open Data Structures: the items in order in a list of blocks of \( b + 1 \) places, as in the SEList of his section 3.3, and the blocks in a skip list ordered by their largest items.

Lemma 6 (the space of the SESSet). With every block but the last holding from \( b - 1 \) to \( b + 1 \) items, \( n \) items take \( n + O( n / b + b ) \) places and \( O( n / b ) \) nodes of the skip list, and a search costs \( O( \log n ) \) expected time.

Indeed, there are at most \( n / ( b - 1 ) + 1 \) blocks, each with at most 2 empty places but the last, with at most \( b + 1 \); the skip list has a node per block. Since the blocks are intervals of the sorted items, the order of their largest items is that of the blocks, and stays so when items move between neighbors: the skip list changes only when a block is created or freed. A search finds the first block whose largest item is not smaller than \( x \), \( O( \log ( n / b ) ) \) in expectation, and \( x \) in it by binary search, \( O( \log b ) \).

An insertion into a full block takes room from the first block that is not full among the next \( b \), moving one item from the end of each block to the front of the next, \( O( 1 ) \) per block in a circular buffer, or spreads \( b ( b + 1 ) \) items over \( b + 1 \) blocks of \( b \); a removal borrows or gathers in the same way. A spread or a gather, of cost \( O( b^2 ) \), leaves \( b \) blocks with \( b \) or \( b - 1 \) items, each of which needs \( \Omega( 1 ) \) changes before it is full or too empty again, and every one of the \( b \) blocks of the next spread or gather must have been so: the potential of Lemma 3.1 of Morin, the number of blocks that are full or too empty, pays them, and an insertion or a removal costs \( O( \log n + b ) \) amortized expected time.

References

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, chapter 8, https://algoritmicamente.it/materiali/libro/capitoli/cap08.pdf
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 6, Set Intersection, and chapter 8, The Dictionary Problem, doi:10.1017/9781009128933
R. Baeza-Yates, A fast set intersection algorithm for sorted sequences, Proceedings of the 15th Symposium on Combinatorial Pattern Matching (CPM), Lecture Notes in Computer Science 3109, 400-408, 2004, doi:10.1007/978-3-540-27801-6_30
J. L. Bentley, A. C.-C. Yao, An almost optimal algorithm for unbounded searching, Information Processing Letters 5(3), 82-87, 1976, doi:10.1016/0020-0190(76)90071-5
P. Sanders, F. Transier, Intersection in integer inverted indices, Proceedings of the 9th Workshop on Algorithm Engineering and Experiments (ALENEX), 71-83, 2007, doi:10.1137/1.9781611972870.7
D. Lemire, L. Boytsov, N. Kurz, SIMD compression and the intersection of sorted integers, Software: Practice and Experience 46(6), 723-749, 2016, doi:10.1002/spe.2326
S. Chambi, D. Lemire, O. Kaser, R. Godin, Better bitmap performance with Roaring bitmaps, Software: Practice and Experience 46(5), 709-719, 2016, doi:10.1002/spe.2325
L. Carter, R. Floyd, J. Gill, G. Markowsky, M. Wegman, Exact and approximate membership testers, Proceedings of the 10th ACM Symposium on Theory of Computing (STOC), 59-65, 1978, doi:10.1145/800133.804332
B. H. Bloom, Space/time trade-offs in hash coding with allowable errors, Communications of the ACM 13(7), 422-426, 1970, doi:10.1145/362686.362692
A. Kirsch, M. Mitzenmacher, Less hashing, same performance: building a better Bloom filter, Proceedings of ESA 2006, Lecture Notes in Computer Science 4168, 456-467, 2006, doi:10.1007/11841036_42
F. Putze, P. Sanders, J. Singler, Cache-, hash-, and space-efficient Bloom filters, Journal of Experimental Algorithmics 14, 4.4, 2009, doi:10.1145/1498698.1594230
S. Cohen, Y. Matias, Spectral Bloom filters, Proceedings of SIGMOD 2003, 241-252, 2003, doi:10.1145/872757.872787
M. Mitzenmacher, Compressed Bloom filters, IEEE/ACM Transactions on Networking 10(5), 604-612, 2002, doi:10.1109/TNET.2002.803864
F. Grandi, On the analysis of Bloom filters, Information Processing Letters 129, 35-39, 2018, doi:10.1016/j.ipl.2017.09.004
B. Fan, D. G. Andersen, M. Kaminsky, M. D. Mitzenmacher, Cuckoo filter: practically better than Bloom, Proceedings of CoNEXT 2014, 75-88, 2014, doi:10.1145/2674005.2674994
T. Even, G. Even, A. Morrison, Prefix filter: practically and theoretically better than Bloom, Proceedings of the VLDB Endowment 15(7), 1311-1323, 2022, doi:10.14778/3523210.3523211
T. M. Graf, D. Lemire, Binary fuse filters: fast and smaller than xor filters, ACM Journal of Experimental Algorithmics 27, 1.5, 2022, doi:10.1145/3510449
M. Dietzfelbinger, S. Walzer, Dense peelable random uniform hypergraphs, Proceedings of ESA 2019, LIPIcs 144, 38:1-38:16, 2019, doi:10.4230/LIPIcs.ESA.2019.38
P. C. Dillinger, S. Walzer, Ribbon filter: practically smaller than Bloom and Xor, arXiv:2103.02515, 2021, https://arxiv.org/abs/2103.02515
M. Costa, P. Ferragina, G. Vinciguerra, Grafite: taming adversarial queries with optimal range filters, Proceedings of the ACM on Management of Data 2(1), 1-23, 2024, doi:10.1145/3639258
M. Goswami, A. Grønlund, K. G. Larsen, R. Pagh, Approximate range emptiness in constant time and optimal space, Proceedings of the 26th ACM-SIAM Symposium on Discrete Algorithms (SODA), 769-775, 2015, doi:10.1137/1.9781611973730.52
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, sections 1.2.3 and 1.2.4, exercises 1.5, 1.6 and 1.7, chapters 3 and 4