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 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 ?" 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 time with 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 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?
| Objects | How many | Minimum bits |
|---|---|---|
| all subsets of | : a plain bit vector is optimal | |
| subsets of size | when | |
| ordinal trees with nodes (children in order) | the Catalan number | |
| permutations of items |
What is the smallest number of bits that can store any 2-element subset of ?
Show the answer
11 bits
There are such sets, and , 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 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
Superblocks of bits each store the rank at their start, a number of about bits:
Blocks of bits each store the rank at their start relative to their superblock, a number below , so only bits:
Inside a block, a precomputed table gives, for every -bit pattern and every , the number of 1s among the pattern's first bits. There are only patterns:
A query adds three numbers, each read with a constant number of word operations, so it takes 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 0001 | 0100 1111 0000 1011 |
| superblock count 0 | superblock count 8 |
| block counts 0, 3, 4, 7 | block counts 0, 1, 5, 5 |
1011 hold one 1. So , the number of 1s in bits 0 to 29.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 in time with extra bits by sampling. Split the 1s into groups of 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 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 , the Elias–Fano encoding (Elias, 1974; Fano, 1971) stores each element's low bits verbatim and its high bits in unary, in a bit vector of about bits; on that vector recovers the -th element in . For the 1,024 elements of the hook it uses bits, within 5% of the minimum of 11,711.
Think first Raman, Raman and Rao (2002) store a bit vector with ones in bits with rank and select. How can a structure use fewer than bits for a bit vector of bits?
It spends about bits, which is far below when is far from . Each small block of bits is written as its class, the number of 1s it holds, plus an offset of bits saying which of the blocks with 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, , 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 nodes spends bits on pointers. Jacobson's level-order unary degree sequence (LOUDS) spends . Visit the nodes in breadth-first order and write each node's number of children in unary, as ones followed by a zero, after an initial 10 for an imaginary super-root. Number the nodes in breadth-first order. Then node corresponds to the -th 1 of the string (the 1 its parent wrote for it), and its own children are written right after the -th 0.
In the example, . Using on the string 10111011001001000, which node is the second child of node 2?
- Node 5
- Node 7
- Node 3
- Node 6
Show the answer
D. Node 6
, 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 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 time with 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 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 among the first positions descends one level per bit of , with one bit-vector rank per level: time, in bits.
That operation, 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 with 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 is the yardstick: bits for any subset of , for a set of elements, for an ordinal tree; succinct means that plus a lower-order term, with fast queries.
- Rank and select on a bit vector take time with extra bits, via two levels of counts and a table or a popcount; for systematic indexes extra bits is optimal, and compressed structures go far lower.
- is an asymptotic promise: the textbook parameters cost about 66% extra at , while word-based layouts spend 12.5% to 25%.
- With rank and select, a -bit LOUDS string or 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.
About how many bits does the most compact encoding need for an arbitrary set of 100 elements of ?
- About
- About 2,000
- About 1,500
- About 100
Show the answer
C. About 1,500
, close to . 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.
For the bit vector 1101 0011 1000 0111 (positions 0 to 15, left to right), what are and ?
- 6 and 7
- 5 and 8
- 5 and 7
- 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. counts the 1s before position 8: positions 0, 1, 3, 6, 7, so 5. The fifth 1 is at position 7, so .
In Jacobson's two-level rank structure, which part takes the most extra space as grows?
- The superblock counts, bits
- The block counts, bits
- The lookup table, bits
- None of them: all three are
Show the answer
B. The block counts, bits
There are blocks, each with a count of bits; the superblock counts take only in total and the table . Golynski's lower bound shows that, for systematic indexes, this cannot be beaten.
Why can no encoding of all ordinal trees with nodes use fewer than about bits for every tree?
- Because each node needs a pointer of bits
- Because LOUDS uses bits, and nothing beats LOUDS
- Because a tree has edges and each edge needs two bits
- Because there are such trees, about divided by a polynomial, and distinct trees need distinct bit strings
Show the answer
D. Because there are such trees, about divided by a polynomial, and distinct trees need distinct bit strings
Counting is the whole argument: . For it is about 1,982 bits, so LOUDS and balanced parentheses, at and 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).