Data Structure Lecture Note (Week 1, Lecture 3)
Recursion
Master Theorem
()
Suppose , let , then:
- $F(N) = \Theta(N^c) $ if
- if
- if
Proof:
How to calculate ? We need to expand it into recursion and non-recursion parts
That is,
If , the bracket is a convergent geometric sequence, which would finally converge to some real number
If , we simply add times 1
If , the brackets give $ (1-(a/bc){\log_bN + 1}) / (1- a/b^c) = \Theta( ((\frac{a}{b^c}) ^{\log_b N}) ) = \Theta(a^{\log_b N} / N^c) = \Theta ( a^{\log_a N / \log_a b } / N^c ) = \Theta (N^{c_0} / N^c) $ , so overall yields
This finished the proof for the Master Theorem.
Remark: the reason why it produces at most polynomial complexity is, each time it solves a subproblem with problem size reduced by greater or equal to 1.
Paradigm in Recursion and Pitfalls
Step1: Identify the subproblem
Step2: Solve the origin problem by solving subproblems, then combine the solutions
Or
Step2’: Solve the origin problem by first tackling an easier part, then reduce it to a smaller problem.
Pitfall: when you try to use recursion, make sure that
- Subproblems CAN be combined to solve the origin problem. For example, if you are asked to carry a big artifact into the museum, and you think it’s too heavy. You should not try to slice it into small pieces and carry multiple times… Because they cannot be glued OR you do want the artifact intact!
- Subproblems should be ABLE to solved. This means, if the problem have certain constraints, your subproblem must satisfy all the constraints!
Examples
Look for a word in a dictionary.
Hanoi Tower Problem (very notorious problem with sole purpose used in academia to train recursion)
Max sum subarray:
Input: Array[1,…,n] containing n intergers
Output: Compute a subarray A[, …, $j^* $] with maximum sum.