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.
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 answerHide 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 value | Recurrence value |
|---|---|---|
| 0 | 0 | 0 โ |
| 1 | 1 | 1 โ |
| 2 | 1 | 1 โ |
| 3 | 2 | 2 โ |
| 4 | 3 | 3 โ |
The sequence produced is the Fibonacci sequence $0, 1, 1, 2, 3, 5, 8, \ldots$
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 answerHide answer
Uses of Randomized Algorithms [2 marks]
A randomized algorithm uses random choices during its execution to influence its behavior. Common uses include:
- Primality Testing - Fast probabilistic tests such as Miller-Rabin and Solovay-Strassen for very large numbers.
- Randomized Quicksort - Random pivot selection gives expected $O(n\log n)$ time and avoids adversarial worst cases.
- Cryptography - Generating random keys, nonces, salts, and initialization vectors.
- Approximation / Monte Carlo Methods - Quick approximate solutions to hard (NP-hard) or numerical problems.
- Load Balancing / Distributed Systems - Randomly assigning tasks to balance load.
- 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 โ |
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]
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]
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]
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]
Make Unit 5 stick
Practice BIT152 with flashcards & quizzes