This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: COT5405  Homework 1 Page 1 of 6 Problem 1 Give a problem P with size n , if it can not be solved in O ( f ( n )) time by any known algorithm, can we say the lower bound of P is Ω( f ( n ))? Why? (5pt) Solution: The answer is no. Not all functions may be asymptotically comparable. A counterex ample is that P is oscillating as P = n 2 , if n is odd; 1 , if n is even And, let f ( n ) = n . Thus it is obvious P ̸ = O ( f ( n )) and P ̸ = Ω( f ( n )) either. Problem 2 Solve the following recurrence relations by giving Θ bound. (20pt) T ( n ) = 3 T ( n/ 2) + n T ( n ) = 8 T ( n/ 2) + n 3 T ( n ) = 2 T ( n/ 4) + √ n ) T ( n ) = T ( √ n ) + 1 Solution: 1. Using Master theorem with a = 3 , b = 2 , f ( n ) = n = O ( n log 2 3 ϵ ), we have: T ( n ) = 3 ∗ T ( n/ 2) + n = θ ( n log 2 3 ) 2. Using Master theorem with a = 8 , b = 2 , f ( n ) = n 3 = n log 2 8 , we have: T ( n ) = 8 T ( n/ 2) + n 3 = θ ( n 3 ∗ logn ) 3. Using Master theorem with a = 2 , b = 4 , f ( n ) = n 1 / 2 = n log 4 2 , we have: T ( n ) = 2 T ( n/ 4) + √ n = θ ( n 1 / 2 ∗ logn ) 4. We have: T ( n ) = T ( n 1 / 2 ) + 1 = T ( n 1 / 4 ) + 2 = ... = T ( n 1 2 k ) + k COT5405  Homework 1 Page 2 of 6 Let c = max { T (1) ,T (2) } . Solve the equation n 1 2 k = 2, we get k = loglogn . Hence: T ( n ) = c + loglogn = θ ( loglogn ) Problem 3 You are planning to buy gifts for your nephews at a gift store where there are n toys all with different prices. Suppose you have k nephews and d dollars in your pocket all of which you need to spend. The kgift problem asks to find whether there are k toys with total cost exactly d . Design and analyze an o ( n 3 )(little oh) time algorithm for the 3gift problem. Note that d should not come into your asymptotic time complexity. (Hint: solve the 2gift problem first.) (15pt) Solution: Firstly we give an algorithm for the 2gift problem. The first step is to use a divideandconquer sorting algorithm to sort the n toys according to their prices. Then pick the first toy and suppose it has price t 1 . Use binary search to find a toy with price d − t 1 in the remaining n − 1 toys. If there is such a toy, return yes; otherwise recursively pick the remaining n − 1 toys as the first toy and do a binary search. If no pair of toys with total price d is found, return no. The time complexity of this algorithm is O ( n log n ) since sorting takes n log n and binary search takes log n and there are at most n binary search steps....
View
Full Document
 Fall '08
 UNGOR
 Divide and conquer algorithm, LEF Ti

Click to edit the document details