Yeditepe University Department of Computer Engineering CSE 311 Algorithm Analisys FALL 2009 MIDTERM Student Number : ___________ Student Name : ______________________ 1 6 2 3 4 5 16 10 18 8 6 7 8 14 12 16 Total 100 There are 8 questions in 5 pages. The exam duration is two hours. 1. (6 points) Find the algorithm complexity (Big-Oh) of the following codes. Assume the input to these functions is a one dimensional array A[n]. (a) total = 0; for(i=1;i<n;i++) for(j=i+1;j≤n;j++) total++; n T(n) = ∑n−1 i=0 ∑j=i+1 1 T(n) = ∑n−1 i=0 (n − (i + 1) + 1) T(n) = ∑n−1 i=0 (n − i − 1 + 1) T(n) = ∑n−1 i=0 (n − i) n−1 T(n) = ∑n−1 i=0 n − ∑i=0 i T(n) = n2 − T(n) = T(n) = (n−1)n 2 2n2 −(n2 −n) n2 +n 2 2 T(n) = O(n2) (b) i = n; while (i>=0){ sum = sum + A[n]; i = i+2; } T(n) O(n) = ∑𝑛/2 𝑘=0 1 (c) s=0; sum1=0; sum2=0; for (int i=1; i< (1500*n) ;i++){ sum1 = sum1 +1; } for (int i=1; i< n ;i++) sum2 = sum2 +A[i]; for (int i=1;i<n;i*=2) { s= s+A[i]; } We have three parts in the algorithm: T(n) = 15000n+n+log n Dominant term is n, so O(n) 2. (16 points) Definitions of Big 0, and are given below: O(g(n))={f(n):there exists a positive constant c and n0 such that 0f(n)cg(n), n, nn0} (g(n))={f(n):there exists a positive constant c and n0 such that 0cg(n)f(n), n, nn0} (g(n))={f(n):there exists positive constants c 1,c2 and n0 such that 0c1g(n)f(n)c2g(n), n, nn0} Using the definitions prove that a.) n2+4n+4 = O(n2) We have to prove that: 0n2+4n+4cn2 for a positive constant c and n0 for all nn0 Let’s divide all sides by n 2 (note that n is always positive) 01+4/n+4/n2c If we pick c as 6 and n0 as 2, the equation holds for all nn0. 01+4/n+4/n2 6 is true for all n2. b.) n3 ≠ O(n2) We have to disprove the following: 0n3cn2 for a positive constant c and n0 for all nn0 Let’s divide all sides by n 2 (note that n is always positive) 0nc It is not possible to pick a constant c to hold this inequality, since n is not a constant. Therefore n3 ≠ O(n2) c.) n2+3n = (n2) We have to prove that: 0 c1n2n2+3nc2n2 for positive constants c1 and c2 and n0 for all nn0 Let’s divide all sides by n 2 (note that n is always positive) 0 c11+3/nc2 If we pick c1 as 1, c2 as 3 and n0 as 3, the equation holds for all nn0. 0 1 1+3/n 3 is true for all n 3. d.) n3+3n= (n) We have to prove that: 0cnn3+3n for a positive constant c and n0 for all nn0 If we pick c as 1 and n0 as 1, the equation holds for all nn0. 0n n3+3n is true for all n1. 3. (10 points) a. What are the Big-Ohs of the following expressions? (No proof is necessary.) i. 𝑛log3 5 +5n2 O(n2) ii. n!+9n O(n!) iii. n+log n O(n) iv. n2+ nlog n O(n2) v. n8+(3/2)n O((3/2)n) vi. log log n + log n O(log n) vii. n3+2n O(2n) viii. n+(n2/log n) O(n2/log n) 3 ix. ( )n+n2 4 O(n2) x. n1/2+n3/2+n4/5 O(n3/2) b. Order the Big-Ohs in part a from smallest to the biggest O(log n) < O(n) < O(n3/2)< O(n2/log n)< O(n2)< O((3/2)n)< O(2n)< O(n!) 4. (18 points) The product of a x a x a x a x......x a for n times is denoted by an and called nth power of an integer a. Example: 2x 2 x 2 x 2 x 2 is written as 25, where 2 is the base and 5 is the index. a. Write a recursive divide and conquer algorithm that finds n th power of an integer. Explain the code by making comments on the code. You may use any programming language or pseudocode. Power (a,n) if a is 1 or n is 1 return a else a x = ; 2 p = power (a,x) if a is even return p*p else return p*p*a end if end if b. What is the efficiency equation T(n) of your code? T(n) = T(n/2)+ 1 c. What is the growth rate of your code? (You can use any method to prove it.) T(n) = Θ(lg n) 5. 8 points Suppose we have the following cities and the distances for 5 cities. We would like to find the minimum length tour for Travelling Salesman Problem using exhaustive search. Suppose tour starts at city A. B 2 4 2 A E 5 3 8 6 5 7 5 C D a. How many different paths do exist? Show at least four possible paths and their lengths. 24 A-B-E-D-C-A = 24 A-E-B-C-D-A = 20 A-C-B-D-E-A = 26 A-B-D-C-E-A = 21 b. How many different paths do exist if we add one more city to our graph? 120 c. What is the algorithmic complexity of examining every possible path using exhaustive search? O((n-1)!) 6. (15 points) a. Find the runtime T(n) function for the following function and then using Master Theorem calculate the time complexity. function funcA(ArrayA,leftIndex,rightIndex,increment){ if (leftIndex==rightIndex) return; for (i=leftIndex;i<rightIndex;i++) ArrayA[i]=Array[i]+increment; rightIndex - leftIndex ; 2 mid = leftindex + funcA(ArrayA,leftIndex,mid,increment); funcA(ArrayA,mid+1, rightIndex,increment); funcA(ArrayA,leftIndex,mid,increment+1); } T(n) = 3 T(n/2) + n Master Theorem Case 1: a = 3 b = 2 f(n)=n T(n) = Θ(n𝑛log2 3 ) b. Find the runtime T(n) function for the following function and then using Substitution Method calculate the time complexity. Use induction to prove your result. function fact (n) { if n=0 return 1 else return n * fact(n-1) } T(n)= T(n-1)+1 T(n) = Θ(n) For proving with substitution method, you need to use induction. 7. (12 points) The Master Theorem applies to recurrences of the following form: T (n) = aT (n/b) + f(n) where a ≥ 1 and b > 1 are constants and f(n) is an asymptotically positive function. There are 3 cases: 1. If f(n) = O(𝑛log𝑏 𝑎−𝜖 ) for some constant 𝜖 > 0, then T (n) = Θ (nlogba). 2. If f(n) = Θ (𝑛log𝑏 𝑎 log k n) with1 k ≥0, then T (n) = Θ (𝑛log𝑏 𝑎 log k+1 n). 3. If f(n) =Ω (𝑛log𝑏 𝑎+𝜖 ) with 𝜖 > 0, and f(n) satisfies the regularity condition, then T (n) = Θ(f(n)). Regularity condition: af(n/b) ≤ cf(n) for some constant c < 1 and all sufficiently large n. Apply master theorem to the following equations. Show your work, and calculate the complexities. a. T (n) = 16T (n/4)+ n Master Theorem Case 1: a = 3 b = 2 f(n)=n T(n) = Θ(nlg3) b. T(n) = 3T (n/4)+ n log n Master Theorem Case 3: a = 3 b = 4 f(n)=n log n T(n) = Θ(nlogn) c. T (n) = 0.5T (n/4)+ 1 Master Theorem can’t be applied: a = 0.5 b = 2 f(n)=1 a<1 d. T(n) =3T (n/3)+ √𝑛 Master Theorem Case 1: a = 3 b = 3 f(n)= √𝑛 T(n) = Θ(n) 8. (16 points) In the following table, you are to examine 5 different sorting algorithms. The algorithms are randomly distributed within the table. Suppose there is an unsorted string array, we are going to sort it. The unsorted array is displayed in the first column of the table, and the sorting transform the array into the one in the second column. We have applied 5 sorting algorithms to the same array with the same original positioning. Then at some point of the execution of the algorithms, we have stopped the execution and printed the arrays as they are at that specific time. So we have 5 semi-sorted arrays which can give you a clue about the algorithms used. Based on the layout of array in columns I to V, figure out which algorithm is used in which column. The used algorithms are Bubble Sort, Insertion Sort, Selection Sort, Quicksort and Merge Sort. ORIGINAL SORTED I II III IV V bath acid acid acid acid azur acid tile ajar bath anti ajar anti anti nine anti game amid anti acid amid mode amid lost bath amid away away lost away mode cash away amid bath game azur nine game azur ajar ajar acid bath tile lost bath bath cash anti bold anti mode nine cool cool cash cash cash nine cash bold doom amid cool amid tile mode cash fame away doom away ajar lost doom bold fame etik fame away fame last etik ajar fame ajar azur tile nine azur zoom game zoom bold zoom zoom game cool last cool cool cool fame last doom lost doom doom doom mode lost last mode last etik last tile mode bold nine bold fame bold game nine etik tile etik last etik etik tile azur zoom azur zoom game lost zoom a. Please write which algorithm is used in the following columns : Column I Column II Column III Column IV Column V :…………..…Insertion…………………………….. :………………Merge…………………………….. :………….…Selection………………………………. :………….…Quick………………………………. :………………Bubble…………………………….. b. Give average time complexities of each algorithm Bubble :…………..…n2…………………………….. Selection :………………n2…………………………….. Quicksort :………….…n lg n ………………………………. Insertion :………….……n 2……………………………. Merge :…………………………n lg n…………………..