🔢 Combinatorics

Probability on a finite, equally likely space is just counting: $P(A)=\frac{|A|}{|\Omega|}$. This page builds the counting toolkit from two questions, does order matter? and can I reuse items?, then adds three power tools: stars and bars, inclusion–exclusion, and lattice paths.

Permutations & combinations

Permutations: order matters

Arrange $k$ of $n$ distinct items in a row. There are $n$ choices for the first slot, $n-1$ for the second, …, and $n-k+1$ for the $k$-th:

$$P(n,k)=n(n-1)\cdots(n-k+1)=\frac{n!}{(n-k)!}$$

With $k=n$ this is $n!$, the number of orderings of all $n$ items.

Combinations: order doesn't matter

Choose a set of $k$ items from $n$ (a committee, a poker hand):

$$\binom nk=\frac{n!}{k!\,(n-k)!}=\frac{P(n,k)}{k!}$$

Every set of $k$ items can be lined up in $k!$ orders. Counting ordered arrangements counts each set exactly $k!$ times, so divide by $k!$ to collapse the orderings into one. For example, $\{A,B,C\}$ appears as ABC, ACB, BAC, BCA, CAB, CBA, which is $3!=6$ times.

Handy identities (and why they're true)
  • $\binom nk=\binom n{n-k}$: choosing who's in is the same as choosing who's out.
  • $\binom nk=\binom{n-1}{k-1}+\binom{n-1}{k}$ (Pascal): either item $n$ is chosen or it isn't.
  • $\sum_k\binom nk=2^n$: each item is in or out.
  • $\sum_k\binom mk\binom{n}{r-k}=\binom{m+n}{r}$ (Vandermonde): choose $r$ people from $m$ men and $n$ women by splitting on the number of men.

The four ways to pick $k$ from $n$

Almost every counting question is one cell of this table. Decide (1) whether the order of your picks matters and (2) whether an item can be picked more than once.

without replacementwith replacement
order matters$P(n,k)=\dfrac{n!}{(n-k)!}$
podium finishes, passwords with distinct characters
$n^k$
PIN codes, sequences of die rolls
order doesn't matter$\dbinom nk$
committees, poker hands
$\dbinom{n+k-1}{k}$
buying $k$ donuts from $n$ flavors (stars & bars)
Enumerate the four cases
Pick a cell and choose $n$ (letters A, B, C, …) and $k$ (the number of picks). Every outcome is listed and counted. In the unordered cases, hover a chip to see how many ordered sequences collapse into it. That number is not always $k!$ when repeats are allowed: chips with a repeated letter (orange border) come from fewer orderings.

"With replacement, order doesn't matter" is not $n^k/k!$. Dividing by $k!$ assumes every unordered outcome came from exactly $k!$ sequences. But $\{A,A\}$ comes from only one sequence (AA), while $\{A,B\}$ comes from two (AB, BA). For $n=k=2$, $n^k/k!=2$, but the true count is $\binom{3}{2}=3$: $\{A,A\},\{A,B\},\{B,B\}$. Use stars and bars.

A 4-digit PIN uses digits 0–9. How many PINs have all four digits distinct?
  • $\binom{10}{4}=210$
  • $10^4=10{,}000$
  • $10\cdot9\cdot8\cdot7=5{,}040$
  • $\binom{13}{4}=715$
Order matters (1234 ≠ 4321) and there is no replacement (distinct digits): $P(10,4)=5040$. The probability that a uniformly random PIN has distinct digits is $5040/10^4\approx 50\%$.

Arrangements with repeated items (multinomial)

How many ways are there to arrange $n$ objects when there are $n_1$ identical objects of type 1, $n_2$ of type 2, …, $n_m$ of type $m$?

$$\binom{n}{n_1,\dots,n_m}=\frac{n!}{n_1!\,n_2!\cdots n_m!}$$

Pretend all objects are distinct and permute them: $n!$ ways. Swapping identical copies doesn't change the visible arrangement, so each visible arrangement was counted $n_1!\,n_2!\cdots n_m!$ times. Divide those rearrangements out.

Second view: choose positions type by type

Choose positions for type 1, then for type 2 among the rest, and so on:

$$\binom{n}{n_1}\binom{n-n_1}{n_2}\cdots\binom{n_m}{n_m}=\frac{n!}{n_1!(n-n_1)!}\cdot\frac{(n-n_1)!}{n_2!(n-n_1-n_2)!}\cdots=\frac{n!}{n_1!\cdots n_m!}.$$

With two types this is just $\binom n{n_1}$. For example, arranging $k$ H's and $n-k$ T's gives $\binom nk$ sequences, which is where the binomial PMF comes from.

Count the arrangements of a word
Type any word. The count is $n!$ divided by the factorial of each letter's multiplicity, computed exactly. For short words, all distinct arrangements are listed.

Stars and bars

Question: how many ways can you write $n$ as an ordered sum of $k$ non-negative integers, $x_1+x_2+\dots+x_k=n$ with each $x_i\ge0$?

Line up $n$ stars and drop $k-1$ bars among them. The bars cut the stars into $k$ groups, and the group sizes are $x_1,\dots,x_k$. For example, with $n=5$ and $k=3$:

$$\star\star\,|\,\,|\,\star\star\star \;\longleftrightarrow\; (2,0,3) \qquad\qquad |\,\star\star\star\star\,|\,\star \;\longleftrightarrow\; (0,4,1)$$

Every arrangement of $n$ stars and $k-1$ bars gives exactly one solution and vice versa. So count arrangements: there are $n+k-1$ symbols, and we choose which $k-1$ of them are bars:

$$\#\{x_1+\dots+x_k=n,\ x_i\ge0\}=\binom{n+k-1}{k-1}=\binom{n+k-1}{n}$$
Stars ↔ solutions
Click a bar to select it (orange), then click a star to move the bar there. Stars are colored by the group they fall in, and the solution $(x_1,\dots,x_k)$ updates live. Next â–¶ walks through every arrangement in order, so you can see that the list of arrangements and the list of solutions match one-to-one.

Combination with replacement = stars and bars

Choosing $k$ items from $n$ types with replacement, order irrelevant is the same problem. Let $x_i$ be how many of type $i$ you take, so $x_1+\dots+x_n=k$. That gives $k$ stars and $n-1$ bars:

$$\binom{n+k-1}{k}$$

Example: 3 donuts from 4 flavors gives $\binom{6}{3}=20$ boxes. Note the roles swap: in "sum of $k$ integers to $n$", $n$ is the number of stars, while in "choose $k$ from $n$ types", $k$ is.

Positive parts. If every $x_i\ge1$, give each group one star up front and distribute the remaining $n-k$ freely: $\binom{n-1}{k-1}$. (Equivalently, place $k-1$ bars into the $n-1$ gaps between stars, at most one per gap.) More generally, lower bounds $x_i\ge a_i$ are handled by subtracting the $a_i$ first. Upper bounds $x_i\le b_i$ need inclusion–exclusion.

How many non-negative integer solutions does $x_1+x_2+x_3=10$ have?
  • $\binom{10}{3}=120$
  • $\binom{12}{2}=66$
  • $\binom{9}{2}=36$
  • $3^{10}$
There are $n=10$ stars and $k-1=2$ bars, so $\binom{12}{2}=66$. ($\binom92=36$ is the count with every $x_i\ge1$.)

Inclusion–exclusion

To count a union, add the pieces. But elements in two sets got counted twice, so subtract the pairwise overlaps. Now elements in all three sets were added 3 times and subtracted 3 times, so add the triple overlap back. Keep alternating until every element is counted exactly once.

$$P(A\cup B)=P(A)+P(B)-P(A\cap B)$$ $$P(A\cup B\cup C)=P(A)+P(B)+P(C)-P(A\cap B)-P(A\cap C)-P(B\cap C)+P(A\cap B\cap C)$$

The same formula holds for counts:

$$|A\cup B\cup C|=|A|+|B|+|C|-|A\cap B|-|A\cap C|-|B\cap C|+|A\cap B\cap C|$$
Watch the overcount cancel
Each region shows its number of elements and, in the badge, how many times it has been counted so far. Apply the terms one at a time with Next term. Hover a term to see its region. Pink badges are overcounted, gray are undercounted, and green are counted exactly once. After all 7 terms, every region is green and the running total equals $|A\cup B\cup C|$.
Why the general formula counts each element exactly once

General form: $\big|\bigcup_{i=1}^n A_i\big|=\sum_{\emptyset\neq S\subseteq[n]}(-1)^{|S|+1}\big|\bigcap_{i\in S}A_i\big|$.

Take an element that lies in exactly $r\ge1$ of the sets. It appears in $\binom rj$ of the $j$-fold intersections, so its total count is

$$\binom r1-\binom r2+\binom r3-\dots=1-(1-1)^r=1.$$

Elements in none of the sets are never counted. ∎

Worked example. How many integers in $1..100$ are divisible by 2, 3, or 5? $|A_2|=50,\ |A_3|=33,\ |A_5|=20$; $|A_6|=16,\ |A_{10}|=10,\ |A_{15}|=6$; $|A_{30}|=3$. So $50+33+20-16-10-6+3=74$. The trick: an intersection of "divisible by" sets is "divisible by the lcm".

The two common slips are forgetting the final triple term (giving 71 above) and using it for disjoint events, where it is overkill: just add. Also, if the question asks for "none of the properties", use the complement: $|\overline{A\cup B\cup C}|=|\Omega|-|A\cup B\cup C|$. That is how derangements are counted next.

Two dice are rolled. Using inclusion–exclusion, what is $P(\text{at least one six})$?
  • $\tfrac16+\tfrac16-\tfrac1{36}=\tfrac{11}{36}$
  • $\tfrac16+\tfrac16=\tfrac13$
  • $\tfrac1{36}$
  • $1-\tfrac16=\tfrac56$
$P(A)+P(B)-P(A\cap B)=\tfrac{11}{36}$. Check via the complement: $1-(\tfrac56)^2=\tfrac{11}{36}$ ✓.

Derangements

A derangement is a permutation with no fixed points: nothing ends up in its original position. In the classic story, $n$ people check their hats, the hats are handed back at random, and nobody gets their own hat. Write $D_n$ for the number of derangements of $n$ items.

Formula
$D_n=n!\displaystyle\sum_{k=0}^n\frac{(-1)^k}{k!}$
Approximation
$D_n\approx\dfrac{n!}{e}$ (exactly the nearest integer, $n\ge1$)
Recurrence
$D_n=(n-1)(D_{n-1}+D_{n-2})$

So $P(\text{random permutation has no fixed point})=\frac{D_n}{n!}\to\frac1e\approx0.3679$, and it converges very fast. Already at $n=5$ it is $0.3667$. First values: $D_1..D_8 = 0, 1, 2, 9, 44, 265, 1854, 14833$, with $D_0=1$.

Proof of the formula via inclusion–exclusion

Let $A_i$ be the set of permutations fixing $i$. Then $|A_{i_1}\cap\dots\cap A_{i_k}|=(n-k)!$, since the $k$ points are pinned and the rest are free. There are $\binom nk$ such intersections, so

$$|A_1\cup\dots\cup A_n|=\sum_{k=1}^n(-1)^{k+1}\binom nk(n-k)!=\sum_{k=1}^n(-1)^{k+1}\frac{n!}{k!}.$$ $$D_n=n!-|A_1\cup\dots\cup A_n|=n!\sum_{k=0}^n\frac{(-1)^k}{k!}\;\xrightarrow{\;n\to\infty\;}\;\frac{n!}{e},$$

because $\sum_k (-1)^k/k!$ is the Taylor series of $e^{-1}$. The tail after $k=n$ is smaller than $\frac1{(n+1)!}$, so $|D_n-n!/e|<\frac1{n+1}\le\frac12$. That is why rounding works.

Proof of the recurrence $D_n=(n-1)(D_{n-1}+D_{n-2})$

Where does item 1 go? It goes to some position $j\neq1$, and there are $n-1$ choices. Now look at item $j$:

  • Item $j$ goes to position 1 (a swap). Remove 1 and $j$. The other $n-2$ items must form a derangement: $D_{n-2}$.
  • Item $j$ does not go to position 1. Then position 1 acts like a "forbidden spot" for $j$, playing the role of $j$'s own spot. The $n-1$ items other than 1 must avoid their (relabeled) spots: $D_{n-1}$.

Total: $(n-1)(D_{n-1}+D_{n-2})$. Check: $D_4=3(2+1)=9$ ✓.

No fixed points → $1/e$
Dots show the exact probability $D_n/n!$ that a random permutation of $n$ items has no fixed point. The dashed line is $1/e$. Pick an $n$ and simulate shuffles to check the probability empirically (orange).

Why $1/e$? Each item is a fixed point with probability $\frac1n$, and these events are only weakly dependent. If they were independent, $P(\text{none})=(1-\frac1n)^n\to e^{-1}$. In fact the number of fixed points is approximately $\Pois(1)$, with mean exactly 1 for every $n$ (see counting in expectation).

Path counting

Question: how many paths go from $(0,0)$ to $(m,n)$ on a grid if each step moves one unit right (R) or one unit up (U)?

$$\#\text{paths }(0,0)\to(m,n)=\binom{m+n}{m}$$

Through a given point $(a,b)$, multiply the ways to get there by the ways to continue:

$$\#\text{paths through }(a,b)=\binom{a+b}{a}\cdot\binom{(m-a)+(n-b)}{m-a}$$
Lattice paths
Drag the sliders to resize the grid. Click a lattice point to pin $(a,b)$, and the number of paths through it appears. Random path animates a uniformly random path (a random shuffle of the R's and U's). Run many to check that the fraction passing through $(a,b)$ matches the formula. Turn on show counts to see that each point's count is the sum of the counts to its left and below, which is Pascal's triangle tilted 45°.
  • Avoiding a point: total minus through, $\binom{m+n}m-\binom{a+b}a\binom{m-a+n-b}{m-a}$. To avoid several points, use inclusion–exclusion over the forbidden points.
  • Random path through a point: divide by the total. For the center of an $n\times n$ grid ($n$ even), $\binom{n}{n/2}^2/\binom{2n}{n}\approx\frac{2}{\sqrt{\pi n}}$, which shrinks only slowly.
  • Never crossing the diagonal (from $(0,0)$ to $(n,n)$ staying on or below $y=x$) is the Catalan number $\frac1{n+1}\binom{2n}{n}$. This is the reflection trick from random walks.
On a grid from $(0,0)$ to $(4,4)$, a path is chosen uniformly at random. What's the probability it passes through $(2,2)$?
  • $\tfrac14$
  • $\tfrac12$
  • $\tfrac{36}{70}$
  • $\tfrac{6}{70}$
Total $\binom84=70$. Through $(2,2)$: $\binom42\cdot\binom42=36$. So $\tfrac{36}{70}\approx 51\%$. The center is a popular spot.