Recursivity

Defining something (function, algorithm, set, etc.) as a function of itself.

Lame's Theorem

Let a,bZa,b \in \Z such that ab>0a \geq b > 0. Euclid's algorithm performs O (logb)(\log{b}) divisions.

Binary Tree

A binary tree is defined recursively by:

  • an isolated node rr is a binary tree with root r
  • Let T1T_1 be a binary tree with root r1r_1
  • Let T2T_2 be a binary tree with root r2r_2
  • the graph obtained by adding the edges {r,r1r, r_1} and {r,r2r, r_2} is a binary tree with root rr.

Height h(T)h(T) of a binary tree TT:

  • h(T)h(T) = 0 if TT is an isolated node, else 1+1 + max(h(T1),h(T2))(h(T_1), h(T_2))

Size n(T)n(T) of a binary tree TT:

  • n(T)n(T) = 0 if TT is an isolated node, else 1+n(T1)+n(T2)1+n(T_1)+n(T_2)

n(T)2h(T)+11n(T) \leq 2^{h(T) + 1} - 1

Recursive Algorithms

Divide and Conquer Approach

To solve a problem of size nn:

  • divide the problem in sub problems of size < nn.
  • conquer, solve each sub problem recursively.
  • combine (merge) the solutions of sub problems to obtain a solution for the original problem.

General structure

For an input xx:

def algo(x):
if (x == "sufficiently small"):
// resolve the problem with brute force and resolve the problem
else:
// decompose x into sub problems x1, x2, x3, ... , xL
// do:
y1 = algo(x1)
y2 = algo(x2)
// ...
y3 = algo(xL)
// combine y1, y2, ... yL into a solution y for x
return y

Solving linear recurrence relations

Linear homogenous recurrence

an=c1an1+c2an2+...+ckanka_n = c_1 \cdot a_{n-1} + c_2 \cdot a_{n-2} + ... + c_k \cdot a_{n-k}
a0=x0a_0 = x_0
a1=x1a_1 = x_1
...
ak1=xk1a_{k-1} = x_{k-1} where ciR(1ik),ck0,xiR(0ik)c_i \in \R (1 \leq i \leq k), c_k \neq 0, x_i \in \R (0 \leq i \leq k)

Theorem For Degree Two

Let c1,c2Rc_1, c_2 \in \R. Suppose that r2c1rc2=0r^2-c_1r-c_2 = 0 has two distinct roots r1r_1 and r2r_2. Then ana_n is a solution of an=c1an1+c2an2a_n = c_1a_{n-1} + c_2a_{n-2}, if and only if an=α1r1n+α2r2na_n = \alpha_1 \cdot r_1^n + \alpha_2 \cdot r_2^n, where α1\alpha_1 and α2\alpha_2 are constants.

Let c1,c2Rc_1, c_2 \in \R. Suppose that r2c1rc2=0r^2-c_1r-c_2 = 0 has a single root r0r_0. So ana_n is a solution of an=c1an1+c2an2a_n = c_1a_{n-1} + c_2a_{n-2}, if and only if an=α1r0n+α2nr0na_n = \alpha_1 \cdot r_0^n + \alpha_2 \cdot n \cdot r_0^n, where α1\alpha_1 and α2\alpha_2 are constants.

Example

an=6an19an2a_n = 6a_{n-1} - 9a_{n-2}
a0=1a_0 = 1 a1=6a_1 = 6

Characteristic equation: r26r+9=0r^2 - 6r+9=0
r26r+9=(r3)2r^2-6r+9 = (r-3)^2

Therefore an=α13n+α2n3na_n = \alpha_1\cdot 3^n + \alpha_2\cdot n \cdot 3^n
1=a0=α130+α2030=α11 = a_0 = \alpha_1\cdot 3^0 + \alpha_2 \cdot 0 \cdot 3^0 = \alpha_1
6=a1=α131+α2131=3α1+3α26 = a_1 = \alpha_1\cdot3^1 + \alpha_2 \cdot 1 \cdot 3^1 = 3\alpha_1 + 3\alpha_2

So α1=1\alpha_1 = 1
and 6=3+3α26 = 3+3\cdot\alpha_2 from which α2=1\alpha_2 = 1 Thus,
an=3n+n3n=(n+1)3na_n = 3^n + n\cdot 3^n = (n+1) \cdot 3^n

Theorem For Any Degree

Let c1,c2Rc_1, c_2 \in \R. Suppose that r2c1rc2...ck=0r^2-c_1r-c_2 - ... - c_k = 0 has two distinct roots r1r_1, r2r_2, ..., rkr_k. Then ana_n is a solution of an=c1an1+c2an2+ckanka_n = c_1a_{n-1} + c_2a_{n-2} + c_ka_{n-k}, if and only if an=α1r1n+α2r2n+...+αkrkna_n = \alpha_1 \cdot r_1^n + \alpha_2 \cdot r_2^n + ... + \alpha_k\cdot r_k^n, where α1\alpha_1, α2\alpha_2, ... , αk\alpha_k are constants.

Example

an=6an111an2+6an3a_n = 6a_{n-1} - 11a_{n-2} + 6a_{n-3}
a0=2a_0 = 2 a1=5a_1 = 5 a1=15a_1 = 15

Characteristic equation: r36r2+11r6=0r^3 - 6r^2+11r-6=0

There exists a general formula for degree 3, but we will restrict ourselves to ones that factorize easily. 0,±1,±20, \pm 1, \pm 2 will be used as roots to help factorize.

r=1r=1 is a root for the characteristic equation above.
Therefore we can factorize and get
Characteristic equation: (r1)(r3)(r2)(r-1)(r-3)(r-2)
So we get roots: 1, 2, 3

Therefore an=α11n+α22n+α33na_n = \alpha_1\cdot 1^n + \alpha_2\cdot 2^n + \alpha_3 \cdot 3^n
2=α110+α220+α330=α1+α2+α32 = \alpha_1\cdot 1^0 + \alpha_2\cdot 2^0 + \alpha_3 \cdot 3^0 = \alpha_1 + \alpha_2 + \alpha_3
5=α111+α221+α331=α1+2α2+3α35 = \alpha_1\cdot 1^1 + \alpha_2\cdot 2^1 + \alpha_3 \cdot 3^1 = \alpha_1 + 2\alpha_2 + 3\alpha_3
15=α112+α222+α332=α1+4α2+9α315 = \alpha_1\cdot 1^2 + \alpha_2\cdot 2^2 + \alpha_3 \cdot 3^2 = \alpha_1 + 4\alpha_2 + 9\alpha_3

So α1=1,α2=1,α3=2\alpha_1 = 1, \alpha_2 = -1, \alpha_3 = 2
an=12n+23na_n = 1-2^n + 2\cdot 3^n

Linear non-homogenous recurrence

an=c1an1+c2an2+...+ckank+F(n)a_n = c_1 \cdot a_{n-1} + c_2 \cdot a_{n-2} + ... + c_k \cdot a_{n-k} + \mathrm{F}(n)

where ciR(1ik),ck0,F(n)c_i \in \R (1 \leq i \leq k), c_k \neq 0, \mathrm{F}(n) is a function depending only on nn.

Theorem

If anpa^p_n is an arbitrary particular solution of (*) an=c1an1+c2an2+...+ckank+F(n)a_n = c_1 \cdot a_{n-1} + c_2 \cdot a_{n-2} + ... + c_k \cdot a_{n-k} + \mathrm{F}(n), then all solutions of (*) are of the form anp+anha_n^p + a_n^h, where anha_n^h is a solution to the associated homogenous recurrence.

Example

an=3an1+2na_n = 3a_{n-1} + 2n
a1=3a_1 = 3
anh=α3na_n^h = \alpha\cdot 3^n is the solution of the homogenous part

Now we need a particular solution for an=3an1+2na_n = 3a_{n-1}+ 2n. Since F(n)=2n\mathrm{F}(n) = 2n is linear ... let's try a particular solution of the form anp=cn+da_n^p = c\cdot n + d. We must guess!

cn+d=3(c(c(n1)+d))+2nc\cdot n + d = 3(c(c\cdot (n-1) + d)) + 2n
cn+d=(3c+2)n+(3c+3d)c\cdot n + d = (3c+2) \cdot n + (-3c + 3d)
So c=3c+2,d=3c+3dc = 3c+2, d = -3c + 3d,
c=1c=-1 and d=3/2=n3/2+α3nd = -3/2 = -n -3/2+\alpha\cdot 3^n

3=a1=13/2+α3n=3α5/23 = a_1 = -1 -3/2 + \alpha \cdot 3^n = 3\alpha - 5/2
α=11/6\alpha = 11/6

an=n3/2+11/63na_n = -n -3/2+ 11/6\cdot 3^n

Theorem

Let an=c1an1+c2an2+...+ckank+F(n)a_n = c_1a_{n-1}+c_2a_{n-2} + ... + c_ka_{n-k} + \mathrm{F}(n), where c1,c2,...,ckRc_1, c_2, ..., c_k \in \R with ck0c_k \neq 0 and F(n)=(btnt+bt1nt1+...+b1n+b0)sn\mathrm{F}(n) = (b_tn^t + b_{t-1}n^{t-1}+...+ b_1n+b_0)s^n, where b0,b1,...,bt,sRb_0, b_1, ..., b_t, s \in \R with bt0b_t \neq 0 and s0s \neq 0.

  • Assume that ss is not a root of the characteristic equations of the associated linear homogenous recurrence relation. Then there is a particular solution an(p)a_n^{(p)} of the form:

    (ptnt+pt1nt1+...+p1n+p0)sn(p_t n^t + p_{t-1}n^{t-1}+...+p_1n+p_0)s^n

  • Otherwise, ss is a root of the characteristic equation of the associated linear homogeneous recurrence relation. Assume that ss is a root with multiplicity mm. Then there is a particular solution an(p)a_n^{(p)} of the form:

    nm(ptnt+pt1nt1+...+p1n+p0)snn^m (p_t n^t + p_{t-1}n^{t-1}+...+p_1n+p_0)s^n

Solving non-linear recurrence relations

Theorem

Let ff be a monotonically increasing function such that f(n)=af(n/6)+cf(n) = af(n/6) + c, where a1,bZ(b>1),c>0a \geq 1, b \in \Z (b > 1), c > 0.

Then f(n)=O(nlogb(a))f(n) = \mathrm{O}(n^{log_b(a)}) if a>1a > 1 else Olog(n)\mathrm{O}log(n) if a=1a=1.

Monotonically increasing means its just increasing, just a fancy way of saying it.

Master Theorem

Let ff be a monotonically increasing function such that f(n)=af(n/b)+cndf(n) = af(n/b) + c^\cdot n^d, where a1,bZ(b>1),c>0,d>0a \geq 1, b \in \Z (b > 1), c > 0, d > 0.

Then, f(n)=O(nd)f(n) = \mathrm{O}(n^d) if d>logbad > \log_b a else Olog(ndlogn)\mathrm{O}log(n^d \cdot \log n) if d=logbad = \log_b a else O(nlogb(a))\mathrm{O}(n^{log_b(a)}) if d<logbad < \log_b a.