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

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
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 0 ) = ( 1 1 1 0 ) n ( 1 0 ) 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 0 ) to get the final result. All the operations done are integer operations. a50 3. (8 pts) Maximum Contiguous Subsequence Sum Problem Let A [1 ..n ] be an array of numbers. The elements in A can be either positive or negative.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern