Homework 1 Solutios MA 5 Fall 017 1. Cosider the searchig problem: Iput A sequece of umbers A = [a 1,..., a ] ad a value v. Output A idex i such that v = A[i] or the special value NIL if v does ot appear i A (a) (0 poits) Write a pseudocode for LINEAR-SEARCH, which scas through the sequece, lookig for v. Usig a loop ivariat, prove that your algorithm is correct. (Make sure that your loop ivariat fulfills the three ecessary properties iitializatio, maiteace, termiatio.) Solutio: LINEAR-SEARCH(A, v) 1 for i 1 to legth[a] do if A[i] = v 3 the retur i retur NIL Correctess: Loop ivariat At the start of each iteratio of the for loop we have A[j] v for all j < i. Iitializatio Before the first loop iteratio the ivariat holds sice the statemet is empty. Maiteace The loop ivariat is maitaied at each iteratio, sice otherwise at the i-th iteratio there is some some j < i such that A[j] = v. However, i that case for the j-th iteratio of the loop the value j is retured, ad there is o i-th iteratio of the loop, a cotradictio. Termiatio Whe the loop termiates, there may be two cases: oe is that it termiates after i legth(a) iteratios, ad returs i, i which case the if coditioal esures that A[i] = v. The other case is that i exceeds legth(a), i this case by the loop ivariat we have that for all j legth(a) A[j] v, this returig NIL is correct. (b) (10 poits) How may elemets of the iput sequece eed to be checked o the avarage, assumig that the elemet beig searched for is equally likely to be ay elemet i the array? How about the worst case? What are the avarage-case ad worst-case ruig times of liear search i Θ otatio? Justify your aswer. 1
Solutio: O avarage 1 (+1) = +1 elemets beig searched for. I the worst case it is. The avarage case ruig time is +1 = Θ() sice for all 1 we have 1 + 1 The worst case ruig time is Θ(). (c) (10 poits) If we assume that A is sorted, the we ca check the midpoit of the sequece agaist v ad elimiate half of the sequece from further cosideratio. BINARY SEARCH is a algorithm that repeats this procedure, halvig the size of the remaiig portio of the sequece each time. Write a pseudocode, either iterative or recursive, for biary search. Solutio:. BINARY-SEARCH(A, p, q, v) 1 if q < p the retur NIL 3 m p + (p q)/ if A[m] = v 5 the retur m 6 if A[m] > v 7 the BINARY-SEARCH(A, 1, m 1, v) else BINARY-SEARCH(A, m + 1, legth(a), v) (d) (10 poits) Use the master method to show that the solutio to the biary-search recurrece T () = T (/) + Θ(1) is T () = Θ(lg ), ad argue that the worst case ruig time for biary search is Θ(lg ). Solutio: We ca use the Master Theorem Case because from a = 1 ad b = we have log b a = 0 = 1, so for k = 0 f() = Θ(1) = Θ( log b a lg k ). This gives that T () = Θ( log b a lg k+1 ) = Θ(lg ). The ruig time of BINARY-SEARCH ca be computed from the followig recurrece equatio: T () = c + T ( ). Thus the above argumet shows that the ruig time is T () = Θ(lg ). (e) (10 poits) Observe that the while loop of the INSERTION SORT procedure (i class) uses a liear search to sca (backward) through the sorted subarray A[1... j 1]. Ca we use biary search istead to improve the overall worst-case ruig time of isertio sort to Θ( lg )?
Solutio: The while loop of procedure INSERTION-SORT scas backward through the sorted array A[1... j 1] to fid the appropriate place for A[j]. The hitch is that the loop ot oly searches for the proper place for A[ j ], but that it also moves each of the array elemets that are bigger tha A[ j ] oe positio to the right. These movemets ca take as much as Θ(j) time, which occurs whe all the j 1 elemets precedig A[j] are larger tha A[j]. We ca use biary search to improve the ruig time of the search to Θ(lg j), but biary search will have o effect o the ruig time of movig the elemets. Therefore, biary search aloe caot improve the worst-case ruig time of INSERTION-SORT to Θ( lg ).. (0 poits) Let f, g : N R be fuctios which are positive for some 0. Prove the equatlity O((f + g) ) = O(f + g ). (Hit: you eed to use the ieaquality betwee geometric ad arithmetic averages.) Solutio: First assume that h O((f + g) ), i.e. there exist c > 0 ad 1 such that 0 h() c(f() + g()) for all 1. Usig the ieaquality betwee geometric ad arithmetic averages we get that if 0 (so f ad g are positive) the thus (f() + g()) f()g(), (f() + g()) = f() + g() + f()g() f() + g() + (f() + g()) which implies that (f() + g()) f() + g(). Thus for max( 0, 1 ) we have that 0 h() c (f() + g() ), i.e. h O(f + g ). For the other directio, if h O(f + g ) i.e. there exist d > 0 ad such that 0 h() d(f() + g()) for all, the usig that for 0 f() + g() f() + g() + f()g() = (f() + g()) we have that for max( 0, ), 0 h() d(f() + g()), i.e. h O((f + g) ). 3. (0 poits) Give asymptotic upper ad lower boud for the recurrece T () = T (/) + T (/) + T (/) +. 3
Solutio: The solutio of the recurrece ca be computed by addig up the odes i the followig tree: 3 3 6..................... Note that if a ode would have a umber < 1 the we write 0 i that ode. Let Σ(i) deote the sum of the odes i lie i. The Σ(1) =, Σ() + + ( = 1 1 ) = 7....... Note that we may have iequality above, i case <. Similarly for sum of the (i + 1)-th row Σ(i + 1) Σ(i) + Σ(i) + Σ(i) ( = Σ(i) 1 1 ) = 7 Σ(i). Thus we get that Σ(i). Summig up the rows we get the followig upper boud: lg T () i=1 i=1 = 1 1 7 =. O the other had is a lower boud for the ruig time. Thus T () = Θ(). We ca also verify that T () = Θ() by the substitutio method. We show that T () = c for some suitable c > 0 costat. We have T () = T ( ) + T ( ) + T ( ) + = c + c + c + = 7 c + = ( 7 c + 1 ).
We eed to fid c such that ( ) 7 c + 1 = c. This equatios has the solutio c =, so we have that T () = c. 5