Skip to content
GATEiit

GATE CSE · Engineering Mathematics

Combinatorics: counting, recurrence relations and generating functions

Permutations, combinations, stars and bars, pigeonhole, inclusion-exclusion, derangements and Catalan numbers, solving linear recurrences by characteristic roots, and using ordinary generating functions to count and to solve recurrences.

Before you start

  • Sets and inclusion-exclusion →

    Most restricted counts are a union of bad cases removed by inclusion-exclusion.

  • Geometric series and the binomial theorem

    Generating functions are power series built from 11−x and (1+x)n.

This topic comes before: Asymptotic worst-case time and space complexity, Conditional probability and Bayes' theorem, Graphs: connectivity, matching and colouring, Random variables, Recursion, Sets, relations, functions, partial orders and lattices, Uniform, normal, exponential, Poisson and binomial distributions

Concept notes

1. Permutations, combinations and the basic rules

  • Sum rule: disjoint cases add. Product rule: independent stages multiply.
  • Bijection: if two sets correspond one-to-one, count whichever is easier.
Selecting r from n distinct objects Order matters Order does not matter
No repetition P(n,r)=n!(n−r)! (nr)=n!r! (n−r)!
Repetition allowed nr (n+r−1r)

More arrangements:

  • Multiset permutations: arrangements of n objects with n1 alike of one kind, n2 of another, and so on: n!n1! n2!⋯nk!. Example: MISSISSIPPI gives 11!1! 4! 4! 2!=34650.
  • Circular arrangements of n distinct objects: (n−1)!; if a mirror image counts as the same (a necklace), (n−1)!/2.
  • Lattice paths from (0,0) to (m,n) using unit right and up steps: (m+nm).

Pigeonhole principle: placing N objects into k boxes forces some box to hold at least ⌈N/k⌉. To guarantee that some box holds r objects you need k(r−1)+1 objects.

Binomial theorem: (x+y)n=∑k=0n(nk)xkyn−k. Useful identities:

  • ∑k(nk)=2n, and the even-size and odd-size subsets each number 2n−1;
  • Pascal: (nk)=(n−1k)+(n−1k−1);
  • ∑kk(nk)=n 2n−1.
(0, 0)(4, 3)Path R R U R U U R= a word with 4 R and 3 UChoose where the 3 U go:C(7, 3) = 35 paths
A monotone lattice path is a word in R and U, so paths from (0, 0) to (4, 3) are counted by choosing the positions of the U steps.

2. Stars and bars: distributing identical objects

The number of ways to put n identical objects into r distinct boxes equals the number of non-negative integer solutions of x1+x2+⋯+xr=n, which is (n+r−1r−1). Think of n stars and r−1 bars in a row: choose where the bars go.

Constraint Count
xi≥0 (n+r−1r−1)
xi≥1 (no empty box) (n−1r−1)
xi≥ai substitute yi=xi−ai, then ∑yi=n−∑ai
xi≤c inclusion-exclusion on the boxes with xi≥c+1

Upper bounds by inclusion-exclusion: for x1+x2+x3=n with each xi≤c, subtract solutions where one variable is at least c+1, add back those where two are, and so on.

Which model? Ask two questions: are the objects identical or distinct, and are the boxes identical or distinct?

Objects Boxes Count
distinct (n) distinct (r) rn
distinct distinct, none empty r! S(n,r) (onto functions)
identical distinct stars and bars
distinct identical, none empty S(n,r) (Stirling, second kind)

Choosing r items from n types with repetition is also stars and bars: (n+r−1r).

3. Inclusion-exclusion, derangements and Catalan numbers

Inclusion-exclusion for n sets: ∣A1∪⋯∪An∣=∑∣Ai∣−∑∣Ai∩Aj∣+∑∣Ai∩Aj∩Ak∣−…. Count the complement ("none of the bad events") as total minus this.

Derangements: permutations of n items with no item in its own place. Dn=n!∑k=0n(−1)kk!,Dn=(n−1)(Dn−1+Dn−2) Values: D1=0, D2=1, D3=2, D4=9, D5=44, D6=265. For large n, Dn≈n!/e. The number of permutations with exactly k fixed points is (nk)Dn−k.

Catalan numbers: Cn=1n+1(2nn),C0,C1,⋯=1,1,2,5,14,42,132 They satisfy Cn+1=∑i=0nCi Cn−i. Cn counts:

  • binary trees with n nodes (structurally different shapes), and so binary search trees on n distinct keys;
  • balanced strings of n pairs of parentheses;
  • lattice paths from (0,0) to (n,n) that never go above the diagonal;
  • stack-permutations of 1,…,n (orders obtainable by pushing in order and popping at will);
  • triangulations of a convex polygon with n+2 sides.

4. Solving linear recurrences

A linear homogeneous recurrence with constant coefficients of order k: an=c1an−1+c2an−2+⋯+ckan−k.

Method: write the characteristic equation rk=c1rk−1+⋯+ck and find its roots.

Roots General solution
distinct r1,r2 an=Ar1n+Br2n
repeated r (twice) an=(A+Bn) rn
r with multiplicity m (A0+A1n+⋯+Am−1nm−1) rn

Then fix the constants from the initial values.

Non-homogeneous an=c1an−1+⋯+f(n): solution = general homogeneous part + one particular solution. Guess the particular solution in the shape of f(n):

  • constant f: try a constant p;
  • f(n)=bn: try p bn, or p n bn if b is a characteristic root.

Standard recurrences:

Problem Recurrence Solution
Tower of Hanoi moves Tn=2Tn−1+1, T1=1 2n−1
Bit strings of length n with no "11" an=an−1+an−2, a1=2, a2=3 Fn+2
Regions made by n lines in general position Ln=Ln−1+n, L0=1 1+n(n+1)2
Fibonacci Fn=Fn−1+Fn−2 roots 1±52

For the no-"11" strings: a valid string ends in 0 (any valid string of length n−1 before it) or in 10 (any valid string of length n−2).

5. Ordinary generating functions

The ordinary generating function of a sequence a0,a1,a2,… is A(x)=∑n≥0anxn. The coefficient of xn, written [xn]A(x), is the answer to the counting question.

Sequence an Generating function
1,1,1,… 11−x
cn 11−cx
n+1 1(1−x)2
n x(1−x)2
(n+k−1k−1) 1(1−x)k
(mn) (1+x)m
1 for n≤m, else 0 1+x+⋯+xm=1−xm+11−x

Counting with products: each box contributes a factor listing its allowed amounts, and the coefficient of xn in the product counts the distributions. Example: putting n identical sweets into 3 boxes holding at most 2 each is [xn] (1+x+x2)3.

Solving recurrences: multiply the recurrence by xn, sum over n, solve for A(x), then expand by partial fractions. For an=3an−1, a0=2: A(x)−2=3xA(x), so A(x)=21−3x and an=2⋅3n.

Reading a generating function backwards is common too: 1(1−x)(1−2x)=21−2x−11−x, so an=2n+1−1.

Formulas to remember

Permutations and combinations
P(n,r)=n!(n−r)!,(nr)=n!r! (n−r)!
Multiset permutations
n!n1! n2!⋯nk!
Stars and bars
#{x1+⋯+xr=n, xi≥0}=(n+r−1r−1)
Derangements
Dn=n!∑k=0n(−1)kk!
Catalan numbers
Cn=1n+1(2nn)
Distinct characteristic roots
an=A r1n+B r2n
Repeated characteristic root
an=(A+Bn) rn
Negative binomial series
1(1−x)k=∑n≥0(n+k−1k−1)xn
Generalised pigeonholeSome box gets at least this many of N objects in k boxes
⌈Nk⌉

Common mistakes

  • Using (n+r−1r) when the question needs (n+r−1r−1); check by trying a tiny case.
  • Treating identical objects as distinct (or the reverse) when choosing the counting model.
  • For a repeated characteristic root, writing Arn+Brn instead of (A+Bn)rn.
  • Forgetting the add-back terms in inclusion-exclusion with upper bounds.
  • Counting circular arrangements as n! instead of (n−1)!.
  • Mis-indexing the no-consecutive-1s count: length 1 already has 2 valid strings.

Worked examples

Try each step yourself, then reveal the working.

Example 1: Stars and bars with lower bounds

numerical

How many integer solutions does x1+x2+x3=10 have with x1≥2, x2≥1, x3≥0?

  1. Step 1: Remove the lower bounds by substitution.

    Try it on paper first.

Example 2: A recurrence with a repeated root

numerical

Solve an=4an−1−4an−2 with a0=1, a1=6.

  1. Step 1: Write and solve the characteristic equation.

    Try it on paper first.

Example 3: Letters in the wrong envelopes

numerical

Four letters are put into four addressed envelopes, one each, at random. In how many ways does no letter reach its own envelope? In how many does exactly one?

  1. Step 1: Set up inclusion-exclusion over the letters that land correctly.

    Try it on paper first.

Example 4: Counting with a generating function

numerical

In how many ways can 4 identical sweets be given to 3 children if no child gets more than 2?

  1. Step 1: Write the generating function.

    Try it on paper first.

Previous year questions

21 official GATE CSE questions on this topic, newest first.

Practise these 21 PYQs →

Topic test

10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.