Chapter 3: Mathematical Foundations for DSA
3.07Beginner7 topics16 min3 interactive animations

Probability fundamentals

Probability lets you reason about randomised algorithms and average-case cost. This lesson covers sample spaces, events, conditional probability, independence, expected value, linearity of expectation and indicator variables.

Learning goals

  • Compute probabilities in a finite sample space
  • Use conditional probability and independence
  • Compute expected values with linearity and indicator variables

Prerequisites

Counting fundamentals
3.07.1

Sample space

The sample space Ω is the set of all possible outcomes of a random experiment. With equally likely outcomes, a probability is a ratio of counts. Two fair dice have |Ω| = 36 outcomes; a random permutation of n items has n! outcomes.

Clearly defining Ω first prevents most counting mistakes.

3.07.2

Events

An event is a subset of the sample space, and its probability is |event| / |Ω| for equally likely outcomes. P(A ∪ B) = P(A) + P(B) - P(A ∩ B) and P(not A) = 1 - P(A).

“The two dice sum to 7” contains 6 outcomes ((1,6), (2,5), (3,4), (4,3), (5,2), (6,1)), so its probability is 6/36 = 1/6.

Java
1int hits = 0;
2for (int a = 1; a <= 6; a++) for (int b = 1; b <= 6; b++) if (a + b == 7) hits++;
3double p = hits / 36.0; // 0.1666...
3.07.3

Conditional probability

P(A | B) = P(A ∩ B) / P(B) is the probability of A once you know B happened; it restricts the sample space to B. Example: given that the first die shows 6, the probability that the sum is at least 10 is 3/6 = 1/2 (second die 4, 5 or 6).

Bayes' rule P(A | B) = P(B | A) · P(A) / P(B) reverses the conditioning and underlies spam filters.

3.07.4

Independence

Events A and B are independent if P(A ∩ B) = P(A) · P(B): knowing one tells nothing about the other. Two separate coin flips are independent; drawing two cards without replacement is not.

Randomised algorithms rely on independence, for example independent hash functions in a Bloom filter. Repeating an independent trial that succeeds with probability p until it succeeds takes 1/p tries on average.

3.07.5

Expected value

The expected value E[X] = Σ x · P(X = x) is the average value of a random variable over many trials. A fair die has E = (1 + 2 + … + 6)/6 = 3.5. Average-case analysis computes the expected running time over a distribution of inputs.

If the target of a linear search is equally likely to be at any of n positions, the expected number of comparisons is (n + 1)/2. The animation shows the matching experiment: random permutations and the average count of fixed points.

Expected fixed points of a random shuffle

Interactive · edit the input

Your input

Try

this shuffle

empty

count by fixed points

00 fixed
01 fixed
02 fixed
03 fixed
04 fixed
Setup

There are 4! = 24 equally likely shuffles. We will average the number of fixed points (cards in their own slot) over all of them.

Step 1 / 26
Java
1// X = number of cards that land in their own position after a random shuffle
2// X = X0 + X1 + ... + X(n-1), where Xi is 1 if card i is fixed, else 0 (indicator variables)
3double expected = 0;
4for (int[] p : allPermutations(n)) { // each equally likely
5 int fixed = 0;
6 for (int i = 0; i < n; i++) if (p[i] == i) fixed++;
7 expected += fixed;
8}
9expected /= factorial(n); // = 1.0 for every n

Live state

0 shuffles

n

4

shuffles

24

No condition evaluated on this step
Execution trace (dry run table)Show 26 steps
StepLineStateWhat happens
1L3n=4 shuffles=24There are 4! = 24 equally likely shuffles. We will average the number of fixed points (cards in their own slot) over all of them.
2L6shuffle=1 fixed=4 total=4 average=4Shuffle 1: 4 fixed points. Running total 4.
3L6shuffle=2 fixed=2 total=6 average=3Shuffle 2: 2 fixed points. Running total 6.
4L6shuffle=3 fixed=2 total=8 average=2.667Shuffle 3: 2 fixed points. Running total 8.
5L6shuffle=4 fixed=1 total=9 average=2.25Shuffle 4: 1 fixed point. Running total 9.
6L6shuffle=5 fixed=1 total=10 average=2Shuffle 5: 1 fixed point. Running total 10.
7L6shuffle=6 fixed=2 total=12 average=2Shuffle 6: 2 fixed points. Running total 12.
8L6shuffle=7 fixed=2 total=14 average=2Shuffle 7: 2 fixed points. Running total 14.
9L6shuffle=8 fixed=0 total=14 average=1.75Shuffle 8: 0 fixed points. Running total 14.
10L6shuffle=9 fixed=1 total=15 average=1.667Shuffle 9: 1 fixed point. Running total 15.
11L6shuffle=10 fixed=0 total=15 average=1.5Shuffle 10: 0 fixed points. Running total 15.
12L6shuffle=11 fixed=0 total=15 average=1.364Shuffle 11: 0 fixed points. Running total 15.
13L6shuffle=12 fixed=1 total=16 average=1.333Shuffle 12: 1 fixed point. Running total 16.
14L6shuffle=13 fixed=1 total=17 average=1.308Shuffle 13: 1 fixed point. Running total 17.
15L6shuffle=14 fixed=0 total=17 average=1.214Shuffle 14: 0 fixed points. Running total 17.
16L6shuffle=15 fixed=2 total=19 average=1.267Shuffle 15: 2 fixed points. Running total 19.
17L6shuffle=16 fixed=1 total=20 average=1.25Shuffle 16: 1 fixed point. Running total 20.
18L6shuffle=17 fixed=0 total=20 average=1.176Shuffle 17: 0 fixed points. Running total 20.
19L6shuffle=18 fixed=0 total=20 average=1.111Shuffle 18: 0 fixed points. Running total 20.
20L6shuffle=19 fixed=0 total=20 average=1.053Shuffle 19: 0 fixed points. Running total 20.
21L6shuffle=20 fixed=1 total=21 average=1.05Shuffle 20: 1 fixed point. Running total 21.
22L6shuffle=21 fixed=1 total=22 average=1.048Shuffle 21: 1 fixed point. Running total 22.
23L6shuffle=22 fixed=2 total=24 average=1.091Shuffle 22: 2 fixed points. Running total 24.
24L6shuffle=23 fixed=0 total=24 average=1.043Shuffle 23: 0 fixed points. Running total 24.
25L6shuffle=24 fixed=0 total=24 average=1Shuffle 24: 0 fixed points. Running total 24.
26L9total=24 shuffles=24 return=1Average = 24 / 24 = 1.000. Linearity of expectation predicts this without enumerating: each of the 4 cards is fixed with probability 1/4, so E[X] = 4 × (1/4) = 1, even though the indicator variables are not independent.
3.07.6

Linearity of expectation

E[X + Y] = E[X] + E[Y] always, even when X and Y are dependent. That is what makes expectations easy to compute: split a complicated variable into simple parts and add their expectations.

A random permutation of n items has, on average, exactly one fixed point (an item in its original position) for every n ≥ 1, although the individual position events are dependent.

Expected fixed points of a random shuffle

Interactive · edit the input

Your input

Try

this shuffle

empty

count by fixed points

00 fixed
01 fixed
02 fixed
03 fixed
04 fixed
Setup

There are 4! = 24 equally likely shuffles. We will average the number of fixed points (cards in their own slot) over all of them.

Step 1 / 26
Java
1// X = number of cards that land in their own position after a random shuffle
2// X = X0 + X1 + ... + X(n-1), where Xi is 1 if card i is fixed, else 0 (indicator variables)
3double expected = 0;
4for (int[] p : allPermutations(n)) { // each equally likely
5 int fixed = 0;
6 for (int i = 0; i < n; i++) if (p[i] == i) fixed++;
7 expected += fixed;
8}
9expected /= factorial(n); // = 1.0 for every n

Live state

0 shuffles

n

4

shuffles

24

No condition evaluated on this step
Execution trace (dry run table)Show 26 steps
StepLineStateWhat happens
1L3n=4 shuffles=24There are 4! = 24 equally likely shuffles. We will average the number of fixed points (cards in their own slot) over all of them.
2L6shuffle=1 fixed=4 total=4 average=4Shuffle 1: 4 fixed points. Running total 4.
3L6shuffle=2 fixed=2 total=6 average=3Shuffle 2: 2 fixed points. Running total 6.
4L6shuffle=3 fixed=2 total=8 average=2.667Shuffle 3: 2 fixed points. Running total 8.
5L6shuffle=4 fixed=1 total=9 average=2.25Shuffle 4: 1 fixed point. Running total 9.
6L6shuffle=5 fixed=1 total=10 average=2Shuffle 5: 1 fixed point. Running total 10.
7L6shuffle=6 fixed=2 total=12 average=2Shuffle 6: 2 fixed points. Running total 12.
8L6shuffle=7 fixed=2 total=14 average=2Shuffle 7: 2 fixed points. Running total 14.
9L6shuffle=8 fixed=0 total=14 average=1.75Shuffle 8: 0 fixed points. Running total 14.
10L6shuffle=9 fixed=1 total=15 average=1.667Shuffle 9: 1 fixed point. Running total 15.
11L6shuffle=10 fixed=0 total=15 average=1.5Shuffle 10: 0 fixed points. Running total 15.
12L6shuffle=11 fixed=0 total=15 average=1.364Shuffle 11: 0 fixed points. Running total 15.
13L6shuffle=12 fixed=1 total=16 average=1.333Shuffle 12: 1 fixed point. Running total 16.
14L6shuffle=13 fixed=1 total=17 average=1.308Shuffle 13: 1 fixed point. Running total 17.
15L6shuffle=14 fixed=0 total=17 average=1.214Shuffle 14: 0 fixed points. Running total 17.
16L6shuffle=15 fixed=2 total=19 average=1.267Shuffle 15: 2 fixed points. Running total 19.
17L6shuffle=16 fixed=1 total=20 average=1.25Shuffle 16: 1 fixed point. Running total 20.
18L6shuffle=17 fixed=0 total=20 average=1.176Shuffle 17: 0 fixed points. Running total 20.
19L6shuffle=18 fixed=0 total=20 average=1.111Shuffle 18: 0 fixed points. Running total 20.
20L6shuffle=19 fixed=0 total=20 average=1.053Shuffle 19: 0 fixed points. Running total 20.
21L6shuffle=20 fixed=1 total=21 average=1.05Shuffle 20: 1 fixed point. Running total 21.
22L6shuffle=21 fixed=1 total=22 average=1.048Shuffle 21: 1 fixed point. Running total 22.
23L6shuffle=22 fixed=2 total=24 average=1.091Shuffle 22: 2 fixed points. Running total 24.
24L6shuffle=23 fixed=0 total=24 average=1.043Shuffle 23: 0 fixed points. Running total 24.
25L6shuffle=24 fixed=0 total=24 average=1Shuffle 24: 0 fixed points. Running total 24.
26L9total=24 shuffles=24 return=1Average = 24 / 24 = 1.000. Linearity of expectation predicts this without enumerating: each of the 4 cards is fixed with probability 1/4, so E[X] = 4 × (1/4) = 1, even though the indicator variables are not independent.
3.07.7

Indicator variables

An indicator variable Xᵢ is 1 if an event happens and 0 otherwise, so E[Xᵢ] = P(event). Write the quantity of interest as a sum of indicators and use linearity. For fixed points, Xᵢ = 1 when item i lands in position i, which has probability 1/n, so the expected total is n · (1/n) = 1.

The same method gives the expected number of comparisons in randomised quick sort, O(n log n), and the expected number of collisions in a hash table with n keys and m buckets, about n(n - 1)/(2m).

Java
1Random rnd = new Random(1);
2int n = 10, trials = 100_000; long total = 0;
3for (int t = 0; t < trials; t++) {
4 List<Integer> p = new ArrayList<>();
5 for (int i = 0; i < n; i++) p.add(i);
6 Collections.shuffle(p, rnd);
7 for (int i = 0; i < n; i++) if (p.get(i) == i) total++;
8}
9double avg = (double) total / trials; // close to 1.0

Expected fixed points of a random shuffle

Interactive · edit the input

Your input

Try

this shuffle

empty

count by fixed points

00 fixed
01 fixed
02 fixed
03 fixed
04 fixed
Setup

There are 4! = 24 equally likely shuffles. We will average the number of fixed points (cards in their own slot) over all of them.

Step 1 / 26
Java
1// X = number of cards that land in their own position after a random shuffle
2// X = X0 + X1 + ... + X(n-1), where Xi is 1 if card i is fixed, else 0 (indicator variables)
3double expected = 0;
4for (int[] p : allPermutations(n)) { // each equally likely
5 int fixed = 0;
6 for (int i = 0; i < n; i++) if (p[i] == i) fixed++;
7 expected += fixed;
8}
9expected /= factorial(n); // = 1.0 for every n

Live state

0 shuffles

n

4

shuffles

24

No condition evaluated on this step
Execution trace (dry run table)Show 26 steps
StepLineStateWhat happens
1L3n=4 shuffles=24There are 4! = 24 equally likely shuffles. We will average the number of fixed points (cards in their own slot) over all of them.
2L6shuffle=1 fixed=4 total=4 average=4Shuffle 1: 4 fixed points. Running total 4.
3L6shuffle=2 fixed=2 total=6 average=3Shuffle 2: 2 fixed points. Running total 6.
4L6shuffle=3 fixed=2 total=8 average=2.667Shuffle 3: 2 fixed points. Running total 8.
5L6shuffle=4 fixed=1 total=9 average=2.25Shuffle 4: 1 fixed point. Running total 9.
6L6shuffle=5 fixed=1 total=10 average=2Shuffle 5: 1 fixed point. Running total 10.
7L6shuffle=6 fixed=2 total=12 average=2Shuffle 6: 2 fixed points. Running total 12.
8L6shuffle=7 fixed=2 total=14 average=2Shuffle 7: 2 fixed points. Running total 14.
9L6shuffle=8 fixed=0 total=14 average=1.75Shuffle 8: 0 fixed points. Running total 14.
10L6shuffle=9 fixed=1 total=15 average=1.667Shuffle 9: 1 fixed point. Running total 15.
11L6shuffle=10 fixed=0 total=15 average=1.5Shuffle 10: 0 fixed points. Running total 15.
12L6shuffle=11 fixed=0 total=15 average=1.364Shuffle 11: 0 fixed points. Running total 15.
13L6shuffle=12 fixed=1 total=16 average=1.333Shuffle 12: 1 fixed point. Running total 16.
14L6shuffle=13 fixed=1 total=17 average=1.308Shuffle 13: 1 fixed point. Running total 17.
15L6shuffle=14 fixed=0 total=17 average=1.214Shuffle 14: 0 fixed points. Running total 17.
16L6shuffle=15 fixed=2 total=19 average=1.267Shuffle 15: 2 fixed points. Running total 19.
17L6shuffle=16 fixed=1 total=20 average=1.25Shuffle 16: 1 fixed point. Running total 20.
18L6shuffle=17 fixed=0 total=20 average=1.176Shuffle 17: 0 fixed points. Running total 20.
19L6shuffle=18 fixed=0 total=20 average=1.111Shuffle 18: 0 fixed points. Running total 20.
20L6shuffle=19 fixed=0 total=20 average=1.053Shuffle 19: 0 fixed points. Running total 20.
21L6shuffle=20 fixed=1 total=21 average=1.05Shuffle 20: 1 fixed point. Running total 21.
22L6shuffle=21 fixed=1 total=22 average=1.048Shuffle 21: 1 fixed point. Running total 22.
23L6shuffle=22 fixed=2 total=24 average=1.091Shuffle 22: 2 fixed points. Running total 24.
24L6shuffle=23 fixed=0 total=24 average=1.043Shuffle 23: 0 fixed points. Running total 24.
25L6shuffle=24 fixed=0 total=24 average=1Shuffle 24: 0 fixed points. Running total 24.
26L9total=24 shuffles=24 return=1Average = 24 / 24 = 1.000. Linearity of expectation predicts this without enumerating: each of the 4 cards is fixed with probability 1/4, so E[X] = 4 × (1/4) = 1, even though the indicator variables are not independent.

Practice ladder

Now try it yourself

Easy

  • What is the probability that two dice show the same number?
  • What is the expected value of a roll of a fair die?

Medium

  • Compute the expected number of heads in 10 fair coin flips using indicators
  • Given a sum of 8 on two dice, what is the chance that one die shows 2?

Advanced

  • Show that the expected number of collisions for n keys in m buckets under a uniform hash is n(n - 1) / (2m)

Key takeaways

  • Define the sample space, then count favourable outcomes.
  • Independence multiplies probabilities; conditioning restricts the sample space.
  • Linearity of expectation plus indicator variables solves many analysis problems without independence.