Recurrence Relations
A recurrence relation defines a sequence where each term is a function of preceding terms. They are the discrete analogues of differential equations.
Classification
- Linear: Terms appear to the first power.
- Homogeneous: No constant terms or separate functions of .
- Order: The number of previous terms required.
Solving Linear Homogeneous Recurrences
For , we solve the Characteristic Equation:
python
1import numpy as np
2
3def solve_recurrence(c1, c2, a0, a1, n):
4 # Solves a_n = c1*a_{n-1} + c2*a_{n-2}
5 roots = np.roots([1, -c1, -c2])
6 r1, r2 = roots
7
8 # a_n = A*r1^n + B*r2^n
9 # a0 = A + B
10 # a1 = A*r1 + B*r2
11 A_mat = np.array([[1, 1], [r1, r2]])
12 B_vec = np.array([a0, a1])
13 A, B = np.linalg.solve(A_mat, B_vec)
14
15 return A * (r1**n) + B * (r2**n)
16
17# Fibonacci example: a_n = 1*a_{n-1} + 1*a_{n-2}, a0=0, a1=1
18val = solve_recurrence(1, 1, 0, 1, 10)
19print(f"F(10) via Characteristic Equation: {round(val.real)}")
Binet’s Formula for Fibonacci
The closed-form solution for Fibonacci is:
python
1def binet_fib(n):
2 phi = (1 + 5**0.5) / 2
3 psi = (1 - 5**0.5) / 2
4 return round((phi**n - psi**n) / 5**0.5)
5
6print(f"F(20) via Binet: {binet_fib(20)}")
Divide and Conquer: The Master Theorem
In CS, we often see .
- Merge Sort: .
- Binary Search: .
What is the characteristic equation for the recurrence $a_n = 5a_{n-1} - 6a_{n-2}$?
Computational Methods: Matrix Exponentiation
For terms like , floating point precision fails. We use the transformation matrix where gives the -th term in time.
python
1def matrix_mult(A, B):
2 C = [[0, 0], [0, 0]]
3 for i in range(2):
4 for j in range(2):
5 for k in range(2):
6 C[i][j] += A[i][k] * B[k][j]
7 return C
8
9def matrix_pow(A, p):
10 res = [[1, 0], [0, 1]]
11 while p > 0:
12 if p % 2 == 1: res = matrix_mult(res, A)
13 A = matrix_mult(A, A)
14 p //= 2
15 return res
16
17T = [[1, 1], [1, 0]]
18T_n = matrix_pow(T, 10)
19print(f"F(10) via Matrix Exponentiation: {T_n[0][1]}")