Information-Theoretic Space Lower Bounds
How small can a data structure be? If a structure must be able to represent any one of different objects, then some object needs at least bits: with fewer bits there are fewer than distinct memory contents. That count, the information-theoretic lower bound , is the yardstick for the rest of this section. It is the same counting as the decision-tree lower bound for sorting and the source coding theorem of information theory, applied to space.
Three levels of compactness
Following the terminology of Jacobson (1989) and Navarro's book on compact data structures, a structure that uses bits plus
| Name | Space | Example |
|---|---|---|
| implicit | bits | a sorted array searched by binary search; a binary heap |
| succinct | bits | rank/select bit vectors, balanced-parentheses trees |
| compact | bits | most compressed indexes |
and, the point of the whole field, still answers its queries fast, usually in or time on a word-RAM with . Storing bits is easy (number the objects and write down the number); answering queries on such a code without decompressing it is the hard part.
Common bounds
| Object | Number of objects | (bits) |
|---|---|---|
| a subset of size of | ||
| a bit vector of length with ones | , $H_0 = $ the entropy of the ones' density | |
| a permutation of elements | ||
| a binary tree with nodes (shape only) | ||
| an ordinal (rooted, ordered) tree with nodes |
The Catalan numbers count binary tree shapes; Stirling's formula gives , so about 2 bits per node suffice for a tree's shape. A pointer-based binary tree uses two pointers of bits each per node, bits: for a million nodes, 40 times the minimum. Entropy explains the bit-vector row: .
import math
def lg_comb(n, k): # fine for moderate n
return (math.lgamma(n + 1) - math.lgamma(k + 1) - math.lgamma(n - k + 1)) / math.log(2)
def lg_comb_big(U, n): # lg C(U, n) for huge U, without cancellation
s = sum(math.log1p(-i / U) for i in range(n)) # ln of (U - i) / U for i < n
return (n * math.log(U) + s - math.lgamma(n + 1)) / math.log(2)
print("binary tree shapes:")
for n in [10, 1000, 10 ** 6]:
lg_catalan = lg_comb(2 * n, n) - math.log2(n + 1)
print(f" n = {n:>7}: lg C_n = {lg_catalan:12.1f} bits ({lg_catalan / n:.3f} per node); "
f"pointers need 2n lg n = {2 * n * math.log2(n):14.0f}")
print("subsets of size n of a universe of size U:")
for U, n in [(2 ** 32, 10 ** 5), (2 ** 64, 10 ** 5), (10 ** 6, 5 * 10 ** 5)]:
print(f" U = {U:.3g}, n = {n:.0e}: lg C(U, n) = {lg_comb_big(U, n) / n:6.2f} bits per element, "
f"n lg(U/n) + n lg e = {(math.log2(U / n) + math.log2(math.e)):6.2f}, plain array {math.log2(U):5.1f}")binary tree shapes:
n = 10: lg C_n = 14.0 bits (1.404 per node); pointers need 2n lg n = 66
n = 1000: lg C_n = 1984.2 bits (1.984 per node); pointers need 2n lg n = 19932
n = 1000000: lg C_n = 1999969.3 bits (2.000 per node); pointers need 2n lg n = 39863137
subsets of size n of a universe of size U:
U = 4.29e+09, n = 1e+05: lg C(U, n) = 16.83 bits per element, n lg(U/n) + n lg e = 16.83, plain array 32.0
U = 1.84e+19, n = 1e+05: lg C(U, n) = 48.83 bits per element, n lg(U/n) + n lg e = 48.83, plain array 64.0
U = 1e+06, n = 5e+05: lg C(U, n) = 2.00 bits per element, n lg(U/n) + n lg e = 2.44, plain array 19.9
The last row shows the approximation failing for dense sets: with the exact count is 2 bits per element, that is, 1 bit per element of the universe, which a plain bit vector of bits already achieves.
Reaching about 2 bits per node: encoding a tree
A binary tree shape can be written in bits: traverse it in preorder and write 1 for each node and 0 for each empty subtree (there are of those). The sequence can be decoded uniquely, because a preorder walk knows when a subtree is complete. This is a succinct encoding but not yet a succinct data structure: to find the parent or the -th child of a node, one would have to decode from the start. The next two lessons add the -bit indexes (rank and select, then balanced parentheses) that make such navigation .
import random
def random_tree(n, rng): # a random binary tree shape as nested tuples
if n == 0:
return None
left = rng.randrange(n)
return (random_tree(left, rng), random_tree(n - 1 - left, rng))
def encode(t, out): # preorder: 1 = node, 0 = empty subtree
if t is None:
out.append(0); return
out.append(1); encode(t[0], out); encode(t[1], out)
def decode(bits, i=0): # returns (tree, next position)
if bits[i] == 0:
return None, i + 1
left, i = decode(bits, i + 1)
right, i = decode(bits, i)
return (left, right), i
def size(t):
return 0 if t is None else 1 + size(t[0]) + size(t[1])
rng = random.Random(1989)
t = random_tree(12, rng)
bits = []
encode(t, bits)
print("12-node tree in", len(bits), "bits:", "".join(map(str, bits)))
back, used = decode(bits)
print("decodes to the same shape:", back == t, "| bits read:", used)
for n in [100, 500]:
t = random_tree(n, rng); bits = []; encode(t, bits)
assert decode(bits)[0] == t and size(t) == n
print(f"n = {n}: {len(bits)} bits = 2n + 1; pointer representation with 32-bit pointers: {64 * n} bits")12-node tree in 25 bits: 1111000111100001010010100
decodes to the same shape: True | bits read: 25
n = 100: 201 bits = 2n + 1; pointer representation with 32-bit pointers: 6400 bits
n = 500: 1001 bits = 2n + 1; pointer representation with 32-bit pointers: 32000 bits
In C++
The same preorder encoding into a std::vector<bool> and back, on a fixed tree written as a string ("(LR)" for a node with subtrees L and R, "." for an empty subtree).
#include <cstdio>
#include <memory>
#include <string>
#include <vector>
struct Node {
std::unique_ptr<Node> left, right;
};
std::unique_ptr<Node> parse(const std::string& s, std::size_t& i) { // "(LR)" or "."
if (s[i] == '.') { ++i; return nullptr; }
++i; // '('
auto n = std::make_unique<Node>();
n->left = parse(s, i);
n->right = parse(s, i);
++i; // ')'
return n;
}
void encode(const Node* t, std::vector<bool>& bits) {
bits.push_back(t != nullptr);
if (t) { encode(t->left.get(), bits); encode(t->right.get(), bits); }
}
std::unique_ptr<Node> decode(const std::vector<bool>& bits, std::size_t& i) {
if (!bits[i++]) return nullptr;
auto n = std::make_unique<Node>();
n->left = decode(bits, i);
n->right = decode(bits, i);
return n;
}
std::string show(const Node* t) { return t ? "(" + show(t->left.get()) + show(t->right.get()) + ")" : "."; }
int main() {
std::string text = "((..)((.(..)).))";
std::size_t i = 0;
auto tree = parse(text, i);
std::vector<bool> bits;
encode(tree.get(), bits);
std::printf("tree %s -> %zu bits: ", text.c_str(), bits.size());
for (bool b : bits) std::printf("%d", b ? 1 : 0);
std::size_t j = 0;
auto back = decode(bits, j);
std::printf("\ndecoded: %s (%s)\n", show(back.get()).c_str(), show(back.get()) == text ? "same" : "different");
}tree ((..)((.(..)).)) -> 11 bits: 11001101000
decoded: ((..)((.(..)).)) (same)
Lower bounds on time as well as space
Space and time trade off. A classic example is the bit-probe complexity of membership: storing a set of elements from a universe of size in close to bits and answering membership with few bit probes. Buhrman, Miltersen, Radhakrishnan and Venkatesh (2000) showed that randomized schemes can use bits and one bit probe (with a small error probability), while deterministic schemes with one probe need much more space. For rank and select, Pătrașcu (2008) showed that -time rank can be achieved with bits for any constant , and Pătrașcu and Viola (2010) proved that this redundancy is essentially optimal. So " extra bits" in the next lesson is not a fixed quantity: the extra space can be made very small, at a cost in time.
Test yourself
About how many bits per node are needed, in the worst case, to store the shape of a binary tree with nodes?
- 1
- 2
A structure using bits is called...
- implicit
- succinct
- compact
- compressed
How many bits does the information-theoretic bound give for storing elements of a universe of size ?