IAE Academy

GATE CS

Recurrence relations and generating functions

A recurrence relation defines each term of a sequence from earlier terms, and a generating function packs a whole sequence into one power series. For GATE you set up a recurrence for a counting problem, solve linear constant-coefficient recurrences by the characteristic equation, and use generating functions to read off terms, solve recurrences and count selections.

3 previous-year questions · 3 solved · 5 practice questions

Setting up a recurrence

Convention: a sequence is and runs over unless a question says otherwise. A recurrence gives in terms of earlier terms; it determines the sequence only together with enough initial conditions (for a recurrence of order , the first consecutive values).

To set up a recurrence for a counting problem, split the objects of size by what happens at the last position (or the first), so that each case reduces to a smaller count of the same kind. The cases must not overlap and must cover everything.

Procedure for a counting recurrence

  1. Define precisely (what is counted, for which ).

  2. Split the objects of size by the last symbol, last tile or last step.

  3. Express each case as a count of a smaller size; add the cases.

  4. Compute the initial values by direct listing, including if it makes sense.

  5. Test the recurrence on a small by listing.

Problem

Recurrence

Initial values

Binary strings of length with no two consecutive 1s

Tilings of a strip with and tiles

Ways to climb stairs taking 1 or 2 steps at a time

Fibonacci numbers

Lucas numbers

Minimum moves for the Tower of Hanoi with discs

Regions of the plane cut by lines, no two parallel and no three through one point

Derangements of objects (permutations with no fixed point)

for

Catalan numbers: binary trees with nodes (unlabelled, left and right child distinguished)

for

Closed forms for the table

  1. No two consecutive 1s: ; tilings and stairs: .

  2. Tower of Hanoi: .

  3. Regions: (so ).

  4. Derangements: ; also for .

  5. Catalan: ; also counts strings of matched pairs of parentheses and triangulations of a convex polygon with sides.

CATALAN NUMBERS AND DERANGEMENTS

For ; for example and .

Why the first row works: for , a valid string of length ends in 0 (the first symbols form any valid string) or ends in 01 (the first symbols form any valid string). Hence for , with (the empty string) and . Here (00, 01, 10), so .

WORKED EXAMPLE

Ternary strings over must not contain two consecutive 's. Let be the number of such strings of length . Find a recurrence and .

Solution
  1. Strings ending in or : 2 choices for the last symbol after any valid string of length , giving .

  2. Strings ending in : the symbol before it must be or (2 choices), and the first symbols form any valid string, giving .

  3. So for , with and .

  4. and .

  5. Check: of the 27 strings of length 3, those containing are (3) and (3), with in both lists, so ; and .

Answer: and .

Linear homogeneous recurrences with constant coefficients

A linear homogeneous recurrence of order with constant coefficients has the form with . Trying gives its characteristic equation.

CHARACTERISTIC EQUATION

For ; it has degree , so roots over the complex numbers counted with multiplicity. Since , no root is 0.

GENERAL SOLUTION, DISTINCT ROOTS

When all roots are distinct (real or complex); the constants come from the initial conditions.

CONTRIBUTION OF A REPEATED ROOT

A root of multiplicity contributes this term; add the contributions of all roots.

FIBONACCI AND LUCAS CLOSED FORMS

Roots of ; and .

Procedure

  1. Write the characteristic equation and find its roots with multiplicities.

  2. Write the general solution (distinct roots: ; repeated root: polynomial in times ).

  3. Substitute the initial conditions to get linear equations for the constants.

  4. Solve for the constants and check the closed form against several terms.

WORKED EXAMPLE

Solve with , , and find .

Solution
  1. Characteristic equation , so or .

  2. .

  3. : . : . Hence , .

  4. . Check: and the recurrence gives .

  5. .

Answer: and .

WORKED EXAMPLE

Solve with , .

Solution
  1. Characteristic equation : a double root .

  2. .

  3. . gives .

  4. . Check: by the formula, and from the recurrence.

Answer: .

EXAM TIP

The number of constants equals the order , and you need exactly initial conditions. A quick check of and from both the recurrence and your closed form catches most algebra slips.

Non-homogeneous recurrences

For , the solution is : the general solution of the homogeneous part plus any one particular solution. Find the particular solution by a trial form matched to , then fix the constants of the homogeneous part using the initial conditions of the full recurrence (not of the homogeneous part alone).

Trial form if the base is not a characteristic root

If the base is a root of multiplicity

constant

(base is 1)

polynomial of degree

polynomial of degree with unknown coefficients

(polynomial of degree ), base is 1

polynomial of degree times

(polynomial of degree )

(polynomial of degree )

If is a sum of terms of different kinds (for example ), find a particular solution for each term separately and add them.

FIRST-ORDER LINEAR RECURRENCE

For with constants and . Example: , gives .

WORKED EXAMPLE

Solve with , .

Solution
  1. Homogeneous part: , roots 1 and 2, so .

  2. and 2 is a root of multiplicity 1, so try .

  3. Substitute and divide by : , so .

  4. . From : . From : , so . Hence , .

  5. . Check: , and the recurrence gives . Also and .

Answer: .

WORKED EXAMPLE

Let and for (powers of 2). Find .

Solution
  1. Put and : then for , with .

  2. Homogeneous part ; 2 is a root, so try : gives .

  3. and gives , so .

  4. With : . Check: ; ; .

Answer: for a power of 2.

EXAM TIP

If the usual trial form for already appears in the homogeneous solution, it fails: substituting it into gives 0, which cannot equal a non-zero . Multiply by , where is the multiplicity of the base as a root. Constants must be fitted after adding the particular solution.

Ordinary generating functions

The ordinary generating function of a sequence is the formal power series . The sequence is the list of coefficients, written . Convergence is not needed for counting; we only manipulate coefficients.

GEOMETRIC SERIES

Coefficients and ; so gives .

LINEAR SEQUENCES

Coefficients and .

NEGATIVE BINOMIAL SERIES

For integer ; counts solutions of in non-negative integers.

FINITE SERIES

For integer ; the second gives a bounded choice of to items.

GENERALISED BINOMIAL THEOREM

For any real , with ; for it gives the negative binomial series with signs, .

SQUARES

Obtained by applying to .

Operation on the sequence

Effect on the generating function

Add:

Scale:

Shift right: ( zeros)

Shift left: (drop )

Multiply by :

Partial sums:

Convolution:

Replace by :

To read a sequence off a rational closed form, first divide out any polynomial part if the numerator's degree is not smaller than the denominator's (it changes only the first few coefficients), then split the rest into partial fractions of the type and use the series above. For example has coefficients .

Solving recurrences with generating functions

Procedure

  1. Multiply the recurrence by and sum over all for which it holds.

  2. Express each sum through , using the shift rules and the initial terms.

  3. Solve the resulting linear equation for as a rational function.

  4. Factor the denominator, split into partial fractions, and expand each piece.

  5. Read off and check against the first terms.

FIBONACCI AND LUCAS GENERATING FUNCTIONS

With and .

WORKED EXAMPLE

Use generating functions to solve for , with , .

Solution
  1. Let . The recurrence holds for , so .

  2. With , : , so .

  3. , so .

  4. Partial fractions: . Check: .

  5. So . Check: and ; and .

Answer: .

CATALAN GENERATING FUNCTION

The quadratic comes from the convolution ; the minus sign is the root with . Expanding by the generalised binomial theorem gives .

EXAM TIP

Before any cancellation with the numerator, the denominator of is the characteristic polynomial written backwards, : for you get . A root of the characteristic equation corresponds to a factor .

Counting with generating functions

To count selections, build one factor per item type: the factor lists the allowed numbers of copies as powers of . The product's coefficient of counts the ways to select items in total, because multiplying the series adds exponents. Copies within a type are identical and the order of selection does not matter (multisets); equivalently, the coefficient counts ways to put identical objects into distinct boxes, one box per factor.

Restriction on a variable

Factor

any

even

odd

a multiple of ()

NON-NEGATIVE INTEGER SOLUTIONS

For ; the coefficient of in , i.e. identical objects into distinct boxes. With every the count is (for ; zero if ).

WORKED EXAMPLE

Find the number of solutions of in non-negative even integers.

Solution
  1. Each variable is even, so each factor is and the product is .

  2. Put : the coefficient of is the coefficient of in .

  3. That is .

Answer: 28

EXAM TIP

For an upper bound use and expand the numerator by the binomial theorem; the terms beyond the target degree can be ignored. Also: always state which variable ranges over which values before writing the factor.

PREVIOUS-YEAR QUESTIONS

3 questions from the CS papers

Try each question first, then open the answer. We placed each question under a topic ourselves; the exam paper does not label topics.

See these papers in full: GATE CSE 2023 · GATE CSE 2022 · GATE CSE 2018

GATE 2023 CS Q.15

GATE 2023 · CSQ.15 · 1 mark · MCQ

The Lucas sequence is defined by the recurrence relation: , for , with and .

Which one of the options given is TRUE?

  1. (A)

  2. (B)

  3. (C)

  4. (D)

Answer and solution
See the question in the paper
GATE 2023 CS Q.15: answer and solution

PRACTICE

5 questions to test yourself

PRACTICE 12 marks · NAT

Let be the number of binary strings of length that do not contain three consecutive 0s, with . Find .

Answer and solution

Answer:

  1. For , split a valid string by its ending: 1, 10 or 100, each after any valid string of length , or . (Ending 000 is not allowed.)

  2. So for , with , , .

  3. (only 000 is excluded), , , .

PRACTICE 22 marks · MCQ

The sequence satisfies for with and . Which is the closed form for ?

  1. (A)

  2. (B)

  3. (C)

  4. (D)

Answer and solution

Answer: (B)

  1. Characteristic equation , a double root 3.

  2. . .

  3. gives .

  4. . Check and .

PRACTICE 32 marks · MSQ

Let be an ordinary generating function. Which of the following statements are correct?

  1. (A)

    The coefficient of in is .

  2. (B)

    is the generating function of .

  3. (C)

    is the generating function of the constant sequence .

  4. (D)

    The coefficient of in is 15.

Answer and solution

Answer: (A), (B), (D)

  1. A: true, gives .

  2. B: true, it is times the series of A.

  3. C: false, , giving .

  4. D: true, .

PRACTICE 42 marks · NAT

How many solutions does have, where each is an integer with ?

Answer and solution

Answer:

  1. Coefficient of in .

  2. ; the term does not reach degree 10.

  3. Count .

  4. Check by symmetry: gives , and then automatically, so .

PRACTICE 51 mark · MCQ

Let and for . What is ?

  1. (A)

    2185

  2. (B)

    2187

  3. (C)

    1457

  4. (D)

    2191

Answer and solution

Answer: (A)

  1. Homogeneous solution ; the constant 4 gives a particular solution with , so .

  2. and gives , so .

  3. . Check: from both the formula and the recurrence.

OFFICIAL GATE 2027 SYLLABUS WORDING

Copied from the GATE 2027 syllabus; see all 2027 syllabi.

GATE CS

recurrence relations, generating functions

Preparing in Kolkata? IAE Academy's course for GATE CSE.