Advanced counting: selections with repetition, the binomial theorem and recurrence relations
Permutations and combinations with repetition (stars and bars), words with repeated letters, the binomial theorem and Pascal's identity, the next permutation in lexicographic order, and building recurrence relations from problems. A final exam topic.
Last week we counted selections without repetition. This week we allow repetition, see the binomial theorem, and learn to turn a problem into a recurrence relation. It is a final exam topic. Next week (solving linear recurrences) is the continuation. Golden rules:
- The four-box table: does order matter, is there repetition? Choose one of the four formulas.
- Combination with repetition = stars and bars. stars, bars.
- When finding a binomial coefficient, raise the numbers inside and the sign to the power as well.
- When building a recurrence, look at the last step and always write the initial conditions.
The four-formula table
You are choosing items from types:
| No repetition | Repetition | |
|---|---|---|
| Order matters | ||
| Order does not matter |
Example (order matters, repetition allowed): a word of length 5 from 26 letters, where letters may repeat: .
Combinations with repetition: stars and bars
Example. A bakery has 4 kinds of pastry, and you will buy 6. You can take as many of each kind as you like, order does not matter. How many different purchases?
Write a purchase with 6 stars and 3 bars that separate the kinds:
**|*||*** -> type1: 2, type2: 1, type3: 0, type4: 3
|******|| -> type1: 0, type2: 6, type3: 0, type4: 0
Each purchase corresponds to choosing the positions of the 3 bars in a string of 9 characters (a one-to-one correspondence, a bijection):
Counting solutions of an equation
The nonnegative integer solutions of : 11 stars, 2 bars, .
If each is required: first put 1 in each box, leaving 8. , .
The same model: distributing 8 identical balls into 4 different boxes is ways.
One more example: distribute 10 identical candies to 3 children so that each child gets at least 2. First give each child 2 ( candies gone), and the remaining 4 candies are free: , ways. Every question with lower bounds finishes in these two steps: first hand out the mandatory share, then count the rest with stars and bars.
PITFALL · identical or distinct objects? Stars and bars works only for identical objects (the same balls, the same kind of pastry). If you throw 8 distinct balls into 4 boxes, each ball chooses one of the 4 boxes: .
Words with repeated letters
If among objects are of one kind, of another kind, …, the number of distinct arrangements is
BANANA: 6 letters, A three times, N twice, B once. . Why do we divide? Swapping the three A’s among themselves () gives the same word, so we remove those repeats (the division rule).
The binomial theorem
Intuition: in the product you pick either or from each parenthesis. How many times the term appears = the number of ways to choose the parentheses from which you pick = .
Example: . The coefficients are 1, 4, 6, 4, 1.
Worked example. The coefficient of in the expansion of .
General term: C(7, j) (2x)^(7-j) (-y)^j
For x^3 y^4, j = 4
C(7, 4) * 2^3 * (-1)^4 = 35 * 8 * 1 = 280
Two important results:
Example: .
Pascal’s identity and combinatorial proofs
Combinatorial proof: show that both sides count the same thing.
Proof. Let S be a set with n + 1 elements and fix one element a in S.
The left side counts the k-element subsets of S.
Such a subset either contains a or does not.
- If it contains a, choose the other k - 1 elements from the
remaining n elements: C(n, k-1) ways.
- If it does not contain a, choose all k elements from the
remaining n elements: C(n, k) ways.
By the sum rule the total is C(n, k-1) + C(n, k). ∎
Check: , . In Pascal’s triangle each number is the sum of the two numbers above it.
Generating permutations: lexicographic order
The permutation that comes after in lexicographic order:
- Going from right to left, find the first with .
- Among the elements to the right of , find the smallest one greater than and swap it with .
- Arrange the part to the right of position in increasing order.
362541
From the right: 4 > 1, 5 > 4, 2 < 5 -> a_j = 2
To the right of 2: 5 4 1, the smallest greater than 2 = 4
Swap: 364521
Sort the right of 4 increasing: 1 2 5 -> 364125
Recurrence relations
A recurrence relation expresses the term of a sequence in terms of earlier terms. Without initial conditions the sequence is not determined.
Example 1 (interest). 1000 TL at 5% annual interest: , . After three years, .
Example 2 (Tower of Hanoi). To move disks: move the top disks to the spare peg (), move the largest (1), move the disks on top of it ():
By iteration : the guess is . Its proof by induction (the template of week 3):
Claim. H_n = 2^n - 1 for all integers n >= 1.
Basis step. n = 1: H_1 = 1 and 2^1 - 1 = 1. True.
Inductive step. Assume H_k = 2^k - 1 for some k >= 1 (IH).
Then H_(k+1) = 2 H_k + 1 = 2(2^k - 1) + 1 = 2^(k+1) - 2 + 1 = 2^(k+1) - 1.
So the claim holds for k + 1.
By mathematical induction, H_n = 2^n - 1 for all n >= 1. ∎
Example 3 (bit strings). : the number of bit strings of length that do not contain two consecutive 0s. Look at the last bit:
- Last bit 1: the part before it is any valid string of length , .
- Last bit 0: the bit before it must be 1, and the part before that is a valid string of length , .
The values are : the Fibonacci sequence, shifted.
TIP · the “look at the last step” recipe. When building a recurrence, split into cases by the last element of the sequence (the last bit, the last disk moved, the last digit). In each case the remaining part is the same problem, only shorter. Add up the cases.
Common mistakes
| Wrong idea | The truth |
|---|---|
| Combination with repetition is | |
| bars in stars and bars | For boxes, bars |
| Stars and bars for distinct objects too | Distinct objects into boxes: |
| Ignoring the condition | First give 1 to each box, count for the remaining |
| BANANA | Repeats are divided out: |
| The coefficient of in is | The numbers are raised to the power too: |
| Writing the recurrence and forgetting the initial condition | Without , the sequence is undefined |
| Leaving the right part in reverse order in the next permutation | After the swap, the right part is arranged in increasing order |
Test yourself
Exam-style questions. Solve them on paper first, then reveal the answer.
How many strings of length 4 can be formed from the letters A, B, C if letters may be repeated?
Show answer
Permutation with repetition: 3 choices for each position, .
An ice cream shop has 3 flavors. In how many ways can you choose 5 scoops (order does not matter, flavors may repeat)?
Show answer
Combination with repetition: types, selections. . Stars and bars: 5 stars, 2 bars, choose the places of the 2 bars among 7 places.
How many nonnegative integer solutions does have?
Show answer
You are distributing 10 stars into 4 boxes, so you need 3 bars. .
How many integer solutions does have with , , ?
Show answer
Give out the lower bounds first: , , . The new equation is , . The number of solutions is .
How many different strings can be made by rearranging the letters of LETTER?
Show answer
6 letters: L, E, T, T, E, R. E twice, T twice. .
What is the coefficient of in the expansion of ?
Show answer
Binomial theorem: the coefficient of the term is . , : .
What is the coefficient of in the expansion of ?
Show answer
The general term is . For , : . Sign: is positive.
Evaluate .
Show answer
In the binomial theorem take : . So . Combinatorial meaning: all the subsets of an 8-element set.
Evaluate .
Show answer
In the binomial theorem take , : .
The Tower of Hanoi numbers satisfy with . Find .
Show answer
Iteration: , , , , , , , . With the closed form too, .
Let be the number of strings of length over that contain no two consecutive 0s. Find a recurrence for with initial conditions, and compute .
Show answer
Look at the last character. If the last character is 1 or 2 (2 choices), the part before it is any valid string of length : . If the last character is 0, the one before it cannot be 0, so it must be 1 or 2 (2 choices), and the part before that is a valid string of length : . Initial: , (only 00 is forbidden). Then , , .
Find the permutation that comes right after 25431 in lexicographic order.
Show answer
Going from right to left, find the first "small-large" pair: 2 < 5 (position 1). In the 5431 to the right of 2, the smallest number greater than 2 is 3. Swap 2 and 3: 35421. Arrange the part to the right of 3 in increasing order: 1245. Result 31245.
Verification: All counts were verified with node in two ways, by formula and by exact counting (generating the solutions, words and strings one by one); binomial coefficients by polynomial multiplication, next permutations by the algorithm and by sorting all permutations, and recurrence values by iteration and brute-force counting (verify/MAT2033/10.mjs).