Before we dive in, let’s test your memory of these key terms:
Let’s review their formal definitions.
Coin toss: \Omega = \{H, T\}
Roll of a die: \Omega = \{1, 2, 3, 4, 5, 6\}
Outcome (\omega): A single result of the experiment (an element of the sample space, \omega \in \Omega).
Event: A subset of the sample space.
Example: “Rolling an even number” = \{2, 4, 6\}
Note on Randomness
Randomness is a property of the process or experiment, and not of the outcome itself.
A Random Variable (RV) is a function X: \Omega \to \mathbb{R} that assigns a real number to each outcome.
Examples:
Random variables can be discrete (countable values) or continuous.
The expected value (or mean) of a discrete random variable X is: E[X] = \sum_{x} x \cdot \Pr(X = x)
Example: Expected value of a fair die roll: E[X] = 1 \cdot \frac{1}{6} + 2 \cdot \frac{1}{6} + 3 \cdot \frac{1}{6} + 4 \cdot \frac{1}{6} + 5 \cdot \frac{1}{6} + 6 \cdot \frac{1}{6} = 3.5
For any random variables X and Y: E[X + Y] = E[X] + E[Y]
This holds even if X and Y are dependent!
Example: Expected sum of two dice rolls: - E[X_1 + X_2] = E[X_1] + E[X_2] = 3.5 + 3.5 = 7
This property is what makes analyzing randomized algorithms tractable.
An Indicator Random Variable I_A for an event A is defined as: I_A = \begin{cases} 1 & \text{if event } A \text{ occurs} \\ 0 & \text{otherwise} \end{cases}
The expected value of an indicator variable is simply the probability of the event: E[I_A] = 1 \cdot \Pr(A) + 0 \cdot \Pr(\text{not } A) = \Pr(A)
Classic Example: Expected number of heads in n coin flips:
We’ll use this to count the expected number of comparisons in Quicksort!
Let X_{ij} be the random variable: X_{ij} = \begin{cases} 1, & \text{if elements } i \text{ and } j \text{ are compared} \\ 0, & \text{otherwise} \end{cases}
Total comparisons: X = \sum_{i < j} X_{ij}
Assume the elements are sorted: z_1 < z_2 < \cdots < z_n.
Elements z_i and z_j (where i < j) are compared only if:
The number of elements in this set is j - i + 1. Since each is equally likely: \Pr(X_{ij} = 1) = \frac{2}{j - i + 1}
By Linearity of Expectation: \mathbb{E}[X] = \sum_{i < j} \mathbb{E}[X_{ij}] = \sum_{i < j} \Pr(X_{ij} = 1) \mathbb{E}[X] = \sum_{i=1}^{n-1} \sum_{j=i+1}^{n} \frac{2}{j - i + 1}
Let k = j - i: \mathbb{E}[X] = \sum_{i=1}^{n-1} \sum_{k=1}^{n-i} \frac{2}{k + 1}
Now swap the order of summation: \mathbb{E}[X] = \sum_{k=1}^{n-1} \sum_{i=1}^{n-k} \frac{2}{k + 1}
Simplifying: \mathbb{E}[X] = \sum_{k=1}^{n-1} \frac{2(n-k)}{k + 1}
Bounding: \mathbb{E}[X] \leq 2n \sum_{k=1}^{n-1} \frac{1}{k + 1}
Using the bound for the Harmonic series H_n = \sum_{k=1}^n \frac{1}{k} = O(\log n): \mathbb{E}[X] = O(n \log n)
Expected running time of Randomized Quicksort: \boxed{O(n \log n)}
Exercise: Show that \mathbb{E}[X] = \Theta(n \log n).
CS F364: Design & Analysis of AlgorithmsTulasimohan Molli