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

The headers of include/ADS/compression realize the lossless compressors of chapters 11, Integer Coding, 12, Statistical Coding, 13, Dictionary-Based Compressors, and 14, Block-Sorting Compression, of Ferragina's book, beyond the book, which meets them only in the code of Huffman of its section 14.4, and the indexes that search a compressed text: the entropy against which every compressor is measured, the statistical codes (Huffman, arithmetic, asymmetric numeral systems, prediction by partial matching), the codes of the integers, the compressors of Lempel and Ziv, the transform of Burrows and Wheeler and the indexes built on it. This page collects what the headers take for granted: the definitions and the notation shared by all of them (see Definitions and notation), the limit that no code can beat and the codes that reach it (see Statistical codes), the codes of the integers and their costs (see Codes of the integers), and, for the dictionary compressors, the transform and the indexes, the property on which each one rests and the argument behind its bound (from Compressors of Lempel and Ziv to Compressed indexes). 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 text \( s = s[ 0 ] s[ 1 ] \cdots s[ n - 1 ] \) is a string of \( n \) symbols of an alphabet \( \Sigma \) of \( \sigma \) symbols, ordered, usually the bytes; \( s[ i .. j ] \) is the substring from \( i \) to \( j \), and \( s[ i .. ] \) the suffix from \( i \). A code maps each symbol \( \sigma \) to a binary codeword \( c( \sigma ) \) of length \( \ell( \sigma ) \), and a text to the concatenation of the codewords of its symbols; the code is prefix-free if no codeword is a prefix of another one, so that the concatenation is decoded from left to right with no separator, each codeword being recognized when its last bit is read. A compressor is lossless when its decoder gives back the text exactly, which is the audit of every encoder of the area.

The symbols of the headers of the area
SymbolMeaning
\( n_c \), \( P[ c ] \)the occurrences of the symbol \( c \) in \( s \), and its probability, \( n_c / n \) for the empirical one
\( H_0 \), \( H_k \)the entropy of order 0 and of order \( k \), in bits per symbol (see (1) and (2))
\( \ell( c ) \), \( \bar\ell \)the length of the codeword of \( c \), and the average \( \bar\ell = \sum_c P[ c ] \ell( c ) \)
\( B( x ) \), \( | B( x ) | \)the binary code of the integer \( x \ge 1 \), and its length \( 1 + \lfloor \log_2 x \rfloor \)
\( \mathit{SA} \)the suffix array of \( s \$ \), the places of the suffixes in lexicographic order, \( \$ \) a sentinel smaller than every symbol [see SuffixArray.h]
\( F \), \( L \)the first and the last column of the matrix of the sorted rotations of \( s \$ \); \( L \) is the transform of Burrows and Wheeler
\( C[ c ] \)the number of the symbols of \( s \$ \) smaller than \( c \), i.e., the row of \( F \) where the rows that start with \( c \) begin
\( \mathrm{rank}_c( L , i ) \)the occurrences of \( c \) in \( L[ 0 .. i - 1 ] \)
\( r \), \( z \)the number of the runs of equal symbols of \( L \), and the number of the phrases of a parse of Lempel and Ziv
\( p \), \( \mathit{occ} \)the length of a pattern \( P \), and the number of its occurrences in \( s \)

The entropy of order 0 of a distribution \( P \) on \( \Sigma \), and the empirical one of \( s \), which takes \( P[ c ] = n_c / n \), are

\[ H_0 = \sum_{c \in \Sigma} P[ c ] \log_2 \frac{1}{P[ c ]} , \tag{1} \]

the term of a symbol of probability 0 being 0; \( 0 \le H_0 \le \log_2 \sigma \), with 0 when one symbol has all the probability and \( \log_2 \sigma \) when they are equally likely. A compressor that codes each symbol alone, whatever comes before it, cannot do better than \( n H_0 \) bits (see Statistical codes), and the compressors that look at the context do better on the texts of a language, where a symbol is often all but fixed by the few before it. With \( w_s \) the string of the symbols that follow the occurrences of \( w \) in \( s \), in their order, the entropy of order \( k \) of \( s \) is

\[ H_k( s ) = \frac{1}{n} \sum_{w \in \Sigma^k} |w_s| \, H_0( w_s ) , \tag{2} \]

the average entropy of the symbols in their contexts of \( k \) symbols; \( H_{k+1} \le H_k \le H_0 \), since conditioning on a longer context does not increase the entropy, and \( n H_k \) is the measure of the compressors of order \( k \) (entropy() of Entropy.h computes both).

Statistical codes

Lemma 1 (Kraft and Shannon). The lengths of a prefix-free code satisfy

\[ \sum_{c \in \Sigma} 2^{-\ell( c )} \le 1 , \tag{3} \]

conversely any lengths that satisfy (3) are those of a prefix-free code, and the average length of a prefix-free code is at least \( H_0 \).

Indeed, a codeword \( c( \sigma ) \) is a node of the complete binary tree of depth \( \max \ell \), and since no codeword is the ancestor of another one the \( 2^{\max \ell - \ell( \sigma )} \) leaves below the codewords are disjoint, which is (3) multiplied by \( 2^{\max \ell} \). Conversely, the canonical code below gives the codewords of given lengths in order, each the first free node of its level. Finally, \( H_0 - \bar\ell = \sum_c P[ c ] \log_2( 2^{-\ell( c )} / P[ c ] ) \le \log_2 \sum_c 2^{-\ell( c )} \le 0 \) by the concavity of the logarithm (the inequality of Jensen) and by (3); hence no code of the symbols one by one beats \( H_0 \) bits per symbol, and a code of lengths \( \ell( c ) = \log_2( 1 / P[ c ] ) \) would reach it, when these are integers.

Theorem 2 (Huffman). The code of Huffman has the least average length among the prefix-free codes of the symbols, and

\[ H_0 \le \bar\ell_H < H_0 + 1 . \tag{4} \]

The code is built by merging the two least probable symbols into one of their total probability, \( \sigma - 1 \) times, the codewords of the two being those of the merged symbol followed by 0 and by 1 (huffman() of Huffman.h, by a heap in \( O( \sigma \log \sigma ) \) time). An optimal code has the two least probable symbols as siblings at its deepest level, since swapping them with the deepest pair does not lengthen it; the merge then turns an optimal code of \( \sigma \) symbols into one of \( \sigma - 1 \) symbols whose average is less by the probability of the pair, which does not depend on the code, and by induction the code built is optimal. The lower bound of (4) is Lemma 1, and the upper one holds since the lengths \( \lceil \log_2( 1 / P[ c ] ) \rceil \) satisfy (3), give an average below \( H_0 + 1 \), and are no better than the optimum.

Only the lengths matter: a canonical code assigns to the symbols of each length consecutive codewords, the first of length \( l \) being \( \mathit{fc}[ l ] = \lceil ( \mathit{fc}[ l + 1 ] + \mathit{num}[ l + 1 ] ) / 2 \rceil \) on \( l \) bits, with \( \mathit{fc}[ \max ] = 0 \) (CanonicalHuffman.h). The codewords of length \( l \) are the values from \( \mathit{fc}[ l ] \) on, and the \( l \) first bits of a longer one are below \( \mathit{fc}[ l ] \); hence the decoder reads bits until the value \( v \) of the \( l \) bits read reaches \( \mathit{fc}[ l ] \), and the symbol is the \( ( v - \mathit{fc}[ l ] ) \)-th of length \( l \), with the lengths alone as preamble. The fast decoders read \( W \) bits at once and find in a table of \( 2^W \) entries the symbols whose codewords fit in them, which needs codewords of at most \( W \) bits: the optimal code of lengths at most \( L \) is found by the package-merge of Larmore and Hirschberg in \( O( \sigma L ) \) time (limitedHuffmanLengths() of HuffmanCoder.h), and more simply, but not optimally, by halving the frequencies until the code of Huffman fits (limitedLengths()).

A code of the symbols one by one loses up to one bit per symbol against \( H_0 \), which is much on a skewed distribution (\( H_0 \) near 0 costs 1 bit per symbol anyway). The arithmetic code codes the whole text as one number: the symbols narrow an interval of \( [ 0 , 1 ) \) in turn, each to the part of its probability, and the code is a short binary fraction inside the last interval.

Theorem 3 (the arithmetic code). With \( s_n = \prod_i P[ s[ i ] ] \) the size of the last interval \( [ l , l + s_n ) \), the first \( d = \lceil \log_2( 2 / s_n ) \rceil \) bits of \( l + s_n / 2 \) are a prefix-free code of \( s \) (among the texts of \( n \) symbols), and with the empirical probabilities

\[ d \le 2 + \sum_{i} \log_2 \frac{1}{P[ s[ i ] ]} = 2 + n H_0 . \tag{5} \]

Truncating \( x = l + s_n / 2 \) to \( d \) bits loses less than \( 2^{-d} \le s_n / 2 \), so the fraction \( x' \) written and every fraction that begins with its bits lie in \( [ x' , x' + 2^{-d} ) \subseteq [ l , l + s_n ) \); since the intervals of two texts of \( n \) symbols are disjoint, no code is a prefix of another one. The sum in (5) is \( \sum_c n_c \log_2( n / n_c ) = n H_0 \). Hence the arithmetic code is within 2 bits of \( n H_0 \) for the whole text, against the \( n \) bits of (4), and it codes as well the probabilities that change at each symbol, as long as the decoder changes them in the same way (the adaptive model of ArithmeticCoding.h, and the contexts of PPM.h).

The exact code needs numbers of \( d \) bits; the coder of Witten, Neal and Cleary (RangeEncoder) keeps the interval on \( w \) bits, \( [ L , H ) \subseteq [ 0 , M ) \), \( M = 2^w \), and emits a bit whenever the interval falls in one half of \( [ 0 , M ) \), doubling it. The interval of a symbol of counts \( [ a , b ) \) out of a total \( T \) is

\[ [ L + \lfloor ( H - L ) a / T \rfloor , L + \lfloor ( H - L ) b / T \rfloor ) , \tag{6} \]

and an interval that straddles the middle within \( [ M / 4 , 3 M / 4 ) \) is doubled around the middle, the bit it owes being emitted after the next one, opposite to it. After these expansions \( H - L > M / 4 \), and with \( T \le M / 4 \) every interval of (6) is nonempty, since \( ( H - L ) ( b - a ) / T > 1 \); the rounding of (6) costs about \( 10^{-4} \) bits per symbol for \( w = 32 \).

The asymmetric numeral systems of Duda reach the same compression with an integer state \( x \) in place of the interval. With the frequencies \( f_c \) scaled to a total \( M = 2^{12} \) and \( c_c \) their prefix sums, the symbol \( c \) moves the state to

\[ C( c , x ) = M \lfloor x / f_c \rfloor + c_c + x \bmod f_c , \tag{7} \]

a bijection between the pairs \( ( c , x ) \) and the states, whose inverse reads \( c \) off \( x \bmod M \), which lies in \( [ c_c , c_c + f_c ) \). The state grows by the factor \( M / f_c \), i.e., by \( \log_2( M / f_c ) \) bits, the information of \( c \) under the scaled frequencies; the encoder keeps it in \( [ 2^{16} , 2^{32} ) \) by moving 16 bits to the stream before \( C \) whenever \( x \ge 2^{20} f_c \), so that \( C( c , x ) < 2^{32} \). The decoder reads the stream backward, and the encoder therefore takes the symbols from the last; Rans.h computes (7) with a multiplication, and Tans.h tabulates it on \( 2^{11} \) states, a lookup per symbol.

The statistical coders of the area, on a text of \( n \) symbols
CoderBitsTime per symbolNote
CanonicalHuffman\( n \bar\ell_H < n ( H_0 + 1 ) \)\( O( \ell( c ) ) \) to decode, a bit at a timeany alphabet, the preamble the lengths
huffmanCompress()as above, lengths at most 11up to 3 symbols per lookup to decodebytes, four streams
arithmeticEncode()\( \le 2 + n H_0 \) (exact), about \( 10^{-4} n \) more on 32 bits\( O( \log \sigma ) \) adaptive (a tree of Fenwick)models that change at each symbol
ransCompress(), tansCompress()\( n H_0 \) plus a fraction of a percent\( O( 1 ) \), a multiplication or a lookupstatic model, bytes
ppmEncode()approaches \( n H_K \) plus the escapes\( O( K ( K + \sigma_K ) \log n ) \)contexts of up to \( K \) symbols

Prediction by partial matching (PPM.h) codes each symbol arithmetically in the longest context of at most \( K \) symbols that has seen it, escaping to the shorter contexts on the way. With the escape of the version C, a context seen \( m \) times with \( q \) distinct symbols gives \( c( \sigma ) / ( m + q ) \) to the symbol \( \sigma \) of count \( c( \sigma ) \) and \( q / ( m + q ) \) to the escape, and the symbols excluded by the longer contexts are left out of the shorter ones, since the decoder knows that the symbol is none of them. The compression grows with \( K \) up to 4 or 5 symbols on the texts of a language, beyond which the escapes cost more than the longer contexts gain.

Codes of the integers

A code of the positive integers is optimal, by Lemma 1, for the distribution \( P[ x ] = 2^{-\ell( x )} \), and choosing a code is choosing the distribution of the data: the d-gaps of a sorted list (the differences of consecutive items, dGaps()) are small and skewed, and a code with short codewords for the small integers takes few bits on them.

The codes of IntegerCodes.h (chapter 11, Integer Coding, of Ferragina's book)
CodeCodewordLengthOptimal for
unary\( 0^{x-1} 1 \)\( x \)\( 2^{-x} \)
\( \gamma \) (Elias)\( 0^{| B( x ) | - 1} B( x ) \)\( 2 \lfloor \log_2 x \rfloor + 1 \)\( 1 / ( 2 x^2 ) \)
\( \delta \) (Elias)\( \gamma( | B( x ) | ) B( x ) \)\( 1 + 2 \lfloor \log_2 | B( x ) | \rfloor + | B( x ) | \)about \( 1 / ( 2 x ( \log_2 x )^2 ) \)
Rice \( R_k \), Golomb\( U( q + 1 ) \) and the remainder\( \lfloor ( x - 1 ) / 2^k \rfloor + 1 + k \) for Rice\( ( 1 - p )^{x-1} p \), modulus about \( \ln 2 / p \)
variable bytegroups of 7 bits, a flag each\( 8 \lceil | B( x ) | / 7 \rceil \)whole bytes, fast to decode
\( ( s , c ) \)-densecontinuers, then a stopper\( k \) words for the \( s c^{k-1} \) integers of that lengththe \( s \) of denseOptimalS()

Two codes adapt to the data rather than assume a distribution. The interpolative code of Moffat and Stuiver writes the middle item of a strictly increasing sequence in \( [ \mathit{low} , \mathit{hi} ] \) between the bounds that the items around it leave, i.e., with \( x[ l .. r ] \) the items and \( m \) the middle one, in

\[ \lceil \log_2( \mathit{hi} - \mathit{low} - r + l + 1 ) \rceil \tag{8} \]

bits, and then the two halves recursively; a run of consecutive integers leaves a single value to each item and costs no bit, which is why the code can beat any code of the items one by one on a clustered list. PForDelta codes a block in \( b \) bits per item, the few that do not fit being exceptions written apart, so that the decoder unpacks the block with no branch per item (encodePForDelta()); its engineered forms (BinaryPacking.h) pack 128 items in the 4 lanes of a register of 128 bits, 4 items per shift and mask, and the patched one chooses the width of least cost from the counts of the widths of the block. The Stream VByte of Lemire, Kurz and Rupp moves the lengths of the variable-byte code out of the data, 2 bits per integer in control bytes, so that one shuffle of bytes decodes 4 integers (StreamVByte.h).

None of these codes reads the \( i \)-th item without decoding the ones before it. The directly addressable codes of Brisaboa, Ladra and Navarro (DirectAccessCodes.h) cut each integer into chunks, the level \( \ell \) holding the chunk \( \ell \) of the integers that reach it and a bit telling whether each one continues. The item \( i \) of a level is the item \( \mathrm{rank}_1( B_\ell , i ) \) of the next one, so that an access costs \( O( 1 ) \) per level of its integer; the widths of the levels that minimize the space, with \( n_s \) the integers of more than \( s \) bits and \( w \) the bits of the largest, satisfy

\[ C( s ) = \min_{s < t \le w} \big( n_s ( t - s ) + [ t < w ] \, 1.25 \, n_s + C( t ) \big) , \qquad C( w ) = 0 , \tag{9} \]

since a level that starts at \( s \) costs \( t - s \) bits for each of the \( n_s \) integers that reach it, and one more (with a quarter for its rank) unless it is the last; (9) is solved from \( s = w - 1 \) down in \( O( w^2 ) \) time. The code of Elias and Fano, which also searches a sorted sequence in place, is in the area of the succinct structures [see EliasFano.h].

Compressors of Lempel and Ziv

The compressors of Lempel and Ziv need no model: they replace a substring with a reference to an earlier occurrence of it (LempelZiv.h, chapter 13, Dictionary-Based Compressors, of Ferragina's book). LZ77 parses the text into phrases, each the longest prefix of the rest of the text that starts in the window of the last \( w \) symbols, written as the triple of its distance, its length and the symbol after it; a copy may overlap the phrase itself, which the decoder handles by copying one symbol at a time. LZSS writes a copy or a literal, and finds the copies as gzip does, by chains of the places with the same hash of the next 3 symbols. LZ78 and LZW keep an explicit dictionary of phrases in a trie, each new phrase being an old one extended by one symbol. All of them are universal: on the texts of an ergodic source their bits per symbol tend to its entropy, and the code of the \( z \) phrases of LZ78 takes at most \( n H_k + O( n \log \log n / \log n ) \) bits for every fixed \( k \) (Kosaraju and Manzini), a term that vanishes slowly.

The search of the window costs \( O( w \ell ) \) for a phrase of length \( \ell \), and \( O( n w ) \) in all; with no window the parse is found in \( O( n ) \) time (lz77ParseLinear() of LZ77Linear.h) by the following property.

Lemma 4 (Kärkkäinen, Kempa and Puglisi). Let \( r \) be the rank of the suffix \( i \) in \( \mathit{SA} \), and \( \mathit{PSV}( i ) \) and \( \mathit{NSV}( i ) \) the suffixes of the closest ranks before and after \( r \) that start before \( i \). The longest prefix of \( s[ i .. ] \) that starts at an earlier place has the length

\[ \ell( i ) = \max\{ \mathrm{lcp}( i , \mathit{PSV}( i ) ) , \mathrm{lcp}( i , \mathit{NSV}( i ) ) \} . \tag{10} \]

Indeed, the longest common prefix of two suffixes is the least of the longest common prefixes of the suffixes adjacent in \( \mathit{SA} \) between them, and it can therefore only decrease as the rank moves away from \( r \); among the suffixes that start before \( i \), the closest on each side share the longest prefix with \( i \). The two values of every \( i \) come from one scan of \( \mathit{SA} \) with a stack, each place pushed and popped once, and the lengths of (10) are found by comparing symbols, at most \( \ell( i ) + 1 \) comparisons per candidate; since the phrases cover the text, these sum to \( O( n ) \), and the parse costs \( O( n ) \) with the suffix array of SA-IS.

The transform of Burrows and Wheeler

The rows of the matrix \( M' \) are the \( n + 1 \) rotations of \( s \$ \) in sorted order, \( F \) its first column and \( L \) its last (bwt() of BurrowsWheeler.h, chapter 14, Block-Sorting Compression, of Ferragina's book). Since \( \$ \) is the smallest symbol and occurs once, sorting the rotations is sorting the suffixes, and

\[ L[ i ] = s[ \mathit{SA}[ i ] - 1 ] , \tag{11} \]

the symbol before the \( i \)-th smallest suffix (\( \$ \) for the suffix 0); hence the transform costs the \( O( n ) \) of the suffix array. The rows that start with a string \( w \) are contiguous, so the symbols that precede the occurrences of \( w \) are a substring of \( L \): on a text whose symbols depend on their context, \( L \) is made of long runs of few distinct symbols, which the move-to-front turns into small integers and the run-length code shortens. Coding \( L \) in this way takes at most \( ( 5 + \epsilon ) n H_k^* \) bits plus lower terms (Manzini), with \( H_k^* \) a variant of (2), and coding the pieces of \( L \) of each context of \( k \) symbols by a code of order 0 reaches \( n H_k \) plus lower terms for every \( k \) at once (the boosting of Ferragina, Giancarlo, Manzini and Sciortino).

Lemma 5 (the LF-mapping). The \( k \)-th occurrence of a symbol \( c \) in \( L \) and the \( k \)-th occurrence of \( c \) in \( F \) are the same symbol of \( s \); hence the row

\[ \mathit{LF}( i ) = C[ L[ i ] ] + \mathrm{rank}_{L[ i ]}( L , i ) \tag{12} \]

is the one that starts with the symbol \( L[ i ] \), i.e., the rotation of the row \( i \) by one place to the right.

Indeed, two rows \( i < j \) with \( L[ i ] = L[ j ] = c \), rotated by one place to the right, become \( c \) followed by the row \( i \) and \( c \) followed by the row \( j \), which compare as the rows did; hence the rows that start with \( c \) are in the order of the occurrences of \( c \) in \( L \), and they begin at the row \( C[ c ] \) of \( F \). Since \( L[ \mathit{LF}( i ) ] \) is the symbol before \( L[ i ] \) in \( s \), the inverse transform (inverseBwt()) starts from the row of \( \$ s \) and spells \( s \) backward by \( n \) steps of (12), in \( O( n + \sigma ) \) time with the counts of \( L \) taken in one scan.

Compressed indexes

The transform is also an index of the substrings of \( s \), which replaces both the text and its suffix array (Ferragina and Manzini).

Lemma 6 (the backward search). If the rows prefixed by a string \( Q \) are \( [ \mathit{first} , \mathit{last} ) \), those prefixed by \( c Q \) are

\[ [ C[ c ] + \mathrm{rank}_c( L , \mathit{first} ) , C[ c ] + \mathrm{rank}_c( L , \mathit{last} ) ) . \tag{13} \]

In fact, a row is prefixed by \( c Q \) if and only if it is the image by (12) of a row prefixed by \( Q \) whose last symbol is \( c \), and the images of the occurrences of \( c \) in \( L[ \mathit{first} .. \mathit{last} - 1 ] \) are consecutive by Lemma 5. Hence count( P ) applies (13) from the last symbol of \( P \) to the first, \( p \) steps of two ranks each, and the occurrences are \( \mathit{last} - \mathit{first} \), with no access to the text. To locate them, the rows of the suffixes that start at a multiple of \( \mu \) keep their places: from any row, fewer than \( \mu \) steps of (12) reach a marked row, each step moving one place to the left in \( s \), and the place is that of the marked row plus the steps. The indexes of the area differ in how they compute the ranks of (13) and where they keep the samples.

The indexes of the area on a text of \( n \) bytes, a pattern of \( p \) symbols with \( \mathit{occ} \) occurrences
IndexSpacecount( P )locate( P ), beyond the count
FMIndex\( 8 n + 32 \sigma n / b \) bits, and the samples\( O( p b ) \), a rank a count every \( b \) places and a scan\( O( \mathit{occ} \, \mu b ) \)
HuffmanFMIndex\( n ( H_0 + 1 ) ( 1 + o( 1 ) ) \) bits, and the samples\( O( p ( H_0 + 1 ) ) \) on average, a descent of the tree\( O( \mathit{occ} \, \mu ( H_0 + 1 ) ) \) on average
RIndex\( O( r ) \) words\( O( p ) \) ranks on the runs, each with a predecessor search\( O( \mathit{occ} ) \) predecessor searches
MoveRIndex\( O( r ) \) words, a few more per run\( O( p ) \) steps of \( O( 1 ) \), plus the ranks of the intervals of another symbol\( O( \mathit{occ} ) \), each in \( O( 1 ) \)

The samples every \( \mu \) places cost \( O( ( n / \mu ) \log n ) \) bits, which is far more than \( L \) on a repetitive text (the versions of a document, the genomes of a species), where the number \( r \) of the runs of \( L \) is far smaller than \( n \). The r-index of Gagie, Navarro and Prezza keeps instead \( O( r ) \) words, by two properties. First, along the backward search the place \( \mathit{SA}[ e - 1 ] \) of the last row of the range (the toehold) is kept: if \( L[ e - 1 ] = c \) the row moves by (12) and the place decreases by 1, and otherwise the new last row comes from the last \( c \) before \( e - 1 \), which ends a run, whose place is sampled. Second, the function \( \phi( \mathit{SA}[ i ] ) = \mathit{SA}[ i - 1 ] \), which lists the other occurrences from the toehold, shifts alike every stretch of places between two places of the starts of the runs:

\[ \phi( x ) = \phi( y ) + ( x - y ) , \qquad y = \max\{ \mathit{SA}[ p_j ] : \mathit{SA}[ p_j ] \le x \} , \tag{14} \]

\( p_j \) the start of the run \( j \). Indeed, if the row \( i \) of \( x = \mathit{SA}[ i ] \) starts no run, then \( L[ i - 1 ] = L[ i ] \), Lemma 5 maps the rows \( i - 1 \) and \( i \) to adjacent rows, those of \( \phi( x ) - 1 \) and of \( x - 1 \), and hence \( \phi( x - 1 ) = \phi( x ) - 1 \); the places \( x \) whose row starts a run are the \( \mathit{SA}[ p_j ] \), and (14) follows by induction on \( x - y \). Hence one predecessor search among the \( r \) places \( \mathit{SA}[ p_j ] \) in an Elias-Fano sequence gives each step of \( \phi \) (RIndex.h). Both (12) on the runs and (14) map each interval of a partition alike, and the move structures of Nishimoto and Tabei split the intervals so that the interval of the image is found from that of the argument in \( O( 1 ) \) steps, which removes the predecessor searches at the price of a few words per run (MoveRIndex.h, after the move-r of Bertram, Fischer and Nalbach).

References

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapters 11, Integer Coding, 12, Statistical Coding, 13, Dictionary-Based Compressors, and 14, Block-Sorting Compression, doi:10.1017/9781009128933
A. Montresor, Algoritmi e Strutture Dati, 2025, section 14.4, https://algoritmicamente.it/materiali/libro/capitoli/cap14.pdf
C. E. Shannon, A mathematical theory of communication, Bell System Technical Journal 27(3), 379-423, 1948, doi:10.1002/j.1538-7305.1948.tb01338.x
D. A. Huffman, A method for the construction of minimum-redundancy codes, Proceedings of the IRE 40(9), 1098-1101, 1952, doi:10.1109/JRPROC.1952.273898
L. L. Larmore, D. S. Hirschberg, A fast algorithm for optimal length-limited Huffman codes, Journal of the ACM 37(3), 464-473, 1990, doi:10.1145/79147.79150
I. H. Witten, R. M. Neal, J. G. Cleary, Arithmetic coding for data compression, Communications of the ACM 30(6), 520-540, 1987, doi:10.1145/214762.214771
J. Duda, Asymmetric numeral systems: entropy coding combining speed of Huffman coding with compression rate of arithmetic coding, arXiv:1311.2540, 2013
J. G. Cleary, I. H. Witten, Data compression using adaptive coding and partial string matching, IEEE Transactions on Communications 32(4), 396-402, 1984, doi:10.1109/TCOM.1984.1096090
P. Elias, Universal codeword sets and representations of the integers, IEEE Transactions on Information Theory 21(2), 194-203, 1975, doi:10.1109/TIT.1975.1055349
A. Moffat, L. Stuiver, Binary interpolative coding for effective index compression, Information Retrieval 3(1), 25-47, 2000, doi:10.1023/A:1013002601898
D. Lemire, L. Boytsov, Decoding billions of integers per second through vectorization, Software: Practice and Experience 45(1), 1-29, 2015, doi:10.1002/spe.2203
N. R. Brisaboa, S. Ladra, G. Navarro, DACs: bringing direct access to variable-length codes, Information Processing and Management 49(1), 392-404, 2013, doi:10.1016/j.ipm.2012.08.003
J. Ziv, A. Lempel, A universal algorithm for sequential data compression, IEEE Transactions on Information Theory 23(3), 337-343, 1977, doi:10.1109/TIT.1977.1055714
S. R. Kosaraju, G. Manzini, Compression of low entropy strings with Lempel-Ziv algorithms, SIAM Journal on Computing 29(3), 893-911, 2000, doi:10.1137/S0097539797331105
J. Kärkkäinen, D. Kempa, S. J. Puglisi, Linear time Lempel-Ziv factorization: simple, fast, small, Proc. 24th CPM, LNCS 7922, 189-200, 2013, doi:10.1007/978-3-642-38905-4_19
M. Burrows, D. J. Wheeler, A block-sorting lossless data compression algorithm, Technical Report 124, Digital Systems Research Center, 1994
G. Manzini, An analysis of the Burrows-Wheeler transform, Journal of the ACM 48(3), 407-430, 2001, doi:10.1145/382780.382782
P. Ferragina, R. Giancarlo, G. Manzini, M. Sciortino, Boosting textual compression in optimal linear time, Journal of the ACM 52(4), 688-713, 2005, doi:10.1145/1082036.1082043
P. Ferragina, G. Manzini, Indexing compressed text, Journal of the ACM 52(4), 552-581, 2005, doi:10.1145/1082036.1082039
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. Gagie, G. Navarro, N. Prezza, Fully functional suffix trees and optimal text searching in BWT-runs bounded space, Journal of the ACM 67(1), 2, 2020, doi:10.1145/3375890
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
N. Bertram, J. Fischer, L. Nalbach, Move-r: optimizing the r-index, Proc. 22nd SEA, LIPIcs 301, 1, 2024, doi:10.4230/LIPIcs.SEA.2024.1