Section 1 · 8 min read

Combinatorics

Counting principles, permutations and combinations, stars and bars, inclusion-exclusion, derangements, pigeonhole, recurrence relations and generating functions.

On this page
  1. Basic principles
  2. Permutations and combinations
  3. Inclusion-exclusion and derangements
  4. Pigeonhole principle
  5. Recurrence relations
  6. Generating functions
  7. Worked examples
  8. Common traps
  9. Quick revision

Counting questions appear in nearly every GATE CS paper, often as numerical answer questions where there are no options to check against. The safest habit is to identify the model first (ordered or unordered, with or without repetition, constrained or not), then apply the matching formula, then check with a tiny case.

Basic principles

  • Sum rule: if a task is done in one of several disjoint ways, add the counts.
  • Product rule: if a task is a sequence of independent choices, multiply.
  • Complement: count what you do not want and subtract from the total. "At least one" problems are almost always easier this way.
  • Division: if every desired object is counted exactly kk times, divide by kk.

Permutations and combinations

Situation Count
Arrange nn distinct objects n!n!
Ordered selection of rr from nn, no repetition P(n,r)=n!(n−r)!P(n, r) = \dfrac{n!}{(n-r)!}
Unordered selection of rr from nn, no repetition (nr)=n!r!(n−r)!\binom{n}{r} = \dfrac{n!}{r!(n-r)!}
Ordered selection of rr from nn, repetition allowed nrn^r
Unordered selection of rr from nn types, repetition allowed (n+r−1r)\binom{n + r - 1}{r}
Arrangements of a multiset with counts n1,…,nkn_1, \dots, n_k n!n1! n2!⋯nk!\dfrac{n!}{n_1!\,n_2! \cdots n_k!}
Circular arrangements of nn distinct objects (n−1)!(n-1)!
Necklaces where flipping is allowed, n≥3n \ge 3 (n−1)!/2(n-1)!/2

Identities that save time: (nr)=(nn−r)\binom{n}{r} = \binom{n}{n-r}, Pascal's rule (nr)=(n−1r−1)+(n−1r)\binom{n}{r} = \binom{n-1}{r-1} + \binom{n-1}{r}, ∑r(nr)=2n\sum_r \binom{n}{r} = 2^n, and Vandermonde ∑k(mk)(nr−k)=(m+nr)\sum_k \binom{m}{k}\binom{n}{r-k} = \binom{m+n}{r}.

Stars and bars

The number of solutions of x1+x2+⋯+xk=nx_1 + x_2 + \dots + x_k = n in

  • non-negative integers is (n+k−1k−1)\binom{n + k - 1}{k - 1};
  • positive integers is (n−1k−1)\binom{n - 1}{k - 1}.

Lower bounds xi≥aix_i \ge a_i: substitute yi=xi−aiy_i = x_i - a_i and reduce nn by ∑ai\sum a_i. Upper bounds xi≤bix_i \le b_i: use inclusion-exclusion on the events xi≥bi+1x_i \ge b_i + 1.

The same formula counts multisets of size nn from kk types, ways to put nn identical balls in kk distinct boxes, and monomials of degree nn in kk variables.

Balls into boxes

Balls Boxes Any number per box Every box nonempty
distinct distinct knk^n k! S(n,k)k!\,S(n, k)
identical distinct (n+k−1k−1)\binom{n+k-1}{k-1} (n−1k−1)\binom{n-1}{k-1}
distinct identical ∑j≤kS(n,j)\sum_{j \le k} S(n, j) S(n,k)S(n, k)
identical identical partitions of nn into at most kk parts partitions of nn into exactly kk parts

Inclusion-exclusion and derangements

∣A1∪⋯∪Ak∣=∑∣Ai∣−∑∣Ai∩Aj∣+∑∣Ai∩Aj∩Al∣−⋯|A_1 \cup \dots \cup A_k| = \sum |A_i| - \sum |A_i \cap A_j| + \sum |A_i \cap A_j \cap A_l| - \cdots

Typical uses: integers up to NN divisible by none of a set of primes, onto functions, bounded stars and bars, and derangements.

A derangement is a permutation with no fixed point: Dn=n!∑i=0n(−1)ii!,Dn=(n−1)(Dn−1+Dn−2).D_n = n!\sum_{i=0}^{n} \frac{(-1)^i}{i!}, \qquad D_n = (n-1)(D_{n-1} + D_{n-2}). Values: D1=0D_1 = 0, D2=1D_2 = 1, D3=2D_3 = 2, D4=9D_4 = 9, D5=44D_5 = 44, D6=265D_6 = 265. Also Dn≈n!/eD_n \approx n!/e, and permutations with exactly rr fixed points number (nr)Dn−r\binom{n}{r} D_{n-r}.

Pigeonhole principle

If NN objects go into kk boxes, some box has at least ⌈N/k⌉\lceil N / k \rceil objects. To force some box to hold at least rr objects you need N=k(r−1)+1N = k(r - 1) + 1.

Standard applications: among any n+1n + 1 integers two have the same remainder mod nn; among any 5 points in a unit square two are within 2/2\sqrt 2 / 2; in any group of people two have the same number of friends inside the group.

Recurrence relations

Setting up a recurrence

Classify objects of size nn by their first or last step. Examples:

  • Binary strings with no two consecutive 1s: end in 0 or end in 01, so an=an−1+an−2a_n = a_{n-1} + a_{n-2} with a1=2a_1 = 2, a2=3a_2 = 3.
  • Tower of Hanoi moves: hn=2hn−1+1h_n = 2h_{n-1} + 1, so hn=2n−1h_n = 2^n - 1.
  • Regions made by nn lines in general position: rn=rn−1+nr_n = r_{n-1} + n, so rn=1+n(n+1)2r_n = 1 + \frac{n(n+1)}{2}.
  • Binary strings of length nn with an even number of 0s: 2n−12^{n-1}.

Linear homogeneous recurrences with constant coefficients

For an=c1an−1+⋯+ckan−ka_n = c_1 a_{n-1} + \dots + c_k a_{n-k}, solve the characteristic equation tk−c1tk−1−⋯−ck=0t^k - c_1 t^{k-1} - \dots - c_k = 0.

  • Distinct roots r1,…,rkr_1, \dots, r_k: an=∑Airina_n = \sum A_i r_i^n.
  • A root rr with multiplicity mm contributes (A0+A1n+⋯+Am−1nm−1)rn(A_0 + A_1 n + \dots + A_{m-1} n^{m-1}) r^n.

Fit the constants with the initial values.

Non-homogeneous recurrences

For an=c1an−1+⋯+f(n)a_n = c_1 a_{n-1} + \dots + f(n), the solution is the homogeneous solution plus one particular solution. Guess the particular solution in the shape of f(n)f(n):

f(n)f(n) Trial particular solution
constant cc AA
polynomial of degree dd polynomial of degree dd
sns^n, ss not a root AsnA s^n
sns^n, ss a root of multiplicity mm AnmsnA n^m s^n

If 1 is a root of the characteristic equation, multiply the polynomial guess by nn.

Generating functions

The ordinary generating function of a0,a1,…a_0, a_1, \dots is A(x)=∑n≥0anxnA(x) = \sum_{n \ge 0} a_n x^n.

Sequence Generating function
1,1,1,…1, 1, 1, \dots 11−x\dfrac{1}{1-x}
1,c,c2,…1, c, c^2, \dots 11−cx\dfrac{1}{1-cx}
(n+k−1k−1)\binom{n + k - 1}{k - 1} 1(1−x)k\dfrac{1}{(1-x)^k}
n+1n + 1 1(1−x)2\dfrac{1}{(1-x)^2}
nn x(1−x)2\dfrac{x}{(1-x)^2}
(mn)\binom{m}{n} (1+x)m(1+x)^m
1,0,1,0,…1, 0, 1, 0, \dots 11−x2\dfrac{1}{1-x^2}
11 for n≤mn \le m, else 00 1−xm+11−x\dfrac{1 - x^{m+1}}{1 - x}

Operations:

  • Multiplying by xx shifts the sequence right by one.
  • Multiplying A(x)A(x) by 11−x\frac{1}{1-x} gives partial sums ∑i≤nai\sum_{i \le n} a_i.
  • Product A(x)B(x)A(x)B(x) gives the convolution ∑iaibn−i\sum_i a_i b_{n-i}. This is why "choose some items of type 1 and some of type 2" problems become products of polynomials.

To solve a recurrence with generating functions, multiply the recurrence by xnx^n, sum over nn, solve for A(x)A(x), then split into partial fractions and read off coefficients.

Worked examples

Worked example 1: Bounded stars and bars

How many solutions does x1+x2+x3+x4=12x_1 + x_2 + x_3 + x_4 = 12 have with 0≤xi≤40 \le x_i \le 4?

  1. Unrestricted: (153)=455\binom{15}{3} = 455.
  2. One variable at least 5: replace it by y=x−5y = x - 5, sum 7: (103)=120\binom{10}{3} = 120, times 4: 480480.
  3. Two variables at least 5: sum 2: (53)=10\binom{5}{3} = 10, times (42)=6\binom{4}{2} = 6: 6060.
  4. Three variables at least 5: sum would be negative: 0.

Answer: 455−480+60=35455 - 480 + 60 = 35.

Worked example 2: Repeated root

Solve an=6an−1−9an−2a_n = 6a_{n-1} - 9a_{n-2}, a0=2a_0 = 2, a1=9a_1 = 9.

  1. t2−6t+9=(t−3)2t^2 - 6t + 9 = (t - 3)^2, double root 3. So an=(A+Bn)3na_n = (A + Bn)3^n.
  2. a0=A=2a_0 = A = 2. a1=3(A+B)=9a_1 = 3(A + B) = 9, so B=1B = 1.
  3. an=(n+2)3na_n = (n + 2)3^n. Check a2=6⋅9−9⋅2=36a_2 = 6 \cdot 9 - 9 \cdot 2 = 36 and (4)(9)=36(4)(9) = 36.

Worked example 3: Non-homogeneous recurrence

Solve an=2an−1+3a_n = 2a_{n-1} + 3, a0=1a_0 = 1.

  1. Homogeneous part: C⋅2nC \cdot 2^n.
  2. Particular: try constant AA: A=2A+3A = 2A + 3, A=−3A = -3.
  3. an=C2n−3a_n = C 2^n - 3, and a0=1a_0 = 1 gives C=4C = 4. So an=2n+2−3a_n = 2^{n+2} - 3. Check: a1=5=2⋅1+3a_1 = 5 = 2 \cdot 1 + 3.

Worked example 4: Coefficient extraction

In how many ways can you pick 10 fruits from apples, bananas and oranges if you take an even number of apples and at most 2 bananas?

Generating function: 11−x2⋅(1+x+x2)⋅11−x.\frac{1}{1-x^2} \cdot (1 + x + x^2) \cdot \frac{1}{1-x}. Count directly instead: choose apples a∈{0,2,4,6,8,10}a \in \{0, 2, 4, 6, 8, 10\} and bananas b∈{0,1,2}b \in \{0, 1, 2\} with a+b≤10a + b \le 10; oranges take the rest. For a≤8a \le 8 all 3 values of bb work, giving 5×3=155 \times 3 = 15. For a=10a = 10 only b=0b = 0 works, giving 1. Total 16.

Worked example 5: Divisibility by inclusion-exclusion

How many integers in 1..10001..1000 are divisible by none of 2, 3, 5?

  1. Divisible by 2, 3, 5: 500,333,200500, 333, 200. Sum 10331033.
  2. By 6, 10, 15: 166,100,66166, 100, 66. Sum 332332.
  3. By 30: 3333.
  4. Divisible by at least one: 1033−332+33=7341033 - 332 + 33 = 734.

Answer: 1000−734=2661000 - 734 = 266.

Common traps

  • Using (n+k−1k−1)\binom{n + k - 1}{k - 1} when the variables must be positive. Positive solutions use (n−1k−1)\binom{n - 1}{k - 1}.
  • Dividing by symmetry when the objects are labelled (distinct people seated in labelled chairs are not circular).
  • Missing higher order terms in inclusion-exclusion.
  • Checking a recurrence's closed form only at the initial values. Several formulas agree on a0a_0 and a1a_1; also check a2a_2.
  • Forgetting the nn factor for repeated roots.
  • Off by one in pigeonhole: to force rr in a box with kk boxes, you need k(r−1)+1k(r-1) + 1, not krkr.
  • Confusing x(1−x)2\frac{x}{(1-x)^2} (sequence nn) with 1(1−x)2\frac{1}{(1-x)^2} (sequence n+1n+1).

Quick revision

  • Non-negative solutions of x1+⋯+xk=nx_1 + \dots + x_k = n: (n+k−1k−1)\binom{n+k-1}{k-1}. Positive: (n−1k−1)\binom{n-1}{k-1}.
  • Multiset arrangements: n!/(n1!⋯nk!)n! / (n_1! \cdots n_k!). Circular: (n−1)!(n-1)!.
  • Onto functions n→kn \to k: ∑j(−1)j(kj)(k−j)n\sum_j (-1)^j \binom{k}{j} (k-j)^n.
  • DnD_n: 0,1,2,9,44,2650, 1, 2, 9, 44, 265. Exactly rr fixed points: (nr)Dn−r\binom{n}{r}D_{n-r}.
  • Pigeonhole: k(r−1)+1k(r-1) + 1 objects force rr in one box.
  • Distinct roots: ∑Airin\sum A_i r_i^n. Repeated root rr of multiplicity mm: polynomial of degree m−1m-1 times rnr^n.
  • Strings with no two adjacent 1s: Fibonacci, length nn gives Fn+2F_{n+2} (with F1=F2=1F_1 = F_2 = 1).
  • 1(1−x)k\frac{1}{(1-x)^k} has coefficients (n+k−1k−1)\binom{n+k-1}{k-1}.
  • Product of generating functions is convolution of sequences.

Further reading

Test this section