5 Recurrence Relations And Recursion

Discrete Structure ยท Unit 5

Recurrence Relations and Recursion

Exam-focused notes for Recurrence Relations and Recursion (Discrete Structure, BIT152): what the TU syllabus asks and how it has actually been tested, with 6 solved past questions from this unit.

Recurrence Relations and Recursion in short

A recurrence relation defines each term of a sequence using earlier terms. Recurrences appear in recursive programs, in counting problems and in finding the running time of algorithms.

Recurrence Relations and Recursion: notes

Recursively defined functions

A recursive definition has two parts: initial conditions that give the first values, and a recursive rule that builds later values from earlier ones.

Example. Factorial: $0! = 1$ and $n! = n \cdot (n-1)!$ for $n \ge 1$. So $3! = 3 \cdot 2 \cdot 1 = 6$.

Example. Define $f(0) = 3$ and $f(n+1) = 2f(n) + 3$. Then:

$$\begin{aligned} f(1) &= 2(3) + 3 = 9 \ f(2) &= 2(9) + 3 = 21 \ f(3) &= 2(21) + 3 = 45 \end{aligned}$$

Example. Sum of the first $n$ numbers: $S(1) = 1$ and $S(n) = S(n-1) + n$. Then $S(4) = 1 + 2 + 3 + 4 = 10$.

A recurrence relation for a sequence ${a_n}$ is an equation giving $a_n$ in terms of $a_0, a_1, \dots, a_{n-1}$. A solution is a formula for $a_n$ that satisfies the relation and the initial conditions.

Example (Tower of Hanoi). To move $n$ discs, move $n-1$ discs aside, move the largest disc, and move $n-1$ discs back. So $H_n = 2H_{n-1} + 1$ with $H_1 = 1$. By repeated substitution:

$$\begin{aligned} H_n &= 2H_{n-1} + 1 \ &= 2(2H_{n-2} + 1) + 1 = 2^2 H_{n-2} + 2 + 1 \ &= 2^3 H_{n-3} + 4 + 2 + 1 \ &= \dots = 2^{n-1} H_1 + (2^{n-2} + \dots + 2 + 1) \ &= 2^{n-1} + 2^{n-1} - 1 = 2^n - 1 \end{aligned}$$

So moving 3 discs needs $2^3 - 1 = 7$ moves.

Linear homogeneous recurrence relations

A linear homogeneous recurrence relation of degree $k$ with constant coefficients has the form:

$$a_n = c_1 a_{n-1} + c_2 a_{n-2} + \dots + c_k a_{n-k}, \quad c_k \ne 0$$

  • Linear: earlier terms appear only to the first power, with no products of terms.
  • Homogeneous: there is no extra term that does not contain some $a_j$.
  • Constant coefficients: each $c_i$ is a constant.

Examples:

  • $a_n = 3a_{n-1}$ is linear homogeneous of degree 1.
  • $a_n = a_{n-1} + a_{n-2}$ (Fibonacci) is of degree 2.
  • $a_n = a_{n-1} + 2$ is not homogeneous because of the constant 2.
  • $a_n = a_{n-1}^2$ is not linear.
  • $a_n = n a_{n-1}$ does not have constant coefficients.

What this unit covers

  • Recursively defined functions
  • Linear homogeneous recurrence relations
  • Linear nonhomogeneous recurrence relations
  • Solving recurrence relations with initial conditions
  • Characteristic equations
  • Fibonacci sequence

Solving recurrence relations with initial conditions

2082 ยท 5 marks

Solve the recurrence relation $a_n = a_{n-1} + a_{n-2}$ with initial conditions $a_0 = 0$ and $a_1 = 1$. [5]

Show solved answer

Solving the Recurrence Relation $a_n = a_{n-1} + a_{n-2}$

Given Data

  • Recurrence: $a_n = a_{n-1} + a_{n-2}$
  • Initial conditions: $a_0 = 0$, $a_1 = 1$

Step 1: Characteristic Equation

This is a linear homogeneous recurrence with constant coefficients. Assume $a_n = r^n$:

$$r^n = r^{n-1} + r^{n-2}$$

Divide by $r^{n-2}$:

$$r^2 = r + 1 \implies r^2 - r - 1 = 0$$


Step 2: Solve for the Roots

$$r = \frac{1 \pm \sqrt{1 + 4}}{2} = \frac{1 \pm \sqrt{5}}{2}$$

$$r_1 = \frac{1 + \sqrt{5}}{2}, \qquad r_2 = \frac{1 - \sqrt{5}}{2}$$

Since the roots are distinct and real.


Step 3: General Solution

$$a_n = A\left(\frac{1+\sqrt{5}}{2}\right)^n + B\left(\frac{1-\sqrt{5}}{2}\right)^n$$


Step 4: Apply Initial Conditions

$a_0 = 0$: $$A + B = 0 \implies B = -A$$

$a_1 = 1$: $$A\left(\frac{1+\sqrt{5}}{2}\right) + B\left(\frac{1-\sqrt{5}}{2}\right) = 1$$

Substitute $B = -A$:

$$A\cdot\frac{(1+\sqrt5)-(1-\sqrt5)}{2} = 1 \implies A\cdot\frac{2\sqrt5}{2}=1 \implies A\sqrt5 = 1$$

$$A = \frac{1}{\sqrt5}, \qquad B = -\frac{1}{\sqrt5}$$


Step 5: Closed-Form Solution

$$\boxed{a_n = \frac{1}{\sqrt{5}}\left(\frac{1+\sqrt{5}}{2}\right)^n - \frac{1}{\sqrt{5}}\left(\frac{1-\sqrt{5}}{2}\right)^n}$$

This is Binet's Formula for the Fibonacci numbers.


Verification

$n$Formula valueRecurrence value
000 โœ“
111 โœ“
211 โœ“
322 โœ“
433 โœ“

The sequence produced is the Fibonacci sequence $0, 1, 1, 2, 3, 5, 8, \ldots$

Open this answer on its own page โ†’

2081 ยท 10 marks

What are the uses of randomized algorithm? Find the solution to the recurrence relation $a_n = 6a_{n-1} - 11a_{n-2} + 6a_{n-3}$ with the initial conditions $a_0 = 2$, $a_1 = 5$ and $a_2 = 15$. [2+8]

Show solved answer

Uses of Randomized Algorithms [2 marks]

A randomized algorithm uses random choices during its execution to influence its behavior. Common uses include:

  1. Primality Testing - Fast probabilistic tests such as Miller-Rabin and Solovay-Strassen for very large numbers.
  2. Randomized Quicksort - Random pivot selection gives expected $O(n\log n)$ time and avoids adversarial worst cases.
  3. Cryptography - Generating random keys, nonces, salts, and initialization vectors.
  4. Approximation / Monte Carlo Methods - Quick approximate solutions to hard (NP-hard) or numerical problems.
  5. Load Balancing / Distributed Systems - Randomly assigning tasks to balance load.
  6. Hashing - Universal and cryptographic hashing to reduce collisions.

Solving the Recurrence Relation [8 marks]

Given data

  • Recurrence: $a_n = 6a_{n-1} - 11a_{n-2} + 6a_{n-3}$
  • $a_0 = 2,\quad a_1 = 5,\quad a_2 = 15$

Step 1: Characteristic Equation

Assume $a_n = r^n$: $$r^3 = 6r^2 - 11r + 6$$ $$r^3 - 6r^2 + 11r - 6 = 0$$

Step 2: Roots

Test $r=1$: $1 - 6 + 11 - 6 = 0$ โœ“

$$r^3 - 6r^2 + 11r - 6 = (r-1)(r^2 - 5r + 6) = (r-1)(r-2)(r-3)$$

Roots: $r_1 = 1,\ r_2 = 2,\ r_3 = 3$ (distinct).

Step 3: General Solution

$$a_n = A(1)^n + B(2)^n + C(3)^n = A + B\cdot 2^n + C\cdot 3^n$$

Step 4: Apply Initial Conditions

$$n=0:\ A + B + C = 2 \quad (1)$$ $$n=1:\ A + 2B + 3C = 5 \quad (2)$$ $$n=2:\ A + 4B + 9C = 15 \quad (3)$$

Solution

$(2)-(1):\ B + 2C = 3 \quad (4)$ $(3)-(2):\ 2B + 6C = 10 \Rightarrow B + 3C = 5 \quad (5)$ $(5)-(4):\ C = 2$ From $(4):\ B = 3 - 4 = -1$ From $(1):\ A = 2 - (-1) - 2 = 1$

So $A=1,\ B=-1,\ C=2$.

Final Answer

$$\boxed{a_n = 1 - 2^n + 2\cdot 3^n}$$

Verification

$n$$1 - 2^n + 2\cdot3^n$Expected
0$1 - 1 + 2 = 2$2 โœ“
1$1 - 2 + 6 = 5$5 โœ“
2$1 - 4 + 18 = 15$15 โœ“
Open this answer on its own page โ†’

2080.1 ยท 10 marks

Define randomized algorithm with an example. Solve the recurrence relation $a_n = -4a_{n-1} - 4a_{n-2}$ for $n \ge 2$, with initial condition $a_0 = 0$ and $a_1 = 1$. [3+7]

  • Recurrence: $an = -4a{n-1} - 4a{n-2}$ for $n \geq 2$ - Initial conditions: $a0 = 0$, $a1 = 1$ All required data present. --- A randomized algorithm is an algorithm that makes one or more of its decisions based on random numbers generated during execution. As a result, for the same input the alg...
Open this answer on its own page โ†’

Recursively defined functions

2079 ยท 5 marks

What is recursively defined function? Suppose that f is defined recursively by f(0) = 3, f(n + 1) = 2f (n) +3. Find f(1), f(2), f(3), and f(4). [5]

  • Base case: $f(0) = 3$ - Recursive step: $f(n+1) = 2f(n) + 3$ - Required: $f(1), f(2), f(3), f(4)$ A recursively defined function is a function defined in terms of itself, using two components: 1. Base case: the value of the function at a starting argument (here $f(0) = 3$) is given explicitly. ...
Open this answer on its own page โ†’

Linear nonhomogeneous recurrence relations

2078 ยท 10 marks

What is linear nonhomogeneous recurrence relation of degree k with constant coefficients? Find all the solutions of the recurrence relation a, 4a+n. Also find the solution of the relation with initial condition a, 1.[10]

  • Recurrence (interpreted): $an = 4a{n-1} + n$ - Initial condition: $a1 = 1$ Note: the question text is garbled ("a, 4a+n" and "a, 1"). The standard textbook reading is $an = 4a{n-1} + n$ with $a1 = 1$, which is used here. --- A linear nonhomogeneous recurrence relation of degree k with constant ...
Open this answer on its own page โ†’

Linear homogeneous recurrence relations

0 ยท 10 marks

Define recurrence relation. What do you mean by linear homogeneous recurrence of degree $k$ with constant coefficients? What is the solution of the recurrence relation $a_n = a_{n-1} + a_{n-2}$ with initial conditions $a_0 = 0$ and $a_1 = 1$? [10]

  • Recurrence: $an = a{n-1} + a{n-2}$ - Initial conditions: $a0 = 0$, $a1 = 1$ --- A recurrence relation for a sequence ${an}$ is an equation that expresses each term $an$ in terms of one or more of the preceding terms $a{n-1}, a{n-2}, \ldots, a{n-k}$, valid for all $n$ greater than or equal to ...
Open this answer on its own page โ†’