Computer Science and IT‹›⌂ 0:00 studied

Module 1 · 16 min read · Free chapter

Combinatorics, recurrence relations and generating functions

In short

Choosing r items from n kinds with repetition gives C(n + r − 1, r) ways. Derangements are 0, 1, 2, 9, 44 for n = 1 to 5. Catalan numbers 1, 1, 2, 5, 14 count binary search trees. A recurrence with distinct characteristic roots r₁, r₂ has the solution αr₁ⁿ + βr₂ⁿ.

What are you preparing for? We show the practice and previous year questions of your exam.

Start from zero: how many ways?

A dabbawala's customer can choose roti, rice or paratha as the main item, and one of four sabzis to go with it. How many different tiffins are possible? For each of the 3 main items there are 4 sabzis, so there are 3×4=123 \times 4 = 12 tiffins.

Now suppose the customer will take either one sweet from a list of 5 or one fruit from a list of 3, but not both. Then there are 5+3=85 + 3 = 8 choices for the last item. Choices made one after another multiply, while choices made instead of each other add.

Counting looks easy until the numbers grow. How many 8-character passwords are there? How many ways can 10 letters be delivered so that nobody gets their own? This chapter gives exact tools for such questions, and then shows how to count things that are defined step by step.

Why it matters. Counting is behind the running time of brute-force search, the probability of a hash collision and the number of keys an attacker must try. GATE asks at least one counting or recurrence question almost every year.

Sum rule, product rule and permutations

The sum rule says that if a task can be done in one of mm ways or one of nn other ways, with no overlap, there are m+nm + n ways. The product rule says that if a task has a first stage with mm ways and a second stage with nn ways whatever the first choice was, there are mn ways. For example, a 4-digit PIN has 104=10,00010^4 = 10{,}000 possibilities, and binary strings of length nn number 2n2^n.

A permutation is an ordered arrangement. The number of ways to arrange rr objects chosen from nn distinct objects is P(n,r)=n×(n−1)×⋯×(n−r+1)=n!(n−r)!P(n, r) = n \times (n - 1) \times \dots \times (n - r + 1) = \dfrac{n!}{(n - r)!}. Arranging all nn objects gives n!n! ways.

When some objects are identical, we divide out the orders that look the same. The word MISSISSIPPI has 11 letters, with S four times, I four times and P twice, so it has 11!4!×4!×2!=34,650\dfrac{11!}{4!\times 4!\times 2!} = 34{,}650 distinct arrangements. When objects sit around a circle, rotations look the same, so nn people at a round table can be seated in (n−1)!(n - 1)! ways.

Worked example. How many 3-letter codes with distinct letters can be made from A to ZZ? That is P(26,3)=26×25×24=15,600P(26, 3) = 26 \times 25 \times 24 = 15{,}600. If letters may repeat, the product rule gives 263=17,57626^3 = 17{,}576 instead.

Why it matters. Permutations count orderings, such as schedules, rankings and the possible inputs to a sorting algorithm. The fact that there are n!n! orderings of nn keys is exactly why comparison sorting needs about nlog⁡nn \log n comparisons.

Combinations, repetition and the binomial theorem

A combination is a selection where order does not matter. Choosing rr objects from nn distinct objects can be done in (nr)=n!r!(n−r)!\binom{n}{r} = \dfrac{n!}{r!(n - r)!} ways, because each unordered choice appears r!r! times among the ordered ones. For example, a committee of 3 from 10 people can be formed in (103)=120\binom{10}{3} = 120 ways.

Sometimes we choose from kinds, with repetition allowed, like picking 10 sweets from 3 kinds. Line up the 10 sweets as stars and separate the kinds with 2 bars. Every arrangement of 10 stars and 2 bars is one choice, so the count is (122)=66\binom{12}{2} = 66.

In general, choosing rr objects from nn kinds with repetition gives (n+r−1r)\binom{n + r - 1}{r} ways. The same count solves equations: x1+x2+⋯+xk=nx_1 + x_2 + \dots + x_k = n has (n+k−1k−1)\binom{n + k - 1}{k - 1} solutions in non-negative integers. If every xix_i must be at least 1, give each one 1 first, and the count becomes (n−1k−1)\binom{n - 1}{k - 1}.

The binomial theorem expands (x+y)n(x + y)^n as the sum of (nk)xkyn−k\binom{n}{k} x^k y^{n-k} for k=0k = 0 to nn. Putting x=y=1x = y = 1 gives the sum of all (nk)=2n\binom{n}{k} = 2^n, which is the number of subsets. Pascal's identity (nr)=(n−1r−1)+(n−1r)\binom{n}{r} = \binom{n - 1}{r - 1} + \binom{n - 1}{r} says that a subset either contains a fixed element or does not.

Worked example. How many non-negative integer solutions does x1+x2+x3=10x_1 + x_2 + x_3 = 10 have with x1≥2x_1 \ge 2? Set y1=x1−2y_1 = x_1 - 2 to get y1+x2+x3=8y_1 + x_2 + x_3 = 8, which has (102)=45\binom{10}{2} = 45 solutions.

Why it matters. Distributing identical items into distinct bins is the model for load balancing and for counting monomials in a polynomial. Stars and bars turns many hard-looking GATE questions into one binomial coefficient.

The pigeonhole principle

If 13 pigeons fly into 12 holes, some hole must hold at least two pigeons. That obvious fact is the pigeonhole principle: if more than kk objects go into kk boxes, some box gets at least two. It proves that something exists without telling you where it is.

The generalised form says that if NN objects go into kk boxes, some box gets at least ⌈Nk⌉\lceil \dfrac{N}{k}\rceil objects. The reason is simple. If every box had at most ⌈Nk⌉−1\lceil \dfrac{N}{k}\rceil - 1 objects, the total would be less than NN.

Worked example 1. How many students must be in a class to be sure that at least 3 share a birth month? With 12 months as boxes, we need ⌈N12⌉≥3\lceil \dfrac{N}{12}\rceil \ge 3, that is N>24N > 24. So 25 students are enough, and 24 are not, since two per month is possible.

Worked example 2. Show that among any 5 integers chosen from 1 to 8, two of them add up to 9. Form the 4 boxes {1,8}\{1, 8\}, {2,7}\{2, 7\}, {3,6}\{3, 6\} and {4,5}\{4, 5\}. Five numbers in four boxes put two numbers in the same box, and those two add to 9.

Why it matters. The pigeonhole principle explains why every hash function on a large key space has collisions and why lossless compression cannot shrink every file. GATE uses it in "minimum number needed to guarantee" questions, where the answer is always one more than the worst case.

Inclusion–exclusion and derangements

When sets overlap, adding their sizes counts the shared elements more than once. Inclusion–exclusion corrects this by alternately subtracting and adding the overlaps. For three sets, ∣A∪B∪C∣=∣A∣+∣B∣+∣C∣−∣A∩B∣−∣A∩C∣−∣B∩C∣+∣A∩B∩C∣\lvert A \cup B \cup C\rvert = \lvert A\rvert + \lvert B\rvert + \lvert C\rvert - \lvert A \cap B\rvert - \lvert A \cap C\rvert - \lvert B \cap C\rvert + \lvert A \cap B \cap C\rvert.

Worked example. How many integers from 1 to 100 are divisible by 2, 3 or 5? The single counts are 50, 33 and 20. The pairs give multiples of 6, 10 and 15, which number 16, 10 and 6, and the triple gives multiples of 30, which number 3. So the answer is 50+33+20−16−10−6+3=7450 + 33 + 20 - 16 - 10 - 6 + 3 = 74.

A derangement is a permutation in which no object stays in its own place, like letters put in envelopes so that none is in the right one. Let AiA_i be the arrangements where object ii is fixed. Inclusion–exclusion over all nn sets gives Dn=n!(1−11!+12!−13!+⋯+(−1)nn!)D_n = n!(1 - \dfrac{1}{1!} + \dfrac{1}{2!} - \dfrac{1}{3!} + \dots + \dfrac{(-1)^n}{n!}).

The first values are D1=0D_1 = 0, D2=1D_2 = 1, D3=2D_3 = 2, D4=9D_4 = 9 and D5=44D_5 = 44. They also follow the recurrence Dn=(n−1)(Dn−1+Dn−2)D_n = (n - 1)(D_{n-1} + D_{n-2}), so D5=4×(9+2)=44D_5 = 4 \times (9 + 2) = 44. Since the bracket tends to 1e\dfrac{1}{e}, DnD_n is the whole number nearest to n!/e, and the chance that a random permutation is a derangement is about 0.368.

Why it matters. Inclusion–exclusion counts onto functions, numbers coprime to nn and failed test cases with several causes. Derangements are a GATE favourite because they combine the formula, the recurrence and a clean numerical answer.

Catalan numbers

Some counting questions look unrelated but share one answer. How many binary trees have 3 nodes, how many balanced strings use 3 pairs of brackets, and how many ways can a pentagon be cut into triangles by non-crossing diagonals? The answer to all three is 5.

These are the Catalan numbers Cn=(2nn)n+1C_n = \dfrac{\binom{2n}{n}}{n + 1}. For n=0n = 0, 1, 2, 3, 4, 5, 6 they are 1, 1, 2, 5, 14, 42 and 132. For example, C4=(84)5=705=14C_4 = \dfrac{\binom{8}{4}}{5} = \dfrac{70}{5} = 14.

The reason they agree is a shared recurrence. A binary tree with n+1n + 1 nodes has a root, a left subtree with ii nodes and a right subtree with n−in - i nodes, for some ii from 0 to nn. So Cn+1C_{n+1} is the sum of CiCn−iC_i C_{n-i} over i=0i = 0 to nn, and bracket strings and triangulations split in the same way.

Worked example. How many binary search trees can be built on the keys 1, 2, 3, 4? Each choice of root splits the remaining keys into a left and a right set. Summing gives C0C3+C1C2+C2C1+C3C0=5+2+2+5=14C_0C_3 + C_1C_2 + C_2C_1 + C_3C_0 = 5 + 2 + 2 + 5 = 14, which equals C4C_4.

Why it matters. Catalan numbers count parse trees of expressions, valid stack push-pop sequences and ways to bracket a matrix chain product. GATE asks the BST count and the bracket count directly.

Linear recurrences with constant coefficients

A recurrence relation defines each term of a sequence from earlier terms, like the Fibonacci rule Fn=Fn−1+Fn−2F_n = F_{n-1} + F_{n-2}. To solve it means to find a closed form, a formula for ana_n directly in terms of nn. For recurrences that are linear with constant coefficients, there is a complete recipe.

Take an=c1an−1+c2an−2a_n = c_1a_{n-1} + c_2a_{n-2}. Try an=rna_n = r^n: dividing by rn−2r^{n-2} gives the characteristic equation r2=c1r+c2r^2 = c_1r + c_2. If its roots r1r_1 and r2r_2 are distinct, the general solution is an=αr1n+βr2na_n = \alpha r_1^n + \beta r_2^n. If there is one repeated root rr, the general solution is an=(α+βn)rna_n = (\alpha + \beta n) r^n.

Worked example. Solve an=5an−1−6an−2a_n = 5a_{n-1} - 6a_{n-2} with a0=1a_0 = 1 and a1=0a_1 = 0. The characteristic equation r2−5r+6=0r^2 - 5r + 6 = 0 has roots 2 and 3, so an=α2n+β3na_n = \alpha 2^n + \beta 3^n. From a0a_0: α+β=1\alpha + \beta = 1, and from a1a_1: 2α+3β=02\alpha + 3\beta = 0, which give β=−2\beta = -2 and α=3\alpha = 3. So an=3⋅2n−2⋅3na_n = 3\cdot 2^n - 2\cdot 3^n, and indeed a2=12−18=−6=5×0−6×1a_2 = 12 - 18 = -6 = 5 \times 0 - 6 \times 1.

A non-homogeneous recurrence has an extra term f(n)f(n), such as an=2an−1+1a_n = 2a_{n-1} + 1. Its solution is the homogeneous solution plus one particular solution. For a constant f(n)=1f(n) = 1, try a constant A: A=2A+1A = 2A + 1 gives A=−1A = -1, so an=α2n−1a_n = \alpha 2^n - 1, and a0=0a_0 = 0 gives an=2n−1a_n = 2^n - 1, the Tower of Hanoi count.

If f(n)=c⋅snf(n) = c\cdot s^n and ss is already a root of multiplicity mm, the guess A⋅snA\cdot s^n fails, so try A⋅nm⋅snA\cdot n^m\cdot s^n instead. For example, an=2an−1+2na_n = 2a_{n-1} + 2^n needs the guess A⋅n⋅2nA\cdot n\cdot 2^n, which gives A=1A = 1.

Why it matters. Many algorithms and data structures produce such recurrences, for example counting binary strings with no two consecutive 1s1s, which gives the Fibonacci numbers. The divide-and-conquer recurrences of algorithms are solved by different tools, covered in asymptotic analysis and recurrences.

Generating functions

A generating function stores a whole sequence a0a_0, a1a_1, a2a_2, … as the coefficients of one power series G(x)=a0+a1x+a2x2+…G(x) = a_0 + a_1x + a_2x^2 + \dots. We never plug in a value for xx. The power series is just a clothes line on which the terms hang, and algebra on G(x)G(x) does counting for us.

A few standard series do most of the work:

  • 11−x=1+x+x2+x3+…\dfrac{1}{1 - x} = 1 + x + x^2 + x^3 + \dots, so every coefficient is 1.
  • 11−ax=1+ax+a2x2+…\dfrac{1}{1 - ax} = 1 + ax + a^2x^2 + \dots, so the coefficient of xnx^n is ana^n.
  • 1(1−x)k\dfrac{1}{(1 - x)^k} has coefficient (n+k−1n)\binom{n + k - 1}{n} for xnx^n.
  • x(1−x)2=x+2x2+3x3+…\dfrac{x}{(1 - x)^2} = x + 2x^2 + 3x^3 + \dots, so the coefficient of xnx^n is nn.

Worked example 1 (counting). In how many ways can 10 identical sweets be shared among 3 children? Each child contributes a factor 1+x+x2+…1 + x + x^2 + \dots, so we need the coefficient of x10x^{10} in 1(1−x)3\dfrac{1}{(1 - x)^3}. That is (1210)=66\binom{12}{10} = 66, the same as stars and bars.

Restrictions are easy to add. If each child must get between 1 and 4 sweets, each factor becomes x+x2+x3+x4x + x^2 + x^3 + x^4, and we read the coefficient of x10x^{10} in its cube.

Worked example 2 (solving a recurrence). Let an=3an−1a_n = 3a_{n-1} for n≥1n \ge 1 with a0=2a_0 = 2. Multiply by xnx^n and sum over n≥1n \ge 1 to get G(x)−2=3xG(x)G(x) - 2 = 3xG(x). So G(x)=21−3xG(x) = \dfrac{2}{1 - 3x}, and reading the coefficient gives an=2⋅3na_n = 2\cdot 3^n.

Why it matters. Generating functions turn counting with restrictions into polynomial multiplication, which a computer can do quickly. GATE asks you to find the sequence behind a given G(x)G(x), or the coefficient of a term in a product.

Descriptive lens: how written papers ask this

Written papers for scientist posts and university-level tests ask about counting in four ways:

  1. Derive a counting formula, such as the derangement formula, from inclusion–exclusion.
  2. Solve a linear recurrence with given initial values and verify the first few terms.
  3. Model a word problem as a recurrence or a generating function and then solve it.
  4. Prove a statement with the pigeonhole principle, stating the boxes and the objects clearly.

Practice questions (our own, not previous-year questions).

  1. Derive the formula for the number of derangements of nn objects and find D6D_6. (10 marks)
  2. Find a recurrence for the number of binary strings of length nn with no two consecutive 0s0s, and solve it. (12 marks)
  3. Use a generating function to find the number of ways to pay ₹20 using coins of ₹2 and ₹5 only. (8 marks)

Answer plan for question 2.

  • Let bnb_n be the number of valid strings of length nn, and note that b1=2b_1 = 2 and b2=3b_2 = 3.
  • Argue that a valid string ending in 1 is any valid string of length n−1n - 1 followed by 1, which gives bn−1b_{n-1} strings.
  • Argue that a valid string ending in 0 must end in 10, so it is a valid string of length n−2n - 2 followed by 10, which gives bn−2b_{n-2} strings.
  • Conclude that bn=bn−1+bn−2b_n = b_{n-1} + b_{n-2}, which is the Fibonacci rule shifted by two places.
  • Solve it with the characteristic equation r2=r+1r^2 = r + 1, whose roots are 1+52\dfrac{1 + \sqrt{5}}{2} and 1−52\dfrac{1 - \sqrt{5}}{2}, and fit the constants to b1b_1 and b2b_2.

Quick revision

  • Stages one after another multiply, and alternatives that do not overlap add.
  • P(n,r)=n!(n−r)!P(n, r) = \dfrac{n!}{(n - r)!}, circular arrangements number (n−1)!(n - 1)!, and MISSISSIPPI has 34,650 arrangements.
  • (nr)=n!r!(n−r)!\binom{n}{r} = \dfrac{n!}{r!(n - r)!}, and choosing rr from nn kinds with repetition gives (n+r−1r)\binom{n + r - 1}{r}.
  • x1+⋯+xk=nx_1 + \dots + x_k = n has (n+k−1k−1)\binom{n + k - 1}{k - 1} non-negative and (n−1k−1)\binom{n - 1}{k - 1} positive solutions.
  • NN objects in kk boxes put at least ⌈Nk⌉\lceil \dfrac{N}{k}\rceil in some box.
  • Three-set inclusion–exclusion adds singles, subtracts pairs and adds the triple.
  • Derangements are 0, 1, 2, 9, 44 for n=1n = 1 to 5, and DnD_n is about n!/e.
  • Catalan numbers are 1, 1, 2, 5, 14, 42, and CnC_n counts BSTs on nn keys.
  • Distinct characteristic roots give αr1n+βr2n\alpha r_1^n + \beta r_2^n, and a repeated root gives (α+βn)rn(\alpha + \beta n)r^n.
  • The coefficient of xnx^n in 1(1−x)k\dfrac{1}{(1 - x)^k} is (n+k−1n)\binom{n + k - 1}{n}.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 15 answered

  1. A tiffin has one of 3 main items and one of 4 sabzis. How many different tiffins are possible?

  2. The number of distinct arrangements of the letters of MISSISSIPPI is:

  3. In how many ways can 6 people sit around a round table?

  4. The number of non-negative integer solutions of x1+x2+x3=10x_1 + x_2 + x_3 = 10 is:

  5. The sum (n0)+(n1)+⋯+(nn)\binom{n}{0} + \binom{n}{1} + \dots + \binom{n}{n} equals:

  6. The minimum number of students needed to guarantee that at least 3 share a birth month is:

  7. How many integers from 1 to 100 are divisible by 2, 3 or 5?

  8. The number of derangements of 4 objects is:

  9. Given D3=2D_3 = 2 and D4=9D_4 = 9, D5D_5 equals:

  10. The number of binary search trees that can be built on 4 distinct keys is:

  11. The solution of an=5an−1−6an−2a_n = 5a_{n-1} - 6a_{n-2} with a0=1a_0 = 1, a1=0a_1 = 0 is:

  12. If the characteristic equation of a recurrence has the root 2 repeated twice, the general solution is:

  13. The solution of an=2an−1+1a_n = 2a_{n-1} + 1 with a0=0a_0 = 0 is:

  14. The coefficient of x10x^{10} in 1(1−x)3\dfrac{1}{(1 - x)^3} is:

  15. GATE CS lists generating functions under:

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

What is the number of derangements of 4 objects?

D₄ = 9, from n!(1 − 1/1! + 1/2! − 1/3! + 1/4!).

How many non-negative solutions does x₁ + x₂ + x₃ = 10 have?

By stars and bars, C(12, 2) = 66.

How many BSTs can be built on n distinct keys?

The Catalan number Cₙ = C(2n, n)/(n + 1), so 14 for 4 keys.

What if the characteristic equation has a repeated root?

For a root r repeated twice the solution is (α + βn)rⁿ.

More in this module: Discrete mathematics

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Discrete Mathematics (IIT Ropar, 106106183): counting, recurrence relations and generating functions