Skip to content

Combinatorics

4.1 Counting Principles

Rule of Sum. If task AA can be done in mm ways and task BB in nn ways, and they cannot both be Done, then AA or BB can be done in m+nm + n ways.

Rule of Product. If task AA can be done in mm ways and task BB in nn ways independently, then AA and BB together can be done in mnmn ways.

4.2 Permutations and Combinations

Permutations: P(n,r)=n!/(nr)!P(n, r) = n! / (n-r)! — ordered arrangements of rr items from nn.

Combinations: (nr)=n!r!(nr)!\binom{n}{r} = \frac{n!}{r!(n-r)!} — unordered selections of rr items from nn.

Theorem 4.1 (Binomial Theorem).

(x+y)n=r=0n(nr)xnryr(x + y)^n = \sum_{r=0}^{n} \binom{n}{r} x^{n-r} y^r

Theorem 4.2 (Pascal”s Identity). (nr)=(n1r)+(n1r1)\binom{n}{r} = \binom{n-1}{r} + \binom{n-1}{r-1}

Proof. Every rr-subset of 1,,n\\{1, \ldots, n\\} either contains nn (giving (n1r1)\binom{n-1}{r-1} ways To choose the remaining r1r-1) or does not contain nn (giving (n1r)\binom{n-1}{r} ways to choose all rr From 1,,n1\\{1, \ldots, n-1\\}). \blacksquare

4.3 Inclusion-Exclusion Principle

Theorem 4.3 (Inclusion-Exclusion). For finite sets A1,,AnA_1, \ldots, A_n:

i=1nAi=iAii<jAiAj+i<j<kAiAjAk+(1)n+1A1An\left|\bigcup_{i=1}^{n} A_i\right| = \sum_i |A_i| - \sum_{i \lt j} |A_i \cap A_j| + \sum_{i \lt j \lt k} |A_i \cap A_j \cap A_k| - \cdots + (-1)^{n+1}|A_1 \cap \cdots \cap A_n|

Proof (for two sets). Every element of A1A2A_1 \cup A_2 is in A1A_1 or A2A_2 or both. Counting A1+A2|A_1| + |A_2| counts elements in A1A2A_1 \cap A_2 twice, so we subtract A1A2|A_1 \cap A_2| once: A1A2=A1+A2A1A2|A_1 \cup A_2| = |A_1| + |A_2| - |A_1 \cap A_2|.

For the general case, an element in exactly tt of the sets is counted (t1)(t2)+(t3)=1(11)t=1\binom{t}{1} - \binom{t}{2} + \binom{t}{3} - \cdots = 1 - (1-1)^t = 1 time, which is correct. \blacksquare

Worked Example. How many integers from 1 to 1000 are not divisible by 2, 3, or 5?

Let A2A_2 = multiples of 2, A3A_3 = multiples of 3, A5A_5 = multiples of 5.

A2=500|A_2| = 500, A3=333|A_3| = 333, A5=200|A_5| = 200.

A2A3=166|A_2 \cap A_3| = 166, A2A5=100|A_2 \cap A_5| = 100, A3A5=66|A_3 \cap A_5| = 66.

A2A3A5=33|A_2 \cap A_3 \cap A_5| = 33.

A2A3A5=500+333+20016610066+33=734|A_2 \cup A_3 \cup A_5| = 500 + 333 + 200 - 166 - 100 - 66 + 33 = 734.

Not divisible by 2, 3, or 5: 1000734=2661000 - 734 = 266. \blacksquare

Worked Example. How many integers from 1 to 500 are divisible by 3 or 7 but not by 5?

Solution

Let A3A_3 = multiples of 3, A7A_7 = multiples of 7, A5A_5 = multiples of 5 in 1,,500\\{1, \ldots, 500\\}.

A3=500/3=166|A_3| = \lfloor 500/3 \rfloor = 166 A7=500/7=71|A_7| = \lfloor 500/7 \rfloor = 71 A5=500/5=100|A_5| = \lfloor 500/5 \rfloor = 100.

A3A7=500/21=23|A_3 \cap A_7| = \lfloor 500/21 \rfloor = 23 A3A5=500/15=33|A_3 \cap A_5| = \lfloor 500/15 \rfloor = 33 A7A5=500/35=14|A_7 \cap A_5| = \lfloor 500/35 \rfloor = 14 A3A7A5=500/105=4|A_3 \cap A_7 \cap A_5| = \lfloor 500/105 \rfloor = 4.

Divisible by 3 or 7: A3A7=166+7123=214|A_3 \cup A_7| = 166 + 71 - 23 = 214.

Divisible by 3 or 7 and by 5: (A3A7)A5=A3A5+A7A5A3A7A5=33+144=43|(A_3 \cup A_7) \cap A_5| = |A_3 \cap A_5| + |A_7 \cap A_5| - |A_3 \cap A_7 \cap A_5| = 33 + 14 - 4 = 43.

Divisible by 3 or 7 but not by 5: 21443=171214 - 43 = 171. \blacksquare

4.4 Stars and Bars

Theorem 4.4. The number of ways to distribute nn identical objects into kk distinct bins is (n+k1k1)\binom{n + k - 1}{k - 1}.

Proof. The problem is equivalent to placing k1k - 1 dividers among nn objects, giving (n+k1n)=(n+k1k1)\binom{n + k - 1}{n} = \binom{n + k - 1}{k - 1} arrangements. \blacksquare

Worked Example. How many solutions does x1+x2+x3=15x_1 + x_2 + x_3 = 15 have with xi1x_i \geq 1?

Solution

Substitute yi=xi10y_i = x_i - 1 \geq 0. Then y1+y2+y3=153=12y_1 + y_2 + y_3 = 15 - 3 = 12 with yi0y_i \geq 0. By stars and bars: (12+3131)=(142)=91\binom{12 + 3 - 1}{3 - 1} = \binom{14}{2} = 91. \blacksquare

Worked Example. How many solutions does x1+x2+x3+x4=20x_1 + x_2 + x_3 + x_4 = 20 have with xi0x_i \geq 0?

Solution

Directly by stars and bars: (20+4141)=(233)=1771\binom{20 + 4 - 1}{4 - 1} = \binom{23}{3} = 1771. \blacksquare

4.5 The Pigeonhole Principle

Theorem 4.5 (Pigeonhole Principle). If nn objects are placed into kk boxes and n>kn \gt kThen at Least one box contains at least n/k\lceil n/k \rceil objects.

Proof. If every box contained at most n/k1\lceil n/k \rceil - 1 objects, the total would be at most k(n/k1)<kn/k=nk(\lceil n/k \rceil - 1) \lt k \cdot n/k = nContradicting that there are nn objects. \blacksquare

Worked Example. In a class of 400 students, at least how many were born in the same month?

Solution

There are 12 months (boxes) and 400 students (objects). By the pigeonhole principle, some month Has at least 400/12=33.33=34\lceil 400/12 \rceil = \lceil 33.33\ldots \rceil = 34 students.

Worked Example. Show that among any n+1n + 1 integers from 1,2,,2n\\{1, 2, \ldots, 2n\\}Two of them Differ by exactly nn.

Solution

Partition 1,2,,2n\\{1, 2, \ldots, 2n\\} into nn pigeonholes: 1,n+1\\{1, n+1\\}, 2,n+2\\{2, n+2\\}, \ldots n,2n\\{n, 2n\\}. Each pair sums to n+(n+k)=2n+kn + (n+k) = 2n + k… Let me rephrase.

Partition into 1,n+1\\{1, n+1\\}, 2,n+2\\{2, n+2\\}, \ldots, n,2n\\{n, 2n\\}. These are nn disjoint sets. If we select n+1n + 1 integers from 1,,2n\\{1, \ldots, 2n\\}By the pigeonhole principle two must lie in the Same set i,n+i\\{i, n+i\\}And their difference is (n+i)i=n(n + i) - i = n. \blacksquare

Worked Example. Prove that any sequence of n2+1n^2 + 1 distinct real numbers contains a monotone (increasing or decreasing) subsequence of length n+1n + 1.

Solution

Let a1,a2,,an2+1a_1, a_2, \ldots, a_{n^2+1} be the sequence. For each aia_iLet did_i be the length of the Longest increasing subsequence starting at aia_iAnd eie_i the length of the longest decreasing Subsequence starting at aia_i.

Suppose for contradiction that every monotone subsequence has length at most nn. Then 1din1 \leq d_i \leq n and 1ein1 \leq e_i \leq nSo there are at most n2n^2 distinct ordered pairs (di,ei)(d_i, e_i). Since we have n2+1n^2 + 1 elements, by the pigeonhole principle two indices i<ji \lt j Have (di,ei)=(dj,ej)(d_i, e_i) = (d_j, e_j).

If ai<aja_i \lt a_jThen didj+1d_i \geq d_j + 1 (append aia_i before the increasing subsequence starting At aja_j), contradicting di=djd_i = d_j.

If ai>aja_i \gt a_jThen eiej+1e_i \geq e_j + 1Contradicting ei=eje_i = e_j.

Either way we have a contradiction. \blacksquare

Theorem 4.6 (Generalised Pigeonhole Principle). If nn objects are placed into kk boxes, then at Least one box contains at least n/k\lceil n/k \rceil objects. Equivalently, if each box contains at most mm objects, then the total number of objects is at most kmkm.

Worked Example. A drawer contains red, blue, and yellow socks. How many socks must be drawn (without looking) to guarantee at least 4 socks of the same colour?

Solution

There are 3 colours (boxes). By the generalised pigeonhole principle, drawing nn socks guarantees At least n/3\lceil n/3 \rceil of one colour. We need n/34\lceil n/3 \rceil \geq 4So n/3>3n/3 \gt{} 3Giving n10n \geq 10.

With 9 socks it is possible to have 3 of each colour (no colour reaches 4). With 10 socks, one Colour must have at least 10/3=4\lceil 10/3 \rceil = 4.

Worked Example. Prove that in any group of nn people, there are at least two who have shaken Hands with the same number of people (within the group).

Solution

Each person can shake hands with between 0 and n1n - 1 others, giving nn possible values. But the Values 0 and n1n - 1 cannot both occur (if someone shook no hands, no one shook everyone’s hand, And vice versa). So there are at most n1n - 1 distinct handshake counts among nn people. By the Pigeonhole principle, at least two people have the same count. \blacksquare

4.6 Catalan Numbers

The nn-th Catalan number is

Cn=1n+1(2nn)=(2n)!(n+1)!n!C_n = \frac{1}{n+1}\binom{2n}{n} = \frac{(2n)!}{(n+1)!\,n!}

The first few values: C0=1C_0 = 1, C1=1C_1 = 1, C2=2C_2 = 2, C3=5C_3 = 5, C4=14C_4 = 14, C5=42C_5 = 42.

Catalan numbers count:

  • The number of valid (properly matched) sequences of nn pairs of parentheses.
  • The number of binary search trees with nn nodes.
  • The number of ways to triangulate a convex (n+2)(n+2)-gon.
  • The number of lattice paths from (0,0)(0,0) to (n,n)(n,n) that never go above the diagonal.

Recurrence. C0=1C_0 = 1 and for n1n \geq 1:

Cn=i=0n1CiCn1iC_n = \sum_{i=0}^{n-1} C_i \, C_{n-1-i}

Worked Example. Verify C3=5C_3 = 5 by listing all valid sequences of 3 pairs of parentheses.

Solution

The five valid sequences are: ((()))((())), (()())(()()), (())()(())(), ()(())()(()), ()()()()()().

Checking: C3=14(63)=1420=5C_3 = \frac{1}{4}\binom{6}{3} = \frac{1}{4} \cdot 20 = 5. ✓

4.7 Generating Functions for Combinatorics

The ordinary generating function (OGF) of a sequence an\\{a_n\\} is

G(x)=n=0anxnG(x) = \sum_{n=0}^{\infty} a_n x^n

Common generating functions:

Sequence ana_nGenerating function G(x)G(x)
1111x\dfrac{1}{1-x}
rnr^n11rx\dfrac{1}{1 - rx}
(n+kk)\binom{n+k}{k}1(1x)k+1\dfrac{1}{(1-x)^{k+1}}
nnx(1x)2\dfrac{x}{(1-x)^2}
n2n^2x(1+x)(1x)3\dfrac{x(1+x)}{(1-x)^3}

Key operations. If A(x)A(x) generates an\\{a_n\\} and B(x)B(x) generates bn\\{b_n\\}:

  • A(x)+B(x)A(x) + B(x) generates an+bn\\{a_n + b_n\\} (choice between types).
  • A(x)B(x)A(x) \cdot B(x) generates cn\\{c_n\\} where cn=i=0naibnic_n = \sum_{i=0}^{n} a_i b_{n-i} (combining two choices).

Worked Example. Find the number of ways to select nn coins from unlimited supplies of 1p, 2p, And 5p coins.

Solution

The generating function is

G(x) = \underbrace{(1 + x + x^2 + \cdots)}_{\mathrm{1p\; coins{}} \cdot \underbrace{(1 + x^2 + x^4 + \cdots)}_{\mathrm{2p\; coins{}} \cdot \underbrace{(1 + x^5 + x^{10} + \cdots)}_{\mathrm{5p\; coins{}}

=11x11x211x5= \frac{1}{1-x} \cdot \frac{1}{1-x^2} \cdot \frac{1}{1-x^5}

The coefficient of xnx^n in the expansion gives the number of ways. For example, expanding the First few terms: 1+x+2x2+2x3+3x4+4x5+1 + x + 2x^2 + 2x^3 + 3x^4 + 4x^5 + \cdotsSo there are 4 ways to make 5p (5×1p; 3×1p + 1×2p; 1×1p + 2×2p; 1×5p).