Professional Documents
Culture Documents
Lec 3
Lec 3
Jared Saia
University of New Mexico
Recurrence Relations
“Oh how should I not lust after eternity and after the nuptial
ring of rings, the ring of recurrence” - Friedrich Nietzsche, Thus
Spoke Zarathustra
1
Alg: Binary Search
2
Recurrence Relations
3
Recurrence Relations
4
Substitution Method
5
Example
6
Proof
Last step holds for all n > 0 if c ≥ 1. Thus, entire proof holds if
n ≥ 2 and c ≥ 2.
7
Recurrences and Induction
8
Some Examples
9
Sum Problem
10
Tree Problem
Recall:
11
Binary Search Problem
12
Dominoes Problem
13
Simpler Subproblems
14
Recurrences
15
Guesses
16
Inductive Proofs
• Now that we’ve made these guesses, we can try using induc-
tion to prove they’re correct (the substitution method)
• We’ll give inductive proofs that these guesses are correct for
the first three problems
17
Sum Problem
f (n) = f (n − 1) + n (6)
= n(n − 1)/2 + n (7)
= (n + 1)n/2 (8)
18
Tree Problem
f (n) = 2 ∗ f (n − 1) (9)
= 2 ∗ (2n−1) (10)
= 2n (11)
19
Binary Search Problem
20
In Class Exercise
21
Recurrences and Inequalities
22
Inequalities (II)
f (n) = f (n − 1) + f (n − 2) (16)
≤ 2n−1 + 2n−2 (17)
< 2 ∗ 2n−1 (18)
= 2n (19)
23
Recursion-tree method
24
Recursion-tree method
• Can use to get a good guess which is then refined and verified
using substitution method
• Best method (usually) for recurrences where a term like
T (n/c) appears on the right hand side of the equality
25
Example 1
n n
n/2 n/2 n
26
Example 1
27
Example 2
n^2 n^2
(n/4)^2 (3/16)n^2
(n/4)^2 (n/4)^2
...
28
Example 2
• We can see that the i-th level of the tree sums to (3/16)in2.
• Further the depth of the tree is log4 n (n/4d = 1 implies that
d = log4 n)
Plog4 n
• So we can see that T (n) = i=0 (3/16)in2
29
Solution
log 4n
(3/16)in2
X
T (n) = (20)
i=0
∞
2 (3/16)i
X
< n (21)
i=0
1
= n2 (22)
1 − (3/16)
= O(n2) (23)
30
Master Theorem
31
Master Theorem
32
Master Theorem
33
The Recursion Tree
34
Details
• The tree stops when we get to the base case for the recur-
rence
• We’ll assume T (1) = f (1) = Θ(1) is the base case
• Thus the depth of the tree is logb n and there are logb n + 1
levels
35
Recursion Tree
• Let T (n) be the sum of all values stored in all levels of the
tree:
36
A “Log Fact” Aside
37
Master Theorem
38
Master Method
39
Proof
40
Example
• T (n) = T (3n/4) + n
• If we write this as T (n) = aT (n/b) + f (n), then a = 1,b =
4/3,f (n) = n
• Here a f (n/b) = 3n/4 is smaller than f (n) = n by a factor of
4/3, so T (n) = Θ(n)
41
Example
42
Example
43
Example
44
In-Class Exercise
45
In-Class Exercise
46
Take Away
• Recursion tree and Master method are good tools for solving
many recurrences
• However these methods are limited (they can’t help us get
guesses for recurrences like f (n) = f (n − 1) + f (n − 2))
• For info on how to solve these other more difficult recur-
rences, review the notes on annihilators on the class web
page.
47
Intro to Annihilators
48
Annihilator Operators
49
Annihilator Description
50
Example
51
Example (II)
52
Distributive Property
53
0, the “Forbidden Annihilator”
54
Uniqueness
55
Example
(L − 3)T = LT + (−3)T
= h21, 22, 23, · · · i + h−3 × 20, −3 × 21, −3 × 22, · · · i
= h(2 − 3) × 20, (2 − 3) × 21, (2 − 3) × 22, · · · i
= (2 − 3)T = −T
56
Example (II)
57
Uniqueness
58
Lookup Table
59
Example
60
Example (II)
61
In Class Exercise
• Q1: Calculate T (0),T (1),T (2) and T (3) and write out the
sequence T
• Q2: Calculate LT , and use it to compute the annihilator of
T
• Q3: Look up this annihilator in the lookup table to get the
general solution of the recurrence for T (n)
• Q4: Now use the base case T (0) = 3 to solve for the con-
stants in the general solution
62
Remaining Outline
63
Multiple Operators
64
Multiple Operators
65
The Details
66
Key Point
67
Lookup Table
68
Fibonnaci Sequence
69
Factoring
70
Quadratic Formula
71
Example
• To factor: L2 − L − 1
• Rewrite: 1 ∗ L2 − 1 ∗ L − 1, a = 1, √b = −1, c = −1
√
• From Quadratic Formula: φ = 1+2 5 and φ̂ = 1−2 5
• So L2 − L − 1 factors to (L − φ)(L − φ̂)
72
Back to Fibonnaci
73
Finding the Constants
√ √
1+ 5
• We know T = hc1φn + c2φ̂ni, where φ = 2 1−
and φ̂ = 2 5
• We know
T (0) = c1 + c2 = 0 (33)
T (1) = c1φ + c2φ̂ = 1 (34)
• We’ve got two equations and two unknowns
• Can solve to get c1 = √1 and c2 = − √1 ,
5 5
74
The Punchline
75
A Problem
76
Example
77
Generalization
78
Lookup Table
79
Lookup Table (Final!)
hp1(n)an
0 + p 2 (n)an + . . . p (n)an i
1 k k
where pi(n) is a polynomial of degree bi − 1 (and ai 6= aj , when
i 6= j)
80
Examples
81
Annihilator Method
82
Annihilator Method
83
Lookup Table
hp0(n)an
0 + p 1 (n)an + . . . p (n)an i
1 k k
where pi(n) is a polynomial of degree bi − 1 (and ai 6= aj , when
i 6= j)
84
Examples
85
Example
86
Example (II)
87
At Home Exercise
(Note: You can check that your general solution works for T (2))
88
Non-homogeneous terms
89
Example
90
Example
91
Example
92
Lookup
93
General Rule
94
Another Example
• Consider T (n) = T (n − 1) + T (n − 2) + 2.
• The residue is h2, 2, 2, · · · i and
• The annihilator is still (L2 − L − 1)(L − 1), but the equation
for T (2) changes!
95
Another Example
96
Another Example
• Consider T (n) = T (n − 1) + T (n − 2) + n.
• The residue is h1, 2, 3, 4, · · · i
• The annihilator is now (L2 − L − 1)(L − 1)2.
97
Another Example
98
Another Example
99
In Class Exercise
• Consider T (n) = 3 ∗ T (n − 1) + 3n
• Q1: What is the homogeneous part, and what annihilates
it?
• Q2: What is the non-homogeneous part, and what annihi-
lates it?
• Q3: What is the annihilator of T (n), and what is the general
form of the recurrence?
100
Limitations
T (n − 1) + lg n
• The problem is that n1 and lg n do not have annihilators.
101
Transformations Idea
102
Transformation
103
Now Solve
104
Reverse Transformation
105
Success!
106
Another Example
107
Now Solve
108
Reverse Transformation
109
In Class Exercise
110
A Final Example
√
• Consider T (n) = 2T ( n) + log n
• Let n = 2i and rewrite T (n):
• T (2i) = 2T (2i/2) + i
• Define t(i) = T (2i):
• t(i) = 2t(i/2) + i
111
A Final Example
112