Simple Algorithm
Algorithms & Data Structures · Optimal · 28 min

Bit Vectors & Succinct Data Structures

Rank and select in a few extra bits, near the information-theoretic minimum.

A set of 1,024 numbers drawn from {0,…,220−1}\{0, \dots, 2^{20} - 1\} takes 1,048,576 bits as a bit vector and 20,480 bits as a sorted array of 20-bit numbers. Counting the possible sets shows that 11,711 bits are enough, and no encoding can use fewer for all of them. Succinct data structures get within a lower-order term of such minima and still answer queries such as "how many elements are below xx?" in constant time. They are why an index of the whole human genome fits in a few gigabytes.

What you’ll learn

  • Compute the information-theoretic minimum for sets, sparse sets and trees, and define implicit, succinct and compact representations.
  • Support rank on a bit vector in O(1)O(1) time with o(n)o(n) extra bits, and compute the overhead of the two-level scheme exactly.
  • Explain how select is answered in constant time, and what lower bounds say about the extra space needed.
  • Navigate an ordinal tree stored in 2n+o(n)2n + o(n) bits (LOUDS, balanced parentheses) with rank and select.
  • Describe wavelet trees and the FM-index, and where succinct structures stand as of 2025.

How few bits can a structure use?

ObjectsHow manyMinimum bits log⁡2∣U∣\log_2 \abs{\mathcal U}
all subsets of {0,…,n−1}\{0, \dots, n-1\}2n2^nnn: a plain bit vector is optimal
subsets of size mm(nm)\binom{n}{m}log⁡2(nm)≈mlog⁡2(en/m)\log_2\binom{n}{m} \approx m \log_2(en/m) when m≪nm \ll n
ordinal trees with nn nodes (children in order)the Catalan number Cn−1=1n(2n−2n−1)C_{n-1} = \tfrac1n\binom{2n-2}{n-1}2n−Θ(log⁡n)2n - \Theta(\log n)
permutations of nn itemsn!n!nlog⁡2n−nlog⁡2e+O(log⁡n)n\log_2 n - n\log_2 e + O(\log n)
For m=1024m = 1024 and n=220n = 2^{20}, log⁡2(nm)≈11,710.3\log_2\binom{n}{m} \approx 11{,}710.3, so 11,711 bits. A sorted array spends 20,480 bits because it also fixes an order and does not exploit that the gaps are small; a pointer-based tree spends Θ(nlog⁡n)\Theta(n \log n) bits against the 2n2n needed.
Predict before you look

What is the smallest number of bits that can store any 2-element subset of {0,1,…,63}\{0, 1, \dots, 63\}?

Show the answer

11 bits

There are (642)=2016\binom{64}{2} = 2016 such sets, and 210=1024<2016≤2048=2112^{10} = 1024 < 2016 \le 2048 = 2^{11}, so 11 bits suffice and 10 do not. Two 6-bit numbers take 12 bits because they also record an order, which a set does not have; a 64-bit vector wastes far more.

Meeting the minimum is easy if you never need to query: number the objects and store the index. The art is to stay within Z+o(Z)Z + o(Z) bits and answer queries fast. The workhorse is a bit vector with two operations, rank and select.

Rank in constant time with o(n) extra bits

DerivationJacobson's two-level scheme (1989)
  1. nlog⁡2n⋅O(log⁡n)  =  O(nlog⁡n) bits\frac{n}{\log^2 n}\cdot O(\log n) \;=\; O\Big(\frac{n}{\log n}\Big) \text{ bits}

    Superblocks of L=⌈log⁡2n⌉2L = \lceil \log_2 n \rceil^2 bits each store the rank at their start, a number of about log⁡2n\log_2 n bits:

  2. n12log⁡n⋅O(log⁡log⁡n)  =  O(nlog⁡log⁡nlog⁡n) bits\frac{n}{\tfrac12\log n}\cdot O(\log \log n) \;=\; O\Big(\frac{n \log\log n}{\log n}\Big) \text{ bits}

    Blocks of b=⌈12log⁡2n⌉b = \lceil \tfrac12 \log_2 n \rceil bits each store the rank at their start relative to their superblock, a number below LL, so only O(log⁡log⁡n)O(\log \log n) bits:

  3. 2b⋅b⋅O(log⁡b)  =  O(n log⁡n log⁡log⁡n)  =  o(n) bits2^b \cdot b \cdot O(\log b) \;=\; O\big(\sqrt n \,\log n \,\log\log n\big) \;=\; o(n) \text{ bits}

    Inside a block, a precomputed table gives, for every bb-bit pattern and every j<bj < b, the number of 1s among the pattern's first jj bits. There are only 2b≤2n2^b \le 2\sqrt n patterns:

  4. rank⁡1(i)  =  Super[⌊i/L⌋]+Block[⌊i/b⌋]+Table[block bits][ i mod b ],extra=O(nlog⁡log⁡nlog⁡n)=o(n)\operatorname{rank}_1(i) \;=\; \mathrm{Super}\big[\lfloor i/L \rfloor\big] + \mathrm{Block}\big[\lfloor i/b \rfloor\big] + \mathrm{Table}\big[\text{block bits}\big]\big[\,i \bmod b\,\big], \qquad \text{extra} = O\Big(\frac{n\log\log n}{\log n}\Big) = o(n)

    A query adds three numbers, each read with a constant number of word operations, so it takes O(1)O(1) time on the word RAM; the extra space is dominated by the block counts:

Bits 0–15 (superblock 0)Bits 16–31 (superblock 1)
1011 0010 1110 00010100 1111 0000 1011
superblock count 0superblock count 8
block counts 0, 3, 4, 7block counts 0, 1, 5, 5
A toy instance with superblocks of 16 bits and blocks of 4. To compute rank⁡1(30)\operatorname{rank}_1(30): superblock ⌊30/16⌋=1\lfloor 30/16 \rfloor = 1 contributes 8; block ⌊30/4⌋=7\lfloor 30/4 \rfloor = 7 contributes 5 within its superblock; the first 30 mod 4=230 \bmod 4 = 2 bits of that block's pattern 1011 hold one 1. So rank⁡1(30)=8+5+1=14\operatorname{rank}_1(30) = 8 + 5 + 1 = 14, the number of 1s in bits 0 to 29.
python
class RankBitVector:
    """rank1(i) in O(1): an absolute count per 512 bits, a relative count per 64-bit word."""
    def __init__(self, bits):
        self.words = [0] * ((len(bits) + 63) // 64)
        for i, x in enumerate(bits):
            if x:
                self.words[i >> 6] |= 1 << (i & 63)
        self.super, self.rel, total = [], [], 0
        for w, word in enumerate(self.words):
            if w % 8 == 0:                       # a new 512-bit superblock
                self.super.append(total)
                base = total
            self.rel.append(total - base)        # 1s in this superblock before word w
            total += word.bit_count()            # popcount (Python 3.10+)

    def rank1(self, i):                          # 1s in bits[0 .. i-1], for 0 <= i < 64 * len(words)
        w, r = i >> 6, i & 63
        return self.super[w >> 3] + self.rel[w] + (self.words[w] & ((1 << r) - 1)).bit_count()

Tested against a brute-force prefix count on random vectors. Python stores each count in a whole object, so this shows the arithmetic, not the space; packed into machine words, the counts take a quarter of the vector's size.

Select, and how many extra bits are necessary

Select in constant time. Clark (1996) answered select⁡1\operatorname{select}_1 in O(1)O(1) time with o(n)o(n) extra bits by sampling. Split the 1s into groups of k=Θ(log⁡2n)k = \Theta(\log^2 n) consecutive 1s and record where each group starts. A group whose 1s are spread over a long stretch of the vector is sparse: there are few such groups, so the positions of all their 1s can be written down explicitly. A dense group spans a short stretch, so a second level of the same idea, ending in table lookups on blocks of 12log⁡n\tfrac12\log n bits, finishes the job. Practical libraries usually binary-search the rank counts and finish inside one word, or add sampled positions to bound the search.

Sparse sets. When m≪nm \ll n, the Elias–Fano encoding (Elias, 1974; Fano, 1971) stores each element's low ⌊log⁡2(n/m)⌋\lfloor \log_2(n/m) \rfloor bits verbatim and its high bits in unary, in a bit vector of about 2m2m bits; select⁡1\operatorname{select}_1 on that vector recovers the jj-th element in O(1)O(1). For the 1,024 elements of the hook it uses 10,240+2,048=12,28810{,}240 + 2{,}048 = 12{,}288 bits, within 5% of the minimum of 11,711.

Think first Raman, Raman and Rao (2002) store a bit vector with mm ones in log⁡2(nm)+O(nlog⁡log⁡nlog⁡n)\log_2\binom{n}{m} + O\big(\tfrac{n\log\log n}{\log n}\big) bits with O(1)O(1) rank and select. How can a structure use fewer than nn bits for a bit vector of nn bits?

It spends about log⁡2(nm)\log_2\binom{n}{m} bits, which is far below nn when mm is far from n/2n/2. Each small block of bb bits is written as its class, the number cc of 1s it holds, plus an offset of ⌈log⁡2(bc)⌉\lceil \log_2\binom{b}{c} \rceil bits saying which of the (bc)\binom{b}{c} blocks with cc ones it is. Rank needs only the classes and the counts; tables decode an offset when the bits inside a block are needed. For a vector that is 99% zeros, log⁡2(nm)≈0.08 n\log_2\binom{n}{m} \approx 0.08\,n, so the main term shrinks to about a twelfth of the plain vector, and rank and select still take constant time.

Trees in 2n + o(n) bits

A pointer-based tree with nn nodes spends Θ(nlog⁡n)\Theta(n \log n) bits on pointers. Jacobson's level-order unary degree sequence (LOUDS) spends 2n+12n + 1. Visit the nodes in breadth-first order and write each node's number of children dd in unary, as dd ones followed by a zero, after an initial 10 for an imaginary super-root. Number the nodes 1,…,n1, \dots, n in breadth-first order. Then node kk corresponds to the kk-th 1 of the string (the 1 its parent wrote for it), and its own children are written right after the kk-th 0.

child⁡(k,i)=rank⁡1(select⁡0(k)+i+1)parent⁡(k)=rank⁡0(select⁡1(k))degree⁡(k)=select⁡0(k+1)−select⁡0(k)−1\begin{aligned} \operatorname{child}(k, i) &= \operatorname{rank}_1\big(\operatorname{select}_0(k) + i + 1\big) \\ \operatorname{parent}(k) &= \operatorname{rank}_0\big(\operatorname{select}_1(k)\big) \\ \operatorname{degree}(k) &= \operatorname{select}_0(k+1) - \operatorname{select}_0(k) - 1 \end{aligned}
Navigation in LOUDS, with 0-based positions, rank⁡b(x)\operatorname{rank}_b(x) counting the bbs before position xx, and select⁡b(j)\operatorname{select}_b(j) the position of the jj-th bb. Each operation is a constant number of rank and select queries: O(1)O(1) time with o(n)o(n) extra bits.
Check your understanding

In the example, select⁡0(2)=5\operatorname{select}_0(2) = 5. Using child⁡(k,i)=rank⁡1(select⁡0(k)+i+1)\operatorname{child}(k, i) = \operatorname{rank}_1(\operatorname{select}_0(k) + i + 1) on the string 10111011001001000, which node is the second child of node 2?

  1. Node 5
  2. Node 7
  3. Node 3
  4. Node 6
Show the answer

D. Node 6

select⁡0(2)+2+1=8\operatorname{select}_0(2) + 2 + 1 = 8, and the first 8 bits, 10111011, contain six 1s, so the child is node 6, as in the tree. Its 1 sits at position 7, the second bit of node 2's code 110.

LOUDS answers parent, child and degree in O(1)O(1) time, but not subtree sizes or depths. The balanced parentheses representation keeps more: a node is its opening parenthesis, its subtree ends at the matching closing one, and its depth is the excess, opens minus closes, up to it. Munro and Raman (1997; journal version 2001) found matching parentheses in O(1)O(1) time with o(n)o(n) extra bits, which gives parent, first child, next sibling and subtree size in constant time; later work extended the same representation to depths, level ancestors and lowest common ancestors.

Sequences, text indexes and the frontier

Rank and select generalize from bits to any alphabet. A wavelet tree (Grossi, Gupta & Vitter, 2003) stores a sequence over an alphabet of σ\sigma symbols as a binary tree of bit vectors: the root's vector marks, for every position, whether its symbol lies in the lower or the upper half of the alphabet, and each child holds, recursively, the subsequence of its half. Counting the occurrences of a symbol cc among the first ii positions descends one level per bit of cc, with one bit-vector rank per level: O(log⁡σ)O(\log \sigma) time, in n⌈log⁡2σ⌉+o(nlog⁡σ)n\lceil\log_2\sigma\rceil + o(n\log\sigma) bits.

That operation, rank⁡c(i)\operatorname{rank}_c(i) on the Burrows–Wheeler transform of a text, is exactly what the FM-index (Ferragina & Manzini, 2000) needs to count the occurrences of a pattern PP with 2∣P∣2\abs{P} rank queries, without the text itself; the Optimal lesson The Burrows–Wheeler Transform & FM-Index derives it. Read aligners such as Bowtie and BWA (both 2009) use FM-indexes to align sequencing reads against the human genome, about 3 billion bases, in a few gigabytes of memory.

Key takeaways

  • The information-theoretic minimum ⌈log⁡2∣U∣⌉\lceil\log_2\abs{\mathcal U}\rceil is the yardstick: nn bits for any subset of {0,…,n−1}\{0, \dots, n-1\}, log⁡2(nm)\log_2\binom{n}{m} for a set of mm elements, 2n−Θ(log⁡n)2n - \Theta(\log n) for an ordinal tree; succinct means that plus a lower-order term, with fast queries.
  • Rank and select on a bit vector take O(1)O(1) time with o(n)o(n) extra bits, via two levels of counts and a table or a popcount; for systematic indexes Θ(nlog⁡log⁡n/log⁡n)\Theta(n\log\log n/\log n) extra bits is optimal, and compressed structures go far lower.
  • o(n)o(n) is an asymptotic promise: the textbook parameters cost about 66% extra at n=232n = 2^{32}, while word-based layouts spend 12.5% to 25%.
  • With rank and select, a 2n+12n + 1-bit LOUDS string or 2n2n balanced parentheses navigate a tree, wavelet trees answer rank over any alphabet, and the FM-index searches genomes in a few bits per base.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

About how many bits does the most compact encoding need for an arbitrary set of 100 elements of {0,1,…,106−1}\{0, 1, \dots, 10^6 - 1\}?

  1. About 10610^6
  2. About 2,000
  3. About 1,500
  4. About 100
Show the answer

C. About 1,500

log⁡2(106100)≈1468\log_2\binom{10^6}{100} \approx 1468, close to mlog⁡2(en/m)=100⋅log⁡2(e⋅104)≈1473m\log_2(en/m) = 100 \cdot \log_2(e \cdot 10^4) \approx 1473. A sorted array of 20-bit numbers takes 2,000 bits; the extra is the order it records and the large numbers it spells out in full.

Question 2

For the bit vector 1101 0011 1000 0111 (positions 0 to 15, left to right), what are rank⁡1(8)\operatorname{rank}_1(8) and select⁡1(5)\operatorname{select}_1(5)?

  1. 6 and 7
  2. 5 and 8
  3. 5 and 7
  4. 6 and 8
Show the answer

C. 5 and 7

The 1s are at positions 0, 1, 3, 6, 7, 8, 13, 14 and 15. rank⁡1(8)\operatorname{rank}_1(8) counts the 1s before position 8: positions 0, 1, 3, 6, 7, so 5. The fifth 1 is at position 7, so select⁡1(5)=7\operatorname{select}_1(5) = 7.

Question 3

In Jacobson's two-level rank structure, which part takes the most extra space as nn grows?

  1. The superblock counts, Θ(n/log⁡n)\Theta(n/\log n) bits
  2. The block counts, Θ(nlog⁡log⁡n/log⁡n)\Theta(n \log\log n / \log n) bits
  3. The lookup table, O(nlog⁡nlog⁡log⁡n)O(\sqrt n \log n \log\log n) bits
  4. None of them: all three are Θ(n)\Theta(\sqrt n)
Show the answer

B. The block counts, Θ(nlog⁡log⁡n/log⁡n)\Theta(n \log\log n / \log n) bits

There are Θ(n/log⁡n)\Theta(n/\log n) blocks, each with a count of Θ(log⁡log⁡n)\Theta(\log\log n) bits; the superblock counts take only Θ(n/log⁡n)\Theta(n/\log n) in total and the table o(n/log⁡n)o(n/\log n). Golynski's lower bound shows that, for systematic indexes, this Θ(nlog⁡log⁡n/log⁡n)\Theta(n\log\log n/\log n) cannot be beaten.

Question 4

Why can no encoding of all ordinal trees with nn nodes use fewer than about 2n−O(log⁡n)2n - O(\log n) bits for every tree?

  1. Because each node needs a pointer of log⁡2n\log_2 n bits
  2. Because LOUDS uses 2n+12n + 1 bits, and nothing beats LOUDS
  3. Because a tree has n−1n - 1 edges and each edge needs two bits
  4. Because there are Cn−1=1n(2n−2n−1)C_{n-1} = \tfrac1n\binom{2n-2}{n-1} such trees, about 4n4^n divided by a polynomial, and distinct trees need distinct bit strings
Show the answer

D. Because there are Cn−1=1n(2n−2n−1)C_{n-1} = \tfrac1n\binom{2n-2}{n-1} such trees, about 4n4^n divided by a polynomial, and distinct trees need distinct bit strings

Counting is the whole argument: log⁡2Cn−1=2n−Θ(log⁡n)\log_2 C_{n-1} = 2n - \Theta(\log n). For n=1000n = 1000 it is about 1,982 bits, so LOUDS and balanced parentheses, at 2n+12n + 1 and 2n2n bits, are within a lower-order term of optimal.

Further reading

  • G. Jacobson, "Space-efficient static trees and graphs," Proceedings of FOCS (1989).
  • D. R. Clark, "Compact Pat trees," PhD thesis, University of Waterloo (1996).
  • J. I. Munro & V. Raman, "Succinct representation of balanced parentheses and static trees," SIAM Journal on Computing 31(3) (2001), 762–776.
  • M. Pătraşcu, "Succincter," Proceedings of FOCS (2008).
  • G. Navarro, Compact Data Structures: A Practical Approach to Compressed Data Structures (Cambridge University Press, 2016).