{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

sol1 - CMPT 307 Data Structures and Algorithms Solutions to...

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

View Full Document Right Arrow Icon
CMPT 307 - Data Structures and Algorithms: Solutions to PS 1 September 25, 2008 1. Asymptotics Rank the following functions by order of growth; that is, arrange the following 14 functions g 1 , . . . , g 14 so that g 1 = O ( g 2 ) , g 2 = O ( g 3 ) , . . . , g 13 = O ( g 14 ). Here are the functions: n 2 , (log n )!, n !, log 2 n , 2 2 n , n 1 / log n , log log n , n log log n , 2 log n , 2 n , 2 2 log n , n , n log n , (3 / 2) n . Solution: n 1 / log n , log log n , log 2 n , 2 2 log n , 2 log n , n , n log n , n 2 , (log n )!, n log log n , (3 / 2) n , 2 n , n !, 2 2 n . Justification (for some of the cases): n 1 / log n = 2 log n/ log n = 2; (log n )! (log n ) log n = 2 (log log n )(log n ) = n log log n ; ( n !) ( n/ 2) n/ 2 2 n ; 2 2 log n < 2 log n = n ; 2 log n = n . 2. Recurrences Give asymptotic upper and lower bounds for T ( n ) in each of the follow- ing recurrences. Assume that T ( n ) is constant for n 2. Make your bounds as tight as possible, and justify your answers: (a) T ( n ) = 2 T ( n/ 2) + n 3 . Solution: Expanding, we get T ( n ) = n 3 + 2( n/ 2) 3 + 4( n/ 4) 3 + 8( n/ 8) 3 + . . . , with log n terms in the summation. Factoring out n 3 , we get T ( n ) = n 3 (1 + 1 / 4 + (1 / 4) 2 + (1 / 4) 3 + . . . ). By the formula for geometric progression, we get 1 + 1 / 4 + (1 / 4) 2 + (1 / 4) 3 + · · · ≤ 1 / (1 - (1 / 4)) = 4 / 3. So, T ( n ) = Θ( n 3 ). (b) T ( n ) = 16 T ( n/ 4) + n 2 . Solution: Expanding, we get T ( n ) = n 2 + 16( n/ 4) 2 + 16 2 ( n/ 4 2 ) 2 + 16 3 ( n/ 4 3 ) 2 + . . . , with log 4 n terms in the summation. Factoring out n 2 , get T ( n ) = n 2 (1 + 16 / 16 + 16 2 / 16 2 + 16 3 / 16 3 + . . . ) = n 2 log 4 n = Θ( n 2 log n ). (c) T ( n ) = T ( n - 1) + n . Solution: Expanding, get T ( n ) = n + ( n - 1) + ( n - 2) + ( n - 3) + · · · + 1, which is equal to n ( n + 1) / 2 = Θ( n 2 ) (by the formula for arithmetic progressions). (d) T ( n ) = T ( n ) + 1. Solution: Expanding, get T ( n ) = 1 + 1 + 1 + · · · + 1, for t terms where t is the smallest integer that satisfies the inequality n 1 / 2 t 2 (since each time, the instance size n gets raised to the power 1 / 2, and we stop when the instance size becomes less than 2). We can solve the above inequality as follows: Take logs on both sides, getting (1 / 2 t ) log n 1. Taking 1 / 2 t to the other side, 1
Image of page 1

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

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