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

The headers of include/ADS/succinct realize the compressed data structures of chapter 15, Compressed Data Structures, of Ferragina's book, and the representation of Elias and Fano of its chapter 11, Integer Coding, beyond the book of Montresor, which does not treat them: the vectors of bits with rank and select, the sequences of integers and of symbols searched in their compressed form, and the trees and the graphs that take a few bits per node or per edge and are navigated without being decoded. This page collects what the headers take for granted: the definitions and the notation shared by all of them, with the lower bounds against which their space is measured (see Definitions and notation), the realizations and the costs of their operations (see Realizations and their costs), and, for each family, the property on which its correctness rests and the argument behind its bounds (from Rank and select to Sequences compressed by a model of lines), the arrays of small fields packed in words and in bytes (see Arrays of packed fields), and the parallel construction of the directories and of the wavelet matrix and tree (see Parallel construction). The pseudocode of each algorithm is instead on the page of its header, in the comment of its function, together with the chapter of Ferragina's book, or the paper, that it follows.

Definitions and notation

A vector of bits \( B[ 0 , m ) \) with \( n \) 1s has, for \( b \in \{ 0 , 1 \} \), the operations

\[ \mathrm{rank}_b( i ) = | \{ j < i : B[ j ] = b \} | , \qquad \mathrm{select}_b( k ) = \min\{ i : \mathrm{rank}_b( i + 1 ) = k \} , \tag{1} \]

i.e., the number of the bits \( b \) before the place \( i \) and the place of the \( k \)-th bit \( b \), with \( 1 \le k \) up to the number of the bits \( b \); hence \( \mathrm{rank}_b( \mathrm{select}_b( k ) ) = k - 1 \), and \( \mathrm{rank}_0( i ) = i - \mathrm{rank}_1( i ) \). The places count from 0 in the library, while Ferragina's book counts them from 1, so that its \( \mathrm{Rank}_1 \) and \( \mathrm{Select}_1 \) are the \( \mathrm{rank}_1 \) and \( \mathrm{select}_1 \) here with the places shifted by one. The two operations are the primitives of every other structure of the area: a structure that encodes its objects as bits uses \( \mathrm{select} \) to go from the number of an object to its place in the bits, and \( \mathrm{rank} \) to go back.

A representation of the objects of a class of \( N \) elements needs \( Z = \lceil \log_2 N \rceil \) bits for some of them, the information-theoretic lower bound; it is succinct if it takes \( Z + o( Z ) \) bits, compact if it takes \( O( Z ) \) bits, and in both cases it is asked to answer its queries in about the time of the plain representation. The bounds of the area are the following three. The vectors of \( m \) bits are \( 2^m \), and those with \( n \) 1s are \( \binom{m}{n} \), with

\[ \log_2 \binom{m}{n} \le m H_0( B ) , \qquad H_0( B ) = \frac{n}{m} \log_2 \frac{m}{n} + \frac{m - n}{m} \log_2 \frac{m}{m - n} , \tag{2} \]

since \( \binom{m}{n} p^n ( 1 - p )^{m - n} \le 1 \) for \( p = n / m \); the sets of \( n \) integers of \( [ 0 , u ) \) are \( \binom{u}{n} \), i.e., about \( n \log_2( u / n ) + n \log_2 e \) bits when \( n \) is much smaller than \( u \); the ordered trees of \( n \) nodes (and the binary trees of \( n \) nodes) are the Catalan number \( C_n = \binom{2 n}{n} / ( n + 1 ) \approx 4^n / ( n^{3/2} \sqrt{\pi} ) \), i.e., \( 2 n - \Theta( \log n ) \) bits.

The symbols of the headers of the area
SymbolMeaning
\( B \), \( m \), \( n \)a vector of bits, its length and the number of its 1s
\( \mathrm{rank}_b( i ) \), \( \mathrm{select}_b( k ) \)the bits \( b \) in \( B[ 0 , i ) \) and the place of the \( k \)-th of them, as in (1); rank1(), select1(), rank0(), select0() in the code
\( H_0 \)the empirical entropy of order 0 of a vector of bits, as in (2), or of a sequence of symbols of frequencies \( f_c \), \( \sum_c ( f_c / n ) \log_2( n / f_c ) \)
\( x_0 , \ldots , x_{n-1} \), \( u \)a non-decreasing sequence of integers and the bound of its items, which lie in \( [ 0 , u ) \)
\( \ell \), \( L \), \( H \)the low bits of an item of Elias-Fano, the vector of the low parts and that of the high parts in unary
\( \sigma \), \( w \)the size of an alphabet of symbols, and the bits of a symbol of a wavelet matrix, \( \sigma \le 2^w \)
\( E( i ) \)the excess of a sequence of parentheses at the place \( i \), the number of '(' less the number of ')' in \( B[ 0 , i ] \)
\( \lvert x \rvert \), \( \mathrm{popcount}( x ) \)the number of the 1s of a word of 64 bits, its sideways sum

The bits are packed in words of 64 bits by BitVector, the bit \( i \) in the bit \( i \bmod 64 \) of the word \( \lfloor i / 64 \rfloor \), and the analysis is in the model of the word RAM with words of \( \Theta( \log m ) \) bits, in which the sideways sum of a word and the place of its \( k \)-th 1 cost \( O( 1 ) \) time (one instruction for the first, a few broadword operations for the second, as Rank and select shows).

Realizations and their costs

The vectors of bits with rank and select are five, which trade the space of the directory against the number of accesses to the memory of a query.

The vectors of bits with rank and select and their costs, for \( m \) bits
RealizationSpace beyond \( m \)rank1( i )select1( k )
RankSelect, LazyRankSelect25% for the rank, 9.4% to 18.8% for the select of each value\( O( 1 ) \), one entry and one word\( O( 1 ) \) when the bits are spread evenly, \( O( \log( m / 256 ) ) \) in a sparse group
CompactRankSelect3.125%, and less than 0.8% for the select of each value\( O( 1 ) \), one entry and the 8 words of a line\( O( 1 ) \) when the bits are spread evenly, \( O( \log( m / 4096 ) ) \) in the worst case
InterleavedRankSelect3.125% inside the lines, and less than 1.2% for the select of each value\( O( 1 ) \), one line\( O( 1 ) \) when the bits are spread evenly, the lines between two samples in the worst case
RRRBitVectorthe whole vector in \( m H_0( B ) + 0.175 m \) bits\( O( 32 + b ) \), blocks of \( b = 63 \) bits\( O( \log( m / 2016 ) + 32 + b ) \)

RankSelect is usually preferable when the queries dominate, since its rank reads one entry and one word and its select one sample and a few words; CompactRankSelect and InterleavedRankSelect cost about seven times fewer bits and a sum of up to 8 words per rank, the second one with the counts in the line of the bits, so that a rank misses the cache once instead of twice; RRRBitVector is the one to choose when the 1s (or the 0s) are rare, since its space follows (2) instead of \( m \), at the price of the decoding of a block at each query. The lazy form of RankSelect builds the inventories of the select at the first select of each value, for a structure that may only rank.

The sequences and their costs
RealizationSpace in bitsAccessSearch
EliasFano\( n ( 2 + \lceil \log_2( u / n ) \rceil ) \), plus the directory of \( H \)\( O( 1 ) \)nextGEQ() in \( O( 1 ) \) expected, \( O( \log c ) \) for a bucket of \( c \) items
PartitionedEliasFanoat most that of Elias-Fano on each block of 128 items, plus a record per blocka record and a select in at most 4 words of the code of a block, from its start or from its 1 of rank 64nextGEQ() by a bucket of the superblocks, a count of 16 records and the scan of a block
WaveletMatrix\( n w \), plus a directory per level\( O( w ) \)rank, select, quantile and range counts in \( O( w ) \)
HuffmanWaveletTreeless than \( n ( H_0 + 1 ) \), plus the directory and \( O( \sigma ) \) words\( O( \ell_c ) \), \( H_0 + 1 \) on averagerank and select of \( c \) in \( O( \ell_c ) \)
MoveStructure\( O( r ) \) words for a permutation of \( r \) intervalsthe image of a place and its interval in \( O( 1 ) \)the interval of a place by a binary search
The trees and the graphs and their costs, for \( n \) nodes and \( m \) edges
RealizationSpace in bitsOperations
JacobsonTree\( 2 n + 1 + o( n ) \)the children and the parent of a binary tree in \( O( 1 ) \)
LoudsTree\( 2 n + 1 + o( n ) \)the parent, the degree, the \( i \)-th child and the next sibling in \( O( 1 ) \)
BPTree, on BalancedParentheses\( 2 n \), and 6.25% of them for the blocks of the range min-max tree, at most as much for its groups and 3.5% for a rank directoryalso the size of a subtree, the depth and the ancestors, by a search of the excess
EliasFanoGraph\( m ( 2 + \lceil \log_2( n^2 / m ) \rceil ) + n ( 2 + \lceil \log_2( m / n ) \rceil ) \)the outdegree and the \( i \)-th successor in \( O( 1 ) \), an edge in \( O( 1 ) \) expected
BVGraphabout 3 per edge on the Web graphsa list in \( O( R d ) \), \( R \) the longest chain of references
K2Tree\( k^2 m ( \log_{k^2}( n^2 / m ) + O( 1 ) ) \) in the worst casean edge in \( O( h ) \), the successors and the predecessors of a node

Among the trees, LOUDS is usually preferable for the navigation from the root down (e.g., in the tries of an index), since its operations are a rank and a select each, while the parentheses answer the size of a subtree, the depth and the ancestors, which the order by levels does not keep, at the price of the searches of the range min-max tree. Among the graphs, the codes of Boldi and Vigna exploit the locality and the similarity of the lists of the Web, the \( k^2 \)-tree the clustering of the adjacency matrix, and give the predecessors too, and Elias-Fano exploits neither and gives the fastest random access.

Rank and select

Lemma 1 (the directory of Jacobson). With superblocks of \( Z \) bits, each with the 1s before it in \( \log_2 m \) bits, and blocks of \( z \) bits, each with the 1s before it since its superblock in \( \log_2 Z \) bits, one has

\[ \mathrm{rank}_1( i ) = R_S[ \lfloor i / Z \rfloor ] + R_b[ \lfloor i / z \rfloor ] + \mathrm{rank}_1 \text{ of } B[ z \lfloor i / z \rfloor , i ) , \tag{3} \]

and with \( Z = \log_2^2 m \) and \( z = \log_2 m / 2 \) the counts take \( o( m ) \) bits, with the rank in \( O( 1 ) \) time.

Indeed, the 1s before \( i \) are those before its superblock, those of its superblock before its block, and those of its block before \( i \), which gives (3). The superblocks take \( ( m / \log_2^2 m ) \log_2 m = m / \log_2 m \) bits and the blocks \( ( 2 m / \log_2 m ) \log_2 \log_2^2 m = O( m \log \log m / \log m ) \), both \( o( m ) \); the last term of (3) is read in a table of the \( 2^z = \sqrt{m} \) blocks of \( z \) bits and of their \( z \) prefixes, \( O( \sqrt{m} \log m \log \log m ) \) bits. The library replaces the table by the sideways sum of a word, an instruction of the processor, which lets \( z \) be the word itself and \( Z \) a constant: RankSelect keeps one word per 256 bits, the 40 bits of the count of the block and the three counts of its words in 8 bits each, so that (3) is one read of the directory and one of the bits, with 25% of space more instead of \( o( m ) \), a fraction that is constant but small at every practical size, since \( m / \log_2 m \) is still 2.5% of \( m \) for \( m = 2^{40} \). CompactRankSelect keeps one entry of 128 bits per 4096 bits and sums up to 8 words, 3.125%, and InterleavedRankSelect writes the count of each line of 512 bits in its first 16 bits, so that (3) reads the line alone.

The select is not reduced to the rank by a binary search, which costs \( O( \log m ) \) steps: Clark cuts the 1s in groups of \( \log_2^2 m \) and writes down the places of all the 1s of a group that spans more than \( \log_2^4 m \) bits, which take \( O( m / \log m ) \) bits in all since such groups are at most \( m / \log_2^4 m \); the other groups are cut again, and the last level is solved by a table, which gives \( O( 1 ) \) time in \( o( m ) \) bits, as Clark shows. The library follows the inventories of Vigna, with groups of constant size: a group of \( 2^\ell \) 1s spans 3072 to 6144 bits on average, its first place is written in 64 bits and the places of its 1s of rank multiple of \( 2^{\max\{ \ell - 5 , 0 \}} \) in 16 bits from it, so that the select reads one place and scans the words from it to the bit asked for. A group that spans \( 2^{16} \) bits or more has offsets that do not fit in 16 bits, and is searched by a binary search of the directory of rank between its ends instead; a group that spans less is scanned over less than \( 2^{16} / 64 = 1024 \) words, and over a few words when the 1s are spread evenly. The search ends in a word, where the place of the \( k \)-th 1 is found by the broadword select of Vigna: the sideways sums of the 8 bytes are multiplied by \( \sum_{j < 8} 2^{8 j} \), which leaves in the byte \( j \) the 1s of the bytes up to \( j \); the bytes whose sum exceeds \( k \) are marked all at once by an addition of \( 127 - k \) to each, and the first marked byte, found by the trailing zeros, holds the bit, which is found inside it by clearing its lower 1s.

Compressed vectors and Elias-Fano

Lemma 2 (the combinatorial number system). The vectors of \( b \) bits with \( c \) 1s at the places \( p_1 < \cdots < p_c \) are numbered from 0 to \( \binom{b}{c} - 1 \) by

\[ o = \sum_{t=1}^{c} \binom{p_t}{t} , \tag{4} \]

their rank in the colexicographic order, and the place \( p \), scanned from \( b - 1 \) down, holds the \( t \)-th 1, \( t \) the 1s not yet met, if and only if \( \binom{p}{t} \le o \), the remainder of \( o \) after the 1s above.

Indeed, the vectors that precede a given one in the colexicographic order are, for each \( t \), those that agree with it above \( p_t \) and have their \( t \) lowest 1s below \( p_t \), \( \binom{p_t}{t} \) of them; and since \( \sum_{t' < t} \binom{p_{t'}}{t'} < \binom{p_t}{t} \), the highest term of (4) is the largest \( \binom{p}{c} \le o \), which gives the decoding. RRRBitVector writes each block of \( b = 63 \) bits as its class \( c \) and its offset (4) in \( \lceil \log_2 \binom{b}{c} \rceil \) bits. The product of the \( \binom{b}{c_j} \) of the blocks counts some of the vectors of \( m \) bits with \( n \) 1s, and hence by (2) the offsets take at most \( m H_0( B ) \) bits and one more per block, i.e., \( m H_0( B ) + m / 63 \); the classes take \( 6 m / 63 \), and the counts of the superblocks of 32 blocks at most \( 128 / 2016 \) of the bits, which gives the \( m H_0( B ) + 0.175 m \) bits of the table (see Realizations and their costs).

Theorem 3 (Elias and Fano). A non-decreasing sequence of \( n \) integers of \( [ 0 , u ) \), each split into its \( \ell = \lceil \log_2( u / n ) \rceil \) low bits, written in \( L \), and its bucket \( \lfloor x_i / 2^\ell \rfloor \), written by setting the bit

\[ H[ \lfloor x_i / 2^\ell \rfloor + i ] = 1 , \tag{5} \]

takes at most \( 2 n + n \lceil \log_2( u / n ) \rceil \) bits, and \( x_i = 2^\ell ( \mathrm{select}_1( H , i + 1 ) - i ) + L[ i ] \).

In fact, the places (5) are strictly increasing, since the buckets do not decrease and \( i \) grows, and the \( i \)-th 1 of \( H \) is therefore the one of \( x_i \), from which its bucket is read back by subtracting \( i \). The 0s of \( H \) before that 1 are the bucket itself, so that \( H \) has \( n \) 1s and \( \lfloor ( u - 1 ) / 2^\ell \rfloor + 1 \le n \) 0s, since \( 2^\ell \ge u / n \), and \( L \) has \( n \ell \) bits. Against the \( n \log_2( u / n ) + n \log_2 e \) bits of the lower bound (see Definitions and notation), this is less than 2 bits per item more when \( n \) is much smaller than \( u \). The first item \( \ge x \) (nextGEQ()) lies in the bucket \( h = \lfloor x / 2^\ell \rfloor \) or after it: the items of the bucket start after the \( h \)-th 0 of \( H \), which \( \mathrm{select}_0( H , h ) \) finds, and they share the high bits of \( x \), so that their low bits, which are sorted, decide. A bucket holds \( n 2^\ell / u \le 2 \) items on average when the items are spread evenly, and the search is \( O( 1 ) \) expected, and a binary search past 8 items keeps it \( O( \log c ) \) in a cluster of \( c \) items.

Elias-Fano depends on \( n \) and \( u \) alone, and ignores the clusters of a sequence. The partitioned form of Ottaviano and Venturini (PartitionedEliasFano) codes each block of 128 items relative to the end of the block before, in the universe \( u_j \) of the block, and in the cheapest of three ways: no bit at all if the block holds every value of its range, a vector of \( u_j \) bits if it is not longer, Elias-Fano otherwise. The cost of a block is never above that of Elias-Fano on its own universe, and the sum of these costs, about \( \sum_j m_j ( 2 + \log_2( u_j / m_j ) ) \), is at most the cost of Elias-Fano on the whole sequence, since \( \sum_j u_j \le u \) and the logarithm is concave (as in (10) below), plus the records of the blocks; it is far smaller on the dense runs of an inverted index.

Trees

A tree is coded by the shape alone, in about the \( 2 n \) bits of the lower bound (see Definitions and notation), and its nodes are numbered by the places of their bits, so that the data of a node go in a vector indexed by its number.

Lemma 4 (the binary tree of Jacobson). Complete a binary tree of \( n \) nodes with a leaf in place of each missing child, and write by levels a 1 for a node and a 0 for a leaf added, in \( B[ 1 , 2 n + 1 ] \). Then the children of the \( x \)-th node are at the places \( 2 x \) and \( 2 x + 1 \), and the item at the place \( p \) is the \( \mathrm{Rank}_1( p ) \)-th node if \( B[ p ] = 1 \).

Indeed, the visit by levels lists the root at the place 1 and then the children of the nodes in the order of the nodes, two per node, so that the children of the first \( x - 1 \) nodes take the places \( 2 , \ldots , 2 x - 1 \) and those of the \( x \)-th one the next two. With rank and select in \( O( 1 ) \) time, the left child of the \( x \)-th node is the \( \mathrm{Rank}_1( 2 x ) \)-th node, if \( B[ 2 x ] = 1 \), and its parent the \( \lfloor \mathrm{Select}_1( x ) / 2 \rfloor \)-th (with a node named by its place \( p \), as in Ferragina's book, the left child is at \( 2 \, \mathrm{Rank}_1( p ) \) and the parent at \( \mathrm{Select}_1( \lfloor p / 2 \rfloor ) \)); hence JacobsonTree takes \( 2 n + 1 + o( n ) \) bits, within \( O( \log n ) \) of the lower bound.

For a tree of any degree, LOUDS (LoudsTree) writes the degree \( d \) of each node by levels as \( 0^d 1 \), after a dummy root of degree 1: each node is the 0 that its parent writes for it, so that the \( k \)-th 0 is the \( k \)-th node by levels, and the children of the \( k \)-th node are the 0s after the \( ( k + 1 ) \)-th 1. In the notation of Ferragina's book, with the nodes and the places from 1,

\[ \mathrm{parent}( x ) = \mathrm{Select}_0( x ) - x , \qquad \mathrm{firstChild}( x ) = \mathrm{Select}_1( x ) + 1 - x , \tag{6} \]

and the degree is the distance of two consecutive 1s less 1, all in \( O( 1 ) \) time and \( 2 n + 1 + o( n ) \) bits, since the code has \( n \) 0s and \( n + 1 \) 1s. The order by levels keeps the children of a node consecutive, which gives the \( i \)-th child at once, but scatters a subtree.

The balanced parentheses (BPTree) write '(' when a visit in depth enters a node and ')' when it leaves it, so that the nodes of a subtree are consecutive in preorder and a node is the pair of its parentheses. With the excess \( E \) of the notation,

\[ \mathrm{findClose}( i ) = \min\{ j > i : E( j ) = E( i ) - 1 \} , \tag{7} \]

since the parentheses strictly inside the pair of \( i \) are balanced and keep the excess at least \( E( i ) \); the size of the subtree is half the distance of the pair, the depth is \( E( i ) - 1 \), and the parent is the pair that encloses \( i \), the last \( k < i \) with \( E( k ) = E( i ) - 2 \), plus 1. Since the excess moves by \( \pm 1 \), the first place with an excess at most a target has exactly that excess, and every operation is a search for the first place, forward or backward, whose excess reaches a target. The range min-max tree of Navarro and Sadakane (BalancedParentheses) keeps, for each block of 512 bits, for each group of 16 blocks and for each node of a complete binary tree over the groups, the total excess of its range and its minimum prefix excess; a search scans the block of \( i \), then climbs to the first range on its side whose minimum reaches the target and goes down to its block, in \( O( \log( n / 8192 ) + 16 + 512 / 8 ) \) time, since a range whose minimum stays above the target holds no answer and is skipped whole.

Sequences of symbols and permutations

Lemma 5 (the wavelet matrix). Let the level \( \ell \) of a sequence \( S \) of \( w \)-bit integers hold the bit \( w - 1 - \ell \) of each item, the items passing to the next level stably sorted by that bit, the \( Z_\ell \) with a 0 first. Then the item at the place \( i \) of a level is at the place

\[ \mathrm{rank}_0( B_\ell , i ) \text{ if its bit is 0}, \qquad Z_\ell + \mathrm{rank}_1( B_\ell , i ) \text{ if it is 1} , \tag{8} \]

of the next one, and the items equal to \( c \) end consecutive and in the order of \( S \) at the last level.

Indeed, a stable partition by a bit sends the item to the place given by the items with its bit before it, which is (8), and keeps the order among the items with the same bit; by induction on the levels the items that share the first \( \ell \) bits stay consecutive, in the order of \( S \), and the place of the range of \( c \) at the last level depends on the bits of \( c \) alone. Hence access(), rank() and select() of WaveletMatrix follow a place, or the two ends of a range, down the \( w \) levels by (8) (or up by the select), in \( O( w ) \) time with rank and select in \( O( 1 ) \); the \( k \)-th smallest item of a range goes down to the 0s or the 1s according to how many 0s the range holds at each level. The wavelet tree of Grossi, Gupta and Vitter splits the alphabet in halves at each node instead, with the same costs and a pointer per node; shaped by the code of Huffman (HuffmanWaveletTree, after Mäkinen and Navarro), the symbol \( c \) of frequency \( f_c \) goes down \( \ell_c \) levels, and the bits of all the nodes are \( \sum_c f_c \ell_c < n ( H_0 + 1 ) \) by the bound of the code of Huffman.

The move structure of Nishimoto and Tabei (MoveStructure) represents a permutation \( \pi \) of \( [ 0 , n ) \) that shifts each of \( r \) intervals \( [ p_i , p_{i+1} ) \) by the same amount, such as the LF-mapping of a transform of Burrows and Wheeler on its runs, by the starts \( p_i \), their images \( q_i \) and the interval \( d_i \) of each image:

\[ \pi( x ) = q_i + ( x - p_i ) , \qquad x \in [ p_i , p_{i+1} ) . \tag{9} \]

The interval of \( \pi( x ) \) is found from \( d_i \) forward, past the starts that the image of the interval \( i \) holds; the structure is balanced when no image holds 4 starts, which the construction obtains by cutting at its third start the interval whose image holds 4, at most doubling the intervals (Nishimoto and Tabei). Hence each step of a walk costs \( O( 1 ) \) time, in \( O( r ) \) words instead of the \( n \) of the permutation written in full.

Graphs

A graph of \( n \) nodes and \( m \) edges, as sorted adjacency lists, is a set of \( m \) pairs among \( n^2 \), i.e., \( \log_2 \binom{n^2}{m} \approx m \log_2( n^2 / m ) \) bits for a graph with no regularity. EliasFanoGraph writes the edge \( ( u , v ) \) as the integer \( u n + v \), so that the lists are one increasing sequence of \( [ 0 , n^2 ) \) coded by Theorem 3, plus a second sequence of the prefix sums of the outdegrees that tells where each list starts: the \( i \)-th successor is an access, and an edge a nextGEQ(). Coding each list on its own, in its own universe, takes

\[ \sum_u d_u \log_2 \frac{n}{d_u} \le m \log_2 \frac{n^2}{m} , \tag{10} \]

by the concavity of the logarithm (the inequality of Jensen on the weights \( d_u / m \)), i.e., fewer bits when the degrees are skewed, at the price of a directory and an offset per list, the form that Ferragina's book describes.

The graphs of the Web have more structure than (10) sees. With the pages sorted by URL, most links stay in the host (locality) and the pages of a host link to the same pages (similarity): BVGraph codes the list of \( x \) as a copy of the list of one of the previous \( W \) lists, by a sequence of runs of bits, then as intervals of consecutive successors and as gaps between the rest, in the codes \( \gamma \) and \( \delta \), about 3 bits per edge on the Web graphs, as Boldi and Vigna measure. A list is decoded after its reference, and the chains of references are bounded by \( R \), so that a list of up to \( d \) successors is decoded in \( O( R d ) \) time. The \( k^2 \)-tree of Brisaboa, Ladra and Navarro (K2Tree) splits the adjacency matrix, padded to the side \( k^h \ge n \), into \( k^2 \) submatrices recursively, marking with 1 those with an edge, and stores the marks by levels: the children of the mark at the place \( p \) start at \( k^2 \mathrm{rank}_1( p + 1 ) \), as in LOUDS, so that an edge is tested on a path of \( h \) marks, and a row or a column in \( O( \sqrt{m} ) \) time on average for edges spread uniformly (\( O( n ) \) in the worst case). Each edge sets at most \( k^2 \) marks per level; below the level where the submatrices outnumber the edges these marks are distinct, which gives the bound \( k^2 m ( \log_{k^2}( n^2 / m ) + O( 1 ) ) \) in the worst case (see Realizations and their costs), as Brisaboa, Ladra and Navarro prove, and far fewer bits when the edges cluster and the empty submatrices are large.

Sequences compressed by a model of lines

Elias-Fano spends on each item of a sequence the same bits, whatever the regularity of the gaps; the LA-vector of Boffa, Ferragina and Vinciguerra (LAVector) spends them on the distance of each item from a line instead. The points \( ( i , x_i ) \) of a non-decreasing sequence are covered by the fewest segments whose lines are within \( \epsilon \) of each of their points, the optimal piecewise linear approximation of the PGM-index (see Searches laid out for the memory), and each item is stored as its correction with respect to the line of its segment.

Lemma 6 (the corrections). Let the segment \( j \), over the places \( [ p_j , p_{j+1} ) \), have a line \( f( i ) = x_{p_j} + \gamma + \alpha ( i - p_j ) \) with \( | f( i ) - x_i | \le \epsilon \) for each of its places, and let \( d_i = x_i - x_{p_j} - \lfloor \alpha ( i - p_j ) \rfloor \). Then the \( d_i \) take at most \( 2 \epsilon + 2 \) values, and with \( \beta_j = x_{p_j} + \min_i d_i \)

\[ x_i = \beta_j + \lfloor \alpha ( i - p_j ) \rfloor + C[ i ] , \quad 0 \le C[ i ] < 2^c \text{ for } \epsilon = 2^{c-1} - 1 . \tag{11} \]

Indeed, \( x_i - x_{p_j} - \alpha ( i - p_j ) \) lies in \( [ \gamma - \epsilon , \gamma + \epsilon ] \), and taking off the integer part of \( \alpha ( i - p_j ) \) instead of the whole of it adds less than 1; hence the \( d_i \) are integers in an interval of length less than \( 2 \epsilon + 2 \), at most \( 2 \epsilon + 2 = 2^c \) of them, and \( C[ i ] = d_i - \min_i d_i \) takes \( c \) bits. The slope \( \alpha \) is kept in a double, whose rounding may spread the \( d_i \) by a few units on items of 64 bits; LAVector computes the corrections with the rounded slope and gives each segment the width \( w_j \) of the range of its own corrections, so that (11) holds exactly, with \( w_j \) in place of \( c \) (\( c \), or one bit more). A segment takes 24 bytes (the base, the slope, the place of its corrections and their width; its first item is its base plus its first correction), its first place a field of \( \lceil \log_2 n \rceil \) bits, and the two tables that find a segment by a place and by a value about two fields of \( \lceil \log_2 m \rceil \) bits per segment; hence the sequence takes about \( \sum_j w_j ( p_{j+1} - p_j ) + m ( 192 + \lceil \log_2 n \rceil + 2 \lceil \log_2 m \rceil ) \) bits for \( m \) segments.

The access to \( x_i \) is (11) once the segment of \( i \) is known, which the table of the segment of every \( 2^t \)-th place gives in \( O( 1 ) \) expected time for \( 2^t \approx n / m \). The rank of \( x \) (the place of the first item \( \ge x \)) first finds the last segment whose first item is below \( x \), through a table of the first segment of each bucket of the high bits of the items, as the buckets of Elias-Fano; then, since by (11) \( \beta_j + \lfloor \alpha ( i - p_j ) \rfloor \le x_i < \beta_j + \lfloor \alpha ( i - p_j ) \rfloor + 2^{w_j} \), the answer is among the places where the line goes from \( x - 2^{w_j} \) to \( x + 1 \), about \( 2^{w_j} / \alpha \) of them, which a binary search on the corrections settles in \( O( w_j - \log_2 \alpha ) \) steps (the whole segment when the floating point misplaces the window, which the search checks on the two items at its ends).

Against Elias-Fano, which takes \( 2 + \lceil \log_2( u / n ) \rceil \) bits per item, the LA-vector wins when the items follow lines closely: on the places of a pattern repeated with some noise, on the keys of a process that grows at a steady rate, on the dense runs of an inverted index, where \( m \) is small and \( c \) is the logarithm of the noise rather than of the average gap. On gaps drawn at random no line follows the items better than the average gap does, the corrections need about \( \log_2( u / n ) \) bits as the low bits of Elias-Fano, and the segments are an overhead; tuned() tries the widths up to six bits above the logarithm of the average gap (or half the bits of the items, if more) and keeps the one of the fewest bits. The LA-vector of the paper chooses the width segment by segment, by a shortest path over the segments of all the widths, a segment of width \( c \) being an edge of length \( c \) bits per item plus those of a segment (a prefix and a suffix of a segment within the error are within it, which gives the edges from and to every place); its gain comes from segments of different widths, which the width \( w_j \) that each segment of LAVector takes from its own corrections already gives in part, and on the sequences of the benchmark the shortest path was within a few hundredths of the bits of tuned(), with a slower construction, so that it is not kept.

Arrays of packed fields

An array of \( n \) values below \( 2^B \) takes \( n B \) bits when its fields are packed in words, against the \( 8 n \) of a byte per value; the tables of the searches in large spaces of states, e.g., the pattern databases of the heuristic search, have \( 10^9 \) entries and more, and fit in the memory only so packed (Felner, Korf and Hanan). PackedArray< B > (PackedArray.h) takes the widths that divide 64, \( B \in \{ 1 , 2 , 4 , 8 , 16 , 32 \} \): then a word holds \( 64 / B \) whole fields, the field \( i \) is the bits \( ( i \bmod 64 / B ) B \) on of the word \( \lfloor i B / 64 \rfloor \), and a read is one load, one shift and one mask, with no test of a field that crosses two words, which BitVector::get( i , len ) pays for the other widths. A write clears the field by a mask and puts the value by an or, in the same word. The saturated array stores a value above \( 2^B - 1 \) as \( 2^B - 1 \): when the values are lower bounds, e.g., the distances of a pattern database, a value cut down is still a lower bound, and \( B \) is chosen for the most of the values rather than for the largest one.

A value of three states takes \( \log_2 3 \approx 1.585 \) bits of information, but 2 bits in a field of PackedArray< 2 >. Five such values (trits) \( t_0 , \ldots , t_4 \) fit in one byte as the number

\[ y = \sum_{j = 0}^{4} t_j 3^j \le 3^5 - 1 = 242 , \tag{12} \]

i.e., \( 1.6 \) bits per value, \( 0.015 \) above the entropy, and one fifth less than the fields of 2 bits (Breyer and Korf, whose tables keep the distances modulo 3, from which the distance of a state is recovered along a path to the goal). TritArray reads the digit \( j = i \bmod 5 \) of the byte \( \lfloor i / 5 \rfloor \) from a table of the five digits of each of the 243 bytes (1215 bytes, in the cache of the first level), and writes it by adding \( ( v - t_j ) 3^j \) to the byte, which by (12) changes that digit alone. Six values would need \( 3^6 = 729 > 256 \) codes, and \( 3^{40} < 2^{64} < 3^{41} \) would put 40 values in a word, 1.6 bits each again, at the price of a division by a power of 3 per read; the byte is the unit with the same density and a read by a table.

Both arrays are built from a function \( f \) of the place by blocks of whole words (of whole bytes for the trits) on the threads of an executor (see Loops, reductions and prefix sums): a word is written by the one thread that owns its block, so that no write needs an atomic and the array is the same as the sequential one, and the work is \( O( n ) \) calls of \( f \) with the depth of one block.

Parallel construction

The structures built by a scan of their input are also built on the threads of an executor (see Executors and schedules), with the same entries, bits and inventories as the sequential construction, so that a query gives the same answer whichever built it. The directories of rank (RankSelect, CompactRankSelect) cut their blocks into chunks of consecutive blocks; a first pass counts the 1s of each chunk, the prefix sums of the counts give the 1s before each chunk, \( c_k \), and a second pass writes the entries of each chunk from \( c_k \) on, which are those of the sequential scan since the entry of a block depends only on the 1s before it. The counts of the top level are the \( c_k \) of the chunks that start a top block; the chunks are a power of 2 of blocks below the size of a top block, which then holds whole chunks, or a multiple of it above, so that a top block that does not start a chunk lies in one, which writes its count. The inventories of select are built in two passes over the chunks: the places of the first bit of each group, then the offsets of the sampled bits from the place of their group, each sample found from the entry of its block by a select in a word, as the sequential construction does. The work is \( O( m / 64 ) \) words, as in the sequential scan, plus the samples, and the depth is that of a chunk and of the prefix sums of the counts.

Lemma 7 (the levels by the reversed prefixes). In the wavelet matrix of Lemma 5, let \( b_j( c ) \) be the bit at the level \( \ell + j \) of the item \( c \), and

\[ \kappa_j( c ) = \sum_{i < j} 2^i \, b_i( c ) , \qquad 0 \le j \le k , \tag{13} \]

the bits of the levels \( \ell , \ldots , \ell + j - 1 \) read from the last one. Then the order of the items at the level \( \ell + j \) is the stable sort of their order at the level \( \ell \) by \( \kappa_j \).

Indeed, the level \( \ell + j \) is reached by \( j \) stable partitions by the bits \( b_0 , \ldots , b_{j-1} \), the last by \( b_{j-1} \), which is the radix sort of the least significant digit first by the key whose most significant bit is \( b_{j-1} \), i.e., \( \kappa_j \); the same key orders the stable sort, and both keep the order of the level \( \ell \) among the items of equal key. Hence the place of an item \( c \) at the level \( \ell + j \) is

\[ P_j[ \kappa_j( c ) ] + | \{ c' \text{ before } c \text{ at the level } \ell : \kappa_j( c' ) = \kappa_j( c ) \} | , \tag{14} \]

with \( P_j[ a ] \) the items whose key is below \( a \), and \( Z_{\ell + j} \) is the number of items with \( b_j = 0 \), both sums of the counts of the \( 2^k \) values of \( \kappa_k \), from which each \( \kappa_j = \kappa_k \bmod 2^j \) follows.

By Lemma 7 one scan of the items in the order of the level \( \ell \) writes the bits of the \( k \) levels \( \ell , \ldots , \ell + k - 1 \) at the places (14) and moves the items to their order at the level \( \ell + k \), where the construction by the partitions of Lemma 5 moves them \( k \) times, once per level: \( \lceil w / 8 \rceil \) passes over the items for \( k \le 8 \) instead of \( w \), the bits written into \( 2^k - 1 \) streams, one per value of \( \kappa_j \) for each \( j < k \), whose next places are consecutive (the bits of the level \( \ell \) itself, in the order of the items, 64 at a time in a word). This is the sort by the prefixes of the bits of Dinklage, Ellert, Fischer, Kurpicz and Löbel, applied to \( k \) levels at a time. The items are cut into slices of at most \( 2^{18} \) items, about four per thread, whose \( k \) levels, of 256 KB at most, stay in the second level of the cache while the slice writes them: each slice writes the levels of its own items, at the places that the counts of its keys give by (14) within the slice, and the levels are then gathered from the slices, each block of words of a level copying the runs of bits of the slices that fall in it, in the order (14) of the runs, so that no two threads write a word (the domain decomposition of Fuentes-Sepúlveda, Elejalde, Ferres and Seco and of Labeit, Shun and Blelloch, with the merge of Dinklage et al.). The items move to the places (14) of the level \( \ell + k \) directly, since those of two slices never meet. The wavelet tree of Huffman is built in slices too: the counts of the symbols of each slice give the place of each slice in each node of the trie of the codes, and each slice writes the bits of its items along their codes into a stream per node, with no item moved; a stream gathers its bits into a word and stores the word when it is full, with a plain store when the word is inside its range and an atomic or when it is at one of its ends, which it may share with the stream of another slice (the words are 0 at first, and only the 1s are written). On one thread the tree is built by the sequential scan, which writes each bit at its place. The work is \( O( n w ) \) bits, \( O( n w / 64 ) \) words copied and \( O( n \lceil w / 8 \rceil ) \) moves of items, or \( O( \sum_c f_c \ell_c ) \) bits for the tree, plus \( O( q 2^k ) \) counts for \( q \) slices (\( O( q \sigma ) \) for the tree, whose slices therefore hold at least \( \sigma \) items each), and the depth is that of a slice.

References

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapters 11, Integer Coding, and 15, Compressed Data Structures, doi:10.1017/9781009128933
G. Navarro, Compact Data Structures, Cambridge University Press, 2016, doi:10.1017/CBO9781316588284
A. Felner, R. E. Korf, S. Hanan, Additive pattern database heuristics, Journal of Artificial Intelligence Research 22, 279-318, 2004, doi:10.1613/jair.1480
T. M. Breyer, R. E. Korf, 1.6-bit pattern databases, Proceedings of the 24th AAAI Conference on Artificial Intelligence, 39-44, 2010, doi:10.1609/aaai.v24i1.7558
G. Jacobson, Space-efficient static trees and graphs, Proc. 30th FOCS, 549-554, 1989, doi:10.1109/SFCS.1989.63533
D. Clark, Compact Pat Trees, PhD thesis, University of Waterloo, 1996
S. Vigna, Broadword implementation of rank/select queries, Proc. WEA 2008, LNCS 5038, 154-168, 2008, doi:10.1007/978-3-540-68552-4_12
F. Kurpicz, Engineering compact data structures for rank and select queries on bit vectors, Proc. 29th SPIRE, LNCS 13617, 257-272, 2022, doi:10.1007/978-3-031-20643-6_19
M. D. Laws, J. Bliven, K. Conklin, E. Laalai, S. McCauley, Z. S. Sturdevant, SPIDER: improved succinct rank and select performance, Proc. 22nd SEA, LIPIcs 301, 21, 2024, doi:10.4230/LIPIcs.SEA.2024.21
R. Raman, V. Raman, S. S. Rao, Succinct indexable dictionaries with applications to encoding k-ary trees, prefix sums and multisets, ACM Transactions on Algorithms 3(4), 43, 2007, doi:10.1145/1290672.1290680
P. Elias, Efficient storage and retrieval by content and address of static files, Journal of the ACM 21(2), 246-260, 1974, doi:10.1145/321812.321820
G. Ottaviano, R. Venturini, Partitioned Elias-Fano indexes, Proc. 37th SIGIR, 273-282, 2014, doi:10.1145/2600428.2609615
J. I. Munro, V. Raman, Succinct representation of balanced parentheses and static trees, SIAM Journal on Computing 31(3), 762-776, 2001, doi:10.1137/S0097539799364092
G. Navarro, K. Sadakane, Fully functional static and dynamic succinct trees, ACM Transactions on Algorithms 10(3), 16, 2014, doi:10.1145/2601073
F. Claude, G. Navarro, A. Ordóñez, The wavelet matrix: an efficient wavelet tree for large alphabets, Information Systems 47, 15-32, 2015, doi:10.1016/j.is.2014.06.002
V. Mäkinen, G. Navarro, Succinct suffix arrays based on run-length encoding, Proc. 16th CPM, LNCS 3537, 45-56, 2005, doi:10.1007/11496656_5
T. Nishimoto, Y. Tabei, Optimal-time queries on BWT-runs compressed indexes, Proc. 48th ICALP, LIPIcs 198, 101, 2021, doi:10.4230/LIPIcs.ICALP.2021.101
P. Boldi, S. Vigna, The WebGraph framework I: compression techniques, Proc. 13th WWW, 595-602, 2004, doi:10.1145/988672.988752
N. R. Brisaboa, S. Ladra, G. Navarro, Compact representation of Web graphs with extended functionality, Information Systems 39, 152-174, 2014, doi:10.1016/j.is.2013.08.003
A. Boffa, P. Ferragina, G. Vinciguerra, A "learned" approach to quicken and compress rank/select dictionaries, Proc. 23rd ALENEX, 46-59, 2021, doi:10.1137/1.9781611976472.4
A. Boffa, P. Ferragina, G. Vinciguerra, A learned approach to design compressed rank/select data structures, ACM Transactions on Algorithms 18(3), 1-28, 2022, doi:10.1145/3524060
L. Fuentes-Sepúlveda, E. Elejalde, L. Ferres, D. Seco, Parallel construction of wavelet trees on multicore architectures, Knowledge and Information Systems 51(3), 1043-1066, 2017, doi:10.1007/s10115-016-1000-6
J. Shun, Parallel wavelet tree construction, Proc. 25th DCC, 63-72, 2015, doi:10.1109/DCC.2015.7
J. Labeit, J. Shun, G. E. Blelloch, Parallel lightweight wavelet tree, suffix array and FM-index construction, Journal of Discrete Algorithms 43, 2-17, 2017, doi:10.1016/j.jda.2017.04.001
P. Dinklage, J. Ellert, J. Fischer, F. Kurpicz, M. Löbel, Practical wavelet tree construction, ACM Journal of Experimental Algorithmics 26, 1-67, 2021, doi:10.1145/3457197
P. Dinklage, J. Fischer, F. Kurpicz, J.-P. Tarnowski, Bit-parallel (compressed) wavelet tree construction, Proc. 33rd DCC, 81-90, 2023, doi:10.1109/DCC55655.2023.00016