Combinatorics
On this page
Laplace's rule from "Probability Foundations" is easy to state, but counting the favorable and possible outcomes can take a lot of work once the sample space gets large. Combinatorics is the toolkit for counting without listing everything. A handful of patterns covers almost every problem, and the skill is recognising which pattern you are looking at.
The Multiplication Principle
Every counting technique in this note is built on one principle. Suppose a store sells 4 computers and 3 printers . How many ways can you buy one computer and one printer? Each computer can be paired with each printer, so there are pairs.
General Statement
If you build an ordered sequence of choices, where the first comes from a set with options, the second from a set with options, and so on, the number of possible sequences is
The choices do not have to come from the same set, and the number of options at a step may depend on earlier steps, as long as the count at each step is fixed. Rolling two dice gives outcomes. Drawing two cards in order gives outcomes with replacement and without.
Nested loops are the programming version: two nested for loops over 4 and 3 items run the body times.
Factorials
The counting formulas use factorials everywhere. For a natural number :
" factorial" counts the ways to put different objects in a row: choices for the first spot, for the second, and so on. The definition says there is exactly one way to arrange nothing, and it makes the formulas below work at the edges.
| Expression | Value |
|---|---|
Factorials grow absurdly fast: already overflows a signed 64-bit integer. That is the real reason brute-forcing every permutation is hopeless beyond about a dozen items.
The Six Counting Patterns
Every pattern answers the same question, "in how many ways can I choose items from ?", and differs only in two yes/no questions: can an item be chosen more than once? and does the order of the choices matter? The book's running example is a rail yard in Leuven where trains are built from coloured cars.
Variations Without Repetition
Question: choose items from different items, no repetition, order matters.
Example: 5 different cars (red, green, blue, black, white). How many trains of 2 cars can you form, coupling them in the order chosen? The train RG differs from GR.
Reasoning: choices for the first item, for the second, ..., for the -th. Multiply, then complete it to factorials by multiplying and dividing by :
For the trains: . A drama club choosing a president, vice president and secretary from 6 members: , because the roles are different, so order matters.
Permutations Without Repetition
Question: arrange all different items in a row.
Example: 3 cars (red, green, blue), how many orders? RGB, RBG, GRB, GBR, BRG, BGR.
Reasoning: this is a variation with , and :
So trains. Seating 10 people on 10 numbered chairs gives arrangements. Around a round table where only the relative positions count, fix one person's seat to remove the rotations, leaving .
Variations With Repetition
Question: choose items from types, repetition allowed, order matters.
Example: red and green cars in unlimited supply. How many trains of 3 cars? RRR, RRG, RGR, RGG, GRR, GRG, GGR, GGG.
Reasoning: every one of the positions independently has all options:
So trains. A slot machine with 5 reels of 6 symbols each has outcomes. A 4-wheel bike lock with digits 1 to 9 has combinations (and is a "combination lock" that actually needs a variation formula).
Permutations With Repetition
Question: arrange items in a row, where are identical of type 1, of type 2, ..., of type , and .
Example: 2 identical red cars, 1 blue, 1 green. How many genuinely different trains?
Reasoning: pretend all items are distinct, giving orders. But swapping the identical items among themselves gives the same train, and there are such swaps. So every real arrangement was counted times: divide by . Do the same for every type:
So trains. The anagrams of BANANA (3 a's, 2 n's, 1 b): . A first aid weekend with 3 hours of first aid, 1 of anatomy, 2 of transport and 2 of bandaging can be scheduled in ways.
Combinations Without Repetition
Question: choose items from different items, no repetition, order does not matter. The result is a group, not a sequence.
Example: 5 cars, money for maintenance on only 3. How many groups of 3? is the same group as .
Reasoning: write a under every chosen item and a under the others. Each selection becomes a string of ones and zeros, and every such string is exactly one selection. So count the arrangements of ones and zeros, which is a permutation with repetition:
The symbol is read " choose " and is called a binomial coefficient. For the cars: . A mother buying 8 different comics out of a series of 20: .
Another way to see it: there are ordered selections, and each unordered group appears in orders, so divide by . Combinations are variations with the order "forgotten".
Combinations With Repetition
Question: choose items from types, repetition allowed, order does not matter.
Example: yellow, red and black cars must go through the wash in groups of 4. How many different groups? is allowed, and a colour may be missing entirely.
Reasoning (stars and bars): represent a selection as crosses (the items) and separators (the walls between the types). means 4 of type 1, 1 of type 2 and 2 of type 3. Every arrangement of crosses and separators is one selection, and every selection is one arrangement. Count arrangements of symbols with identical crosses and identical separators:
For the car wash: . Saint Nicholas sharing 9 identical speculaas among 4 children: .
Summary Of Formulas
| Pattern | Repetition | Order matters | Formula |
|---|---|---|---|
| Variation | No | Yes | |
| Permutation | No | Yes, all items | |
| Variation with repetition | Yes | Yes | |
| Permutation with repetition | Fixed counts per type | Yes | |
| Combination | No | No | |
| Combination with repetition | Yes | No |
The distinction between the two "repetition, order matters" rows: in a variation with repetition you decide freely at every position how many of each type you use. In a permutation with repetition the number of each type is fixed in advance and you only decide where they go.
Python Checks
Python's standard library has these built in, and itertools can brute force small cases to confirm a formula. Brute forcing a small version of the problem is the best way to check a formula you are unsure about.
from math import comb, perm, factorial
from itertools import permutations, combinations, product, combinations_with_replacement
print(perm(5, 2), len(list(permutations("RGBKW", 2)))) # 20 20
print(comb(5, 3), len(list(combinations("RGBKW", 3)))) # 10 10
print(2**3, len(list(product("RG", repeat=3)))) # 8 8
print(comb(6, 4), len(list(combinations_with_replacement("YRB", 4)))) # 15 15
print(factorial(6) // (factorial(3) * factorial(2)), len(set(permutations("BANANA")))) # 60 60Worked Exercises
The trick in every exercise is to answer the two questions (repetition? order?) before reaching for a formula, and to split complicated problems into steps joined by the multiplication principle.
Warm Up
Socks and shoes: 3 pairs of socks and 2 pairs of shoes give outfits.
A flag of 3 horizontal stripes from 4 colours, no two adjacent stripes the same: . The bottom stripe only has to differ from the middle one, so it has 3 options again.
Covering 3 books with 5 types of paper, each book a different type: .
8-digit numbers using four 1s, three 2s and one 3: .
Groups And Committees
A folk dance group of 24 people (8 boys, 16 girls). A committee of 6: . A representative committee of 2 boys and 4 girls: . Two independent choices, joined by the multiplication principle.
Dividing a class of 24 into three labelled groups of 8 (each group gets a different exam): .
Twelve scouts split into two unlabelled teams of 6: . Dividing by removes the double count, because choosing team automatically fixes team , and is the same split as . Into four unlabelled groups of 3: .
Seating
Five couples around a round table, each couple sitting together. Glue each couple into one block: circular orders of blocks, and each block can sit in 2 orders: .
The Leading Zero Trap
How many five-digit numbers with distinct digits contain exactly 2 even and 3 odd digits, with no leading ? Split on whether is used.
- Without : choose 2 evens from and 3 odds from 5, then arrange: .
- With : choose 1 more even and 3 odds, arrange, then subtract arrangements starting with : .
Total: .
Poker Hands
A 5-card hand from 52 cards: . Count special hands by choosing ranks first, then suits:
| Hand | Count | Reasoning |
|---|---|---|
| Four of a kind | rank of the four, then any of the 48 other cards | |
| Full house | rank and suits of the triple, then of the pair | |
| Two pair | two pair ranks (unordered), their suits, then the fifth card |
Computer Science Applications
8-character passwords from 26 uppercase letters and 10 digits: . With at least one digit, use the complement: .
Binary strings of length 16: . With exactly 5 ones: . With at least one 1: .
IPv4 addresses (four numbers from 0 to 255): . Those starting with 192: , which is of all addresses.
Execution orders of 8 tasks: . If task A must run before task B: by symmetry, exactly half of all orders have A first, so .
An 8-character hexadecimal hash has values, so a random hash equals 1A2B3C4D with probability .
Assigning the four different roles web, database, mail and backup to 4 of 12 servers (order matters, roles differ): . Just choosing 4 servers for maintenance (no roles): .
Choosing 30 of 40 examples for a training set: . Once the training set is fixed, the test set is determined, so there is only way left. In general : choosing what goes in is the same as choosing what stays out.
Quick Recap
- Multiplication principle: independent steps multiply, .
- Factorial counts orders of distinct items; .
- Ask two questions: repetition allowed? and order matters?
- No repetition: variation , permutation , combination .
- With repetition: variation , permutation with fixed counts , combination (stars and bars).
- Combination = variation with the orders divided out.
- Use the complement for "at least one", split into cases for awkward constraints (leading zero), and divide out double counts for unlabelled groups.