COMPX270 — Randomised and Advanced Algorithms

Chapter 1: Randomness, Probability, and Algorithms

An interactive study guide covering randomized algorithms, expected-time analysis, QuickSort, and essential probability tools.

🃏 Card Deck Experiment
QuickSort Analysis
📊 Probability Toolkit
Tutorial & Quiz
📐 Math Foundations → 🗺 Mind Map →

All symbols, notation & math concepts explained from scratch

The Card Deck Experiment

Question: Take a standard deck of 52 cards (13 of each suit). Shuffle it uniformly at random. How many consecutive pairs of the same suit do you expect?

Interactive Simulation

Click "Shuffle & Count" to simulate shuffling a deck and counting consecutive same-suit pairs. Run it many times to see the average converge!

-
Pairs This Shuffle
0
Total Trials
-
Running Average
12
Theoretical Answer
1

Define indicator variables. Let \(X_i\) be the suit of the \(i\)-th card. Define \(Y_i = \mathbf{1}\{X_i = X_{i+1}\}\) for \(i = 1, \ldots, 51\).

The total number of consecutive same-suit pairs is \(Y = \sum_{i=1}^{51} Y_i\).

2

Compute each probability. For any position \(i\), once card \(i\) has some suit, there are 12 remaining cards of that suit out of 51 remaining cards total:

\[\Pr[Y_i = 1] = \Pr[X_i = X_{i+1}] = \frac{13 - 1}{52 - 1} = \frac{12}{51}\]

3

Apply linearity of expectation. We do not need independence of the \(Y_i\)!

\[\mathbb{E}[Y] = \sum_{i=1}^{51} \mathbb{E}[Y_i] = 51 \cdot \frac{12}{51} = 12 \quad \blacksquare\]

Key Insight: This generalizes! For a deck of \(4n\) cards (\(n\) of each suit), the expected number of consecutive same-suit pairs is \(\frac{(4n-1)(n-1)}{4n-1} = n-1\).

What is a Randomized Algorithm?

A randomized algorithm is an algorithm whose behaviour depends not only on the input \(x\), but also on random bits \(R \in \{0,1\}^*\).

Deterministic Algorithm

Input \(x\)
Algorithm \(A\)
Output \(A(x)\)

Same input → always same output, same running time.

Randomized Algorithm

Input \(x\)
Algorithm \(A\)
Output \(A(x; R)\)
Random bits \(R\)

Same input → output and/or runtime may vary.

Equivalent View: A randomized algorithm is a probability distribution over deterministic algorithms. First pick \(R\) at random, then run the deterministic algorithm \(A_R\) on input \(x\).

Types of Analysis

Analysis TypeFormulaWhat varies?
Worst-case\(T(n) = \max_{|x|=n} \tau_A(x)\)Nothing random
Expected (this course!)\(T(n) = \max_{|x|=n} \mathbb{E}_R[\tau_A(x; R)]\)Algorithm uses random bits \(R\)
Average-case\(T(n) = \mathbb{E}_x[\tau_A(x)]\)Input \(x\) is random
Amortized\(T(n) = \lim_{k\to\infty} \frac{1}{k}\max \tau_{A^k}(\ldots)\)Sequence of inputs

Why Randomize?

  • Avoid pathological corner cases
  • Simpler, faster algorithms
  • Avoid predictable outcomes
  • Cryptography & privacy
  • Break ties / impossibility results

Drawbacks

  • Non-deterministic behaviour
  • Random bits don't grow on trees!
  • Bad random bits → bad outputs

Las Vegas vs Monte Carlo

Las Vegas

Always Correct

Output is always correct, but running time is only bounded in expectation.

Example: Finding an even number in a permutation of \(\{1,\ldots,n\}\). Pick a random index — expected \(O(1)\) time, always correct.

Monte Carlo

Bounded Time

Running time is always bounded, but output is only correct with high probability.

Example: Check 100 random indices for an even number — worst-case \(O(1)\) time, succeeds with probability \(\geq 0.99\).

Explore: Even-Number Search

Array \(A\) of size \(n\) contains all numbers \(1\) to \(n\). Find an index holding an even number.

Worst-case: \(\Omega(n)\). An adversary can put all even numbers at the end. You must scan linearly.

for i in range(n):
    if A[i] % 2 == 0:
        return i

Expected time: \(O(1)\). Pick random index; half the entries are even, so expected 2 attempts.

while True:
    i = random.randint(0, n-1)
    if A[i] % 2 == 0:
        return i  # Always correct!

Worst-case time: \(O(1)\). Try 100 random indices. Probability of failure \(\leq (1/2)^{100}\).

for _ in range(100):
    i = random.randint(0, n-1)
    if A[i] % 2 == 0:
        return i
return "FAIL"  # Might be wrong!

Randomized QuickSort

Algorithm: QuickSort

QuickSort(A):
    if |A| ≤ 1: return A
    Pick pivot p = A[i] for random i in {1,...,n}
    Partition into A1 (< p), A2 (= p), A3 (> p)   // O(n) time
    Recursively sort A1 and A3
    Merge sorted A1, A2, A3                         // O(n) time
    return A
Always correct (Las Vegas)
Expected \(O(n \log n)\)
Worst-case \(O(n^2)\)
1

Set up the recurrence. Let \(T(n)\) be the expected running time. The pivot is the \(k\)-th largest with probability \(1/n\):

\[T(n) = cn + \frac{1}{n}\sum_{k=1}^{n}\bigl(T(k-1) + T(n-k)\bigr) = cn + \frac{2}{n}\sum_{k=0}^{n-1}T(k)\]

2

Convert to a differential equation. Using the fact that sums approximate integrals for non-decreasing functions, consider the continuous version:

\[T(x) = cx + \frac{2}{x}\int_0^x T(u)\,du\]

Let \(F(x) = \int_0^x T(u)\,du\), so \(F'(x) = T(x)\). This gives:

\[F'(x) = cx + \frac{2}{x}F(x)\]

3

Solve the ODE. Divide by \(x^2\):

\[\frac{F'(x)}{x^2} - \frac{2F(x)}{x^3} = \frac{c}{x}\]

Recognize the left side as \(\frac{d}{dx}\!\left[\frac{F(x)}{x^2}\right]\). Integrate:

\[\frac{F(x)}{x^2} = c\ln x + C \implies F(x) = cx^2 \ln x + Cx^2\]

4

Recover \(T\).

\[T(x) = F'(x) = 2cx\ln x + (2C+c)x = O(x\log x)\]

Therefore \(T(n) = O(n\log n)\). \(\blacksquare\)

Visualize: Randomized QuickSort

Watch how pivot selection affects partitioning. Click "Step" to advance one recursion level, or "Auto-Sort" to animate the full sort.

Comparisons: 0 Recursion Depth: 0

Counting Comparisons via Linearity of Expectation

Instead of solving a recurrence, we can elegantly count the expected number of comparisons using indicator random variables.

1

Set up indicator variables. Let \(a_1 < a_2 < \cdots < a_n\) be the elements in sorted order. Define \(X_{ij} \in \{0,1\}\) as the indicator of whether \(a_i\) and \(a_j\) are ever compared.

\[C(n) = \mathbb{E}\!\left[\sum_{i=1}^{n-1}\sum_{j=i+1}^{n} X_{ij}\right] = \sum_{i

2

Key insight: \(a_i\) and \(a_j\) are compared if and only if the first element from \(\{a_i, a_{i+1}, \ldots, a_j\}\) chosen as a pivot is \(a_i\) or \(a_j\) itself.

If any \(a_k\) with \(i < k < j\) is chosen as pivot first, then \(a_i\) and \(a_j\) are separated into different subarrays and never meet again.

\[\Pr[a_i \text{ and } a_j \text{ are compared}] = \frac{2}{j - i + 1}\]

3

Sum it up. Substituting \(k = j - i\):

\[C(n) = \sum_{i=1}^{n-1}\sum_{j=i+1}^{n} \frac{2}{j-i+1} = 2\sum_{i=1}^{n-1}\sum_{k=1}^{n-i}\frac{1}{k+1}\]

Using Harmonic numbers \(H_k = \sum_{j=1}^k \frac{1}{j} \leq \ln k + 1\):

\[C(n) = 2\sum_{i=1}^{n-1}(H_{n-i+1} - 1) = 2\sum_{m=2}^{n}(H_m - 1) \leq 2\sum_{m=2}^{n}\ln m \leq 2n\ln n\]

\(\blacksquare\)

Why this proof is elegant: No recurrences to solve! Just define the right indicator variables, compute a simple probability for each, and sum up. This is the power of linearity of expectation.

Probability Toolkit

These essential facts are used throughout the course. Click each card to reveal the details.

Tail-Sum Formula

Alternative way to compute \(\mathbb{E}[X]\) for non-negative integer-valued r.v.

Click to reveal

If \(X\) takes values in \(\mathbb{N} = \{0,1,2,\ldots\}\):

\[\mathbb{E}[X] = \sum_{n=1}^{\infty} \Pr[X \geq n]\]

Remember: starts at \(n=1\), not \(n=0\). (Check: if \(X=0\) always, \(\mathbb{E}[X]=0\) but \(\Pr[X\geq 0]=1\).)

Linearity of Expectation

The single most important tool in this chapter.

Click to reveal

\[\mathbb{E}[aX + bY] = a\,\mathbb{E}[X] + b\,\mathbb{E}[Y]\]

Extends to any sum: \(\mathbb{E}\!\left[\sum_i X_i\right] = \sum_i \mathbb{E}[X_i]\).

No independence needed!

Variance Formulas

How spread out is a random variable?

Click to reveal

\[\text{Var}[X] = \mathbb{E}[X^2] - \mathbb{E}[X]^2\]

\[\text{Var}[aX] = a^2 \text{Var}[X]\]

If \(X_1,\ldots,X_n\) are pairwise independent:

\[\text{Var}\!\left[\sum_i X_i\right] = \sum_i \text{Var}[X_i]\]

Jensen's Inequality

Convex functions and expectations.

Click to reveal

If \(f\) is convex:

\[f(\mathbb{E}[X]) \leq \mathbb{E}[f(X)]\]

Memory trick: Check with \(f(x)=x^2\). We know \(\text{Var}[X]\geq 0\), i.e., \(\mathbb{E}[X^2]\geq \mathbb{E}[X]^2\). ✔

Bernoulli & Binomial

The building blocks of discrete probability.

Click to reveal

Bernoulli(\(p\)): \(X\in\{0,1\}\), \(\Pr[X=1]=p\).

  • \(\mathbb{E}[X]=p\), \(\text{Var}[X]=p(1-p)\leq 1/4\)

Binomial(\(n,p\)): Sum of \(n\) i.i.d. Bernoulli(\(p\)).

  • \(\mathbb{E}[X]=np\), \(\text{Var}[X]=np(1-p)\)

Indicator Variables

The technique behind most proofs in this chapter.

Click to reveal

For an event \(E\), define \(\mathbf{1}_E\):

\[\mathbf{1}_E = \begin{cases} 1 & \text{if } E \text{ occurs} \\ 0 & \text{otherwise}\end{cases}\]

\(\mathbb{E}[\mathbf{1}_E] = \Pr[E]\). Combine with linearity of expectation to count expected number of "good" events!

Tutorial Problems

Work through each problem, then reveal the solution. Try before you look!

Problem 1 Warm-up

Consider a deck of \(4n\) cards, with \(n\) of each suit. After it is shuffled uniformly at random, what is the expected number of consecutive pairs of the same suit?

Answer: \(n - 1\)

Let \(Y_i = \mathbf{1}\{X_i = X_{i+1}\}\). For any position \(i\):

\[\Pr[X_i = X_{i+1}] = \frac{n-1}{4n-1}\]

By linearity of expectation:

\[\mathbb{E}[Y] = (4n-1)\cdot\frac{n-1}{4n-1} = n - 1\]

Sanity check: \(n=13 \Rightarrow 12\). ✔

Problem 2 Warm-up

A computer randomly generates a 2024-bit long binary string. What is the expected number of consecutive runs of 3 ones?

Answer: 252.75

Define \(Y_i = \mathbf{1}\{X_i = X_{i+1} = X_{i+2} = 1\}\) for \(i=1,\ldots,2022\). By independence of each bit:

\[\mathbb{E}[Y_i] = \left(\frac{1}{2}\right)^3 = \frac{1}{8}\]

By linearity: \(\mathbb{E}[Y] = \frac{2022}{8} = 252.75\).

Problem 3 Warm-up

Show that the expected number of fixed points of a uniformly random permutation of \(\{1,\ldots,n\}\) is 1. What is the variance?

Expectation = 1, Variance = 1

Let \(X_i = \mathbf{1}\{\pi(i)=i\}\). Then \(\Pr[X_i=1]=\frac{1}{n}\), so \(\mathbb{E}[X]=\sum_{i=1}^n \frac{1}{n} = 1\).

For variance: \(\text{Var}[X] = \mathbb{E}[X^2] - 1\). Expanding \(X^2 = (\sum_i X_i)^2\):

\[\mathbb{E}[X^2] = \sum_i \mathbb{E}[X_i] + \sum_{i\neq j}\Pr[\pi(i)=i \text{ and } \pi(j)=j]\]

\[= 1 + n(n-1)\cdot\frac{1}{n(n-1)} = 1 + 1 = 2\]

So \(\text{Var}[X] = 2 - 1 = 1\).

Problem 4 Warm-up

(1) Give a random variable \(X\) over \([0, \infty)\) such that \(\mathbb{E}[X] = \infty\). (2) Give a random variable \(X\) over \(\mathbb{N}\) such that \(\mathbb{E}[X] = \infty\).

(1) Continuous case: A random variable with probability density function \(f(x) = \frac{2}{\pi} \cdot \frac{1}{x^2 + 1}\) (the Cauchy distribution). Since \(\int_0^\infty \frac{2x}{\pi(x^2+1)}\,dx\) diverges, \(\mathbb{E}[X] = \infty\).

(2) Discrete case: A random variable with probability mass function \(p(n) = \frac{6}{\pi^2} \cdot \frac{1}{(n+1)^2}\). Since \(\sum_{n=0}^{\infty} \frac{n}{(n+1)^2}\) diverges, \(\mathbb{E}[X] = \infty\).

Problem 5 Warm-up

Prove the fact from the lecture: if \(X\) has a finite variance, then \(\text{Var}[X] = \mathbb{E}[X^2] - \mathbb{E}[X]^2\).

Proof: Let \(\mu = \mathbb{E}[X]\). By definition:

\[\text{Var}[X] = \mathbb{E}[(X - \mu)^2] = \mathbb{E}[X^2 - 2\mu X + \mu^2]\]

Applying linearity of expectation:

\[= \mathbb{E}[X^2] - 2\mu\,\mathbb{E}[X] + \mu^2 = \mathbb{E}[X^2] - 2\mu^2 + \mu^2 = \mathbb{E}[X^2] - \mu^2\]

\[= \mathbb{E}[X^2] - \mathbb{E}[X]^2 \quad \blacksquare\]

Problem 6 Problem Solving

Prove the fact from the lecture: if \(X\) takes values in \(\mathbb{N} = \{0, 1, 2, \ldots\}\) and \(\mathbb{E}[X]\) is finite, then \(\mathbb{E}[X] = \sum_{n=1}^{\infty} \Pr[X \geq n]\).

Proof (swap the sums):

\[\sum_{n=1}^{\infty} \Pr[X \geq n] = \sum_{n=1}^{\infty} \sum_{k=n}^{\infty} \Pr[X = k] = \sum_{k=1}^{\infty} \sum_{n=1}^{k} \Pr[X = k]\]

The inner sum has \(k\) identical terms:

\[= \sum_{k=1}^{\infty} k \cdot \Pr[X = k] = \mathbb{E}[X] \quad \blacksquare\]

The swap of summation is justified because all terms are non-negative and \(\mathbb{E}[X]\) is finite.

Problem 7 Problem Solving

You have a biased coin with unknown \(p = \Pr[\text{Heads}] \in (0,1)\). Using only this coin, generate a fair coin toss. How many biased tosses do you need in expectation?

Von Neumann's Trick:

  1. Toss the biased coin twice.
  2. If HT → output Heads. If TH → output Tails.
  3. If HH or TT → discard and repeat.

This works because \(\Pr[\text{HT}] = p(1-p) = (1-p)p = \Pr[\text{TH}]\), so both are equally likely.

Expected tosses: Each attempt succeeds with probability \(2p(1-p)\). Expected attempts = \(\frac{1}{2p(1-p)}\), and each attempt uses 2 tosses, so:

\[\text{Expected tosses} = \frac{2}{2p(1-p)} = \frac{1}{p(1-p)}\]

Problem 8 Problem Solving

A mouse (M) wants to reach cheese (C). There are 3 paths: top (2 edges), middle (3 edges), bottom (4 edges). Each edge has a cat with probability \(p\), independently. What is the probability the mouse can still reach the cheese?

A path is open iff all its edges are cat-free. The mouse succeeds iff at least one path is open.

\[\Pr[\text{path}] = 1 - \Pr[\text{no path}]\]

\[= 1 - \Pr[\text{top blocked}]\cdot\Pr[\text{mid blocked}]\cdot\Pr[\text{bot blocked}]\]

\[= 1 - \bigl(1-(1-p)^2\bigr)\bigl(1-(1-p)^3\bigr)\bigl(1-(1-p)^4\bigr)\]

Part (b): Paths of length \(\leq 3\): only top and middle. Answer: \(1 - (1-(1-p)^2)(1-(1-p)^3)\).

Part (c): Expected cats = \(9p\) (9 edges, each has a cat with probability \(p\), by linearity).

Problem 9 Problem Solving

An index \(i\) is a "prefix-maximum" of array \(A\) if \(A[i]\) is the largest among \(A[1],\ldots,A[i]\). If we permute \(A\) uniformly at random, show that the expected number of prefix-maxima is \(H_n = O(\log n)\).

(a) If \(A\) is sorted (increasing), every element is a prefix-max, so \(\text{pf}(A) = n\).

(b) Let \(X_i = \mathbf{1}\{B[i]\text{ is prefix-max}\}\). The \(i\)-th element is a prefix-max iff it is the largest among the first \(i\) elements. By symmetry:

\[\Pr[X_i = 1] = \frac{1}{i}\]

By linearity of expectation:

\[\mathbb{E}[\text{pf}(B)] = \sum_{i=1}^n \frac{1}{i} = H_n = O(\log n) \quad \blacksquare\]

Problem 10 Advanced

Let \(X_1,X_2,\ldots\) be i.i.d. Bernoulli(\(p\)) and \(Y_n = X_1 \oplus X_2 \oplus \cdots \oplus X_n\). Find \(p_n = \Pr[Y_n = 1]\) and show it converges to \(1/2\).

Recurrence: \(p_{n+1} = (1-p)\,p_n + p\,(1-p_n) = (1-2p)\,p_n + p\)

Solution: Let \(q_n = p_n - 1/2\). Then \(q_{n+1} = (1-2p)\,q_n\), so \(q_n = (1-2p)^n \cdot q_0\) with \(q_0 = -1/2\).

\[\boxed{p_n = \frac{1}{2}\bigl(1 - (1-2p)^n\bigr)}\]

Since \(|1-2p| < 1\) for \(p\in(0,1)\), this converges exponentially fast to \(1/2\).

Chapter quizzes

Self-test and math questions for this chapter are in the Quiz Hub (practice or exam mode).

Open Quiz Hub Chapter flashcards