This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: COT 5405 Analysis of Algorithms, Spring 2010. Homework 1 Due Wednesday, February 10 2010, 11:59pm. Notes You may hand the homework to TAs during office hours. You may also hand the homework to Prof. Ranka in class. No late submissions. To force you to write succinctly, we have enforced page limits. These are noted in front of each question. Answer each question on a fresh page. If the problem necessitates writing an algorithm, you must first informally describe the algorithm, in brief, in a paragraph. You can choose to follow this up with pseudocode that formally describes the algorithm. We will peruse your pseudocode only if your English description is not clear. Write your name on the top right hand corner of your homework. Be sure to write your last name as the last word in your name. If you are designing an algorithm, you must write a formal proof of correctness. Please write legibly. 1. [ 1 2 page] (15pts) Solve the following recurrences without using masters theorem. (a) T ( n ) = 2 T ( n/ 2) + n 3 (b) T ( n ) = T ( n ) + 1 (c) T ( n ) = 2 T ( n/ 3) + T ( n/ 4) + n Solution: (a) T ( n ) = 2(2 T ( n/ 2 2 ) + ( n/ 2) 3 ) + n 3 T ( n ) = 2 k T ( n/ 2 k ) + n 3 * (1 + 1 / 2 1 * 2 + 1 / 2 2 * 2 ) ... ) T ( n ) = n + n 3 * c 1 T ( n ) = O ( n 3 ) (b) T ( n ) = T ( n 1 / 2 k ) + k n 1 / 2 k = c 1 / 2 k * lg n = c 1 2 k = lg n T ( n ) = O (lg lg n ) 1 (c) T ( n ) = cn T ( n ) = 2( c n 3 ) + c n 4 + n T ( n ) = ( c 11 12 + 1) n < cn , if c > 12 T ( n ) = O ( n ) Or Replace T ( n/ 4) with T ( n/ 3) T ( n ) = O ( n lg n ) 2. [1 page] (10pts) You are given an array of numbers of size n, where each number in the array is either 0, 1, or 2. Derive an algorithm that only uses comparisons to sort the array in O ( n ). Write the pseudocode for that algorithm and prove that it takes O ( n ). Also give the invariant condition and the proof of correctness Solution: Let A [1 ..N ] be the input array. We maintain three indices i , j and k , and the invariant condition would be that A [1 ..i 1] are 0s, A [ i..j 1] are all 1s, A [ j..k ] are unknown and A [ k + 1 ..n ] are all 2s. Termination will be when j > k . The algorithm is as follows. SORT ( A [1 ..n ] ,i,j,k ) If n = 1 return A If j > k return A If A [ j ] = 0 swap ( A [ i ] ,A [ j ]) return SORT ( A,i + 1 ,j + 1 ,k ) If A [ j ] = 1, return SORT ( A,i,j + 1 ,k ) If A [ j ] = 2 swap ( A [ j ] ,A [ k ]) return SORT ( A,i,j,k 1) Initially i = j = 1 and k = n . In each step the unknown region A [ j..k ] decreases by one element. Termination occurs when k < j . Therefore this algorithm takes O ( n ) time. 3. [1 page] (15pts) In this problem we are interested in solving a modified closest pair problem. This time rather than having n indistinguishable points, there will be n red and n blue points. The task is to find the closest redblue pair (i.e. the minimum distance between a red point and a blue point). Can this problem be solved within the same O ( nlogn ) bound by modifying the divideandconquer algorithm...
View
Full
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.
 Fall '08
 UNGOR
 Algorithms

Click to edit the document details