Advanced Data Structures: What Part II Covers
Part I of the Algorithm Theory track built the classic toolkit: hash tables, balanced search trees, B-trees, union-find and the amortized analysis that explains them. Every one of those structures was analysed in one model, the RAM model, and most bounds were of the form per operation. This course, the first of Part II (Graduate theory), asks three sharper questions.
- Is really the limit? Only for algorithms that learn about keys by comparing them. If keys are integers that fit in a machine word, structures that look at the bits beat : van Emde Boas trees answer predecessor queries in time, fusion trees in .
- What else can a structure remember or support? Old versions of itself (persistence), changes inserted into the past (retroactivity), a forest that gains and loses edges while answering "are and connected?" (dynamic connectivity).
- What does an operation really cost on a machine with caches and disks, and how little space can a structure use? The external-memory and cache-oblivious models count block transfers instead of instructions; succinct structures use space close to the information-theoretic minimum and still answer queries fast.
The material follows the topics of a graduate advanced data structures course (in the spirit of MIT 6.851) and of the literature on compact data structures. The text, the proofs as sketched here and all programs are written for this site.
How to read the bounds in this course
Graduate-level bounds come with conditions, and the conditions matter as much as the formula. This course always states which kind of bound it means.
| Kind | Meaning | Example |
|---|---|---|
| worst case | every single operation obeys the bound | red-black tree search, |
| amortized | any sequence of operations costs at most times the bound; one operation may cost more | splay trees and link-cut trees, amortized |
| expected | averaged over the structure's own random choices, for every input | hashing with a universal family, expected |
| with high probability (w.h.p.) | fails with probability at most for a constant you may choose | skip-list search, w.h.p. |
| in a model | the cost measure itself: comparisons, word operations, cell probes, block transfers | predecessor in on a word-RAM |
When a result is quoted from the literature without a proof, the lesson says so and sketches the idea. When a proof is given, it is usually a sketch: the key inequality and why it holds, with the routine algebra left out. Long sketches are inside collapsible "Proof sketch" boxes.
The same question in two models
Here is the first example of how the model changes the answer. The question is predecessor: given a sorted set of integers from and a query , find the largest element of that is at most . A comparison-based algorithm needs about comparisons (binary search on the array). A word-RAM structure can instead binary search over the bits of : it asks "does some stored key share the top bits of ?" with a hash-table lookup and halves the range of each time, so it needs about lookups, where is the key length. The program counts both for keys of bits.
import random
rng = random.Random(6851)
w = 32
keys = set()
while len(keys) < 2 ** 14: # distinct random w-bit keys
keys.add(rng.randrange(2 ** w))
keys = sorted(keys)
def pred_binary_search(a, x):
lo, hi, probes = 0, len(a), 0 # invariant: a[:lo] <= x < a[hi:]
while lo < hi:
mid = (lo + hi) // 2
probes += 1
if a[mid] <= x: lo = mid + 1
else: hi = mid
return (a[lo - 1] if lo else None), probes
prefixes = [set() for _ in range(w + 1)] # prefixes[l] = top l bits of every key
for k in keys:
for l in range(w + 1):
prefixes[l].add(k >> (w - l))
def longest_common_prefix(x):
lo, hi, probes = 0, w, 0 # some key shares lo bits; none shares more than hi
while lo < hi:
mid = (lo + hi + 1) // 2
probes += 1 # one hash lookup
if (x >> (w - mid)) in prefixes[mid]: lo = mid
else: hi = mid - 1
return lo, probes
queries = [rng.randrange(2 ** w) for _ in range(1000)]
cmp = [pred_binary_search(keys, q)[1] for q in queries]
lcp = [longest_common_prefix(q) for q in queries]
print("n =", len(keys), " w =", w)
print("comparison model: binary search, max probes", max(cmp), " (lg n =", len(keys).bit_length() - 1, ")")
print("word-RAM: binary search on prefix length, max lookups", max(p for _, p in lcp), " (lg w =", w.bit_length() - 1, ")")
print("typical longest common prefix with a stored key:", sorted(l for l, _ in lcp)[500], "bits")n = 16384 w = 32
comparison model: binary search, max probes 14 (lg n = 14 )
word-RAM: binary search on prefix length, max lookups 5 (lg w = 5 )
typical longest common prefix with a stored key: 14 bits
Five hash lookups against fourteen comparisons. The longest common prefix is not yet the predecessor, and the prefix tables use words of space, so this is only the skeleton of the x-fast trie. Turning it into a correct, linear-space structure takes three lessons: x-fast tries, y-fast tries and, before them, van Emde Boas trees, which reach the same bound by recursion instead of hashing.
Amortized and worst case, side by side
The difference between an amortized and a worst-case bound is easy to say and easy to forget. A dynamic array that doubles when full has amortized appends but an occasional append that copies everything (Dynamic Arrays and Doubling). For a real-time system, or for a persistent structure where an expensive operation can be replayed from an old version again and again, that difference decides whether the structure is usable at all. The program shows both numbers.
def append_costs(n):
cap, size, costs = 1, 0, []
for _ in range(n):
cost = 1
if size == cap:
cost += size # copy every element into the new array
cap *= 2
size += 1
costs.append(cost)
return costs
c = append_costs(1 << 12)
print("appends:", len(c), " total cost:", sum(c), " amortized:", round(sum(c) / len(c), 3))
print("worst single append:", max(c))
# Replaying from an old version: take the state just before a resize and append to it 100 times.
print("100 appends, each from the version of size 2048:", 100 * (2048 + 1))appends: 4096 total cost: 8191 amortized: 2.0
worst single append: 2049
100 appends, each from the version of size 2048: 204900
The last line is the reason persistence needs care: an amortized analysis assumes each expensive state is passed through once, and a structure that lets you branch from any old version breaks that assumption.
The map of the course
| Section | Main results | Model |
|---|---|---|
| Word-RAM and integer structures | vEB trees and y-fast tries: predecessor in ; fusion trees: ; integer sorting in | word-RAM |
| Dynamic connectivity | Euler-tour trees and link-cut trees in ; fully dynamic graph connectivity in amortized updates | RAM (pointer machine) |
| Persistence and retroactivity | path copying; node copying with amortized overhead; retroactive queues and the lower bound idea | pointer machine |
| Memory hierarchies | ; B-trees optimal for search; cache-oblivious transposition, layout and funnelsort | DAM and ideal-cache models |
| Succinct and probabilistic structures | rank and select in extra bits; -bit trees; Bloom filters, cuckoo hashing, sketches; suffix arrays and FM-indexes | word-RAM, bit counts |
The course ends with exercises and a mini project: a persistent key-value store with versions, built on path copying. Later Part II courses build on it: Randomized Algorithms proves the tail bounds behind the "w.h.p." claims, and Streaming, Sketching & Sublinear Algorithms develops the sketches met here.
Note: Every program in this course counts operations (comparisons, lookups, block transfers, bits), never seconds. Counts are exactly reproducible in the browser and say what the theory says; timings in a WebAssembly Python say mostly how fast the interpreter is.
Test yourself
A structure has **amortized** cost per operation. Which statement is guaranteed?
- Every operation takes time
- Any sequence of operations takes time in total
- The expected time of each operation is
- The time is with probability
Why can a word-RAM predecessor structure beat the comparison bound?
- It uses more memory than words
- It reads and manipulates the bits of keys (prefixes, hashing) instead of only comparing keys
- It only works for random keys
- It is amortized
In the first program, binary search over prefix lengths needs about lookups. With -bit keys, about how many is that?
- 6
- 8
- 32
- 64