COMP600/6466 Algorithms Lecture 5 Pseudocode Analysis Iterative Recursive S 0 Dr. Hassan Hijazi Prof. Weifa Liang Recursive Algorithms Asymptotic Bounds for Recursions Total running time = Sum of times in each node of the recursion tree. Substitution method: Guess a bound and use mathematical induction to prove that the guess is correct. Can also be written as a recursion! For example, the running time T (n) can be expressed: ( (1) if n is small, T (n) = T (b n c)+ (n) otherwise. Recursion-tree method: Convert the recurrence into a tree, Use this tree to rewrite the function as a sum, Use techniques of bounding summations to solve the recurrence. We usually write: T (n) =T (b n c)+ (n) assuming that T (n) is constant for small values of n. 4
Substitution method The substitution method consists of two steps: Step 1. Guess the form of the solution. Step. Use mathematical induction to show that the guess is correct. It can be used to obtain either upper or lower bounds on a recurrence. A good guess is vital when applying this method. If the initial guess is wrong, it needs to be adjusted later. Mathematical Induction To prove that T (n) =O(g(n)) (Same principle for and ): Base case: Prove that there exists positive constants c, n 0 and n b such that T (n ) apple c g(n ) 8n s.t. n 0 apple n apple n b. Induction: Assume that T (n ) apple c g(n ) 8n s.t. n b apple n apple n, then show that T (n + 1) apple c g(n + 1). In the base case check for conditions on c and n 0. In the induction step, after substituting T (n + 1) using the recursion, make sure that the resulting number is n 0 and apple n. 5 COMP600/6466 - Lecture 4-0 6 Exercise 5.1 Using the substitution method, prove that T (n) =T + n = O(n log n) Complete the proof by hand Wrong Proofs T (n) =T + n T (n) =O(n)? 1. Base case: Let n b = 4, T (n )=c apple c n, n 0 apple n apple n b if c c, n 0 1.. Inductive step and substitution: assume that T (n ) apple c n, 8n s.t. n b apple n apple n, n +1 T (n + 1) = T apple c n +1 apple c(n + 1) apple (c + 1)(n + 1) = O(n) since n 0 apple n +1 Not same c! apple n, 8n n b 7 8
Wrong Proofs T (n) =T + n T (n) =O(n lg n)? Right Proof T (n) =T + n T (n) =O(n lg n)? 1. Base case: Let n b =, T (n )=c apple c n lg n, n 0 apple n apple n b if c c, n 0.. Inductive step and substitution: assume that T (n ) apple c n lg n, 8n s.t. n b apple n apple n, n +1 T (n + 1) = T n +1 apple c lg n +1 since n 0 apple n +1 apple n, 8n n b apple c(n + 1) lg(n + 1) c(lg )(n + 1) since lg a b =lga lg b apple c(n + 1) lg(n + 1) (n + 1)(c 1) (since lg = 1) apple c(n + 1) lg(n + 1) if c 1. Not True! Take n = nb 1. Base case: Let n b = 4, T (n )=c apple c n lg n, n 0 apple n apple n b if c c, n 0.. Inductive step and substitution: assume that T (n ) apple c n lg n, 8n s.t. n b apple n apple n, n +1 T (n + 1) = T n +1 apple c lg n +1 since n 0 apple n +1 apple n, 8n n b apple c(n + 1) lg(n + 1) c(lg )(n + 1) since lg a b =lga lg b apple c(n + 1) lg(n + 1) (n + 1)(c 1) (since lg = 1) apple c(n + 1) lg(n + 1) if c 1. 9 10 Exercise 5. Exercise 5. T (n) =T Show that l n m + T = O(n) Give an asymptotic upper bound for l n m T (n) =T + T +5 Complete the proof by hand Complete the proof by hand 11 1
Asymptotic Bounds for Recursions Also called iteration method How to make a good guess? Can be used to guess or find the solution Experience When guessing, we can make simplifying assumptions (e.g., ignore floor and ceiling) The goal is to expand the recurrence and express it as a summation 1 14 Consider the recurrence T (n) =T + cn 4 Simplification: we assume that n is a power of 4. 15
At depth d the subproblem size is n 4 d. We stop building the tree when we reach subproblem size 1, so when n = 1. 4 d This gives i = log 4 n.thus,thedepthof the tree is log 4 n. The number of levels is log 4 n + 1. d The cost at depth d is cn, except for the bottom level, whose cost is its number of nodes times T (1), that is, log 4 n T (1) = n log 4 T (1) = (n log 4 ). T (n) = Note that log 4 n 1 X i=0 log 4 n 1 X i=0 This leads to: i cn + (n log 4 )=cn log 4 n 1 X i=0 i + (n log 4 ). i = (largest term) = (1) (decreasing geometric sum). Finally, observe that n grows faster than n log 4 as its exponent is larger. Thus, T (n) = (n ) 17 18 n T (n) =T + T n + cn Why is the depth of this tree log n? 19 0
Do all paths from the root to tree leaves have the same length? If all paths were equal to the longest path, what would the cost of the last level be? 1 Exercise 5.4 n T (n) =T + T n + cn Prove that T (n) =O(n lg n) by induction.