4. Recurrence Relations
A well-defined recurrence
relation needs a nonrecursive
base case that gives at least
one value of the function
explicitly.
recursive part
2023/2/22 4
5. Binary search (of sorted array)
https://en.wikipedia.org/wiki/Binary_search_algorithm
A tree representing binary search.
The array being searched here is
[20,30,40,50,80,90,100]
and the target value is 40.
https://www.geeksforgeeks.org/binary-search/
The array being searched here is
[2,5,8,12,16,23,38,56,72,91]
and the target value is 23.
𝑇 𝑛 = ቐ
Θ(1) 𝑖𝑓 𝑛 = 1,
𝑇
𝑛
2
+ Θ(1) 𝑖𝑓 𝑛 > 1.
▪ The recurrence describing the worst-
case running time of Binary search
▪ Example
▪ Example
2023/2/22 5
6. MERGE-SORT
▪ The recurrence describing the worst-case
running time of MERGE-SORT
https://ithelp.ithome.com.tw/articles/10278179
利用30 10 40 70 50 90 60 20由小到大排序。
▪ Example
2023/2/22 6
7. Fibonacci Numbers
A tiling with squares whose side lengths are
successive Fibonacci numbers:
1, 1, 2, 3, 5, 8, 13 and 21.
https://en.wikipedia.org/wiki/Fibonacci_number
https://betterprogramming.pub/
the-beauty-of-the-fibonacci-
sequence-c6f95674b94e
▪ Examples
2023/2/22 7
8. Solving Recurrence Equations
▪ Guess and proof by Induction
▪ Substitution method
▪ Recursion-tree method
▪ Change of Variables (Domain
Transformations)
▪ Characteristic Equations
▪ Extending Results for n, a
Power of a Positive Constant b,
to n in General
▪ Master method
2023/2/22 8
10. Example 1 for Guess and proof by Induction
▪ Consider the recurrence 𝑡𝑛 = ቊ
𝑡𝑛−1+1, for 𝑛 > 0
𝑡0 = 0
▪ Guess
▪ Induction
proof:
2023/2/22 10
11. Example 2 for Guess and proof by Induction
▪ Consider the recurrence 𝑡𝑛 = ൝
𝑡 Τ
𝑛
2
+1, for 𝑛 > 1, 𝑛 𝑎 𝑝𝑜𝑤𝑒𝑟 𝑜𝑓 2
𝑡1 = 1
▪ Guess ▪ Induction
proof:
2023/2/22 11
12. Example 3 for Guess and proof by Induction
▪ Consider the recurrence 𝑡𝑛 = ൝
7𝑡 Τ
𝑛
2
, for 𝑛 > 1, 𝑛 𝑎 𝑝𝑜𝑤𝑒𝑟 𝑜𝑓 2
𝑡1 = 1
▪ Guess ▪ Induction
proof:
2023/2/22 12
13. Example 4 (unfinished) for Guess and proof by Induction
▪ Consider the recurrence 𝑡𝑛 = ൝
2𝑡 Τ
𝑛
2
+𝑛−1, for 𝑛 > 1, 𝑛 𝑎 𝑝𝑜𝑤𝑒𝑟 𝑜𝑓 2
𝑡1 = 0
▪ There is no obvious candidate solution suggested by these values.
Learn more
techniques!!
Induction can only
verify that a solution
is correct!!
2023/2/22 13
14. SUBSTITUTION METHOD (代換法)
In the substitution method, we guess a bound and then use mathematical induction to prove our guess
correct.
2023/2/22 14
15. Example 1 of The substitution method
▪ Consider the recurrence 𝑡𝑛 = ቊ
𝑡𝑛−1+n, for 𝑛 > 1
𝑡1 = 1
2023/2/22 16
16. Example 2 of The substitution method
▪ Consider the recurrence 𝑡𝑛 = ቐ
𝑡𝑛−1+
2
𝑛
, for 𝑛 > 1
𝑡1 = 0
2023/2/22 17
18. Height, Depth and Level of a Tree (2022/2/22 add )
2023/2/22 19
http://typeocaml.com/2014/11/26/height-depth-and-level-of-a-tree/
Height of node – The height of a node is the
number of edges on the longest downward
path between that node and a leaf.
Depth –The depth of a node is the number of
edges from the node to the tree's root node.
Level – The level of a node is defined
by 1 + the number of connections
between the node and the root.
19. Example 1 for recursion tree
The construction of a recursion tree for the recurrence
T (n) = 2T (n/2) + cn.
In a recursion tree, each node represents the cost of a
single subproblem somewhere in the set of recursive
function invocations.
A recursion tree is best used to generate a good guess,
which is then verified by the mathematical induction .
2023/2/22 20
20. Example 2 for recursion tree
The construction of a recursion tree for the recurrence
T (n) = 3T (n/4) + cn2.
2023/2/22 21
21. Use the substitution method to verify that our guess
was correct for example 2
Prove that T (n) = O(n2) is an upper bound for the
recurrence T (n) = 3T (n/4) + cn2.
Only induction
step for short
2023/2/22 22
22. Example 3 for recursion tree
The construction of a recursion tree for the recurrence
T (n) = T (n/3) + T (2n/3) + O(n).
The longest path from the root to a leaf is n→(2/3)n→(2/3)2n→· · ·→1.
Since (2/3)kn = 1 when k = log3/2 n, the height of the tree is log3/2 n.
2023/2/22 23
23. Use the substitution method to verify that our guess
was correct for example 2
Prove that T (n) = O(n lg n) is an upper bound for the
recurrence T (n) = T (n/3) + T (2n/3) + O(n).
We show that T (n) ≤ dn lg n, where d is a suitable positive
constant. We have
as long as d ≥ c/(lg 3−(2/3))
Only induction
step for short
2023/2/22 24
Goto the master method
25. Example 1 for Change of Variables
▪ Consider the recurrence T n = ቐ
𝑇
𝑛
2
+1, for 𝑛 > 1, 𝑛 𝑎 𝑝𝑜𝑤𝑒𝑟 𝑜𝑓 2
𝑇(1) = 1
Let
Let
2023/2/22 26
26. Example 2 for Change of Variables
Solve Induction example 4
▪ Consider the recurrence 𝑇(𝑛) = ൝
2𝑇(
𝑛
2
)+𝑛−1, for 𝑛 > 1, 𝑛 𝑎 𝑝𝑜𝑤𝑒𝑟 𝑜𝑓 2
𝑇(1) = 0
Let 𝑛 = 2𝑘
Then
Let
Then
2023/2/22 27
27. Example 3 for Change of Variables
▪ Consider the recurrence 𝑇(𝑛) = ൝
7𝑇(
𝑛
2
)+18(
𝑛
2
)2, for 𝑛 > 1, 𝑛 𝑎 𝑝𝑜𝑤𝑒𝑟 𝑜𝑓 2
𝑇(1) = 0
Let 𝑛 = 2𝑘
Let
Then
Then
2023/2/22 28
30. Example :Solve a homogeneous linear recurrence
▪ Consider the recurrence
𝑡𝑛 − 5𝑡𝑛−1 + 6𝑡𝑛−2 = 0 𝑓𝑜𝑟 𝑛 > 1
𝑡0 = 0
𝑡1 = 1
▪ Let 𝑡𝑛 = 𝑟𝑛
▪ Then 𝑡𝑛 − 5𝑡𝑛−1 + 6𝑡𝑛−2 = 𝑟𝑛 − 5𝑟𝑛−1 + 6𝑟𝑛−2
▪ Therefore, tn = rn is a solution to the recurrence if r is a root of 𝑟𝑛 − 5𝑟𝑛−1 + 6𝑟𝑛−2 = 0
▪ Because 𝑟𝑛 − 5𝑟𝑛−1 + 6𝑟𝑛−2 = 𝑟𝑛−2(𝑟2 − 5𝑟1 + 6𝑟0)
▪ the roots are r = 0, and the roots of 𝑟2 − 5𝑟 + 6=0
𝑟2 − 5𝑟 + 6 = (𝑟 − 3)(𝑟 − 2)
▪ The roots are r = 3 and r = 2.
▪ Then 𝑡𝑛 = 0, 3𝑛, and 2𝑛
2023/2/22 32
31. Example : Solve a homogeneous linear recurrence
▪ Consider the recurrence
𝑡𝑛 − 5𝑡𝑛−1 + 6𝑡𝑛−2 = 0 𝑓𝑜𝑟 𝑛 > 1
𝑡0 = 0
𝑡1 = 1
▪ Then 𝑡𝑛 = 𝑐13𝑛 + 𝑐22𝑛
▪ Consider The initial conditions 𝑡0 = 0, 𝑡1 = 1
▪ We have 𝑡0 = 𝑐130 + 𝑐220 = 0
𝑡1 = 𝑐131 + 𝑐221 = 1
▪ That is 𝑐1 + 𝑐2 = 0
3𝑐1 + 2𝑐2 = 1
▪ Therefore 𝑐1 = 1, 𝑐2 = −1
𝑡𝑛 = 3𝑛 − 2𝑛
Reminder:
If we had different initial conditions in the preceding
example, we would get a different solution.
A recurrence actually represents a class of functions,
one for each different assignment of initial conditions.
2023/2/22 33
36. Theorem B.2 for a root of multiplicity m for characteristic
equation
2023/2/22 38
37. Example: a root of multiplicity
The roots are r = 1 and r = 3, and
r = 3 is a root of multiplicity 2.
▪ Consider the recurrence
𝑡𝑛 − 7𝑡𝑛−1 + 15𝑡𝑛−2 − 9𝑡𝑛−3 = 0 𝑓𝑜𝑟 𝑛 > 2
𝑡0 = 0
𝑡1 = 1 𝑡2 = 2
2023/2/22 39
38. Example : a root of multiplicity
▪ Consider the recurrence
𝑡𝑛 − 5𝑡𝑛−1 + 7𝑡𝑛−2 − 3𝑡𝑛−3 = 0 𝑓𝑜𝑟 𝑛 > 2
𝑡0 = 1
𝑡1 = 2 𝑡2 = 3
The roots are r = 3 and r = 1, and
r = 1 is a root of multiplicity 2.
2023/2/22 40
46. Strictly increasing
▪ Many of the functions we encounter in algorithm analysis are strictly increasing
for nonnegative values of n.
▪ For example, lg n, n, n lg n, n2, and 2n are all strictly increasing as long as n is
nonnegative.
2023/2/22 48
47. Nondecreasing
▪ Any strictly increasing function is nondecreasing, but a function that can level out
is nondecreasing without being strictly increasing.
2023/2/22 49
49. Smooth
Example: The functions lg n, n, n lg n, and nk, where k ≥ 0, are all smooth.
Example: Proof for lg n:
Example: The function 2nis not smooth
2023/2/22 51
51. Example for Theorem B.4
▪ Consider the recurrence T n = ቐ
𝑇(
𝑛
2
)+1, for 𝑛 > 1
𝑇(1) = 1
Because lg n is smooth, we need only show that T (n) is
eventually nondecreasing in order to apply Theorem B.4
to conclude that
2023/2/22 53
52. Prove: is eventually nondecreasing
T n = ቐ
𝑇(
𝑛
2
)+1, for 𝑛 > 1
𝑇(1) = 1
2023/2/22 54