CS395/Ma395 (Sprig 2005) Test Oe Name: Page 1 Test Oe (Aswer Key) CS395/Ma395: Aalysis of Algorithms This is a closed book, closed otes, 70 miute examiatio. It is worth 100 poits. There are twelve (12) questios o five (5) pages. Please put your ame o each page of this examiatio. Aswer Some of these aswers have far more detail tha was required from the test questio. For example, there are questios that ask for oly the recurrece, but the aswer has a full solutio. This is to help yo use this aswer key to prepare for future exams.
CS395/Ma395 (Sprig 2005) Test Oe Name: Page 2 Questio 1 [6 poits] Prove or disprove each of the followig: 1. log Ω( 2 ) 2. 3 Θ(2 ) ( + 2) 2 O( 4 ) Aswer 1. 2. log lim 2 log = lim = lim 1 = 0 (by simple maipulatios ad L Hôpitals rule) ad so log O( 2 ), but log Ω( 2 ). 3 ( ) 3 lim 2 = lim = 2 So 2 O(3 ), but 2 Ω(3 ) ad hece 3 Θ(2 ). ( + 2) 2 = 2 + 4 + 4 By the max rule, this is i O( 2 ), ad O( 2 ) O( 4 ) sice lim 2 1 lim = 0. 2 So ( + 2) 2 O( 4 ), but ( + 2) 2 Ω( 4 ). 4 = Questio 2 [5 poits] Show that the fuctio f() = 2 is 2-smooth. Aswer We eed to show that f() is evetually o-decreasig, ad that f(2) O(f()), where f() = 2. f() = 2 2 + 2 + 1 = ( + 1) 2 = f( + 1) for all 0, so f is clearly evetually odecreasig. Also, f(2) = (2) 2 = 4 2 O( 2 ) sice oe ca igore the costat factor of 4.
CS395/Ma395 (Sprig 2005) Test Oe Name: Page 3 Questio 3 [10 poits] Suppose algorithm Foo() has 2 istaces of size (for ay ) ad it requires steps for 2 1 of them, ad 2 + steps for the oe remaiig istace. What is the average case complexity of Foo()? Aswer It is the average umber of steps required for all istaces of size, which is ( (2 1)) + (2 + ) = 2 + 2 = + 1 2 2 So the umber of steps i the average case, assumig all iput istaces equally likely, is Θ() far better tha the worst case behavior of Θ(2 ). Questio 4 [10 poits] Suppose that algorithm Bar() uses at most Θ(2 ) i the worst case, but that k repetitios of Bar() always requires Θ(3k) time. What is the worst case ad amortized complexity of Foo()? Aswer Worst case is, by defiitio, Θ(2 ). But amortized complexity is Θ(3k/k) = Θ(1), per ivocatio. Questio 5 [5 poits] Suppose the algorithm MyAlg is O( 2 is a power of 2). What ca you say about the complexity of MyAlg, give the results of questio 2? Aswer We eed to show that f(2) O(f()), where f() = 2. But f(2) = (2) 2 = 4 2 O( 2 ) sice oe ca igore the costat factor of 4.
CS395/Ma395 (Sprig 2005) Test Oe Name: Page 4 Questio 6 [10 poits] Express the complexity of the followig algorithm as a recurrece relatio (do ot solve the recurrece!) Procedure fu(); { if ( > 0) { LoadsOfFu = 0; for i = 1 to { LoadsOfFu++; } retur (fu(/2) + fu(/2) + LoadsOfFu); // / is iteger divisio } else retur 1; } Aswer Let T () be the time for this algorithm i iput. The the time required is T () = + 2T (/2) + c This is the time for the loop, ad for the two recursive calls. It assumes that additio ad multiplicatio are elemetary operatios, ad that the istaces sizes are the values of. Questio 7 [4 poits] Suppose you test algorithm Foo o all iputs up to size = 10, ad it requires time as follows: 1 2 3 4 5 6 7 8 9 10 Time 2 5 9 17 33 65 129 257 513 1025 Ca you coclude that Foo is i Θ(2 )? Why or why ot? Aswer No, you caot. Asymptotic otatio is expressed i terms of behavior for sufficietly large. We have o assurace that = 10 is large eough these could all be special cases for the algorithm. Besides, you caot aalyze a algorithm from empirical behavior, that is aalyzig a process, ot a algorithm.
CS395/Ma395 (Sprig 2005) Test Oe Name: Page 5 Questio 8 [10 poits] Aalyze the worst case complexity of the followig algorithm. Procedure Foo(); { for i = 1 to do { for j = 1 to do { for k = j to do { cout << "Hello!"; } } } } Aswer The barometer will be the umber of Hello messages prited, ad we assume all operatios are uit time. The cout << will execute oce for every selectio of i,j,k, regardless of their order (sice oly oe order, amely i < j < k is relevat). There are O( ( ) 3 ) = O( 3 ) ways for this to happe. Or, otice that the two ier loops produce + ( 1) + + 2 + 1 = i=1 = ( + 1)/2 couts. These two loops get executed times, so the total umber of couts is ( + 1)/2 = ( 3 + 2 )/2 Θ( 3 ) total. Alteratively, make a table of the possible values for the three idex variables: i j k 1 1 1 2 2 2 3 3.. 2 1 1 2 2 2 3 3.. 3 1 1... 1 1..
CS395/Ma395 (Sprig 2005) Test Oe Name: Page 6 Questio 9 [10 poits] What is the characteristic polyomial for the followig recurrece? Aswer This yields t = 6t 1 9t 2 + 2 t 6t 1 + 9t 2 = 2 The characteristic polyomial for this recurrece is (x 2 6x + 9)(x 2) 2 = (x 3) 2 (x 2) 2 Questio 10 [10 poits] The recurrece t +1 5t +6 = 2 has a characteristic polyomial of (x 3)(x 2) 3 What is the closed form for this recurrece? Aswer This has root 3 with multiplicity 1 ad root 2 with multiplicity 3, so the closed form solutio is t = c 0 3 + c 1 2 + c 2 2 + c 3 2 2 for some costats c 0, c 1, c 2, ad c 3. This is clearly O(3 ).
CS395/Ma395 (Sprig 2005) Test Oe Name: Page 7 Questio 11 [10 poits] prove that the ith biomial tree, B i, has 2 i odes. Aswer The proof is by recursio o i. Base case (i = 0). B 0 has 1 ode by defiitio, ad 1 = 2 0. Assume the ith biomial tree has 2 i odes for 0 i k, ad prove that B k+1 has 2 k+1 odes. The umber of odes i B k+1 is oe, for the root, plus oe for each subtree of the root. By defiitio, these subtrees are B 0, B 1... B k. So, usig the iductive assumptio, B k+1 has 1+2 0 +2 1 + +2 k = 1+(2 k+1 1) = 2 k+1 odes, which was to be show. May people oticed that is is possible to defie biomial trees a differet way. Our defiitio i class was: B 0 is a sigle ode, B i+1 is a root ode with each of B 0... B i as a subtree. The above proof follows that defiitio. Alteratively, oe ca defie biomial trees as follows: B 0 is a sigle ode, B i+1 is B i with a copy of B i added as a subtree to the root of the first B i. Note that these are differet defiitios, but they defie the same objects (the proof of that fact is a good exercise, do t just take it for grated! The proof is by iductio.) Give this secod defiitio, oe ca prove that B i has 2 i odes as follows: Base case (i = 0). B 0 has a sigle ode by defiitio, ad 1 = 2 0. Assume the ith biomial tree has 2 k odes, ad prove that B k+1 has 2 k+1 odes. The umber of odes i B k+1 by defiitio is 2 2 k, usig the iductive assumptio, sice B k+1 comprises two copies of B k. But 2 2 k = 2 k+1, which was to be show. This ca also be prove, rather cleverly, by expressig the size of B i as a recurrece: s(0) = 1 ad s(i + 1) = 2s(i). Oe ca the solve this recurrece. The characteristic polyomial is: x 2 = 0, which has a closed form of c 1 2 + c 2 = 0. Solvig for the costats, give the iitial coditio (which is ecessary i this case) gives that c 2 = 0 ad c 1 = 1. So, the closed form for the recurrece is s(i) = 2 i, which was to be show! Note: bioomial trees are ot the same thig as biary trees. Several of you cofused the two, eve though I aouced durig the exam that the questio was ideed about the former!
CS395/Ma395 (Sprig 2005) Test Oe Name: Page 8 Questio 12 [10 poits] Why is the isert operatio for regular heaps (ot biomial heaps) a O(log ) operatio, whe the heap has elemets i it? Aswer Because regular heaps are essetially complete biary trees, ad so the height of the data structure is O(log ). The height of the structure is the maximum umber of data exchages required to move a elemet from the ext available slot to where it belogs, i order to restore the heap property. I gave extra credit for provig that the height of a full biary tree is log.