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
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 times, divide by .
Permutations and combinations
| Situation | Count |
|---|---|
| Arrange distinct objects | |
| Ordered selection of from , no repetition | |
| Unordered selection of from , no repetition | |
| Ordered selection of from , repetition allowed | |
| Unordered selection of from types, repetition allowed | |
| Arrangements of a multiset with counts | |
| Circular arrangements of distinct objects | |
| Necklaces where flipping is allowed, |
Identities that save time: , Pascal's rule , , and Vandermonde .
Stars and bars
The number of solutions of in
- non-negative integers is ;
- positive integers is .
Lower bounds : substitute and reduce by . Upper bounds : use inclusion-exclusion on the events .
The same formula counts multisets of size from types, ways to put identical balls in distinct boxes, and monomials of degree in variables.
Balls into boxes
| Balls | Boxes | Any number per box | Every box nonempty |
|---|---|---|---|
| distinct | distinct | ||
| identical | distinct | ||
| distinct | identical | ||
| identical | identical | partitions of into at most parts | partitions of into exactly parts |
Inclusion-exclusion and derangements
Typical uses: integers up to 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: Values: , , , , , . Also , and permutations with exactly fixed points number .
Pigeonhole principle
If objects go into boxes, some box has at least objects. To force some box to hold at least objects you need .
Standard applications: among any integers two have the same remainder mod ; among any 5 points in a unit square two are within ; 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 by their first or last step. Examples:
- Binary strings with no two consecutive 1s: end in 0 or end in 01, so with , .
- Tower of Hanoi moves: , so .
- Regions made by lines in general position: , so .
- Binary strings of length with an even number of 0s: .
Linear homogeneous recurrences with constant coefficients
For , solve the characteristic equation .
- Distinct roots : .
- A root with multiplicity contributes .
Fit the constants with the initial values.
Non-homogeneous recurrences
For , the solution is the homogeneous solution plus one particular solution. Guess the particular solution in the shape of :
| Trial particular solution | |
|---|---|
| constant | |
| polynomial of degree | polynomial of degree |
| , not a root | |
| , a root of multiplicity |
If 1 is a root of the characteristic equation, multiply the polynomial guess by .
Generating functions
The ordinary generating function of is .
| Sequence | Generating function |
|---|---|
| for , else |
Operations:
- Multiplying by shifts the sequence right by one.
- Multiplying by gives partial sums .
- Product gives the convolution . 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 , sum over , solve for , then split into partial fractions and read off coefficients.
Worked examples
Worked example 1: Bounded stars and bars
How many solutions does have with ?
- Unrestricted: .
- One variable at least 5: replace it by , sum 7: , times 4: .
- Two variables at least 5: sum 2: , times : .
- Three variables at least 5: sum would be negative: 0.
Answer: .
Worked example 2: Repeated root
Solve , , .
- , double root 3. So .
- . , so .
- . Check and .
Worked example 3: Non-homogeneous recurrence
Solve , .
- Homogeneous part: .
- Particular: try constant : , .
- , and gives . So . Check: .
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: Count directly instead: choose apples and bananas with ; oranges take the rest. For all 3 values of work, giving . For only works, giving 1. Total 16.
Worked example 5: Divisibility by inclusion-exclusion
How many integers in are divisible by none of 2, 3, 5?
- Divisible by 2, 3, 5: . Sum .
- By 6, 10, 15: . Sum .
- By 30: .
- Divisible by at least one: .
Answer: .
Common traps
- Using when the variables must be positive. Positive solutions use .
- 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 and ; also check .
- Forgetting the factor for repeated roots.
- Off by one in pigeonhole: to force in a box with boxes, you need , not .
- Confusing (sequence ) with (sequence ).
Quick revision
- Non-negative solutions of : . Positive: .
- Multiset arrangements: . Circular: .
- Onto functions : .
- : . Exactly fixed points: .
- Pigeonhole: objects force in one box.
- Distinct roots: . Repeated root of multiplicity : polynomial of degree times .
- Strings with no two adjacent 1s: Fibonacci, length gives (with ).
- has coefficients .
- Product of generating functions is convolution of sequences.