Foundations to Frontier
Tests
A. Mathematical Foundations › A4 Probability Theory › Basics

Combinatorics: Permutations and Combinations

With equally likely outcomes, probability is counting, and two questions pick the formula: does order matter, and is repetition allowed? Permutations, combinations, multisets (stars and bars), lattice paths, inclusion–exclusion for derangements, and Stirling's formula, which turns counting into entropy. It closes with getting uniform random shuffles and subsets right, a source of subtle bugs in real code.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Counting without listing

How many ways can 3 books stand on a shelf? The first spot has 3 choices, the next 2, the last 1: 3×2×1=63\times2\times1 = 6. How many outfits from 3 shirts and 2 pairs of trousers? 3×2=63\times2 = 6. Multiplying choices is the counting principle, and it lets you count huge sets without writing them out.

Two questions decide the formula. Does order matter? (A race podium: yes. A team: no.) Can things repeat? (PIN digits: yes. Lottery balls: no.) Counting ordered arrangements gives permutations. Counting unordered groups gives combinations.

Counting: does order matter, and can you repeat?
caseformulacount (n = 5, k = 3)examples
ordered, with replacementnᵏ125PIN codes, dice sequences
ordered, no replacementn!/(n−k)!60podium finishes, task schedules
unordered, no replacementC(n, k)10committees, poker hands, minibatches
unordered, with replacementC(n+k−1, k)35scoops of ice cream, multisets
monotone paths = C(a+b, a) = C(10, 6) = 210
Pascal's triangle (row n = 5 highlighted; C(n, k) in bold)
1
11
121
1331
14641
15101051
1615201561
172135352171
18285670562881
193684126126843691
1104512021025221012045101
1115516533046246233016555111
1126622049579292479249522066121
Each path is a choice of which a of the a + b steps go right, so there are C(a+b, a) of them. The random path is drawn uniformly by picking "right" with probability (rights left)/(steps left). Each Pascal entry is the sum of the two above it.
Why engineers care

How many test cases cover every pair of options? How many ways can a planner route a rover on a grid? How likely is a random hash collision? Engineers answer all of these by counting, and the same formulas size the search spaces that AI algorithms must explore.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Ways to arrange 3 books?
  2. 2. 4-digit PIN codes (digits may repeat)?
  3. 3. A team (unordered) vs a podium (ordered): which has more possibilities?

Level 1: Beginner

★★☆☆☆ Basic⏱ 15 minSecondary-school mathematics: arithmetic and elementary algebra.

The four basic counts (choosing k of n)

without repetition with repetition
ordered nk‾=n!(n−k)!n^{\underline k} = \frac{n!}{(n-k)!} nkn^k
unordered (nk)=n!k!(n−k)!\binom nk = \frac{n!}{k!(n-k)!} (n+k−1k)\binom{n+k-1}{k}

(nk)\binom nk divides the n!/(n−k)!n!/(n-k)! ordered selections by the k!k! orders of each group.

Examples:

  • 10×9×8=72010\times9\times8 = 720 podiums from 10 runners;
  • (103)=120\binom{10}3 = 120 3-person teams;
  • (525)=2,598,960\binom{52}5 = 2{,}598{,}960 poker hands.

Probability by counting: P(full house) =13(43)⋅12(42)(525)=37442598960≈0.00144= \frac{13\binom43\cdot12\binom42}{\binom{52}5} = \frac{3744}{2598960}\approx0.00144.

📝 Quick check (Level 1)
0/3 answered
  1. 1. C(10, 3) = ?
  2. 2. C(n, k) vs C(n, n − k)?
  3. 3. Ordered, with repetition, k of n:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.

More counting patterns

  • Binomial theorem: (x+y)n=∑k(nk)xkyn−k(x + y)^n = \sum_k\binom nkx^ky^{n-k}. Setting x=y=1x = y = 1 gives ∑k(nk)=2n\sum_k\binom nk = 2^n, the number of subsets.
  • Pascal’s rule: (nk)=(n−1k−1)+(n−1k)\binom nk = \binom{n-1}{k-1} + \binom{n-1}k. Either item nn is in the group or it isn’t.
  • Stars and bars: distributing kk identical items into nn bins can be done in (k+n−1n−1)\binom{k+n-1}{n-1} ways. Write kk stars and n−1n-1 bars in a row and choose the bars’ positions.
  • Lattice paths: monotone paths across an a×ba\times b grid number (a+ba)\binom{a+b}a, since you choose which steps go right.
  • Multinomial: arrangements of a word with repeated letters number n!n1!⋯nr!\frac{n!}{n_1!\cdots n_r!}.
  • Binomial probabilities: P(exactly kk heads in nn fair tosses) =(nk)/2n= \binom nk/2^n. This leads into the binomial distribution (A4.2-1).
🐍 Exercise 1: the four counts and poker
Implement the four basic counts and check them against brute-force enumeration with itertools. Then compute exact probabilities of a full house, a flush (excluding straight flushes) and two pair.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Ways to put 10 identical items in 4 bins?
  2. 2. Monotone paths on a 6 × 4 grid?
  3. 3. The sum of row n of Pascal's triangle is…

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Proofs by counting two ways

  • Pascal’s rule. Count the kk-subsets of {1,…,n}\{1,\dots,n\} by whether they contain nn: (n−1k−1)\binom{n-1}{k-1} do and (n−1k)\binom{n-1}k don’t. ∎
  • ∑k(nk)=2n\sum_k\binom nk = 2^n. Each element is in or out, giving 2n2^n subsets. Grouping them by size gives the sum. ∎
  • Stars and bars. There’s a bijection between distributions and arrangements of kk stars and n−1n-1 bars. There are (k+n−1n−1)\binom{k+n-1}{n-1} such arrangements. ∎

Derangements (inclusion–exclusion)

How many permutations of nn items leave nothing in its original place? Let AiA_i be the permutations fixing ii. Inclusion–exclusion (A4.1-1) gives !n=n!∑k=0n(−1)kk!!n = n!\sum_{k=0}^n\frac{(-1)^k}{k!}. So !n/n!→e−1≈0.368!n/n!\to e^{-1}\approx0.368. If nn people grab hats at random, the chance nobody gets their own is about 37%, whether nn is 10 or 10 million.

Stirling’s formula and entropy

n!≈2πn (n/e)nn!\approx\sqrt{2\pi n}\,(n/e)^n, with relative error about 1/(12n)1/(12n). Taking logs gives log⁡(npn)≈nH(p)\log\binom n{pn}\approx nH(p), where H(p)=−plog⁡p−(1−p)log⁡(1−p)H(p) = -p\log p - (1-p)\log(1-p) is the entropy. The number of length-nn binary strings with a fraction pp of ones is about enH(p)e^{nH(p)}. That’s the counting core of information theory and of compression limits (A5).

Exercise 3.1: How many 5-card hands contain at least one ace?

Use the complement: (525)−(485)=2598960−1712304=886656\binom{52}5 - \binom{48}5 = 2598960 - 1712304 = 886656.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The fraction of derangements tends to…
  2. 2. log C(n, pn) ≈ …
  3. 3. Pascal's rule is proved by…

Level 4: Expert

★★★★★ Expert⏱ 40 minResearchers: original papers and open problems.

Uniform sampling of combinatorial objects

Shuffling a dataset, picking a minibatch, sampling a random subset of a stream: each needs every outcome equally likely.

  • Fisher–Yates shuffle (Durstenfeld, 1964; Knuth, TAOCP Vol. 2). Swapping position ii with a uniform position in [i,n)[i, n) gives each of the n!n! permutations probability 1/n!1/n!.
  • The tempting bug swaps with a position in [0,n)[0, n). It has nnn^n equally likely runs, which can’t divide evenly among n!n! permutations (27 runs vs 6 permutations for n=3n = 3). Some orders become more likely.
  • Reservoir sampling (Vitter, 1985) draws a uniform kk-subset from a stream of unknown length in one pass. It’s used for replay buffers and telemetry downsampling.
🐍 Exercise 2 (research): are your shuffles uniform?
Implement Fisher–Yates, the naive shuffle and reservoir sampling. Use a chi-square statistic over all 24 permutations of 4 items, and over all 3-subsets of 8, to detect the naive shuffle's bias and confirm the others are uniform.

Counting in AI, robotics and engineering

  • Search-space size. A 7-DOF arm discretized at 100 values per joint has 1007=1014100^7 = 10^{14} configurations. That’s why sampling planners (RRT, PRM) replace enumeration (LaValle, 2006). Task orderings of nn sub-tasks number n!n!, so scheduling and TSP-like problems explode.
  • Combinatorial testing. Covering all pairs of kk options with vv values needs far fewer tests than vkv^k (covering arrays). It’s used in flight-software verification (Kuhn, Kacker and Lei, 2013).
  • Learning theory. Finite hypothesis classes of size ∣H∣|\mathcal H| give sample complexity ∝log⁡∣H∣\propto\log|\mathcal H| via the union bound. The Sauer–Shelah lemma counts labelings, ∑i≤d(ni)\sum_{i\le d}\binom ni, to define VC dimension (B1).
  • Hash collisions and IDs. The birthday bound (A4.1-1) says random bb-bit IDs collide after about 2b/22^{b/2} draws. It sizes UUIDs, checksums and cryptographic hashes.

References

  1. R. Graham, D. Knuth, O. Patashnik (1994). Concrete Mathematics, 2nd ed. Addison-Wesley.
  2. D. Knuth (1997). The Art of Computer Programming, Vol. 2: Seminumerical Algorithms, 3rd ed. Addison-Wesley. (Shuffling.)
  3. R. Durstenfeld (1964). “Algorithm 235: random permutation.” Communications of the ACM, 7(7).
  4. J. Vitter (1985). “Random sampling with a reservoir.” ACM Transactions on Mathematical Software, 11(1).
  5. R. Stanley (2011). Enumerative Combinatorics, Vol. 1, 2nd ed. Cambridge. (The twelvefold way.)
  6. D. R. Kuhn, R. Kacker, Y. Lei (2013). Introduction to Combinatorial Testing. CRC Press.
  7. T. Cover, J. Thomas (2006). Elements of Information Theory, 2nd ed., Ch. 11. Wiley. (Method of types.)
  8. S. LaValle (2006). Planning Algorithms. Cambridge.