Homework_2_Solutions

# Homework_2_Solutions - CS180 Spring 2009 Homework 2...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS180 Spring 2009 Homework 2 Solutions 2.3 √ 2 n , n + 10, n 2 log n , n 2 . 5 , 10 n , 100 n 2.6 (a) This algorithm is O ( n 3 ) (b) The number of additions made by this algorithm: ∑ n i =1 ( ∑ n j = i +1 ( j- i + 1)) = ∑ n i =1 ( ∑ n j = i +1 j- ( n- i ) i + n- i ) = ∑ n i =1 ( n ( n +1) 2- i ( i +1) 2- ni + i 2 + n- i ) Now that the sum over j has been removed, all remaining sums are from i = 1 to n . This will make the rest a bit easier to read. = n 3 +3 n 2 2 + 1 2 ∑ ( i 2 )- 3 2 ∑ ( i )- n ∑ ( i ) = n 3 +3 n 2 2 + n ( n +1)(2 n +1) 12- 3( n )( n +1) 4- n 3 + n 2 2 = n 3 +3 n 2 2 + 2 n 3 +3 n 2 + n 12- 3 n 2 +3 n 4- n 3 + n 2 2 Let’s collect the n 3 terms, as they are the highest order terms. We have: ( 1 2 + 1 6- 1 2 ) n 3 = n 3 6 , so we have Ω( n 3 ). (c) The better algorithm is based on the observation that the sum of A [ i..j ] is calculated for B [ i,j ] and again for B [ i,j + 1]. This is wasteful. Instead, let B [ i,i ] = A [ i ] and B [ i,j ] = B [ i,j...
View Full Document

## This note was uploaded on 11/28/2009 for the course CS CS180 taught by Professor Mayer during the Spring '09 term at UCLA.

Ask a homework question - tutors are online