Skip to main content

Recurrence Relations

A recurrence relation defines each term of a sequence from previous terms. It is a natural language for processes that evolve step by step: population models, recursive algorithms, dynamic programming, and counting strings with constraints.

Solving a recurrence means replacing the step-by-step definition with a closed form or at least a sharp growth estimate. Modeling and solving are different skills. The modeling step explains why the terms satisfy the recurrence; the solving step uses iteration, characteristic equations, generating functions, recursion trees, or asymptotic methods.

Definitions

A recurrence relation for a sequence {an}\{a_n\} is an equation that expresses ana_n in terms of earlier terms. Initial conditions specify enough starting values to determine the sequence uniquely.

A recurrence is linear homogeneous with constant coefficients if it has the form

an=c1an1+c2an2++ckank.a_n=c_1a_{n-1}+c_2a_{n-2}+\cdots+c_ka_{n-k}.

It is nonhomogeneous if an extra term depending on nn appears:

an=c1an1++ckank+F(n).a_n=c_1a_{n-1}+\cdots+c_ka_{n-k}+F(n).

A divide-and-conquer recurrence often has the form

T(n)=aT(n/b)+g(n),T(n)=aT(n/b)+g(n),

where aa subproblems of size n/bn/b are solved and g(n)g(n) additional work is done to split or combine.

A closed form expresses ana_n directly in terms of nn without referring to earlier terms. An asymptotic solution describes growth, such as T(n)=Θ(nlogn)T(n)=\Theta(n\log n), even if no exact closed form is needed.

Key results

For the first-order geometric recurrence

an=can1,a0=A,a_n=ca_{n-1},\qquad a_0=A,

iteration gives an=Acna_n=Ac^n.

For the arithmetic recurrence

an=an1+d,a0=A,a_n=a_{n-1}+d,\qquad a_0=A,

iteration gives an=A+nda_n=A+nd.

For a second-order linear homogeneous recurrence

an=c1an1+c2an2,a_n=c_1a_{n-1}+c_2a_{n-2},

try an=rna_n=r^n. This yields the characteristic equation

r2c1rc2=0.r^2-c_1r-c_2=0.

If it has distinct roots r1,r2r_1,r_2, then

an=αr1n+βr2na_n=\alpha r_1^n+\beta r_2^n

for constants determined by the initial conditions. If there is a repeated root rr, then

an=αrn+βnrn.a_n=\alpha r^n+\beta n r^n.

For merge sort,

T(n)=2T(n/2)+cnT(n)=2T(n/2)+cn

solves to Θ(nlogn)\Theta(n\log n) because each recursion level costs Θ(n)\Theta(n) and there are log2n\log_2 n levels. More generally, recursion trees expose whether cost is concentrated near the root, evenly across levels, or near the leaves.

The master theorem for many recurrences T(n)=aT(n/b)+g(n)T(n)=aT(n/b)+g(n) compares g(n)g(n) with nlogban^{\log_b a}. It is powerful but not universal; it requires regularity conditions and does not handle every recurrence.

Visual

RecurrenceMethodResult pattern
an=can1a_n=ca_{n-1}iterationexponential or constant
an=an1+da_n=a_{n-1}+dtelescopinglinear
an=an1+an2a_n=a_{n-1}+a_{n-2}characteristic equationFibonacci-type
T(n)=2T(n/2)+cnT(n)=2T(n/2)+cnrecursion treeΘ(nlogn)\Theta(n\log n)
an=an1+F(n)a_n=a_{n-1}+F(n)summationaccumulated forcing term

Worked example 1: Solve a linear recurrence

Problem. Solve

an=5an16an2,a0=2,a1=5.a_n=5a_{n-1}-6a_{n-2},\qquad a_0=2,\quad a_1=5.

Method.

  1. Try an=rna_n=r^n:
rn=5rn16rn2.r^n=5r^{n-1}-6r^{n-2}.
  1. Divide by rn2r^{n-2} for r0r\ne0:
r2=5r6.r^2=5r-6.
  1. The characteristic equation is
r25r+6=0=(r2)(r3).r^2-5r+6=0=(r-2)(r-3).
  1. Distinct roots give
an=α2n+β3n.a_n=\alpha2^n+\beta3^n.
  1. Use a0=2a_0=2:
α+β=2.\alpha+\beta=2.
  1. Use a1=5a_1=5:
2α+3β=5.2\alpha+3\beta=5.
  1. Subtract twice the first equation from the second:
β=1.\beta=1.

Then α=1\alpha=1.

Checked answer. The solution is

an=2n+3n.a_n=2^n+3^n.

Checking: a0=1+1=2a_0=1+1=2, a1=2+3=5a_1=2+3=5, and 5(2n1+3n1)6(2n2+3n2)=2n+3n5(2^{n-1}+3^{n-1})-6(2^{n-2}+3^{n-2})=2^n+3^n.

Worked example 2: Count bit strings with no consecutive zeros

Problem. Let ana_n be the number of bit strings of length nn with no consecutive zeros. Find a recurrence and compute a5a_5.

Method.

  1. Separate valid strings by their ending.
  2. If a valid string of length nn ends in 11, then the first n1n-1 bits can be any valid string of length n1n-1. This contributes an1a_{n-1} strings.
  3. If it ends in 00, then the previous bit must be 11, so the string ends in 1010. The first n2n-2 bits can be any valid string of length n2n-2. This contributes an2a_{n-2} strings.
  4. Therefore
an=an1+an2(n2).a_n=a_{n-1}+a_{n-2}\quad(n\ge2).
  1. Initial values:
a0=1a_0=1

for the empty string, and

a1=2a_1=2

for strings 00 and 11.

  1. Compute:
a2=a1+a0=3,a3=a2+a1=5,a4=a3+a2=8,a5=a4+a3=13.\begin{aligned} a_2&=a_1+a_0=3,\\ a_3&=a_2+a_1=5,\\ a_4&=a_3+a_2=8,\\ a_5&=a_4+a_3=13. \end{aligned}

Checked answer. There are 1313 length-55 bit strings with no consecutive zeros. A brute-force listing confirms this count.

Code

def no_consecutive_zeros(n):
if n == 0:
return 1
if n == 1:
return 2
prev2, prev1 = 1, 2
for _ in range(2, n + 1):
prev2, prev1 = prev1, prev1 + prev2
return prev1

def solve_second_order(a0, a1, c1, c2, nmax):
seq = [a0, a1]
for n in range(2, nmax + 1):
seq.append(c1 * seq[n - 1] + c2 * seq[n - 2])
return seq

print([no_consecutive_zeros(n) for n in range(8)])
print(solve_second_order(2, 5, 5, -6, 8))

The second function numerically checks the recurrence from the first worked example, producing values of 2n+3n2^n+3^n.

Common pitfalls

  • Giving a recurrence without enough initial conditions. A second-order recurrence usually needs two starting values.
  • Solving a recurrence but not checking the initial conditions.
  • Treating the characteristic-equation method as valid for non-linear recurrences.
  • Forgetting the empty object in counting recurrences. Often a0=1a_0=1 is the clean base case.
  • Miscounting ending cases that overlap. In the bit-string example, "ends in 11" and "ends in 1010" are disjoint and exhaustive.
  • Applying the master theorem outside its hypotheses.

A good recurrence solution has three separate checks: the model, the algebra, and the initial conditions. The model explains why the recurrence counts the intended objects or measures the intended running time. The algebra solves that recurrence. The initial conditions pin down the constants. A closed form that satisfies the recurrence but misses a0a_0 or a1a_1 is not the solution to the original problem.

When deriving counting recurrences, make cases disjoint by using the end, beginning, or first special position. For bit strings with no consecutive zeros, "ends in 11" and "ends in 1010" work because they are disjoint and cover all valid strings. Cases such as "has a 11 somewhere" and "has a 00 somewhere" overlap and cannot simply be added.

Characteristic equations apply to linear homogeneous recurrences with constant coefficients. They do not directly solve an=an12+1a_n=a_{n-1}^2+1, an=nan1a_n=na_{n-1}, or an=an/2+1a_n=a_{\lfloor n/2\rfloor}+1. Those require other methods. Before using the characteristic equation, identify the recurrence type and verify that the coefficients are constant and the recurrence is linear in previous terms.

For nonhomogeneous recurrences, solve the homogeneous part and then find one particular solution. For example, an=2an1+3a_n=2a_{n-1}+3 has homogeneous solution C2nC2^n and a constant particular solution a=3a=-3, giving an=C2n3a_n=C2^n-3. Substituting back is the fastest way to check the guessed particular form.

For algorithmic recurrences, specify the input sizes covered. Many divide-and-conquer formulas are first solved for powers of two and then extended asymptotically. That is acceptable when stated, but exact formulas may differ for non-powers of two because floors and ceilings change subproblem sizes.

Iteration is often the simplest first attempt. For an=3an1+2a_n=3a_{n-1}+2, write an=3(3an2+2)+2=32an2+2(3+1)a_n=3(3a_{n-2}+2)+2=3^2a_{n-2}+2(3+1) and continue until a pattern appears. The pattern can then be proved by induction. This method is especially useful before introducing characteristic equations, because it keeps the meaning of each term visible.

For recurrences that count strings, draw the last few valid objects. If a recurrence predicts a3=5a_3=5 for no consecutive zeros, list 111,110,101,011,010111,110,101,011,010. The list confirms both the count and the decomposition. If the list contains an object in two cases or misses an object, the recurrence needs adjustment.

For dynamic programming recurrences, identify the state carefully. A state should contain enough information to make the next decision without remembering the entire history. For example, counting strings with no consecutive zeros needs to know whether the previous bit was zero if building left to right. Choosing a state that is too small gives wrong transitions; choosing one that is too large makes the algorithm harder than necessary.

When a closed form is proposed, verify it in two places: the initial conditions and the recurrence. Checking only the first few terms can miss a formula that matches early values by coincidence. Checking only the recurrence can miss the constants. A complete verification substitutes the formula into the recurrence and separately checks all required initial values.

In algorithm analysis, decide whether the recurrence is exact or asymptotic. Writing T(n)=2T(n/2)+nT(n)=2T(n/2)+n may ignore floor, ceiling, and base-case details. That is often fine for Θ\Theta bounds, but exact operation counts require a precise domain such as powers of two and a precise base case.

When a recurrence is solved by a table of values, look for both additive and multiplicative patterns. First differences may reveal an arithmetic component; ratios may reveal geometric growth. These observations are not proofs, but they suggest which formal method to try next: telescoping, characteristic equations, or generating functions.

Connections