HW1sol_001

# HW1sol_001 - COT5405 Analysis of Algorithms Homework 1...

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

COT5405 Analysis of Algorithms Homework 1 Solutions Four questions were graded, 25 points each. Solutions 1. (a) Similar to binary search, takes O ( lg n ). SEARCH ( S, N ) r = 1 , s = N while r s do t = floor (( r + s ) / 2) if t S [ t ] then s = t else r = t + 1 if r == S [ r ] then return r else return 0 (b) Takes θ ( N ) MaxSubSum ( S, N ) lMax = 0 , gMax = 0 for i = 1 to n do lMax = max ( S [ i ] , lMax + A [ i ]) lMax is maximum of all sequences ending with S [ i ]. gMax = max ( lMax, gMax ) gMax is maximum of all sequences ending in S [1 ..i ]. return gMax Grading: Not graded. 1

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

View Full Document
2. (a) Pass array by copying all its elements. T ( m ) = 2 T ( m/ 2) + θ ( n ) + θ ( m ) but m n . T ( m ) = 2 T ( m/ 2) + c θ ( n ) T ( m ) = θ ( m n ) OR T ( n ) = θ ( n 2 ) (b) Copy elements of required segment only. This does not change the original recurrence of Merge-sort. So it takes θ ( n log n ). (Your solution should verify this.) Grading: First part 15, second part 10 points. - 10 points for omitting the θ ( n ) part in the recurrence and getting θ ( n log n ). 3. Store n intervals in a data structure which holds right endpoints and left endpoints of intervals. Sort the data structure according to left endpoints of intervals using any of O ( n lg n ) sorting algorithms (Merge- sort, Quicksort, Heapsort, etc.). Let, I ( i ) represent the i’th interval data after sorting phase, a i and b i be left and right endpoints of the corresponding interval. Let, T be the total length of intervals and initially T = 0. Scanning all the data structure starting from i = 1 to i = n we need to consider 3 cases: Case1( b i a i +1 ): I ( i ) not intersecting with I ( i + 1) then T = T + ( b i - a i ) Case2( b i > a i +1 AND b i b i +1 ): I ( i + 1) is included in I ( i ) then T = T Case3( b i > a i +1 AND b i < b i +1 ): I ( i ) is intersecting with I ( i +1) then T = T + ( b i +1 - b i ) This algorithm correctly computes the total length of n intervals given
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/15/2012 for the course COT 5405 taught by Professor Ungor during the Fall '08 term at University of Florida.

### Page1 / 6

HW1sol_001 - COT5405 Analysis of Algorithms Homework 1...

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

View Full Document
Ask a homework question - tutors are online