Comp 363 Study Problems for Test 2 October 24, 2003 Dr. Joh G. Del Greco Name Istructios. There are o special istructios for these practice problems. 1. For Test #2, you should kow... Chapter 1 ad Sectios 2.1 through 2.4 of the text (Sectios 2.5 through 2.7 are optioal sectios) what the three characteristics of divide-ad-coquer algorithms are as discussed i class how QuickSort qualiþes as a divide-ad-coquer algorithm, that is: (a) umber of subproblems the problem is divided ito (b) size of the subproblems (c) whether the recombiatio step is empty or ot how MergeSort works ad how it qualiþes as a divide-ad-coquer algorithm (see questios (a), (b) ad (c) for the correspodig questio o QuickSort) how to demostrate MergeSort o a give array (see p. 125 of the text) howtomergetwosortedlists the recurrece relatio for the worst-cost ruig time of MergeSort how to solve the recurrece relatio for the worst-cost ruig time of MergeSort whe the array is a power of two i legth ad oly comparisos are couted what the efficiecy class of MergeSort is how to geerate a decisio tree for a sortig algorithm like IsertioSort at least how may leaf odes a decisio tree for a sortig algorithm must have for iputs of size that a upper boud o the umber of leaves i a biary tree of depth d is 2 d ad be able to prove it by iductio o d that d dlog 2 #e where d is the depth of a biary tree ad # is the umber of leaves for ay algorithm A that sorts lists of size, iffollowsthatc A () dlog 2!e (ad how to prove this from the previous two bullet items) that if f(x) is a mootoically icreasig fuctio o a iterval [m 1,+1], the R m 1 f(x)dx P i=m f(i) R +1 m f(x)dx how to use the result i the previous bullet item to show that log 2! Θ( log 2 ) P how to use the result i the previous bullet item to show that i k Θ( k+1 ) i=1
how BiarySearch works ad how it qualiþes as a divide-ad-coquer algorithm (see questios (a), (b) ad (c) for the correspodig questio o QuickSort) both versios of BiarySearch, the orecursive ad recursive versios how to demostrate BiarySearch o a give array (that is, keepig track of upper, lower ad middle) what the recurrece relatio for BiarySearch is how to solve the recurrece relatio for BiarySearch whe the array is a power of two i size what the exact solutio to the recurrece relatio for BiarySearch is, eve whe the array is ot a power of two i size how to geerate a decisio tree for BiarySearch for a array of ay size how to compute the average-case ruig time of BiarySearch give the decisio tree (see text, p. 136, problem #1(c)(d)) how to state all three cases of the Master Theorem how to apply the Master Theorem to determie the efficiecy class of the solutio to a recurrece relatio how to perform Strasse s algorithm what the recurrece relatio for Strasse s algorithm is ad how to solve it usig the Master Theorem how to multiply large umbers what the recurrece relatio is for the algorithm for multiplyig large umbers ad how to solve it how c A () is deþed as a sum what iput equivalecig is how to perform a average-case aalysis of LiearSearch uder differet assumptio: X is i the list, X is equally likely to be i the list as ot i the list, etc. what the recurrece relatio is for the average-case ruig time of LiearSearch whe X is i the list what the recurrece relatio is for the average-case ruig time of QuickSort what the average-case ruig time of QuickSort is (that is, the solutio to the recurrece relatio for QuickSort) what the th harmoic umber is what its growth rate is 2. Perform MergeSort o the array below. 16 33 56 11 50 31 7 49 60 2 3. Carefully costruct a decisio tree for SelectioSort o lists of size =3. Assume the lists cotai distict etries. 2
4. Aswer the followig questios. (a) Let f(x) be a mootoe decreasig fuctio o the iterval [m 1,+1]. Prove that R +1 f(x)dx P f(i) R f(x)dx. m m 1 i=m (b) Usig the result i part (a), prove that the th harmoic umber H = P satisþes l( +1) H l +1. (Hit: Cosider f(x) =1/x o a suitable iterval.) (c) Prove that H Θ(l ) usig the deþitio of Θ. (That is, do ot use limits.) 5. Suppose a certai array has the followig values for its compoets. -7-4 -1 0 3 4 5 6 9 11 13 14 15 19 23 Perform a trace of BiarySearch for the search key X =16. That is, write dow the sequece of values for the variables upper, lower ad middle. 6. Complete the followig questios about BiarySearch. (a) Costruct a decisio tree for BiarySearch for lists of size =15. (b) Fid the average-case ruig time for BiarySearch i a successful search of a array of size =15. (See text, p. 136, problem #1(c).) (c) Fid the average-case ruig time for BiarySearch i a usuccessful search of a array of size =15. (Your may assume that searches for keys i each of the 16 itervals formed by the arrays elemets are equally likely.) 7. Aswer the followig questios about the average-case ruig time of LiearSearch. (a) Write dow a recurrece relatio for the average-case ruig time of LiearSearch whe the search key X is equally likely ot to be i the list as to be ay of the elemets of the list. (b) Solve the recurrece relatio i part (a) by urollig. 8. Solve the followig recurreces usig the Master Theorem. (a) T () =5T (/2) + 2 log 2 (b) T () =27T (/3) + 3 log (c) T () =3T (/4) + log 9. What is the largest value of a such that if we could multiply two 3 3 matrices usig oly a multiplicatios, the we could multiply two matrices i time faster tha log 7. (Recall that log 7 is the ruig time of Strasse s algorithm.) What would be the ruig time of the resultig algorithm? 10. Use divide-ad-coquer multiplicatio to multiply the umbers 2,345 ad 6,789. 3 i=1 1 i
11. Show how two biomials ax + b ad cx + d ca be multiplied usig oly three multiplicatios. 12. Based o your experiece i the previous problem, show how to multiply two th degree polyomials i log 3 time. 13. The purpose of this exteded problem is to ivestigate the average-case ruig time of BiarySearch. You should cosider it a jourey durig which you ca tur back at ay time. (The istructor strogly suggests that you at least do parts (a) through (d).) Fiishig the jourey however is very satisfyig! Oe way to aalyze the average-case ruig time of BiarySearch is to costruct a recurrece relatio for it. LetussupposethatthesearchkeyX is i the list ad that X is equally likely to be i ay of the positios. Cosider the followig recurrece relatio where A is BiarySearch. (For coveiece, let f() =c A ().) 0,if =0 f() = 1,if =1 d 2 e 1 f b 2 1 + 2 c f 2 +1,if 2 (a) Explai the Þrst two lies i the recurrece relatio for f(). (b) Now cosider the third lie of the recurrece relatio. 1. What does the quatity d 2 e 1 represet? Why is it beig multiplied by f 2 1? 2. Explai the secod term of the sum. 3. Fially, explai why the last term of the sum is 1. (c) Solvig the recurrece is difficult! Nevertheless, some progress ca be made by simplifyig it somewhat. Multiply the recurrece through by, adthelet g() =f(). Show that the recurrece ca be writte as 0,if =0 g() = 1,if =1 g 2 1 + g 2 +,if 2 (d) Show that g() g( 1) = g 2 g 2 1 +1(Use the easily prove idetity 1 2 = 2 1 i your derivatio.) (e) Let h() =g() g( 1). Show that ½ 1,if =1 h() = h 2 +1,if 2 (f) The recurrece i part (e) has already bee solved i class! Why did we solve this recurrece, ad what was the solutio? (g) From part (f), show that g() =g( 1) + dlog 2 ( +1)e. (h) Uroll the equatio i part (g) to show that g() = +1 P dlog 2 e. 4
(i) Sice g() =f() (recall part (c)), show that f() = 1 (j) Evaluatig the sum +1 P It ca be show that +1 P dlog 2 e. dlog 2 e i the formula for f() i part (i) is rather tricky! X+1 dlog 2 e =( +1)dlog 2 ( +1)e 2 dlog 2 (+1)e +1 Give this result, show that the Þal aswer for f() is µ 2 dlog (+1)e 2 dlog f() =dlog 2 ( +1)e 2 ( +1)e 1 (k) The Þrst term i the formula for f(), amelydlog 2 ( +1)e, istheworst-case ruig time of BiarySearch. (This was prove i class. See your otes.) So otice what the formula is sayig. The average-case ruig time of BiarySearch is equal to its worst-case ruig time mius the quatity i paretheses. The crucial questio is how large ca this quatity i the parethesis ca get? Plug i some umerical values for ad try to get a feel for what is happeig. Make a cojecture as to how large the quatity ca get. What does your cojecture say about the average-ruig time of BiarySearch? Is it disappoitig to you? 5