← Back to Chapter 1 Study Guide | ← Course Home Math Foundations

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

Definition — Set

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.

Symbol
LaTeX
Meaning
\(\{1, 2, 3\}\)
\{1,2,3\}
A set containing elements 1, 2, 3
\(\in\)
\in
"is an element of" — e.g. \(3 \in \{1,2,3\}\)
\(\notin\)
\notin
"is not an element of"
\(\subseteq\)
\subseteq
"is a subset of" — every element of the left is in the right
\(\emptyset\)
\emptyset
The empty set (contains nothing)
\(|S|\)
|S|
Cardinality — the number of elements in \(S\)
\(\{0,1\}^*\)
\{0,1\}^*
All finite-length binary strings (used for random bits)

Standard Number Systems

Symbol
LaTeX
Meaning
\(\mathbb{N}\)
\mathbb{N}
Natural numbers \(\{0, 1, 2, 3, \ldots\}\)
\(\mathbb{Z}\)
\mathbb{Z}
Integers \(\{\ldots, -2, -1, 0, 1, 2, \ldots\}\)
\(\mathbb{R}\)
\mathbb{R}
Real numbers (the entire number line)
\([0,1]\)
[0,1]
Closed interval: all reals \(x\) with \(0 \leq x \leq 1\)
\((0,1)\)
(0,1)
Open interval: all reals \(x\) with \(0 < x < 1\)

Summation & Products

Definition — Summation

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)\]

Example

\(\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

Formula
Name
Value / Bound
\(\sum_{i=1}^n 1\)
Count
\(= n\)
\(\sum_{i=1}^n i\)
Arithmetic
\(= \frac{n(n+1)}{2}\)
\(\sum_{i=1}^n \frac{1}{i}\)
Harmonic (\(H_n\))
\(\approx \ln n + 0.577\ldots\)
\(\sum_{k=0}^{\infty} x^k\)
Geometric
\(= \frac{1}{1-x}\) for \(|x|<1\)
Definition — Double Summation

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\).

Quick Check: What is \(\displaystyle\sum_{k=1}^{3} \frac{1}{k+1}\)?
\(1\)
\(\frac{13}{12}\)
\(\frac{11}{12}\)
\(\frac{3}{2}\)

Big-O Notation (Asymptotic Analysis)

Definition — \(O(\cdot)\), \(\Omega(\cdot)\), \(\Theta(\cdot)\)

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\)).
Examples from Chapter 1
  • \(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)

Notation
Name
Example (\(n=1000\))
\(O(1)\)
Constant
1
\(O(\log n)\)
Logarithmic
\(\approx 10\)
\(O(n)\)
Linear
1,000
\(O(n \log n)\)
Linearithmic
\(\approx 10{,}000\)
\(O(n^2)\)
Quadratic
1,000,000

Logarithms

Definition

\(\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

Rule
Formula
Example
Product
\(\ln(ab) = \ln a + \ln b\)
\(\ln 6 = \ln 2 + \ln 3\)
Quotient
\(\ln(a/b) = \ln a - \ln b\)
\(\ln(1/2) = -\ln 2\)
Power
\(\ln(a^k) = k \ln a\)
\(\ln(n^2) = 2\ln n\)
Derivative
\(\frac{d}{dx}\ln x = \frac{1}{x}\)
Used in QuickSort ODE proof
Integral
\(\int \frac{1}{x}dx = \ln x + C\)
Used to solve \(F'(x)/x^2\)
Quick Check: What is \(\ln(e^3)\)?
\(e^3\)
\(3e\)
\(3\)
\(1/3\)

Probability Basics

Definition — Probability

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
Symbol
LaTeX
Meaning
\(\Pr[E]\)
\Pr[E]
Probability of event \(E\)
\(\Pr[A \mid B]\)
\Pr[A \mid B]
Probability of \(A\) given \(B\) has occurred
\(\Pr[A \cap B]\)
\Pr[A \cap B]
Probability both \(A\) and \(B\) occur
\(\Pr[A \cup B]\)
\Pr[A \cup B]
Probability at least one of \(A\), \(B\) occurs
Example — Chapter 1 usage

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\)."

Quick Check: If \(\Pr[\text{rain}] = 0.3\), what is \(\Pr[\text{no rain}]\)?
\(0.7\)
\(0.3\)
\(1.3\)
\(0\)

Random Variables

Definition

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\).
Example

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)

Definition — \(\mathbb{E}[X]\)

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.

Symbol
LaTeX
Meaning
\(\mathbb{E}[X]\)
\mathbb{E}[X]
Expected value of \(X\)
\(\mathbb{E}_R[\cdot]\)
\mathbb{E}_R[\cdot]
Expectation over the randomness in \(R\)
\(\mathbb{E}[X^2]\)
\mathbb{E}[X^2]
Expected value of \(X\) squared (not \(\mathbb{E}[X]^2\)!)
Example — Fair Die

\(\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.

Watch Out

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.

Quick Check: If \(\Pr[X=0]=1/4\), \(\Pr[X=1]=1/2\), \(\Pr[X=2]=1/4\), what is \(\mathbb{E}[X]\)?
\(0.5\)
\(1\)
\(1.5\)
\(0.75\)

Linearity of Expectation

Theorem — The Most Important Tool

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]\]

Why is this so powerful? It lets us break a complicated random quantity into simple pieces. We don't need to know how the pieces relate to each other — even if they are dependent! Every major proof in Chapter 1 uses this.
Example — Card Deck

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

Definition — \(\text{Var}[X]\)

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

Property
Formula
Note
Non-negative
\(\text{Var}[X] \geq 0\)
Always!
Scaling
\(\text{Var}[aX] = a^2\,\text{Var}[X]\)
Squared constant
Shift
\(\text{Var}[X + c] = \text{Var}[X]\)
Adding a constant doesn't change spread
Sum (indep.)
\(\text{Var}\!\left[\sum_i X_i\right] = \sum_i \text{Var}[X_i]\)
Requires pairwise independence!
Key Difference from Expectation

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!

Quick Check: If \(X \in \{0,1\}\) with \(\Pr[X=1] = 1/2\), what is \(\text{Var}[X]\)?
\(1/2\)
\(1\)
\(1/4\)
\(0\)

Indicator Random Variables

Definition — \(\mathbf{1}_E\)

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]\).

The Recipe (used in almost every problem):
  1. Decompose a count \(X\) as a sum of indicators: \(X = \sum_i \mathbf{1}_{E_i}\)
  2. Compute each \(\mathbb{E}[\mathbf{1}_{E_i}] = \Pr[E_i]\) (usually easy for a single event)
  3. Apply linearity: \(\mathbb{E}[X] = \sum_i \Pr[E_i]\)
Examples in Chapter 1
  • 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

Definition

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!

When it Matters
  • 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\))

Single trial

\(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\))

\(n\) independent trials

\(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. Explained

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

Definition — Geometric(\(q\))

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}\]

Example — Von Neumann Trick

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\)

Quick Check: How many times do you expect to roll a fair die before getting a 6?
\(3\)
\(6\)
\(36\)
\(5\)

Tail-Sum Formula

Theorem

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]\]

Memory Trick

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\).

Why it's useful

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

Definition — \(H_n\)

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

Fact

\[\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)\).

Where they appear in Chapter 1
  • 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)\)
Quick Check: What is \(H_4 = 1 + \frac{1}{2} + \frac{1}{3} + \frac{1}{4}\)?
\(\frac{10}{4}\)
\(2\)
\(\frac{25}{12}\)
\(\frac{12}{7}\)

Jensen's Inequality

Definition — Convex Function

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}\).

Jensen's Inequality

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)]\)

Sanity Check

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

Definition — Permutation

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.

Definition — Fixed Point

An integer \(i\) is a fixed point of permutation \(\pi\) if \(\pi(i) = i\) — the element stays in its original position.

Symbol
LaTeX
Meaning
\(\pi\)
\pi
A permutation function
\(n!\)
n!
Factorial: \(n! = n \cdot (n-1) \cdots 2 \cdot 1\)
\(\binom{n}{k}\)
\binom{n}{k}
Binomial coefficient: \(\frac{n!}{k!(n-k)!}\), "n choose k"
Remarkable Result

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

Definition — Recurrence Relation

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)

Key Idea

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.

Symbol
LaTeX
Meaning
\(\int_a^b\)
\int_a^b
Definite integral from \(a\) to \(b\)
\(F'(x)\)
F'(x)
Derivative of \(F\) with respect to \(x\)
\(\frac{d}{dx}\)
\frac{d}{dx}
Differentiation operator
\(\oplus\)
\oplus
XOR (exclusive or): sum modulo 2

Math quiz

Chapter math quizzes are in the Quiz Hub. Filter by this chapter and choose Study / Math / All.

Open Quiz Hub