Recursion, Catalan Numbers & Generating Functions

Study Sheet

Recursion, Catalan Numbers & Generating Functions

Competition counting & probability, Unit 6

Setting Up Recurrences

Concept
The core idea

A recurrence counts a large problem in terms of smaller versions of itself. To build one:

  • Define the sequence precisely. Let ana_n be the number of ways to do the task of “size” nn. Being sloppy here is the #1 source of errors — write a one-sentence definition.
  • Condition on a first (or last) choice. Look at the object and split into cases based on one feature: the first tile, the last step, whether an element is used, the height of the last column, etc.
  • Express each case using smaller aka_k. Each case should leave a strictly smaller sub-problem of the same type, so it is counted by aka_{k} for some k<nk<n.
  • Nail the base cases. Determine a0a_0 (or a1a_1) directly by hand. Count the “empty” object carefully: usually a0=1a_0=1.

The recurrence plus base cases determines every term.

Recursion counts by relating a size-nn problem to smaller ones: tilings, strings with forbidden patterns, and lattice paths all satisfy recurrences, often Fibonacci-like. Catalan numbers count balanced parentheses, non-crossing paths and triangulations. Generating functions package a whole sequence into one power series, where recurrences become algebra.

Example
Worked example — ways to make a total of nn with 1s and 2s

Let ana_n be the number of ordered sequences of 11s and 22s summing to nn (order matters). Condition on the first number in the sequence:

  • If it is a 11, the rest sums to n−1n-1: that is an−1a_{n-1} ways.
  • If it is a 22, the rest sums to n−2n-2: that is an−2a_{n-2} ways.

So an=an−1+an−2a_n=a_{n-1}+a_{n-2}. Base cases: a0=1a_0=1 (the empty sequence) and a1=1a_1=1. This gives 1,1,2,3,5,8,…1,1,2,3,5,8,\dots — the Fibonacci numbers.

Side note
Where it goes wrong

Setting up a recurrence with the wrong initial values. Compute the first two or three terms by hand — a correct recurrence with wrong seeds gives a wrong sequence forever.

Tip

Tips. (1) Prefer conditioning on the first or last element — it usually leaves a clean sub-problem. (2) Always sanity-check by hand-counting a1,a2,a3a_1,a_2,a_3 against your formula. (3) Decide up front whether order matters and whether a0a_0 should be 11 or 00.

Try it
Quick check

Let an=an−1+3a_n=a_{n-1}+3 with a0=2a_0=2. Compute a1a_1, a2a_2, and a3a_3.

Work it on paper before revealing the answer.

Show the answer

Answer: Arithmetic recurrence. The rule an=an−1+3a_n=a_{n-1}+3 adds 33 at every step, starting from a0=2a_0=2. Iterate term by term:

a1=a0+3=2+3=5,a2=a1+3=5+3=8,a3=a2+3=8+3=11.a_1=a_0+3=2+3=5,\quad a_2=a_1+3=5+3=8,\quad a_3=a_2+3=8+3=11.

Thus a1=5, a2=8, a3=11\boxed{a_1=5,\ a_2=8,\ a_3=11}.

Fibonacci & Tiling / Domino Problems

Concept
Fibonacci from tilings

The Fibonacci numbers satisfy

fn=fn−1+fn−2,f1=f2=1,f_n=f_{n-1}+f_{n-2},\qquad f_1=f_2=1,

giving 1,1,2,3,5,8,13,21,34,55,…1,1,2,3,5,8,13,21,34,55,\dots Many tiling problems reduce to this.

Square–domino tilings. Let tnt_n be the number of ways to tile a 1×n1\times n strip using 1×11\times1 squares and 1×21\times2 dominoes. Look at the tile covering the last cell: a square (leaving a 1×(n−1)1\times(n-1) strip) or a domino (leaving a 1×(n−2)1\times(n-2) strip). So tn=tn−1+tn−2t_n=t_{n-1}+t_{n-2} with t0=1, t1=1t_0=1,\ t_1=1, i.e. tn=fn+1t_n=f_{n+1}.

Example
Worked example — tiling a 2×n2\times n board with dominoes
V firstV firstV firstH firstH firstthe 5 domino tilings of a 2 × 4 boardV first: 3 (a 2 × 3 board); H first: 2 (a 2 × 2 board)
Sorted by the leftmost column: a vertical domino leaves a 2×32\times3 board, a stacked pair leaves a 2×22\times2 board.

Let DnD_n be the number of ways to tile a 2×n2\times n board with 1×21\times2 dominoes. Consider the leftmost column:

  • One vertical domino fills the column, leaving a 2×(n−1)2\times(n-1) board: Dn−1D_{n-1} ways.
  • Two horizontal dominoes fill the first two columns, leaving a 2×(n−2)2\times(n-2) board: Dn−2D_{n-2} ways.

Hence Dn=Dn−1+Dn−2D_n=D_{n-1}+D_{n-2}, with D0=1D_0=1, D1=1D_1=1. So Dn=fn+1D_n=f_{n+1}; e.g. a 2×42\times4 board has D4=5D_4=5 tilings.

Side note
Where it goes wrong

Allowing a horizontal domino alone in the first column. If the top-left cell is covered by a horizontal domino, the cell below it cannot be covered by a vertical one (that would overlap) and must also be a horizontal domino, so the two horizontals always come as a stacked pair covering two columns. Counting "horizontal on top, anything below" as a separate case adds tilings that do not exist; the recurrence has exactly the two cases Dn−1D_{n-1} and Dn−2D_{n-2}.

Tip

Tips. The recurrence comes from “what covers the boundary cell?” Different tile sets change the recurrence: squares + dominoes + trominoes on a strip give tn=tn−1+tn−2+tn−3t_n=t_{n-1}+t_{n-2}+t_{n-3}. Watch indexing: confirm which Fibonacci index your base cases land on.

Try it
Quick check

You climb a staircase of nn steps, taking either 11 or 22 steps at a time. Let ana_n be the number of ways to reach the top. Write a recurrence for ana_n and compute a4a_4.

Work it on paper before revealing the answer.

Show the answer

Answer: Staircase. Let ana_n be the number of ways to climb nn steps using 11-step and 22-step moves. Condition on the last move: if it is a single step, the first n−1n-1 steps can be climbed in an−1a_{n-1} ways; if it is a double step, the first n−2n-2 steps can be climbed in an−2a_{n-2} ways. These cases are disjoint and exhaustive, so

an=an−1+an−2.a_n=a_{n-1}+a_{n-2}.

Base cases: a0=1a_0=1 (do nothing) and a1=1a_1=1. Iterate:

a2=1+1=2,a3=2+1=3,a4=3+2=5.a_2=1+1=2,\quad a_3=2+1=3,\quad a_4=3+2=5.

So the recurrence is an=an−1+an−2a_n=a_{n-1}+a_{n-2} and a4=5\boxed{a_4=5}.

State-Based Counting Recursions

Concept
Counting with states

When the next choice depends on the current situation, define one sequence per state and let them recurse together. Steps:

  • Identify the finite information (the “state”) you must remember to extend a valid object by one step.
  • Define an,bn,…a_n,b_n,\dots = number of length-nn objects ending in each state.
  • Write transition equations: each state's next value is a sum over states that may legally precede it.
  • The answer is usually the sum of all states at length nn.

This is the “transfer matrix” / finite-automaton viewpoint.

Example
Worked example — binary strings with no two consecutive 1s
endsin 0endsin 1append 1append 0append 0no 1 → 1
Every legal string is a walk on this diagram: from "ends in 1" the only move is to append 00.

Count length-nn binary strings with no “11”. Track the last bit. Let AnA_n = number ending in 00, BnB_n = number ending in 11.

  • A string ending in 00 can be preceded by anything: An=An−1+Bn−1A_n=A_{n-1}+B_{n-1}.
  • A string ending in 11 must have 00 before it: Bn=An−1B_n=A_{n-1}.

With A1=1,B1=1A_1=1,B_1=1, the total Tn=An+BnT_n=A_n+B_n satisfies Tn=Tn−1+Tn−2T_n=T_{n-1}+T_{n-2} (Fibonacci again!), giving T1=2,T2=3,T3=5,…T_1=2,T_2=3,T_3=5,\dots

Tip

Tips. Keep the state as small as possible — only remember what the next transition needs. If a constraint says “no three in a row,” your state is the length of the current run, so you may need two or three coupled sequences.

Try it
Quick check

How many binary strings of length 55 have no two consecutive 11s?

Work it on paper before revealing the answer.

Show the answer

Answer: Track AnA_n (ending in 00) and BnB_n (ending in 11), with An=An−1+Bn−1A_n = A_{n-1} + B_{n-1} and Bn=An−1B_n = A_{n-1}. From A1=B1=1A_1 = B_1 = 1:

(A2,B2)=(2,1),(A3,B3)=(3,2),(A4,B4)=(5,3),(A5,B5)=(8,5).(A_2,B_2) = (2,1),\quad (A_3,B_3) = (3,2),\quad (A_4,B_4) = (5,3),\quad (A_5,B_5) = (8,5).

The total is 8+5=138 + 5 = \boxed{13}.

Catalan Numbers

Concept
The Catalan numbers
RRRUUURRURUURRUURURURRUURURURUthe 5 paths to (3,3) that never rise above y = x
Read RR as "(" and UU as ")": staying below the diagonal means never closing more brackets than you have opened.

The Catalan numbers are

Cn=1n+1(2nn)=(2nn)−(2nn+1),Cn+1=∑i=0nCi Cn−i.C_n=\frac{1}{n+1}\binom{2n}{n}=\binom{2n}{n}-\binom{2n}{n+1}, \qquad C_{n+1}=\sum_{i=0}^{n}C_i\,C_{n-i}.

First values:

n0123456Cn11251442132\begin{array}{c|ccccccc} n & 0 & 1 & 2 & 3 & 4 & 5 & 6\\\hline C_n & 1 & 1 & 2 & 5 & 14 & 42 & 132 \end{array}

They count an astonishing number of things. Three canonical interpretations:

  • Lattice paths / ballots: monotonic paths from (0,0)(0,0) to (n,n)(n,n) using steps right/up that never cross above the diagonal y=xy=x. Equivalently, sequences of nn +1+1s and nn −1-1s with every partial sum ≥0\ge 0.
  • Balanced parentheses: the number of ways to correctly match nn pairs of parentheses (every prefix has #(\#( ≥\ge #)\#)).
  • Triangulations: the number of ways to cut a convex (n+2)(n+2)-gon into triangles using non-crossing diagonals.

Other appearances: binary trees with nn internal nodes, non-crossing handshakes among 2n2n people, mountain ranges, and stack-sortable permutations.

Example
Worked example — parenthesizations and the convolution

How many ways to fully parenthesize a product of 44 factors, e.g. a⋅b⋅c⋅da\cdot b\cdot c\cdot d? This is C3=5C_3=5:

((ab)c)d,(a(bc))d,(ab)(cd),a((bc)d),a(b(cd)).((ab)c)d,\quad (a(bc))d,\quad (ab)(cd),\quad a((bc)d),\quad a(b(cd)).

Why Cn+1=∑CiCn−iC_{n+1}=\sum C_iC_{n-i}? In a balanced string, the first “(” closes at some point, splitting the string into an inner balanced block (size ii) and a following balanced block (size n−in-i). Summing over the split point gives the convolution. This same split proves the triangulation and binary-tree counts.

Side note
Where it goes wrong

Assuming symmetry splits the paths in half. Of the (63)=20\binom63 = 20 paths to (3,3)(3,3), it is tempting to say "half stay below the diagonal", giving 1010. But most paths cross the diagonal, spending time on both sides, so they belong to neither half. Only 55 stay below. The reflection argument counts the bad ones exactly: (64)=15\binom64 = 15 of them, and 20−15=520 - 15 = 5.

Tip

Tips. If a counting problem involves “never falls behind,” “non-crossing,” or “a valid bracket/matching structure,” suspect Catalan. Memorize C0C_0 through C5C_5: 1,1,2,5,14,421,1,2,5,14,42. The ratio Cn+1/Cn=2(2n+1)n+2C_{n+1}/C_n=\frac{2(2n+1)}{n+2} makes them fast to extend.

Try it
Quick check

In how many ways can a convex hexagon be cut into triangles by non-crossing diagonals?

Work it on paper before revealing the answer.

Show the answer

Answer: A convex (n+2)(n+2)-gon has CnC_n triangulations; a hexagon is n=4n = 4. From the convolution, C4=C0C3+C1C2+C2C1+C3C0=5+2+2+5=14C_4 = C_0C_3 + C_1C_2 + C_2C_1 + C_3C_0 = 5 + 2 + 2 + 5 = 14, which matches 15(84)=705=14\tfrac15\binom84 = \tfrac{70}{5} = 14. The convolution is the triangulation argument itself: the triangle on one fixed side splits the rest into two smaller polygons.   14\boxed{14}

Generating Functions (introduction)

Concept
Series as bookkeeping devices

A (ordinary) generating function packages a sequence a0,a1,a2,…a_0,a_1,a_2,\dots into one formal power series

A(x)=∑n≥0anxn.A(x)=\sum_{n\ge 0}a_n x^n .

The exponent of xx is a “label” tracking a total (size, weight, sum), and the coefficient counts objects with that total. The magic:

choosing independently  ⟷  multiplying generating functions.\textbf{choosing independently} \;\longleftrightarrow\; \textbf{multiplying generating functions.}

If A(x)A(x) counts choices for part 1 (weighted by size) and B(x)B(x) counts part 2, then the product A(x)B(x)A(x)B(x) has xnx^n-coefficient ∑kakbn−k\sum_k a_k b_{n-k} — exactly the number of ways to split total nn between the two parts. Useful building blocks:

11−x=∑n≥0xn,11−xk=∑j≥0xjk,(1+x)m=∑n(mn)xn.\frac{1}{1-x}=\sum_{n\ge0}x^n,\qquad \frac{1}{1-x^k}=\sum_{j\ge0}x^{jk},\qquad (1+x)^m=\sum_{n}\binom{m}{n}x^n .
Example
Worked example — coins as a product of GFs

In how many ways can you make nn cents using pennies (1c), nickels (5c), and dimes (10c), unlimited supply, order irrelevant? Each coin type contributes a geometric factor tracking how much value it supplies:

G(x)=11−x⏟pennies⋅11−x5⏟nickels⋅11−x10⏟dimes.G(x)=\underbrace{\frac{1}{1-x}}_{\text{pennies}}\cdot\underbrace{\frac{1}{1-x^{5}}}_{\text{nickels}}\cdot\underbrace{\frac{1}{1-x^{10}}}_{\text{dimes}} .

The coefficient of xnx^{n} in G(x)G(x) is the answer. For instance [x10]G(x)=4[x^{10}]G(x)=4: 1010 pennies; 5+55{+}5 (nickels); a nickel +5+5 pennies; one dime. Products of GFs automatically handle “distribute the total among independent parts.”

Tip

Tips. Treat xx as a formal symbol — convergence does not matter. “Choose one of several” becomes a sum of terms; “do several things independently” becomes a product of series. To read off a coefficient, use 1(1−x)k=∑n(n+k−1k−1)xn\frac{1}{(1-x)^{k}}=\sum_n\binom{n+k-1}{k-1}x^n or partial fractions.

Try it
Quick check

Find the coefficient of x5x^5 in 1(1−x)(1−x2)\dfrac{1}{(1-x)(1-x^2)}, and say what it counts.

Work it on paper before revealing the answer.

Show the answer

Answer: The factor 11−x2=1+x2+x4+⋯\tfrac1{1-x^2} = 1 + x^2 + x^4 + \cdots chooses how much of the total comes from 22s (00, 22 or 44); the factor 11−x\tfrac1{1-x} fills the rest with 11s in exactly one way. So the coefficient is 33, counting the ways to write 55 as an unordered sum of 11s and 22s: 1+1+1+1+11+1+1+1+1, 2+1+1+12+1+1+1, 2+2+12+2+1.   3\boxed{3}

Solving Simple Linear Recurrences

Concept
The characteristic-equation method

For a linear recurrence with constant coefficients

an=c1an−1+c2an−2,a_n=c_1a_{n-1}+c_2a_{n-2},

guess an=rna_n=r^n. Substituting and dividing by rn−2r^{n-2} gives the characteristic equation

r2=c1r+c2.r^2=c_1 r + c_2 .
  • Distinct roots r1≠r2r_1\ne r_2: the general solution is an=α r1 n+β r2 na_n=\alpha\, r_1^{\,n}+\beta\, r_2^{\,n}.
  • Repeated root rr: use an=(α+βn) r na_n=(\alpha+\beta n)\,r^{\,n}.

Fix α,β\alpha,\beta from two initial values. The same idea extends to order kk: a degree-kk characteristic polynomial with kk roots.

Example
Worked example — solving an=5an−1−6an−2a_n=5a_{n-1}-6a_{n-2}

Given a0=0, a1=1a_0=0,\ a_1=1. Characteristic equation: r2=5r−6r^2=5r-6, i.e. r2−5r+6=0r^2-5r+6=0, so (r−2)(r−3)=0(r-2)(r-3)=0 and r=2,3r=2,3. Thus

an=α 2n+β 3n.a_n=\alpha\, 2^{n}+\beta\, 3^{n}.

Initial conditions: a0=α+β=0a_0=\alpha+\beta=0 and a1=2α+3β=1a_1=2\alpha+3\beta=1. Solving, β=1, α=−1\beta=1,\ \alpha=-1, so

an=3n−2n.a_n=3^{n}-2^{n}.

Check: a2=9−4=5=5(1)−6(0)a_2=9-4=5=5(1)-6(0). ✓

Tip

Tips. Bring everything to one side so the characteristic polynomial is clear. Complex roots are fine — they produce oscillating solutions. For a nonhomogeneous recurrence (an=c1an−1+⋯+g(n)a_n=c_1a_{n-1}+\dots+g(n)), add a particular solution (try a constant for constant gg, a linear guess for linear gg) to the homogeneous solution.

Try it
Quick check

Solve an=an−1+6an−2a_n = a_{n-1} + 6a_{n-2} with a0=2a_0 = 2, a1=1a_1 = 1.

Work it on paper before revealing the answer.

Show the answer

Answer: Characteristic equation r2=r+6r^2 = r + 6, i.e. (r−3)(r+2)=0(r-3)(r+2) = 0, so an=α3n+β(−2)na_n = \alpha 3^n + \beta(-2)^n. Then α+β=2\alpha + \beta = 2 and 3α−2β=13\alpha - 2\beta = 1, giving α=1\alpha = 1, β=1\beta = 1:

an=3n+(−2)n.a_n = 3^n + (-2)^n.

Check: a2=9+4=13a_2 = 9 + 4 = 13 and a1+6a0=1+12=13a_1 + 6a_0 = 1 + 12 = 13.

Going Deeper: Closed Forms and Proofs

Concept
Three derivations worth knowing
(4,4)(3,5)first touchy = x + 1
A bad path (solid) and its tail reflected in y=x+1y = x+1 (dashed): the reflected path ends at (n−1,n+1)=(3,5)(n-1, n+1) = (3,5).

(A) The Catalan closed form by reflection. Count lattice paths from (0,0)(0,0) to (n,n)(n,n) (steps R and U) that stay weakly below the diagonal. Total paths: (2nn)\binom{2n}{n}. A bad path touches the line y=x+1y=x+1. Reflect the portion of a bad path after its first touch across y=x+1y=x+1: this bijects bad paths with all paths from (0,0)(0,0) to (n−1,n+1)(n-1,n+1), of which there are (2nn+1)\binom{2n}{n+1}. Hence

Cn=(2nn)−(2nn+1)=1n+1(2nn).C_n=\binom{2n}{n}-\binom{2n}{n+1}=\frac{1}{n+1}\binom{2n}{n}.

(B) Solving a recurrence with a generating function. Let C(x)=∑n≥0CnxnC(x)=\sum_{n\ge0}C_nx^n. The convolution Cn+1=∑iCiCn−iC_{n+1}=\sum_i C_iC_{n-i} says C(x)=1+xC(x)2C(x)=1+xC(x)^2. Solving the quadratic,

C(x)=1−1−4x2x,C(x)=\frac{1-\sqrt{1-4x}}{2x},

and expanding 1−4x\sqrt{1-4x} by the binomial series recovers Cn=1n+1(2nn)C_n=\frac1{n+1}\binom{2n}{n}. This “turn the recurrence into an equation for A(x)A(x), solve, expand” pipeline is the general method.

(C) Binet's formula for Fibonacci. The characteristic equation r2=r+1r^2=r+1 has roots φ=1+52\varphi=\frac{1+\sqrt5}{2} and ψ=1−52\psi=\frac{1-\sqrt5}{2}. Then

fn=φ n−ψ n5.f_n=\frac{\varphi^{\,n}-\psi^{\,n}}{\sqrt5}.

Since ∣ψ∣<1|\psi|<1, fnf_n is the nearest integer to φ n/5\varphi^{\,n}/\sqrt5, so Fibonacci numbers grow like φ n\varphi^{\,n}.

Example
Worked example — coefficients from 11−x−x2\dfrac{1}{1-x-x^2}

The Fibonacci GF is F(x)=∑fnxn=x1−x−x2F(x)=\sum f_nx^n=\dfrac{x}{1-x-x^2}. Factor the denominator using φ,ψ\varphi,\psi and split by partial fractions:

F(x)=15 ⁣(11−φx−11−ψx)=15∑n≥0(φ n−ψ n)xn,F(x)=\frac{1}{\sqrt5}\!\left(\frac{1}{1-\varphi x}-\frac{1}{1-\psi x}\right) =\frac{1}{\sqrt5}\sum_{n\ge0}\bigl(\varphi^{\,n}-\psi^{\,n}\bigr)x^n,

which reads off Binet's formula directly. Partial fractions on 1/(1−x−x2)1/(1-x-x^2)-type denominators is the bridge from recurrence to closed form.

Tip

Big picture. Recursion, Catalan numbers, and generating functions are one connected toolkit. Recursion is how you discover a count by conditioning on a first/last choice or a state. Generating functions are how you package and solve those recurrences — products encode independent choices, and solving an algebraic equation for A(x)A(x) yields closed forms. Catalan numbers are the flagship example where all three views (a convolution recurrence, a reflection/bijection argument, and a GF quadratic) meet in one answer. When you see “count in terms of smaller cases,” set up ana_n; when you see “combine independent totals,” multiply GFs; when you see “non-crossing / balanced / never-behind,” reach for Catalan.

Try it
Quick check

How many ways are there to tile a 2×52\times 5 board completely with 1×21\times2 dominoes?

Work it on paper before revealing the answer.

Show the answer

Answer: Tiling a 2×52\times5 board with dominoes. Let DnD_n count domino tilings of a 2×n2\times n board. Consider the leftmost column: it is covered either by one vertical domino (leaving a 2×(n−1)2\times(n-1) board) or by the left halves of two horizontal dominoes stacked (leaving a 2×(n−2)2\times(n-2) board). Hence Dn=Dn−1+Dn−2D_n=D_{n-1}+D_{n-2} with D0=1, D1=1D_0=1,\ D_1=1:

D2=2, D3=3, D4=5, D5=8.D_2=2,\ D_3=3,\ D_4=5,\ D_5=8.

So 8\boxed{8}.

Nonhomogeneous & Higher-Order Linear Recurrences

Concept
Particular ++ homogeneous, with resonance

A linear recurrence with a forcing term,

an=c1an−1+⋯+ckan−k+g(n),a_n=c_1a_{n-1}+\dots+c_ka_{n-k}+g(n),

is solved by

an  =  an(h)  +  an(p),a_n \;=\; a_n^{(h)} \;+\; a_n^{(p)},

where an(h)a_n^{(h)} is the general solution of the homogeneous part (characteristic roots, as before) and an(p)a_n^{(p)} is any one particular solution. To find an(p)a_n^{(p)}, guess a form matching g(n)g(n) and solve for its constants:

g(n)try an(p)=constantApolynomial of degree dAdnd+⋯+A1n+A0exponential b nA b n\begin{array}{c|c} g(n) & \text{try } a_n^{(p)}=\\\hline \text{constant} & A\\ \text{polynomial of degree } d & A_dn^d+\dots+A_1n+A_0\\ \text{exponential } b^{\,n} & A\,b^{\,n}\\ \end{array}

Resonance (the crucial trap). If your guess already solves the homogeneous equation — e.g. g(n)=b ng(n)=b^{\,n} but bb is a characteristic root, or g(n)g(n) is constant but r=1r=1 is a root — multiply the guess by nn (by nmn^m if bb is a root of multiplicity mm). This is the same “multiply by nn for a repeated root” rule seen in the homogeneous case.

Example
Worked example — a nonhomogeneous recurrence with resonance

Solve an=3an−1−2an−2+2 na_n=3a_{n-1}-2a_{n-2}+2^{\,n} with a0=0, a1=0a_0=0,\ a_1=0.

Homogeneous part: r2−3r+2=(r−1)(r−2)=0r^2-3r+2=(r-1)(r-2)=0, so r=1,2r=1,2 and an(h)=α⋅1 n+β⋅2 na_n^{(h)}=\alpha\cdot1^{\,n}+\beta\cdot2^{\,n}.

Particular part: the natural guess A 2 nA\,2^{\,n} is resonant because r=2r=2 is already a root. Multiply by nn: try an(p)=A n 2 na_n^{(p)}=A\,n\,2^{\,n}. Substituting,

An2n=3A(n−1)2n−1−2A(n−2)2n−2+2n.\begin{aligned} A n 2^{n} &= 3A(n-1)2^{n-1}-2A(n-2)2^{n-2}+2^{n}. \end{aligned}

Divide by 2n−22^{n-2}: 4An=6A(n−1)−2A(n−2)+44An = 6A(n-1)-2A(n-2)+4, i.e. 4An=4An−2A+44An=4An-2A+4, giving A=2A=2. So an(p)=2n 2 n=n 2 n+1a_n^{(p)}=2n\,2^{\,n}=n\,2^{\,n+1}.

Combine and fit: an=α+β 2 n+n 2 n+1a_n=\alpha+\beta\,2^{\,n}+n\,2^{\,n+1}. From a0=α+β=0a_0=\alpha+\beta=0 and a1=α+2β+4=0a_1=\alpha+2\beta+4=0 we get β=−4, α=4\beta=-4,\ \alpha=4. Thus

an=4−4⋅2 n+n 2 n+1=4−2 n+2+n 2 n+1.a_n=4-4\cdot2^{\,n}+n\,2^{\,n+1}=4-2^{\,n+2}+n\,2^{\,n+1}.

Check: a2=4−16+16=4a_2=4-16+16=4, and 3a1−2a0+22=0−0+4=43a_1-2a_0+2^2=0-0+4=4. ✓

Tip

Tips. (1) Always solve the homogeneous roots first — you cannot detect resonance until you know them. (2) A constant forcing term with a root r=1r=1 needs the guess AnAn (not AA). (3) If g(n)g(n) is a sum of pieces, find a particular solution for each piece separately and add. (4) Fit the constants α,β\alpha,\beta using the full solution an(h)+an(p)a_n^{(h)}+a_n^{(p)}, never the homogeneous part alone.

Try it
Quick check

Solve an=2an−1+1a_n = 2a_{n-1} + 1 with a0=0a_0 = 0.

Work it on paper before revealing the answer.

Show the answer

Answer: Homogeneous part: r=2r = 2, so an(h)=β2na_n^{(h)} = \beta 2^n. The forcing term is a constant and 11 is not a root, so try an(p)=Aa_n^{(p)} = A: A=2A+1A = 2A + 1 gives A=−1A = -1. Then an=β2n−1a_n = \beta 2^n - 1, and a0=0a_0 = 0 gives β=1\beta = 1:

an=2n−1.a_n = 2^n - 1.

Check: a1=1a_1 = 1, a2=3a_2 = 3, a3=7a_3 = 7, and 2⋅3+1=72\cdot 3 + 1 = 7. (This counts the moves in the Tower of Hanoi.)

The Transfer-Matrix Method

Concept
Recurrences as matrix powers

A coupled linear recurrence among finitely many states is one matrix TT acting on a state vector. If vn\mathbf{v}_n collects the state values at step nn and vn=Tvn−1\mathbf{v}_n=T\mathbf{v}_{n-1}, then

vn=T nv0.\mathbf{v}_n=T^{\,n}\mathbf{v}_0 .

For the Fibonacci recurrence, taking vn=[fn+1fn]\mathbf{v}_n=\begin{bmatrix}f_{n+1}\\ f_n\end{bmatrix},

[fn+1fn]=[1110][fnfn−1],[fn+1fnfnfn−1]=[1110] n.\begin{bmatrix}f_{n+1}\\ f_n\end{bmatrix} =\begin{bmatrix}1 & 1\\ 1 & 0\end{bmatrix} \begin{bmatrix}f_n\\ f_{n-1}\end{bmatrix}, \qquad \begin{bmatrix}f_{n+1} & f_n\\ f_n & f_{n-1}\end{bmatrix} =\begin{bmatrix}1 & 1\\ 1 & 0\end{bmatrix}^{\,n}.

The matrix's eigenvalues are exactly the characteristic roots φ,ψ\varphi,\psi, which is why T nT^{\,n} (hence every entry) is a combination of φ n\varphi^{\,n} and ψ n\psi^{\,n} — another route to Binet's formula. Taking determinants of the boxed identity gives fn+1fn−1−fn2=(−1)nf_{n+1}f_{n-1}-f_n^2=(-1)^n instantly (Cassini's identity).

Counting walks. If MM is the adjacency matrix of a (multi)graph, then (M n)ij(M^{\,n})_{ij} counts walks of length nn from vertex ii to jj, and tr⁡(M n)=∑kλk n\operatorname{tr}(M^{\,n})=\sum_k\lambda_k^{\,n} counts closed walks. This packages “count paths through states” as matrix powers.

Example
Worked example — walks on a triangle

Three vertices {1,2,3}\{1,2,3\} of a triangle are all mutually adjacent. How many closed walks of length nn start and end at vertex 11? The adjacency matrix is M=J−IM=J-I where JJ is all-ones:

M=[011101110].M=\begin{bmatrix}0&1&1\\ 1&0&1\\ 1&1&0\end{bmatrix}.

Its eigenvalues: JJ has eigenvalues 3,0,03,0,0, so M=J−IM=J-I has eigenvalues 2,−1,−12,-1,-1. By symmetry every diagonal entry of M nM^{\,n} is equal, and tr⁡(M n)=2 n+2(−1) n\operatorname{tr}(M^{\,n})=2^{\,n}+2(-1)^{\,n}, so

(M n)11=2 n+2(−1) n3.(M^{\,n})_{11}=\frac{2^{\,n}+2(-1)^{\,n}}{3}.

Check n=2n=2: closed walks 1→k→11\to k\to1 number 22, and 4+23=2\tfrac{4+2}{3}=2. ✓ For n=3n=3 we get 8−23=2\tfrac{8-2}{3}=2, the two directed triangles 1→2→3→11\to2\to3\to1 and 1→3→2→11\to3\to2\to1. This is the standard AIME-style “ant walks on a solid/graph and returns home” setup.

Tip

Tips. (1) Order-kk scalar recurrences become k×kk\times k companion matrices whose characteristic polynomial is the recurrence's. (2) For symmetric transition structures, diagonalizing via eigenvalues turns T nT^{\,n} into a clean closed form — look for an all-ones or circulant pattern. (3) tr⁡(M n)=∑λ n\operatorname{tr}(M^{\,n})=\sum\lambda^{\,n} and det⁡\det identities (Cassini) fall out for free. (4) On a highly symmetric graph, “lump” equivalent vertices into one state to shrink the matrix.

The Cycle Lemma & the Ballot Problem

Concept
Rotation counting and the general ballot theorem

The reflection principle is one proof of the Catalan/ballot counts; the cycle lemma is a slicker, purely combinatorial one.

Cycle Lemma (Dvoretzky–Motzkin). Take any sequence of nn steps that are +1+1's and −1-1's summing to +1+1 (so n+12\frac{n+1}{2} ups and n−12\frac{n-1}{2} downs). Among its nn cyclic rotations, exactly one has all partial sums strictly positive.

Ballot Theorem. In an election where AA gets aa votes and BB gets b<ab<a votes, the number of orderings of the ballots in which AA is strictly ahead throughout is

a−ba+b(a+ba).\frac{a-b}{a+b}\binom{a+b}{a}.

Catalan is the boundary case a=n+1, b=na=n+1,\ b=n: an ordering in which AA stays strictly ahead must open with an AA vote, and the remaining 2n2n votes (nn each) form a path to (n,n)(n,n) that never rises above the diagonal. The ballot formula gives 12n+1(2n+1n+1)=1n+1(2nn)=Cn\frac{1}{2n+1}\binom{2n+1}{n+1}=\frac{1}{n+1}\binom{2n}{n}=C_n.

Example
Worked example — counting good arrangements by rotation

In how many ways can nn people holding a $5 bill and nn people holding a $10 bill line up at a box office that sells $5 tickets and starts with an empty till, so that the cashier can always give change?

Model each $5 as +1+1 and each $10 as −1-1. The line works exactly when every partial sum is ≥0\ge 0. Now put an imaginary extra $5 customer at the front. The new sequence has n+1n+1 steps +1+1 and nn steps −1-1, total +1+1, and its partial sums are the old ones plus 11: the line works exactly when every partial sum of the new sequence is strictly positive. Conversely, a sequence of this kind with all partial sums positive must start with +1+1, and deleting that first step gives a working line.

There are (2n+1n)\binom{2n+1}{n} sequences with n+1n+1 ups and nn downs. Their 2n+12n+1 rotations are all different (a sequence built from a repeated block would have a total divisible by the number of blocks, and the total is 11), so they fall into 12n+1(2n+1n)\tfrac{1}{2n+1}\binom{2n+1}{n} rotation classes. By the cycle lemma each class contains exactly one sequence with all partial sums positive. Hence

#{working lines}=12n+1(2n+1n)=Cn.\#\{\text{working lines}\}=\frac{1}{2n+1}\binom{2n+1}{n}=C_n .

For n=2n=2: 15(52)=2\tfrac15\binom52 = 2, and indeed only $5 $5 $10 $10 and $5 $10 $5 $10 work. ✓

Tip

Tips. (1) The cycle lemma also proves the Fuss–Catalan numbers 1kn+1(kn+1n)\frac{1}{kn+1}\binom{kn+1}{n} (paths with up-steps +1+1 and down-steps −(k−1)-(k-1)) — same one-good-rotation idea. (2) “Strictly ahead” vs. “never behind” shifts an index by one; recount a tiny case to pin it down. (3) When a problem says “running total never goes negative” and you can cyclically shift without changing the count, reach for the cycle lemma instead of reflection.

Exponential Generating Functions

Concept
EGFs for labeled structures

When objects are built on labeled elements (people, positions 1..n1..n that are distinguishable), the right bookkeeping tool is the exponential generating function

A^(x)=∑n≥0anxnn!.\widehat{A}(x)=\sum_{n\ge0}a_n\frac{x^n}{n!}.

The key rule mirrors the ordinary case but with a binomial-flavored product:

A^(x) B^(x)=∑n≥0(∑k(nk)ak bn−k)xnn!.\widehat{A}(x)\,\widehat{B}(x)=\sum_{n\ge0}\left(\sum_{k}\binom{n}{k}a_k\,b_{n-k}\right)\frac{x^n}{n!}.

The (nk)\binom{n}{k} appears because you first choose which labels go to the AA-part. Building blocks:

ex=∑n≥0xnn! (one labeled set),11−x=∑n≥0n! xnn! (a linear order).e^{x}=\sum_{n\ge0}\frac{x^n}{n!}\ (\text{one labeled set}),\qquad \frac{1}{1-x}=\sum_{n\ge0}n!\,\frac{x^n}{n!}\ (\text{a linear order}).

The Exponential Formula A^(x)=eC^(x)\widehat{A}(x)=e^{\widehat{C}(x)} passes from “connected” pieces C^\widehat{C} to arbitrary disjoint unions of them (set partitions into blocks, permutations into cycles, graphs into components).

Example
Worked example — derangements via an EGF

A derangement is a permutation with no fixed point; let DnD_n be their count. Every permutation splits its nn labels into a set of fixed points and a derangement of the rest:

n!=∑k=0n(nk)Dn−k⋅1.n!=\sum_{k=0}^{n}\binom{n}{k}D_{n-k}\cdot 1 .

In EGF language this is a product: (all permutations) == (choose fixed points) ×\times (derange the rest), i.e.

11−x=ex⋅D^(x)⟹D^(x)=e−x1−x.\frac{1}{1-x}=e^{x}\cdot \widehat{D}(x) \quad\Longrightarrow\quad \widehat{D}(x)=\frac{e^{-x}}{1-x}.

Reading off the coefficient of xn/n!x^n/n!,

Dn=n!∑k=0n(−1)kk!,D_n=n!\sum_{k=0}^{n}\frac{(-1)^k}{k!},

the familiar inclusion–exclusion formula, and DnD_n is the nearest integer to n!/en!/e. From D^(x)=e−x/(1−x)\widehat{D}(x)=e^{-x}/(1-x) one also reads the recurrences Dn=nDn−1+(−1)nD_n=nD_{n-1}+(-1)^n and Dn=(n−1)(Dn−1+Dn−2)D_n=(n-1)(D_{n-1}+D_{n-2}).

Tip

Big picture. Use an ordinary GF ∑anxn\sum a_nx^n when parts combine by “split the total” (unlabeled: coins, compositions, tilings); use an exponential GF ∑anxn/n!\sum a_n x^n/n! when parts combine by “distribute distinct labels” (labeled: permutations, set partitions, surjections). The dictionary — product == independent combination, eC^=e^{\widehat{C}}= disjoint components — lets you write the answer's generating function directly from a structural description, then extract ana_n by expanding.

Why It Works

Why Cn=1n+1(2nn)C_n = \tfrac{1}{n+1}\binom{2n}{n}: paths from (0,0)(0,0) to (n,n)(n,n) that never cross above the diagonal are all (2nn)\binom{2n}{n} paths minus the bad ones, and reflecting a bad path after its first illegal step gives a bijection with paths to (n−1,n+1)(n-1, n+1), of which there are (2nn−1)\binom{2n}{n-1}. The difference simplifies to the formula. Generating functions work because multiplying series adds exponents — a convolution — which is exactly the structure of "choose a first part, then the rest".

Tip
Reading a proof

Do not read a proof the way you read a rule. Cover the last line, ask what the previous line forces, and only then uncover it. If a step feels like magic, that is the step to work by hand with a concrete number.

Formulas, Proofs & Tips

Tip
Recursions and Catalan numbers
an=an−1+an−2 (Fibonacci),Cn=1n+1(2nn)a_n=a_{n-1}+a_{n-2} \ (\text{Fibonacci}),\qquad C_n=\frac{1}{n+1}\binom{2n}{n}

What it means. Define a term from earlier terms; Catalan numbers count balanced structures.

Example. Fibonacci: 1,1,2,3,5,8,…1,1,2,3,5,8,\dots; and C3=14(63)=5C_3=\tfrac14\binom63=5.

Why it works. Fibonacci-style counts split on the last step: a tiling ending in a single square leaves an−1a_{n-1} ways, one ending in a domino leaves an−2a_{n-2}, and the cases do not overlap. Catalan counts paths that never dip below the axis — the reflection argument removes exactly (2nn+1)\binom{2n}{n+1} bad paths.

Tip. Always state the base cases; a recursion without them defines nothing.