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 1: The Philosophy and Mathematics of Data Structures

1.1 What Data Structures Really Are

A data structure is a systematic organization of data that enables efficient access and modification. But this dry definition obscures what data structures truly represent: the art of translating abstract relationships into concrete, manipulable forms. Every program is a model of some reality, and data structures are the vocabulary of that model.

Consider the challenge of representing a family tree. You could use an array, storing each person and a numeric index indicating their parent. This works, but querying becomes cumbersome, what is the average age of all grandchildren of a particular person? The structure constrains the operations you can perform efficiently.

Now consider representing the same family tree as a tree structure, where each node contains a person’s data and pointers to their children. Suddenly, the query becomes trivial: traverse from the given person to their children, then to their grandchildren, computing ages along the way. The data structure has made the problem tractable.

This relationship between structure and operation lies at the heart of data structure design. There is no universally “best” data structure; there are only structures that are better or worse for particular access patterns, modification patterns, and constraints.

1.2 The Abrahamic Truth: No Free Lunch

The No Free Lunch Theorem, originally formulated in optimization, has profound implications for data structure design. Simply stated: no data structure can excel at all operations. If you want fast search, you sacrifice fast insertion (or vice versa). If you want minimal memory, you sacrifice speed. If you want deterministic worst-case performance, you sacrifice average-case performance.

This fundamental trade-off is why we have so many data structures. Each structure represents a different point in the multi-dimensional space of possible trade-offs. The practitioner’s art lies in understanding which trade-offs matter for their specific application.

Let us enumerate the dimensions of this trade-off space:

Time Complexity Dimensions:

  • Search time (point queries)
  • Insertion time
  • Deletion time
  • Range query time
  • Successor/predecessor time
  • Maximum/minimum access time
  • Traversal time

Space Complexity Dimensions:

  • Raw space usage
  • Overhead per element
  • Space amplification under modification
  • Fragmentation behavior

Operational Dimensions:

  • Sequential access patterns
  • Random access patterns
  • Bulk operations
  • Persistence and versioning

Implementation Dimensions:

  • Complexity of implementation
  • Debugging difficulty
  • Cache behavior
  • Thread safety

1.3 Asymptotic Analysis: The Language of Efficiency

Computer scientists use asymptotic notation to describe the behavior of algorithms and data structures as input sizes grow arbitrarily large. This approach abstracts away machine-specific constants and focuses on the fundamental growth rate.

Big-O Notation: Upper Bounds

O(f(n)) describes an upper bound on running time. f(n) = O(g(n)) means that f grows no faster than some constant multiple of g for sufficiently large n. Formally:

∃ c > 0, ∃ n₀ > 0, such that ∀ n ≥ n₀: 0 ≤ f(n) ≤ c·g(n)

When we say array indexing is O(1), we mean the access time is bounded by a constant, regardless of how many elements are stored. This is an upper bound, we’re saying access will never be worse than constant time.

Big-Omega Notation: Lower Bounds

Ω(f(n)) describes a lower bound. f(n) = Ω(g(n)) means f grows at least as fast as g for sufficiently large n. Formally:

∃ c > 0, ∃ n₀ > 0, such that ∀ n ≥ n₀: 0 ≤ c·g(n) ≤ f(n)

When we say comparison-based sorting requires Ω(n log n), we’re establishing that no comparison sort can do better in the worst case. This is a fundamental lower bound.

Theta Notation: Tight Bounds

Θ(f(n)) indicates both upper and lower bounds. f(n) = Θ(g(n)) means f grows asymptotically the same rate as g. Formally:

∃ c₁ > 0, ∃ c₂ > 0, ∃ n₀ > 0, such that ∀ n ≥ n₀: c₁·g(n) ≤ f(n) ≤ c₂·g(n)

When we say mergesort is Θ(n log n), we mean this is both an upper bound (it won’t be slower) and a lower bound (you can’t do better).

Little-o and Little-Omega: Asymptotic Inequalities

o(f(n)) means “grows strictly slower than f(n)”: lim(n→∞) g(n)/f(n) = 0

ω(f(n)) means “grows strictly faster than f(n)”: lim(n→∞) g(n)/f(n) = ∞

1.4 Common Complexity Classes

input size n → operations → O(1) O(log n) O(n) O(n log n) O(n²) Feasible at n = 10⁶: O(1), O(log n), O(n), O(n log n). Not feasible: O(n²) and worse.
How the common complexity classes diverge. The gaps are what decide feasibility.

Understanding the practical implications of different complexity classes is essential:

Constant Time: O(1)

  • Array indexed access
  • Hash table lookup (with good hash function)
  • Bit operations
  • Stack push/pop
  • Queue enqueue/dequeue

The signature of O(1) is “doesn’t depend on n.” Whether you have 10 elements or 10 million, the operation takes the same time.

Logarithmic Time: O(log n)

  • Binary search in sorted array
  • Balanced tree operations (BST, AVL, Red-Black, B-Tree)
  • Skip list operations (expected, not worst case)

Logarithmic growth is remarkably slow. Even at n = 1 billion, log₂(n) ≈ 30. This means an O(log n) operation takes at most about 30 steps for a billion elements.

O(log n) is the floor for comparison-based search, but not for every kind of search. When keys are integers below some bound u, a van Emde Boas tree answers predecessor queries in O(log log u), about 5 steps for 32-bit keys, by exploiting the bits of the key rather than comparing whole keys (Chapter 24).

Linear Time: O(n)

  • Sequential scan of array
  • Linked list traversal
  • Breadth-first or depth-first graph search
  • Counting sort (under constraints)
  • Hash table operations with poor hash function

Linearithmic Time: O(n log n)

  • Comparison-based sorting (merge sort, heap sort, quicksort average case)
  • Building a heap by n successive inserts (bottom-up heapify builds one in O(n))
  • Balanced tree operations if rebuilding required

Polynomial Time: O(n^k)

  • Simple matrix operations
  • Naive string matching (O(nm))
  • Certain dynamic programming solutions

Exponential Time: O(2^n)

  • Generating all subsets
  • Naive solutions to NP-complete problems
  • Recursive Fibonacci without memoization

Factorial Time: O(n!)

  • Generating all permutations
  • Traveling salesman brute force
  • Certain combinatorial problems

1.5 Amortized Analysis: Averaging the Worst

Sometimes we care less about individual operation cost and more about total cost over a sequence of operations. Amortized analysis computes the average cost per operation over a worst-case sequence.

Consider a dynamic array (like Python’s list or Java’s ArrayList). When it fills, it doubles its capacity and copies all elements. This copy costs O(n), which seems expensive. However, this expensive operation happens only rarely, specifically, when the array size is a power of 2.

Over n insert operations, the total cost is: n + (1 + 2 + 4 + 8 + … + n) ≤ n + 2n = 3n

So the amortized cost per insert is O(3n/n) = O(1). Each individual insert is O(1) on average, even though occasional inserts are O(n).

Three techniques for amortized analysis exist:

Aggregate Analysis: Simply sum the costs of all operations and divide by n. If the total cost of any sequence of n operations is T(n), the amortized cost is T(n)/n.

Accounting Method: Assign different charges to different operations. Some operations are charged more than they cost; the excess is stored as “credit” and used to pay for operations that cost more than they were charged. The total credit never goes negative.

For dynamic arrays, we might charge 3 units for each insert: 1 unit for the immediate insert, 1 unit to copy this element at the next resize, and 1 unit to copy one of the older elements that were already copied at the previous resize.

Potential Method: Define a potential function Φ that maps data structure states to non-negative numbers. The amortized cost of an operation is the real cost plus the change in potential:

amortized_cost = real_cost + ΔΦ

If Φ is always non-negative and Φ(start) = 0, the total amortized cost is an upper bound on total real cost.

For a dynamic array, define Φ = 2n - m, where n is the current size and m is the capacity. Right after a doubling, before the new element is written, the array is half full and Φ = 0. When the array is full, Φ = n (its maximum), exactly enough to pay for copying the n elements at the next resize.

1.6 The RAM Model and Real Costs

Theoretical analysis assumes the Random Access Machine (RAM) model, where:

  • All operations (arithmetic, memory access) take constant time
  • Memory is unbounded
  • No cache effects

Reality is more complex. Modern computers have hierarchical memory:

  • L1 cache: ~32KB, ~1ns access
  • L2 cache: ~256KB, ~4ns access
  • L3 cache: ~8MB, ~15ns access
  • Main memory: ~64GB, ~100ns access
  • SSD: ~100GB, ~100μs access
  • Hard disk: ~TB, ~10ms access

A structure that minimizes main memory accesses may perform better than one with theoretically superior asymptotic complexity. This is why:

  • Arrays often outperform linked lists (cache-friendly sequential access)
  • B-trees outperform binary trees for database indexes (fewer disk accesses)
  • Cache-oblivious structures adapt to all levels automatically

1.7 Lower Bounds: How Low Can You Go?

A lower bound establishes that no algorithm can do better than a certain complexity. Proving lower bounds is often more difficult than proving upper bounds.

Comparison-Based Sorting: Ω(n log n) The proof uses a decision tree argument. Any comparison sort can be represented as a binary decision tree, where each internal node represents a comparison and each leaf represents a final ordering. A binary tree with L leaves must have height at least log₂ L. Since there are n! possible orderings, the tree needs at least n! leaves, requiring height at least log₂(n!) = Ω(n log n).

Static Dictionary: Ω(log n) For static sets (no insertions or deletions), any comparison-based structure answering membership queries needs Ω(log n) comparisons in the worst case. This is why binary search and balanced binary trees are optimal among comparison-based structures for static ordered sets. Hashing escapes the bound because it does not compare keys: FKS perfect hashing answers static membership in O(1) worst-case time with O(n) space.

Dynamic Dictionary: Ω(1) Surprisingly, with amortization or randomization, we can achieve O(1) expected time for dynamic sets. Hash tables achieve this, though at the cost of worst-case degradation.

1.8 References for Further Study

The mathematical foundations of data structures draw from several disciplines:

  • Combinatorics: Counting arguments in decision tree lower bounds
  • Information Theory: Entropy bounds on compression and encoding
  • Algebra: Group theory in symmetric structures, polynomial methods
  • Probability: Randomization in skip lists, hashing, and probabilistic data structures
  • Algebraic Topology: Recent connections to persistent homology and spatial data structures

Exercises

Difficulty: ★ check your understanding · ★★ apply it · ★★★ stretch.

1.1 ★ Constants for a Θ bound. Show that f(n) = 3n² + 10n + 5 is Θ(n²) by giving explicit constants c₁, c₂ and n₀ that satisfy the formal definition in Section 1.3. Then explain why the definition needs n₀ at all.

Solution

Take c₁ = 3 and c₂ = 4.

  • Lower bound: 3n² ≤ 3n² + 10n + 5 holds for every n ≥ 1, since 10n + 5 > 0.
  • Upper bound: 3n² + 10n + 5 ≤ 4n² is equivalent to 10n + 5 ≤ n². At n = 10 this is 105 ≤ 100, which fails. At n = 11 it is 115 ≤ 121, which holds, and for larger n the left side grows by 10 per step while the right side grows by 2n + 1 ≥ 23, so it keeps holding.

So c₁ = 3, c₂ = 4, n₀ = 11 work. Other choices are fine too: c₂ = 18 works from n₀ = 1, because 3n² + 10n + 5 ≤ 3n² + 10n² + 5n² for n ≥ 1.

n₀ exists so that small inputs, where lower-order terms dominate, do not decide the bound. With c₂ = 4 the inequality is false for n ≤ 10 and true forever after; asymptotic notation only cares about “forever after”.

1.2 ★ Count the copies. A dynamic array starts with capacity 1 and doubles its capacity whenever an insert finds it full. Starting from empty, you insert 17 elements. Which inserts trigger a resize, how many element copies happen in total, and what is the total cost if each write of a new element costs 1 and each copy costs 1? Compare the total with the 3n bound from Section 1.5.

Solution

A resize happens when an insert finds size = capacity:

Insert #Size beforeCapacity beforeCopiesCapacity after
21112
32224
54448
988816
1716161632

Total copies: 1 + 2 + 4 + 8 + 16 = 31. Total cost: 17 writes + 31 copies = 48. The bound is 3 × 17 = 51, so 48 ≤ 51 as promised.

The copies form a geometric series whose largest term (16) is less than n = 17, so the copies sum to less than 2n. Insert 17 is the worst case for this n: it pays for a 16-element copy and then leaves the new array almost empty.

1.3 ★★ Rank the growth rates. Order these functions from slowest-growing to fastest-growing as n → ∞. Mark any two that are Θ of each other.

n log₂ n, n^1.5, 2^(log₂ n), log₂(n!), n² / log₂ n, √n, (log₂ n)³, 1.01ⁿ

Hint

Simplify first: one of these is just n in disguise, and Section 1.7 tells you the order of log₂(n!). For a polylog against a power, substitute n = 2^k.

Solution

(log₂ n)³ < √n < 2^(log₂ n) < n log₂ n ≡ log₂(n!) < n^1.5 < n² / log₂ n < 1.01ⁿ

Reasons, pair by pair:

  • (log₂ n)³ vs √n. With n = 2^k the ratio is k³ / 2^(k/2), which goes to 0. Any power of a logarithm is o(n^ε) for every ε > 0. The crossover is late, though: √n only passes (log₂ n)³ at about n ≈ 6 × 10⁸, so for small inputs the “slower” function is larger.
  • 2^(log₂ n) is exactly n.
  • n vs n log₂ n. The ratio is log₂ n, which is unbounded.
  • n log₂ n vs log₂(n!). These are Θ of each other: log₂(n!) ≤ n log₂ n trivially, and log₂(n!) ≥ (n/2) log₂(n/2) (see Exercise 1.5).
  • n log₂ n vs n^1.5. Divide both by n: log₂ n is o(√n).
  • n^1.5 vs n² / log₂ n. The ratio (n² / log₂ n) / n^1.5 = √n / log₂ n is unbounded.
  • n² / log₂ n vs 1.01ⁿ. Every exponential with base > 1 eventually beats every polynomial. Here 1.01ⁿ overtakes n² / log₂ n at about n ≈ 1,190.

1.4 ★★ The potential method by hand. Use the potential Φ = 2n − m from Section 1.5 (n = size, m = capacity) for a doubling dynamic array. Charge real cost 1 for writing the new element plus 1 per element copied. Compute the amortized cost (real cost + ΔΦ) of (a) an insert that does not resize and (b) an insert that finds the array full with n = m elements.

Hint

In case (b), compute Φ before the insert with n = m, and Φ after with size m + 1 and capacity 2m.

Solution

(a) No resize. Real cost 1. n grows by 1 and m is unchanged, so ΔΦ = 2. Amortized cost = 1 + 2 = 3.

(b) Resize. Real cost is m copies plus 1 write, so m + 1.

  • Before: Φ = 2m − m = m.
  • After: size m + 1, capacity 2m, so Φ = 2(m + 1) − 2m = 2.
  • ΔΦ = 2 − m.

Amortized cost = (m + 1) + (2 − m) = 3.

Both cases cost exactly 3, so n inserts have amortized cost 3n. The potential built up by cheap inserts (2 per insert) is what pays for the copy: by the time the array is full, Φ has climbed to m, and the resize spends it. Because Φ ≥ 0 after the first insert, total real cost is at most total amortized cost (up to the constant Φ at the start), which gives the same 3n bound as aggregate analysis.

1.5 ★★ How many comparisons does sorting 10 items need? Section 1.7 claims log₂(n!) = Ω(n log n) without proof.

(a) Prove log₂(n!) ≥ (n/2) log₂(n/2) for n ≥ 2, and conclude that log₂(n!) = Θ(n log n).

(b) Use the decision-tree argument to give a lower bound on the number of comparisons any comparison sort needs in the worst case on 10 elements.

Hint

For (a), throw away the smaller half of the factors in n! = 1 × 2 × … × n, and make each remaining factor smaller too.

Solution

(a) n! = 1 × 2 × … × n. The largest ⌈n/2⌉ factors are each at least n/2, and the rest are at least 1, so

n! ≥ (n/2)^(n/2)

Taking log₂ of both sides gives log₂(n!) ≥ (n/2) log₂(n/2) = (n/2)(log₂ n − 1), which is at least (n/4) log₂ n once n ≥ 4. So log₂(n!) = Ω(n log n). For the upper bound, each of the n factors is at most n, so n! ≤ nⁿ and log₂(n!) ≤ n log₂ n. Together: log₂(n!) = Θ(n log n).

(b) 10! = 3,628,800 orderings, so the decision tree needs at least that many leaves. A binary tree of height h has at most 2^h leaves, so 2^h ≥ 3,628,800, giving h ≥ log₂(3,628,800) ≈ 21.79. Height is an integer, so h ≥ 22: some input forces at least 22 comparisons. (This bound happens to be achievable; 22 comparisons suffice for 10 elements. Merge sort uses up to 25.)

1.6 ★★★ Shrinking without thrashing. You add a pop operation to the doubling array, and to save memory you halve the capacity whenever a pop leaves the array at most half full (n ≤ m/2).

(a) Find a sequence of operations that costs Θ(n) per operation.

(b) Change the rule to: halve the capacity when a pop leaves the array at most a quarter full (n ≤ m/4). Argue that every operation is now amortized O(1).

Hint

For (a), start from a full array and alternate push and pop. For (b), ask how full the array is immediately after any resize, and how many operations must happen before the next one.

Solution

(a) Fill the array so that n = m = 2^k. Then repeat: push, pop, push, pop, …

  • push: the array is full, so it doubles to capacity 2m and copies m elements. Now n = m + 1.
  • pop: now n = m ≤ 2m/2, so it halves to capacity m and copies m elements. Now n = m = capacity again.
  • push: full again, double, copy m elements.

Every operation after the fill copies m ≈ n elements, so a sequence of length 2n costs Θ(n²), Θ(n) per operation. The two thresholds coincide, so the array never gets to accumulate slack between resizes. In a simulation with m = 1024, 4,000 alternating operations after the fill copy about 4.1 million elements.

(b) Immediately after any resize, the array is (essentially) half full:

  • After a grow from capacity m to M = 2m, the size is m + 1 ≈ M/2.
  • After a shrink from capacity 2M to M (triggered at n = 2M/4 = M/2), the size is M/2.

From that state, the next grow needs the size to rise from about M/2 to M: at least about M/2 pushes. The next shrink needs the size to fall from M/2 to M/4: at least M/4 pops. Either way, at least about M/4 operations happen between consecutive resizes, and a resize copies at most M elements. Charging the copy to those operations costs at most about 4 copies each, so every operation is amortized O(1). (Rerunning the simulation from (a) with the quarter rule, the 4,000 alternating operations trigger exactly one resize, a 1,024-element copy on the first push, instead of 4.1 million copies.)

The general lesson: the grow threshold and the shrink threshold must be separated by a constant factor, so that a resize always leaves room for Θ(m) cheap operations before the next one.


Where this connects