COMPX270 — Chapter 1 Supplement
Mathematical Foundations
Every symbol, notation, and concept used in Chapter 1 — explained from scratch with examples and inline checks.
Sets & Number Systems
A set is an unordered collection of distinct objects. We write \(x \in S\) to mean "\(x\) is an element of \(S\)" and \(x \notin S\) for the opposite.
Standard Number Systems
Summation & Products
The sigma notation \(\displaystyle\sum_{i=a}^{b} f(i)\) means "add up \(f(i)\) for each integer \(i\) from \(a\) to \(b\)":
\[\sum_{i=1}^{n} f(i) = f(1) + f(2) + f(3) + \cdots + f(n)\]
\(\displaystyle\sum_{i=1}^{4} i^2 = 1^2 + 2^2 + 3^2 + 4^2 = 1 + 4 + 9 + 16 = 30\)
Key Summation Facts Used in Chapter 1
When we have two indices, we iterate over both. The QuickSort analysis uses:
\[\sum_{i=1}^{n-1}\sum_{j=i+1}^{n} f(i,j)\]
This means: for each \(i\) from 1 to \(n{-}1\), for each \(j\) from \(i{+}1\) to \(n\), add \(f(i,j)\). It sums over all pairs \((i,j)\) with \(i < j\).
Big-O Notation (Asymptotic Analysis)
These describe how a function grows as its input \(n \to \infty\):
- \(f(n) = O(g(n))\): \(f\) grows at most as fast as \(g\). There exist \(c > 0, n_0\) such that \(f(n) \leq c\,g(n)\) for all \(n \geq n_0\).
- \(f(n) = \Omega(g(n))\): \(f\) grows at least as fast as \(g\).
- \(f(n) = \Theta(g(n))\): \(f\) grows at exactly the same rate as \(g\) (both \(O\) and \(\Omega\)).
- \(3n^2 + 5n = O(n^2)\) — the dominant term is \(n^2\)
- QuickSort worst-case: \(O(n^2)\), expected: \(O(n \log n)\)
- Any deterministic even-number finder: \(\Omega(n)\) worst-case
Common Growth Rates (slowest to fastest)
Logarithms
\(\log_b(x) = y\) means \(b^y = x\). In this course:
- \(\ln x = \log_e x\) — natural logarithm (base \(e \approx 2.718\))
- \(\log x\) — in CS typically means \(\log_2 x\); in analysis often \(\ln x\). The chapter uses both.
Since all logarithms differ by a constant factor, \(O(\log n) = O(\ln n) = O(\log_2 n)\).
Key Properties
Probability Basics
A probability \(\Pr[E]\) (or \(\mathbb{P}[E]\)) is a number between 0 and 1 that describes how likely an event \(E\) is to occur:
- \(\Pr[E] = 0\) means the event never happens
- \(\Pr[E] = 1\) means the event always happens
- \(\Pr[\text{not } E] = 1 - \Pr[E]\) — the complement rule
In the card deck problem: \(\Pr[X_i = X_{i+1}] = \frac{12}{51}\). This reads: "the probability that card \(i\) has the same suit as card \(i{+}1\) is \(12/51\)."
Random Variables
A random variable (r.v.) \(X\) is a numerical outcome of a random experiment. It maps each outcome of the experiment to a number.
- Discrete: takes values in a countable set (e.g., \(\mathbb{N}\), \(\{0,1\}\)). Described by a probability mass function (PMF): \(p(k) = \Pr[X = k]\).
- Continuous: takes values in \(\mathbb{R}\) or an interval. Described by a probability density function (PDF) \(f(x)\) where \(\Pr[a \leq X \leq b] = \int_a^b f(x)\,dx\).
In the card deck problem, \(Y_i \in \{0, 1\}\) is a discrete random variable: it equals 1 if cards \(i\) and \(i{+}1\) have the same suit, and 0 otherwise.
Expectation (Expected Value)
The expected value (or mean) of a discrete random variable \(X\) is the weighted average of its possible values:
\[\mathbb{E}[X] = \sum_{x} x \cdot \Pr[X = x]\]
Think of it as the "long-run average" if you repeat the experiment many times.
\(\mathbb{E}[X] = \sum_{k=1}^{6} k \cdot \frac{1}{6} = \frac{1+2+3+4+5+6}{6} = 3.5\)
You can never roll 3.5, but if you roll many times, the average converges to 3.5.
Not every random variable has a well-defined expectation! The lecture notes give the example \(\Pr[X = k] = \frac{1}{C(1+k^2)}\) on \(\mathbb{Z}\), which has no expectation because the sum diverges.
Linearity of Expectation
For any random variables \(X\) and \(Y\) (independent or not!) and constants \(a, b \in \mathbb{R}\):
\[\mathbb{E}[aX + bY] = a\,\mathbb{E}[X] + b\,\mathbb{E}[Y]\]
This extends to any finite sum:
\[\mathbb{E}\!\left[\sum_{i=1}^n X_i\right] = \sum_{i=1}^n \mathbb{E}[X_i]\]
Total pairs \(Y = Y_1 + Y_2 + \cdots + Y_{51}\). The \(Y_i\) are not independent (if card 5 is a spade, that affects what card 6 can be). But linearity doesn't care:
\[\mathbb{E}[Y] = \sum_{i=1}^{51} \mathbb{E}[Y_i] = 51 \cdot \frac{12}{51} = 12\]
Variance
The variance measures how "spread out" a random variable is around its mean:
\[\text{Var}[X] = \mathbb{E}\!\left[(X - \mathbb{E}[X])^2\right]\]
Shortcut formula (very useful):
\[\boxed{\text{Var}[X] = \mathbb{E}[X^2] - \mathbb{E}[X]^2}\]
Properties of Variance
Linearity of expectation works without independence. Variance of a sum equals the sum of variances only when the variables are (pairwise) independent. This distinction matters!
Indicator Random Variables
For an event \(E\), the indicator random variable is:
\[\mathbf{1}_E = \begin{cases} 1 & \text{if } E \text{ occurs} \\ 0 & \text{otherwise}\end{cases}\]
Crucially: \(\mathbb{E}[\mathbf{1}_E] = 1 \cdot \Pr[E] + 0 \cdot \Pr[\text{not }E] = \Pr[E]\).
- Decompose a count \(X\) as a sum of indicators: \(X = \sum_i \mathbf{1}_{E_i}\)
- Compute each \(\mathbb{E}[\mathbf{1}_{E_i}] = \Pr[E_i]\) (usually easy for a single event)
- Apply linearity: \(\mathbb{E}[X] = \sum_i \Pr[E_i]\)
- Card pairs: \(Y_i = \mathbf{1}\{X_i = X_{i+1}\}\) — is pair \(i\) same-suit?
- Fixed points: \(X_i = \mathbf{1}\{\pi(i) = i\}\) — is \(i\) a fixed point?
- QuickSort comparisons: \(X_{ij} = \mathbf{1}\{a_i \text{ and } a_j \text{ compared}\}\)
- Binary runs: \(Y_i = \mathbf{1}\{X_i = X_{i+1} = X_{i+2} = 1\}\)
- Cat edges: \(X_e = \mathbf{1}\{\text{edge } e \text{ has cat}\}\)
Independence
Two events \(A\) and \(B\) are independent if:
\[\Pr[A \cap B] = \Pr[A] \cdot \Pr[B]\]
Random variables \(X\) and \(Y\) are independent if for all values \(x, y\):
\[\Pr[X = x \text{ and } Y = y] = \Pr[X = x] \cdot \Pr[Y = y]\]
Mutual Independence
\(X_1, \ldots, X_n\) are mutually independent if for every subset \(S\):
\[\Pr\!\left[\bigcap_{i \in S} \{X_i = x_i\}\right] = \prod_{i \in S} \Pr[X_i = x_i]\]
Pairwise Independence
\(X_1, \ldots, X_n\) are pairwise independent if every pair \(X_i, X_j\) (\(i \neq j\)) is independent.
This is weaker than mutual independence but still enough for variance of sums!
- Linearity of expectation: No independence needed.
- Variance of sums: Needs pairwise independence.
- Multiplying probabilities: \(\Pr[A \cap B] = \Pr[A]\Pr[B]\) only if independent.
Bernoulli & Binomial Distributions
Bernoulli(\(p\))
\(X \in \{0, 1\}\), \(\Pr[X = 1] = p\), \(\Pr[X = 0] = 1-p\).
- \(\mathbb{E}[X] = p\)
- \(\text{Var}[X] = p(1-p)\)
- Since \(X^2 = X\), we get \(\mathbb{E}[X^2] = p\)
- Max variance: \(1/4\) at \(p = 1/2\)
Binomial(\(n, p\))
\(X = \sum_{i=1}^n X_i\) where each \(X_i \sim \text{Bernoulli}(p)\), i.i.d.
\(\Pr[X = k] = \binom{n}{k} p^k (1-p)^{n-k}\)
- \(\mathbb{E}[X] = np\) (by linearity!)
- \(\text{Var}[X] = np(1-p)\) (by independence)
i.i.d. stands for independent and identically distributed. It means each \(X_i\) has the same distribution, and knowing the value of one tells you nothing about the others.
Geometric Distribution
The number of independent trials needed until the first success, where each trial succeeds with probability \(q\).
\[\Pr[X = k] = (1-q)^{k-1} \cdot q, \quad k = 1, 2, 3, \ldots\]
\[\mathbb{E}[X] = \frac{1}{q}\]
In the biased-to-fair coin problem, each attempt succeeds with probability \(q = 2p(1-p)\). The number of attempts until success is Geometric(\(q\)), so the expected number of attempts is \(\frac{1}{2p(1-p)}\).
We need: \(\displaystyle\sum_{k=1}^{\infty} k \cdot q(1-q)^{k-1} = \frac{1}{q}\), i.e., \(\displaystyle\sum_{k=0}^{\infty}(k+1)(1-q)^k = \frac{1}{q^2}\).
Recall the geometric series: \(f(x) = \displaystyle\sum_{k=0}^{\infty} x^k = \frac{1}{1-x}\) for \(|x| < 1\).
Differentiate: \(f'(x) = \displaystyle\sum_{k=1}^{\infty} k\,x^{k-1} = \sum_{k=0}^{\infty}(k+1)x^k = \frac{1}{(1-x)^2}\).
Set \(x = 1-q\): \(\displaystyle\sum_{k=0}^{\infty}(k+1)(1-q)^k = \frac{1}{q^2}\). Multiply by \(q\) to get \(\frac{1}{q}\). \(\blacksquare\)
Tail-Sum Formula
If \(X\) takes values in \(\mathbb{N} = \{0, 1, 2, \ldots\}\) and \(\mathbb{E}[X]\) is finite:
\[\mathbb{E}[X] = \sum_{n=1}^{\infty} \Pr[X \geq n]\]
The sum starts at \(n = 1\), not \(n = 0\). Check: if \(X = 0\) always, then \(\mathbb{E}[X] = 0\). But \(\Pr[X \geq 0] = 1 \neq 0\), so we must skip \(n = 0\).
Sometimes computing \(\Pr[X \geq n]\) is easier than computing \(\Pr[X = n]\). This formula lets you switch between the two approaches for computing expectation.
Harmonic Numbers
The \(n\)-th Harmonic number is:
\[H_n = \sum_{k=1}^{n} \frac{1}{k} = 1 + \frac{1}{2} + \frac{1}{3} + \cdots + \frac{1}{n}\]
Key Bounds
\[\ln n \leq H_n \leq \ln n + 1\]
More precisely, \(H_n = \ln n + \gamma + O(1/n)\) where \(\gamma \approx 0.5772\) is the Euler-Mascheroni constant. So \(H_n = \Theta(\log n)\).
- QuickSort comparisons: \(C(n) = 2\sum_{i=1}^{n-1}(H_{n-i+1} - 1) \leq 2n\ln n\)
- Prefix-maxima: \(\mathbb{E}[\text{pf}(B)] = H_n = O(\log n)\)
Jensen's Inequality
A function \(f: \mathbb{R} \to \mathbb{R}\) is convex if for all \(x, y\) and \(\lambda \in [0,1]\):
\[f(\lambda x + (1-\lambda)y) \leq \lambda f(x) + (1-\lambda) f(y)\]
Visually: the function curves upward. Examples: \(x^2\), \(e^x\), \(|x|\). A function is concave if \(-f\) is convex (curves downward): e.g., \(\ln x\), \(\sqrt{x}\).
If \(f\) is convex: \(\quad f(\mathbb{E}[X]) \leq \mathbb{E}[f(X)]\)
If \(f\) is concave: \(\quad f(\mathbb{E}[X]) \geq \mathbb{E}[f(X)]\)
Take \(f(x) = x^2\) (convex). Jensen says \(\mathbb{E}[X]^2 \leq \mathbb{E}[X^2]\). But \(\text{Var}[X] = \mathbb{E}[X^2] - \mathbb{E}[X]^2 \geq 0\), which is exactly this! Consistent.
Permutations & Fixed Points
A permutation of \(\{1, 2, \ldots, n\}\) is a rearrangement: a bijection \(\pi: \{1,\ldots,n\} \to \{1,\ldots,n\}\). There are \(n!\) permutations of \(n\) elements.
An integer \(i\) is a fixed point of permutation \(\pi\) if \(\pi(i) = i\) — the element stays in its original position.
The expected number of fixed points of a uniformly random permutation is exactly 1, regardless of \(n\). And the variance is also 1!
Recurrences & Differential Equations
A recurrence relation defines \(T(n)\) in terms of smaller values. Example from QuickSort:
\[T(n) = cn + \frac{2}{n}\sum_{k=0}^{n-1} T(k)\]
To solve it, we can convert to a differential equation.
The ODE Approach (used in QuickSort proof)
Replace the discrete recurrence with a continuous version using Fact: for non-decreasing \(f\):
\[\int_0^n f(x)\,dx \leq \sum_{k=0}^{n} f(k) \leq \int_1^{n+1} f(x)\,dx\]
The continuous version \(T(x) = cx + \frac{2}{x}\int_0^x T(u)\,du\) leads to the ODE \(F'(x) = cx + \frac{2}{x}F(x)\), which is solvable in closed form.
Math quiz
Chapter math quizzes are in the Quiz Hub. Filter by this chapter and choose Study / Math / All.