COMP85 Midterm Exam Departmet of Mathematics Fall 010/011 - November 8, 010 Name: Studet Number: Please fiish withi 90 miutes. All poits above 100 are cosidered as bous poit. You ca reach maximal 1 poits. Good Luck!!Mustafa Riza 1. Asymptotic Notatios: (a) Use the formal defiitios of O, Θ, ad Ω to determie weather the followig assertios are true or false. i. ( + 1)/ O( 3 ) ( + 1)/ ( 3 1 lim 3 = lim + ) 1 3 = lim 1 + 1 Therefore the statemet is true. ii. 0 + 7 + 3 Θ() = 0 iii. lim 0 + 7 + 3 = lim Therefore the statemet is true. 3 O() 0 + 7 + 3 = lim 0 + 7 + 3 = 0 = cost Therefore the statemet is true. (b) Cosider the followig Algorithm: Algorithm 1 Eigma(A[0.. 1, 0.. 1] lim 3 = lim /3 = 0 1: {Iput: A matrix A[0.. 1, 0.. 1] of real umbers} : for i 0 to do 3: for j i + 1 to 1 do 4: if A[i, j] A[j, i] the 5: retur false 6: ed if 7: ed for 8: ed for 9: retur true (a) What does this algorithm compute? ( P) This program checks if the matrix is symmetric or ot. 1
(b) What is its basic operatio? (3 P) The basic operatio is the compariso of the matrix elemets. (c) How may times is the basic operatio executed ad what is the efficiecy class of this altorithm? Depedig o if the matrix is symmetric or ot the umber of comparisos differ. I the best case the first compariso shows that the matrix is ot symmetric so i the best case we have Θ(1) efficiecy class. I the worst case we have to perform / comparisos, so i the worst case we have Θ( ) efficiecy class. I average we have to calculate the followig: 1 1 i = i=0 ( 1) = 1 Θ(). Recurrece Relatios: (10 P) (a) Solve the recurrece relatio: x() = x( 1) +, for > 0, x(0) = 0 x() = x( 1) + x() = x( ) + ( 1) + x() = x( 3) + ( 3) + ( 1) +. =. x() = x(0) + 1 + + + x() = 1 ( + 1) (b) Solve the recurrece relatio: Substitute = k x() = x(/) +, for > 1, x(1) = 1 (Solve for = k ) x() = x( k ) + k x() = x( k ) + k 1 + k x() = x( k 3 ) + k + k 1 + k. =. x() = x(0) + 1 + + + k + k 1 + k x() = 1 + 1 + + + k + k 1 + k k x() = i = k+1 1 i=0 back substitutio k = x() = 1
3. Brute Force Algorithms: (1 P) (a) For the the Selectio Sort Algorithm give below Algorithm SelectioSort(A[0.. 1] 1: {Sorts a give array by selectio sort} : {Iput: A usorted Array A[0.. 1] of elemets.} 3: {Output: A sorted Array A[0.. 1] i ascedig order.} 4: for i 0 to do 5: mi i 6: for j i + 1 to 1 do 7: if A[j] < A[mi] the 8: mi j 9: ed if 10: swap A[i] ad A[mi] 11: ed for 1: ed for (a) Please sort the list A, L, G, O, R, I, T, H, M i alphabetical order usig Selectio Sort Algorithm give below. A L G O R I T H M A G L O R I T H M A G H O R I T L M A G H I R O T L M A G H I L O T R M A G H I L M T R O A G H I L M O R T A G H I L M O R T (b) What is the basic operatio of the selectio sort algorithm? ( P) The basic operatio is compariso of elemets. (c) What is the efficiecy class of the selectio sort algorithm? The efficiecy class is Θ( ) 4. Divide ad Coquer: (35P) (a) Usig the Master Theorem, fid the order of growth of the followig recurreces: Recallig the Master Theorem: Theorem 1 If f() Θ( d ) with d 0 i the recurrece relatio T () = at (/b) + f(), the Θ( d ) if a < b d T () = Θ( d log ) if a = b d Θ( log b a ) if a > b d. i. T () = 4T (/) +, T (1) = 1. Accordig to the master theorem 1 we have a = 4, b =, d = 1, so we have the case of 4 > 1, i.e. T () = Θ( log 4 ) = Θ( ) 3
ii. T () = 4T (/) + 3, T (1) = 1. Accordig to the master theorem 1 we have a = 4, b =, d = 3, so we have the case of 4 < 3, i.e. T () = Θ( 3 ) (b) Apply the mergesort algorithm to sort the list A, L, G, O, R, I, T, H, M i alphabetical order. ALGORITHM ALGOR ITHM ALG OR IT HM AL G O R I T H M A L OR IT HM AL HIMT AGL AGLOR AGHILMORT (c) Give is the Quicksort Algorithm as followig: The partitio fuctio partitios the subarray usig a pivot key, where all elemets less tha the pivot key are put ito the array A[l..s 1] ad all elemets greater tha the pivot key are put ito the array A[s + 1..r]. The pivot key will be put as first elemet of the secod array, i.e. A[s]. i. State the recurrece relatio for umber of key comparisos for the best case ad solve this recurrece relatio to get the exact umber of key comparisos. (10 P) 4
Algorithm 3 Quicksort(A[l..r] 1: {Sorts a subarray by quicksort} : {Iput: A usorted subarray A[l..r] of A[0.. 1], defied by its left ad right idices l ad r} 3: {Output: Subarray A[l..r] i odecreasig order.} 4: if l < r the 5: s P artitio(a[l..r]) {s is the split positio} 6: Quicksort(A[l..s 1]) 7: Quicksort(A[s..r]) 8: ed if I the best case the Quicksort algorithm works like the Mergesort algorithm so its recursio relatio is give as: C() = C(/) + Usig the Master Theorem Theorem 1 we ca easily verify that C() Θ( log ). ii. Describe the worst case sceario ad state the umber of comparisos i the worst case sceario usig the Θ otatio. I the worst case the pivot key is either the smallest or the biggest elemet of the list, so that the other elemets are either all greater or all smaller. So the we ca ot really say that we have a partitio. I this case the Algorithm works like the selectio sort algorithm ad its efficiecy class is Θ( ). iii. Sort the list A, L, G, O, R, I, T, H, M usig the quicksort algorithm for the partitio fuctio usig the first elemet of the subarray as pivot key. A G I H L O R T M A G I H L M O R T 5. Biary Trees: (15 P) (a) Draw the biary search tree by isertig the elemets of the list L, O, G, A, R, I, T, H, M oe by oe to the biary search tree. (8 P) L G O A I M R H T (b) Perform pre-, i-, ad postorder traversal for the biary search tree i questio 5a. (7 P) pre-order: i-odert: post-order: L G A I H O M R T A H I G M T R O L 5
6. Decrease ad Coquer: Give is the followig Isertio Sort Algorithm: (15 P) Algorithm 4 IsertioSort(A[l..r]) 1: {Sorts a array by isertiosort} : {Iput: A usorted array A[0.. 1] of elemets.} 3: {Output: A array A[0.. 1] sorted i odecreasig order.} 4: for i 1 to 1 do 5: v A[i] 6: j i 1 7: while j 0 ad A[j] > v do 8: A[j + 1] A[j] 9: j j 1 10: ed while 11: A[j + 1] v 1: ed for (a) Use the isertio sort algorithm to sort the array A, L, G, O, R, I, T, H, M. A G L O R I T H M A G L O R I T H M A G L O R I T H M A G I L O R T H M A G I L O R T H M A G H I L O R T M Note: The bold face letters deote the sorted array. We start at the secod positio as oe elemet i a array is by defiitio already sorted. The we go oe by oe through the array by comparig startig from the biggest elemet to the smallest elemet i the sorted array. Oce we have foud the correct positio we isert the elemet ito the correct positio. (b) Describe the worst case sceario for the isertio sort algorithm ad state the umber of key comparisios usig the Θ otatio. I the worst case the array has to be sorted i reverse order so we have to carry out 1 + + 3 + 1 comparisos, i.e. Θ( ) comparisos. (c) Describe the best case sceario for the isertio sort algorithm ad state the umber of key comparisos usig the Θ otatio. I the best case the array is already sorted. Therefore for each step we have to perform exactly oe compariso, i.e. i total we have to perform Θ() comparisos for a array with elemets. 6