Advanced Counting

Study Sheet

Advanced Counting

Recursion, expectation, and the binomial theorem

Building Recurrences

Concept
Condition on the last step
n − 1 cells: a(n − 1) ways1n − 2 cells: a(n − 2) ways2
Every tiling ends in a square or in a domino, and removing that last piece leaves a shorter tiling.

Count by splitting on the final move: a tiling ending in a square leaves an−1a_{n-1} ways, one ending in a domino leaves an−2a_{n-2}, so an=an−1+an−2a_n = a_{n-1}+a_{n-2}.

In plain terms. Ask "what could the last piece be?" Each choice reduces to a smaller version of the same problem, giving a recurrence.

Example. Ways to tile a 1×n1\times n strip with squares and dominoes: a1=1,a2=2,a3=3,a4=5a_1=1, a_2=2, a_3=3, a_4=5 — the Fibonacci numbers.

Why it works. The cases "last piece is a square" and "last piece is a domino" do not overlap and together cover every tiling, so their counts add. Removing the last piece is a one-to-one correspondence with tilings of the shorter strip, so each case is counted by an earlier term. That is the whole recipe: split by what happens at the end, check the cases are disjoint and exhaustive, and identify each case with a smaller instance.

Tip
Binomial theorem

(x+y)n=∑k=0n(nk)xn−kyk(x+y)^n = \sum_{k=0}^n\dbinom nk x^{n-k}y^k.

In plain terms. Expanding a power of a sum spreads it into terms weighted by "choose" numbers; plugging in clever x,yx,y evaluates tricky sums.

Example. Set x=y=1x=y=1: ∑k(nk)=2n\sum_k\dbinom nk = 2^n. Set x=1,y=−1x=1,y=-1: the alternating sum is 00.

Reminder — The binomial theorem:  (x+y)n=∑k=0n(nk)x n−kyk(x+y)^{n}=\sum_{k=0}^{n}\binom{n}{k}x^{\,n-k}y^{k}
Example
Worked: no two consecutive ones

How many strings of ten 00s and 11s contain no two consecutive 11s?

Spotting it. Condition on the last character. If it is 00, anything valid of length n−1n - 1 can precede it. If it is 11, the character before must be 00, and anything valid of length n−2n - 2 precedes that.

Step 1. an=an−1+an−2a_n = a_{n-1} + a_{n-2}, with a1=2a_1 = 2 (00, 11) and a2=3a_2 = 3 (0000, 0101, 1010).

Step 2. a3,…,a10=5,8,13,21,34,55,89,144a_3, \dots, a_{10} = 5, 8, 13, 21, 34, 55, 89, 144. The answer is 144144.

Example
Worked: no three consecutive ones

How many strings of ten 00s and 11s contain no three consecutive 11s?

Spotting it. Now condition on how the string ENDS: in 00, in 0101, or in 011011. These are disjoint, cover every valid string, and in each case what precedes the ending is an arbitrary valid string.

Step 1. tn=tn−1+tn−2+tn−3t_n = t_{n-1} + t_{n-2} + t_{n-3}, with t0=1t_0 = 1 (the empty string), t1=2t_1 = 2, t2=4t_2 = 4.

Step 2. t3,…,t10=7,13,24,44,81,149,274,504t_3, \dots, t_{10} = 7, 13, 24, 44, 81, 149, 274, 504. The answer is 504504.

Check t3t_3: of the 88 strings of length 33 only 111111 is excluded ✓

Example
Worked: lattice paths around a blocked point
11111123451361015141025155154016112666171844110
Each entry is the one to its left plus the one below; the blocked point contributes 0, and the corner reads 110.

How many paths of unit steps right or up go from (0,0)(0, 0) to (6,4)(6, 4) without passing through (3,2)(3, 2)?

Spotting it. "Condition on the last step" on a grid is Pascal's rule: the paths to a point come from the point to its left or the point below. Two ways to finish: complement, or fill in the grid with (3,2)(3, 2) set to 00.

By complement. All paths: (104)=210\dbinom{10}{4} = 210. Paths through (3,2)(3, 2): (52)\dbinom52 ways to reach it times (52)\dbinom52 ways to finish, 10⋅10=10010 \cdot 10 = 100. So 210−100=110210 - 100 = 110.

By the grid. Write 11 along the bottom row and left column, add left-plus-below everywhere else, and put 00 at the blocked point. The top-right entry is 110110, matching.

Side note
Where it goes wrong

Wrong starting values. In the no-two-consecutive-11s example, starting from a1=1a_1 = 1, a2=2a_2 = 2 (the tiling numbers) produces 8989 instead of 144144. The recurrence was right; the base cases were copied from a different problem. Always count the first two or three cases by hand, and check one more term by brute force before trusting the recursion.

Try it
Try it: tiles of length 1 and 3

In how many ways can a 1×101 \times 10 strip be tiled with 1×11 \times 1 and 1×31 \times 3 tiles?

Show the answer

Condition on the last tile: an=an−1+an−3a_n = a_{n-1} + a_{n-3} with a0=a1=a2=1a_0 = a_1 = a_2 = 1. Then a3,…,a10=2,3,4,6,9,13,19,28a_3, \dots, a_{10} = 2, 3, 4, 6, 9, 13, 19, 28. The answer is 2828.

Expected Value

Tip
Linearity of expectation

The expected value of a sum equals the sum of expected values, even when the parts depend on each other.

In plain terms. To find an average total, add up the average of each little piece — you never need the pieces to be independent.

Example. Expected number of heads in 1010 flips: each flip contributes 12\tfrac12, so the total is 10⋅12=510\cdot\tfrac12 = 5.

Why it works. Write the total as a sum of indicators, X=I1+I2+⋯+ImX = I_1 + I_2 + \cdots + I_m, where IkI_k is 11 if the kkth small event happens and 00 otherwise. Expectation is an average over outcomes, and an average of sums is the sum of the averages, whatever the dependence between the IkI_k. Each E[Ik]E[I_k] is just the probability of its event. So "expected number of ..." becomes "add up the probabilities of each ...", one easy probability at a time.

Example
Worked: adjacent pairs

Five boys and five girls stand in a random order in a row. What is the expected number of adjacent pairs consisting of one boy and one girl?

Spotting it. The pairs overlap and are far from independent, but linearity does not care: sum over the 99 adjacent positions.

Step 1. For a fixed pair of neighbouring positions, the probability they hold a boy and a girl is 5⋅5⋅210⋅9=59\dfrac{5 \cdot 5 \cdot 2}{10 \cdot 9} = \dfrac{5}{9}.

Step 2. The expected count is 9⋅59=59 \cdot \tfrac59 = 5.

Example
Worked: descents in a permutation

The numbers 1,2,…,101, 2, \dots, 10 are arranged in random order. The expected number of positions ii (with 1≤i≤91 \le i \le 9) where the iith number is larger than the (i+1)(i+1)st is mn\tfrac mn in lowest terms. Find m+nm + n.

Spotting it. One indicator per adjacent pair of positions again.

Step 1. For each ii, the two numbers in positions ii and i+1i + 1 are equally likely to be in either order, so the probability of a descent is 12\tfrac12.

Step 2. The expectation is 9⋅12=929 \cdot \tfrac12 = \tfrac92, so m+n=11m + n = 11.

Example
Worked: how many different faces

Three fair dice are rolled. The expected number of different values shown is mn\tfrac mn in lowest terms. Find m+nm + n.

Spotting it. Count distinct values by one indicator per face value: Iv=1I_v = 1 if the value vv appears at least once.

Step 1. P(v appears)=1−(56)3=91216P(v \text{ appears}) = 1 - \left(\tfrac56\right)^3 = \tfrac{91}{216}.

Step 2. Six values: 6⋅91216=91366 \cdot \tfrac{91}{216} = \tfrac{91}{36}, in lowest terms since 91=7⋅1391 = 7 \cdot 13. So m+n=127m + n = 127.

Sanity check: the value lies between 11 and 33 (9136≈2.53\tfrac{91}{36} \approx 2.53) ✓

Side note
Where it goes wrong

Adding PROBABILITIES as if they were expectations. The expected number of sixes in three rolls is 3⋅16=123 \cdot \tfrac16 = \tfrac12, but the probability of AT LEAST ONE six is not 12\tfrac12: it is 1−(56)3=912161 - \left(\tfrac56\right)^3 = \tfrac{91}{216}. Linearity is about expected counts; probabilities of unions need complements or inclusion–exclusion.

Try it
Try it: runs of coin flips

A fair coin is flipped 1010 times. A run is a maximal block of equal consecutive results (HHTHH has three runs). The expected number of runs is mn\tfrac mn in lowest terms. Find m+nm + n.

Show the answer

The number of runs is 11 plus the number of positions ii (1≤i≤91 \le i \le 9) where flip i+1i + 1 differs from flip ii. Each such change has probability 12\tfrac12, so the expectation is 1+9⋅12=1121 + 9 \cdot \tfrac12 = \tfrac{11}{2} and m+n=13m + n = 13.

Probability With States and Symmetry

Tip
Set up states, then solve the linear system

Name pAp_A, pBp_B, … for the probability of the goal from each state; write one equation per state from its transitions; solve.

The expected wait for HH is 66 flips but for HT only 44, even though each pattern has probability 14\tfrac14 at any fixed pair of positions. The difference is what a failure costs: from "just saw H", a T sends the HH-hunter back to the start, while an extra H leaves the HT-hunter exactly where it was, one flip from done. State equations capture that; intuition usually does not. Always include the starting state, and give the absorbing states their known values explicitly.

Tip
Symmetry collapses the work

If the labels can be permuted without changing the process, the corresponding probabilities are equal — solve one and multiply.

Asking for the probability that a specific person ends up in a specific seat, when the process treats everybody alike, gives 1n\tfrac1n with no computation. The classic case is the airplane-seating problem, whose answer is exactly 12\tfrac12 for any n≥2n \ge 2, because the process is symmetric between the first passenger's seat and the last passenger's seat.

Example
Worked: expected flips to see HH

What is the expected number of fair coin flips to first see two heads in a row?

Step 1 — states: SS (no progress), HH (one head so far).

Step 2 — ES=1+12EH+12ESE_S = 1 + \tfrac12E_H + \tfrac12E_S and EH=1+12⋅0+12ESE_H = 1 + \tfrac12\cdot0 + \tfrac12E_S.

Step 3 — the first gives ES=2+EHE_S = 2 + E_H; substituting into the second, EH=1+12(2+EH)E_H = 1 + \tfrac12(2 + E_H), so EH=4E_H = 4 and ES=6E_S = 6. Six flips on average — compare HT, which takes 44.

Example
Worked: a biased walk
012342/31/32/31/32/31/3p = 0p = 1
States 0 to 4 with their moves; 0 and 4 are absorbing, with p=0p = 0 and p=1p = 1.

A token starts at 11 on the number line and moves each second: up 11 with probability 23\tfrac23, down 11 with probability 13\tfrac13. It stops on reaching 00 or 44. The probability that it reaches 44 first is mn\tfrac mn in lowest terms. Find m+nm + n.

Spotting it. The future depends only on the current position, so let pkp_k be the probability of reaching 44 first from kk; then p0=0p_0 = 0 and p4=1p_4 = 1.

Step 1. One-step equations: pk=23pk+1+13pk−1p_k = \tfrac23p_{k+1} + \tfrac13p_{k-1} for k=1,2,3k = 1, 2, 3. Rearranged: pk+1−pk=12(pk−pk−1)p_{k+1} - p_k = \tfrac12(p_k - p_{k-1}). The GAPS between consecutive pp's halve each step.

Step 2. With first gap g=p1−p0=p1g = p_1 - p_0 = p_1, the gaps are g,g2,g4,g8g, \tfrac g2, \tfrac g4, \tfrac g8, and they add to p4−p0=1p_4 - p_0 = 1: 158g=1\tfrac{15}{8}g = 1, so g=815g = \tfrac{8}{15}.

Step 3. p1=815p_1 = \tfrac{8}{15}, so m+n=23m + n = 23.

Side note
Where it goes wrong

Writing a transition that does not happen. In the HH example, "EH=1+12⋅0+12EHE_H = 1 + \tfrac12 \cdot 0 + \tfrac12E_H" pretends a tail leaves you in state HH; it actually sends you back to the start, ESE_S. The wrong equation gives EH=2E_H = 2 and ES=4E_S = 4, which is the answer for HT, not HH. For each state, ask "after this flip, where exactly am I?"

Try it
Try it: a state equation

A fair die is rolled until a 66 appears. What is the expected number of rolls?

Show the answer

E=1+56EE = 1 + \tfrac56E, so E=6E = 6 — the general fact that a probability-pp event takes 1/p1/p trials on average.