Module 1 · 16 min read · Free chapter
Combinatorics, recurrence relations and generating functions
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₂ⁿ.
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 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 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 ways or one of other ways, with no overlap, there are ways. The product rule says that if a task has a first stage with ways and a second stage with ways whatever the first choice was, there are mn ways. For example, a 4-digit PIN has possibilities, and binary strings of length number .
A permutation is an ordered arrangement. The number of ways to arrange objects chosen from distinct objects is . Arranging all objects gives 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 distinct arrangements. When objects sit around a circle, rotations look the same, so people at a round table can be seated in ways.
Worked example. How many 3-letter codes with distinct letters can be made from A to ? That is . If letters may repeat, the product rule gives instead.
Why it matters. Permutations count orderings, such as schedules, rankings and the possible inputs to a sorting algorithm. The fact that there are orderings of keys is exactly why comparison sorting needs about comparisons.
Combinations, repetition and the binomial theorem
A combination is a selection where order does not matter. Choosing objects from distinct objects can be done in ways, because each unordered choice appears times among the ordered ones. For example, a committee of 3 from 10 people can be formed in 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 .
In general, choosing objects from kinds with repetition gives ways. The same count solves equations: has solutions in non-negative integers. If every must be at least 1, give each one 1 first, and the count becomes .
The binomial theorem expands as the sum of for to . Putting gives the sum of all , which is the number of subsets. Pascal's identity says that a subset either contains a fixed element or does not.
Worked example. How many non-negative integer solutions does have with ? Set to get , which has 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 objects go into boxes, some box gets at least two. It proves that something exists without telling you where it is.
The generalised form says that if objects go into boxes, some box gets at least objects. The reason is simple. If every box had at most objects, the total would be less than .
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 , that is . 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 , , and . 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, .
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 .
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 be the arrangements where object is fixed. Inclusion–exclusion over all sets gives .
The first values are , , , and . They also follow the recurrence , so . Since the bracket tends to , 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 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 . For , 1, 2, 3, 4, 5, 6 they are 1, 1, 2, 5, 14, 42 and 132. For example, .
The reason they agree is a shared recurrence. A binary tree with nodes has a root, a left subtree with nodes and a right subtree with nodes, for some from 0 to . So is the sum of over to , 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 , which equals .
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 . To solve it means to find a closed form, a formula for directly in terms of . For recurrences that are linear with constant coefficients, there is a complete recipe.
Take . Try : dividing by gives the characteristic equation . If its roots and are distinct, the general solution is . If there is one repeated root , the general solution is .
Worked example. Solve with and . The characteristic equation has roots 2 and 3, so . From : , and from : , which give and . So , and indeed .
A non-homogeneous recurrence has an extra term , such as . Its solution is the homogeneous solution plus one particular solution. For a constant , try a constant A: gives , so , and gives , the Tower of Hanoi count.
If and is already a root of multiplicity , the guess fails, so try instead. For example, needs the guess , which gives .
Why it matters. Many algorithms and data structures produce such recurrences, for example counting binary strings with no two consecutive , 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 , , , … as the coefficients of one power series . We never plug in a value for . The power series is just a clothes line on which the terms hang, and algebra on does counting for us.
A few standard series do most of the work:
- , so every coefficient is 1.
- , so the coefficient of is .
- has coefficient for .
- , so the coefficient of is .
Worked example 1 (counting). In how many ways can 10 identical sweets be shared among 3 children? Each child contributes a factor , so we need the coefficient of in . That is , the same as stars and bars.
Restrictions are easy to add. If each child must get between 1 and 4 sweets, each factor becomes , and we read the coefficient of in its cube.
Worked example 2 (solving a recurrence). Let for with . Multiply by and sum over to get . So , and reading the coefficient gives .
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 , 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:
- Derive a counting formula, such as the derangement formula, from inclusion–exclusion.
- Solve a linear recurrence with given initial values and verify the first few terms.
- Model a word problem as a recurrence or a generating function and then solve it.
- Prove a statement with the pigeonhole principle, stating the boxes and the objects clearly.
Practice questions (our own, not previous-year questions).
- Derive the formula for the number of derangements of objects and find . (10 marks)
- Find a recurrence for the number of binary strings of length with no two consecutive , and solve it. (12 marks)
- 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 be the number of valid strings of length , and note that and .
- Argue that a valid string ending in 1 is any valid string of length followed by 1, which gives strings.
- Argue that a valid string ending in 0 must end in 10, so it is a valid string of length followed by 10, which gives strings.
- Conclude that , which is the Fibonacci rule shifted by two places.
- Solve it with the characteristic equation , whose roots are and , and fit the constants to and .
Quick revision
- Stages one after another multiply, and alternatives that do not overlap add.
- , circular arrangements number , and MISSISSIPPI has 34,650 arrangements.
- , and choosing from kinds with repetition gives .
- has non-negative and positive solutions.
- objects in boxes put at least in some box.
- Three-set inclusion–exclusion adds singles, subtracts pairs and adds the triple.
- Derangements are 0, 1, 2, 9, 44 for to 5, and is about n!/e.
- Catalan numbers are 1, 1, 2, 5, 14, 42, and counts BSTs on keys.
- Distinct characteristic roots give , and a repeated root gives .
- The coefficient of in is .