hw2-sol - Assignment #2 CS4/531 Due Date: Tuesday, Oct. 3,...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Assignment #2 CS4/531 Due Date: Tuesday, Oct. 3, 2011 UNSUPPORTED SOLUTIONS RECEIVE NO CREDIT. Total points: 53 1. (5 pts) Let a be a real number and n a positive integer. We want to compute a n . This, of course, can be done using n − 1 multiplications. Describe an algorithm that computes an using O (log n ) basic arithmetic operations. Note: A basic arithmetic operation is one of the following: x + y , x − y , x × y , x/y (which returns the integer part of x divided by y ); and ( x mod y ) (which returns the remainder of x divided by y ). But, x y is NOT a basic arithmetic operation. Although most programming languages allow this operation, x y is calculated by a subroutine, not by a CPU instruction. Answer: • Idea: If n is even, a n = a n 2 ∗ a n 2 . If n is odd, a n = a ⌊ n 2 ⌋ ∗ a ⌊ n 2 ⌋ ∗ a . Hence we only need to call the procedure to compute a n 2 (or a ⌊ n 2 ⌋ ), and use one (or two) extra multiplication(s) to get the final result. • Pseudocode: Power-Alg (a , n) 1 if n=0 2 then return 1 3 if n=1 4 then return a 5 if n%2=0 6 then b= Power-Alg (a , n/2) 7 return b*b 8 else b= Power-Alg (a , n/2) 9 return b*b*a The running time satisfies T ( n ) = T ( n/ 2) + Θ(1) which is O (log n ) by Master Theo- rem. • Extra note: The running time of the above algorithm is O (log n ). But from a com- plexity point of view, the input size is n = 2 N , where N is the number of bits needed to represent n in binary. Hence the run time is linear in N , i.e. O ( N ). a50 2. (8 pts). We discussed the Fibonacci numbers in class. To calculate Fib( n ), the recur- sive function (using the recursive definition) takes exponential time. The simple for loop function takes O ( n ) time. We can do better than this: only in O (log n ) time. We have shown that: Fib( n ) = α n 1 √ 5 − α n 2 √ 5 where α 1 = 1+ √ 5 2 and α 2 = 1 − √ 5 2 . By using this formula and the algorithm in Problem 1, we easily get an algorithm for computing Fib( n ) in O (log n ) time. The drawback of this algorithm, however, is that it uses real arithmetic operations, which takes more time than integer arithmetic operations. (Roughly speaking, a real arithmetic operation is 5 times slower than the corresponding 1 integer arithmetic operation). Describe an O (log n ) time algorithm that uses ONLY integer arithmetic operations. Hint: Derive a formula for computing Fib(n) using matrix and vector multiplication. Answer: We have ( F n F n- 1 ) = ( 1 1 1 0 ) ( F n- 1 F n- 2 ) = ( 1 1 1 0 )( 1 1 1 0 ) ( F n- 2 F n- 3 ) . . . = ( 1 1 1 0 ) n ( F 1 F ) = ( 1 1 1 0 ) n ( 1 ) Let A = ( 1 1 1 0 ). We can calculate A n in O (log n ) time by using Power- Alg , and multiply it by ( 1 ) to get the final result. All the operations done are integer operations....
View Full Document

This note was uploaded on 02/27/2012 for the course CSE 431/531 taught by Professor Xinhe during the Fall '11 term at SUNY Buffalo.

Page1 / 6

hw2-sol - Assignment #2 CS4/531 Due Date: Tuesday, Oct. 3,...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online