Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Chapter 25: Complete Selection Guide and Complexity Reference

25.1 Data Structure Selection Matrix

NeedPrimary ChoiceCP AlternativeResearch Alternative
Range sum/queryFenwick TreeSegment TreeSuccinct Range Sum
Range min/maxSegment TreeSparse TableRange Min Query
K-th order statWavelet TreeMerge Sort TreeWavelet Matrix
Path queries (tree)HLD + SegTreeLink-Cut TreeEuler Tour Tree
String searchTrieSuffix AutomatonFM-Index
PalindromesPalindromic TreeHash + binaryLCS-based
Dynamic connectivityDSU (offline)HDT (Holm–de Lichtenberg–Thorup)Euler Tour Trees
Priority queueBinary HeapFibonacci HeapBuffered Heap
Ordered statisticsOrder Statistic TreeTreapvan Emde Boas
Approx membershipBloom FilterCuckoo FilterQuotient Filter
Approx countingHyperLogLogCount-Min SketchCount Sketch

25.2 Complete Complexity Reference

Competitive Programming Structures

StructureBuildQueryUpdateSpace
Segment TreeO(n)O(log n)O(log n)O(n)
Fenwick TreeO(n)O(log n)O(log n)O(n)
Heavy-Light DecompO(n)O(log² n)O(log² n)O(n)
Link-Cut TreeO(n)O(log n) amortizedO(log n) amortizedO(n)
DSU with rollbackO(n)O(log n)O(log n)O(n)
Mo’s Algorithmn/aO(√n) amortized per queryn/aO(n)
Sparse TableO(n log n)O(1)Not supportedO(n log n)
Wavelet TreeO(n log σ)O(log σ)StaticO(n log σ)
Suffix AutomatonO(n)O(m)IncrementalO(n), ≤ 2n−1 states
Palindromic TreeO(n)O(1) amortizedIncrementalO(n)
Li Chao TreeO(n)O(log C)O(log C)O(n)

Two rows are easy to get wrong. DSU with rollback is O(log n), not O(α(n)). Undoing a union requires knowing exactly which parent pointers changed, and path compression rewrites pointers all along the path, so rollback DSU must use union by rank alone. You trade the inverse-Ackermann bound for the ability to undo. And Mo’s algorithm is offline: it answers q queries in O((n + q)√n) total, so the O(√n) is an amortized per-query share, not a bound on any individual query.

Research-Grade Structures

StructureSpaceQueryNotes
Succinct Bit Vectorn + o(n) bitsO(1) rank and selectOptimal to within lower-order terms
FM-Indexn·H_k(T) + o(n log σ) bitsO(m) countSelf-indexing: replaces the text
LOUDS Tree2n + o(n) bitsO(1) navigationNeeds rank/select support
Wavelet Matrixn log σ + o(n log σ) bitsO(log σ)Faster constants than a wavelet tree
Cache-oblivious BSTO(n)O(log_B n) I/OsOptimal without knowing B
CRDT G-CounterO(r) for r replicasO(1) read, O(r) mergeConverges without coordination
Fibonacci HeapO(n)O(1) find-min, O(log n)★ delete-minO(1) insert and O(1)★ decrease-key
HDT Dynamic ConnectivityO(n log n)O(log n / log log n)O(log² n)★ update (Holm–de Lichtenberg–Thorup)

★ = amortized. H_k(T) is the k-th order empirical entropy of the text. The FM-index is compressed to the text’s own entropy, which is what “self-indexing” means: you can discard the original text and still reconstruct any substring.

25.3 Algorithm Design Patterns

Each paradigm is defined by a bookkeeping question it must answer repeatedly, and becomes practical when a structure answers that question fast enough. Chapter 21 develops this in full; the summary:

ParadigmQuestion asked repeatedlyStructure that answers itExamples
Divide and conquerWhere do I resume?Stack (implicit or explicit)Quicksort, merge sort, binary search
Dynamic programmingHave I computed this state?Array (dense) or hash map (sparse)LCS, knapsack, edit distance
GreedyWhat is the best remaining option?Priority queue; union-find for connectivityHuffman, Dijkstra, Prim, Kruskal
BacktrackingCan this branch still succeed, and how cheaply do I undo?Bitmask, DLX, rollback DSUN-Queens, Sudoku, exact cover
Randomized(Defeat the adversary)Depends, randomness is the techniqueQuicksort, skip lists, treaps, Bloom filters

The recurring trap: greedy is only correct when the problem has the matroid property or an equivalent exchange argument. Without it, greedy produces plausible wrong answers, which is worse than obviously wrong ones, because they pass casual testing.


Where this connects