DM 07 Counting
Previous: DM 06 Induction and Recursion | DM 00 Course Map | Next: DM 08 Number Theory and Modular Arithmetic
Counting comes down to two rules (multiply for “and then”, add for “or”) and two questions about any selection: does order matter, and is repetition allowed?
The two basic rules
Product rule. If a task is a sequence of steps, with ways to do the first, ways to do the second whatever was chosen first, and so on, there are ways in total.
Example. 3 shirts and 4 trousers: outfits. A 4-digit PIN: . Bit strings of length 8: .
Sum rule. If a task can be done in one of several ways that cannot overlap, add.
Example. Choose one dessert from 5 cakes or 3 ice creams: .
If the options overlap, subtract the overlap (inclusion-exclusion, DM 04 Sets).
Complement. Sometimes counting what you do not want is easier: wanted total unwanted. “At least one” almost always means: total minus “none”.
Example. 8-bit strings with at least one 1: .
Permutations: order matters
The number of ways to arrange distinct objects in a row:
choices for the first position, for the second, and so on. by convention (there is one way to arrange nothing).
The number of ways to pick and arrange of distinct objects:
Example. Gold, silver and bronze among 8 runners: .
Combinations: order does not matter
The number of ways to choose a set of objects from :
Where the formula comes from. Count ordered selections: . Each unordered set of objects was counted once for each of its orderings. Divide:
“Count with order, then divide by the overcounting” is one of the most useful ideas in combinatorics.
Example. A committee of 3 from 8 people: . Compare with 336 for the medals: each committee corresponds to podium orders.
The four cases
Choosing items from types:
| Order matters | Order does not matter | |
|---|---|---|
| No repetition | ||
| Repetition allowed |
The bottom right is stars and bars. To choose items from types with repetition, write stars (the items) and bars (dividers between types). Each arrangement of the symbols is one selection, and an arrangement is fixed by choosing which positions hold stars.
Example. 3 scoops from 4 flavours, repeats allowed: . The picture **|*|| means two of flavour 1, one of flavour 2, none of 3 or 4.
Before using any formula, ask the two questions. Most counting errors come from answering them wrongly.
Arrangements with identical objects
The number of distinct arrangements of objects where are identical of one kind, of another, and so on:
Example. Arrangements of the letters of MISSISSIPPI (11 letters: 1 M, 4 I, 4 S, 2 P):
Same idea again: pretend all are distinct (), then divide by the rearrangements that make no visible difference.
Properties of binomial coefficients
Symmetry.
Choosing the to include is the same as choosing the to leave out.
Pascal’s rule.
Counting proof. Single out one object. A set of either contains it (choose the other from the remaining ) or does not (choose all from the remaining ). The two cases do not overlap, so add.
This rule builds Pascal’s triangle, each entry the sum of the two above:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Row sum.
Both sides count all subsets of an -element set: the left by size, the right by a yes/no decision per element.
Arguments like these, showing two expressions are equal because they count the same thing, are called combinatorial proofs. They explain why an identity is true, not merely that it is.
The binomial theorem
Why. Expanding means picking or from each of the brackets. A term arises once for each way to choose which brackets supply the : ways.
Example. , with coefficients from row 4 of the triangle.
Setting recovers the row sum. Setting , shows the binomial probabilities sum to 1 (PS 03 Discrete Random Variables).
Pigeonhole principle
If objects are placed in boxes and , some box holds at least two. More generally, some box holds at least .
Examples. Among 13 people, two share a birth month. Among 367 people, two share a birthday. In any group of 6 socks drawn from 5 colours, two match.
A less obvious one. Among any 5 points in a unit square, two are at most apart. Divide the square into four half-size squares; two points share one, and the diagonal of a half-size square is .
The principle proves that something exists without finding it. It is the reason lossless compression cannot shrink every file, and hash functions must have collisions.
Worked example: poker hands
A hand is 5 cards from 52, order irrelevant: .
Full house (three of one rank, two of another): choose the rank of the triple (13), its three suits (), the rank of the pair (12), its two suits ():
Flush (all one suit, including straight flushes): .
Break the object into a sequence of independent choices, then multiply.
Common mistakes
- Using permutations when order is irrelevant, or the reverse.
- Double counting. Choosing “one ace, then four other cards” counts hands with two aces twice.
- Adding when you should multiply. “And then” multiplies; “or” adds.
- Forgetting that the options in a sum must be disjoint.
Learn by doing
- How many 5-letter strings over 26 letters have no repeated letter?
- A team of 4 is chosen from 6 engineers and 5 designers. How many teams have exactly 2 of each?
- How many 8-bit strings contain exactly three 1s? At least one 0?
- How many ways can 10 identical sweets be shared among 3 children?
- How many integers must you pick to guarantee two with the same remainder mod 7?
- .
- .
- . And (all except the all-ones string).
- Stars and bars: .
- 8, by pigeonhole (7 possible remainders).
Where this shows up
Probability with equally likely outcomes is counting (PS 01 Sample Spaces Events and Axioms), and the binomial distribution is built from . Counting the states or action sequences of a problem tells you immediately whether exhaustive search is feasible: actions over a horizon of steps give sequences, which is why planning needs something cleverer than enumeration (11 World Models and Planning).