COMP171 Data Structures and Algorithms Fall 2006 Midterm Examination L1: Dr Qiang Yang L2: Dr Lei Chen Date: 6 Nov 2006 Time: 6-8p.m. Venue: LTA November 7, 2006 Question Marks 1 /12 2 /8 3 /25 4 /7 5 /15 6 /15 7 /18 Total /100 1
1. (12 marks) Recursion Consider the following recursive program. //n is any positive integer //m is a positive integer between 2 and 9 void fun(int n, int m) { if (n<m) cout << n; else { fun(n/m, m); cout << n%m; (a) (3 marks) What is the output for fun(15, 2)? Answer: 1111 (b) (3 marks) Given the following recursive code. Point out the possible errors in the code. //num is a positive integer int fac(int num) { if (num 1) return 1; else { return num*fac(num+1); Answer: The recursive function will never converge to the base case (ie. num 1) as the value passed to the function is always increasing (ie. fac(num+1)). 2
(c) (6 marks) Given the following function which computes fibonacci numbers using recursive function, write a non-recursive version of function f ib. //num is a positive integer int fib(int num) { if (num==0) return 0; if (num==1) return 1; return (fib(num 1)+fib(num 2)); Answer: int fib(int n) { int f[n+1]; f[0] = 0; f[1] = 1; for (int i=2; i<= n; i++) f[i] = f[i-1] + f[i-2]; return f[n]; 3
2. (8 marks) Merge Sort and Insertion Sort (a) (4 marks) Draw a binary tree to show step by step how Merge Sort sorts {142, 543, 123, 65, 453, 879, 572, 434. {142, 543, 123, 65, 453, 879, 572, 434 {142, 543, 123, 65 {453,879, 572, 434 {142, 543 {123, 65 {453, 879 {572, 434 {142 {543 {123 {65 {453 {879 {572 {434 {142, 543 {65, 123 {453, 879 {434,572 {65, 123, 142, 543 {434, 453, 572, 879 {65, 123, 142, 434, 453, 543, 572, 879 (b) (4 marks) Show under what order of input, the insertion sort will have worst-case and best-case situations for sorting the set {142, 543, 123, 65, 453, 879, 572, 434. Worst Case:the element always gets inserted at the front, so all the sorted elements must be moved at each insertion. The ith insertion requires (i-1) comparisons and moves. sorting in ascending order: {879, 572, 543, 453, 434, 142, 123, 65 sorting in descending order: {65, 123, 142, 434, 453, 543, 572, 879 Best Case: the element always gets inserted at the end, so we don t have to move anything, and only compare against the last sorted element. We have (n-1) insertions, each with exactly one comparison and no data moves per insertion. sorting in ascending order: {65, 123, 142, 434, 453, 543, 572, 879 sorting in descending order: {879, 572, 543, 453, 434, 142, 123, 65 4
3. Big-Oh (a) What is the time complexity of the equation T(n) given below?please give the tightest possible bound in big-oh notation. Assume n is a power of 2 (that is n = 2 k for some k). { T (n) = 2T (n/2) + n log n T (1) = 1 Results without derivations do not receive any points. Anawer: T (n = 2 k ) = 2T (n/2) + 2 k k = 2(2T (n/4) + 2 k 1 (k 1)) + 2 k k = 2 2 T (n/4) + 2 k (k 1) + 2 k k = 2 3 T (n/8) + 2 k (k 2) + 2 k (k 1) + 2 k k =... = 2 k T (1) + 2 k (1 + 2 + 3 +... + k) = 2 k ((k + 1) k/2 + 1) = n(((log n + 1) log n)/2 + 1) = O(n log n log n) 5
(b) Give a tightest possible bound for the Big-Oh runtime complexity of the following function in terms of the value of the input parameter n. Results without derivations do not receive any points. (Hint: The complexity of easy(n) is T (n).) 1 int easy(int n){ 2 int a, b, c; 3 if (n <= 1) 4 return 0; 5 else { 6 a = easy(n-1); 7 b = easy(n % 2); 8 c = easy(n-2)/2; 9 return a + b + c; 10 11 Answer: T(N)=T(N-1)+ T(N-2)+ T(1) T(N-1)=T(N-2)+ T(N-3)+ T(1)...... T(2)=T(1)+T(0)+T(1) F(0)*T(N)=F(0)*T(N-1)+ F(0)*T(N-2)+ F(0)*T(1) F(1)*T(N-1)=F(1)*T(N-2)+ F(1)*T(N-3)+ F(1)*T(1) F(2)*T(N-2)=F(2)*T(N-3)+ F(2)*T(N-4)+ F(2)*T(1)......... F(N-2)*T(2)=F(N-2)*T(1)+F(N-2)*T(0)+ F(N-2)*T(1) Sum up T(N)=F(N-3)*T(1)+ F(N-2)*T(1)+F(N-2)*T(0)+ N 2 i=0 F(i)*T(1) =F(N)*T(1)+(F(N)-F(1))*T(1) =2F(N)*T(1)-T(1) (5/3) N 1 = O(2 N ) 6
(c) Multiple Choice: Circle the best answer for each question. If you circle two answers, you will receive zero for that question. i. The recurrence solves to: A. O(n) B. O( n) C. O(log n) D. O(log log n) E. O(log log log n) F. O(1) { T (n) = T ( 3 n) + 1 n > 2 T (n) = 1 n 2 Answer: D.Suppose the recurrence for T(n) stops at some n = a < 2, and takes k steps, then a 3k = n,then k = log 3 (log a n),so the answer is D,O(log log n). ii. If f(n) = n+n log n,which of the following is/are true? 1) O(n) 2) O(n 2 ) 3) Ω(n 2 ) 4) Ω(n log n) 5) Θ(n log n) 6) Θ(n) A. 1) only. B. 1) & 6). C. 2) only. D. 2) & 3). E. 2) & 4) & 5). F. 4) & 5). G. 2) & 3) & 4) & 5). Answer: E. n log n < f(n) < 2n log n,so 5) is correct.then 4 must be correct. Also notice that, 2) is correct,because n log n < n 2. 7
4. In the following code for a stack machine, PUSH X means push X onto the stack, POP X means pop the top of the stack into X, and an operator without an operand (ADD, MULT) means pop the top two items off the stack, perform the indicated operation on them, and push the result back onto the stack.(note: ADD=Addition, MULT=Mulitplication.) 1) PUSH A 2) PUSH B 3) ADD 4) POP T 1 5) PUSH B 6) PUSH C 7) PUSH T 1 8) PUSH T 1 9) MULT 10) MULT 11) ADD 12) POP Z Please write down in the table below the content of the stack after each stack operation (the first two operations have already been given). In addition, please specify values of T 1 and Z using symbols A, B and C. No. The Content of the Stack 0) empty 1) A 2) A, B 3) A + B 4) empty 5) B 6) B, C 7) B, C, T 1 (or A + B) 8) B, C, T 1, T 1 (or B, C, A + B, A + B) (or B, C, T 1, A + B) (or B, C, A + B, T 1 ) 9) B, C, T1 2 (or B, C, (A + B) 2 ) 10) B, C T1 2 (or B, C (A + B) 2 ) 11) B + C T1 2 (or B + C (A + B) 2 ) 12) empty T 1 = A + B, Z = B + C (A + B) 2. 8
5. Heap (a) (10 marks) Table 1 shows an array of numbers. In Table 2, build a min-heap by inserting these numbers one by one from left to right, where you can use X to mean unoccupied. Fill in the results after each of the remaining insertions and heap-restoring operations. Note that the heap is stored in the array starting from array index 1 (so that the index 0 of the array is left empty). Index 1 2 3 4 5 6 Input array 17 10 4 16 33 3 Table 1: Input Table 2: Fill in the slots Index 1 2 3 4 5 6 Input array 17 10 4 16 33 3 Insert 17 17 X X X X X Insert 10 10 17 X X X X Insert 4 4 17 10 X X X Insert 16 4 16 10 17 X X Insert 33 4 16 10 17 33 X Insert 3 3 16 4 17 33 10 9
(b) (5 marks) Heap Sort: For the array in Table 3, perform a heap sort using the same array and no additional array. Fill in the result after each of the deletion and heap-restoration operations. Also show the array after the deleted element is inserted back in order to produce a sorted array in the end. Table 3: Fill in the slots for sorting in increasing order Index 1 2 3 4 5 6 Max Heap 33 17 16 10 4 3 Deletion number 33 17 16 10 4 3 first 17 10 16 3 4 33 second 16 10 4 3 17 33 third 10 3 4 16 17 33 fourth 4 3 10 16 17 33 fifth 3 4 10 16 17 33 sixth 3 4 10 16 17 33 10
6. Multiple Choice Questions (Stack and Linked List) (a) What is the minimum height of the decision trees whose nodes are the comparison operations and whose leave nodes are the linear ordering of N numbers? (A) O(N^2) (B) O(N) (C) O(2^N) (D) Omega(Nlog(log(N))) (E) Omega(log(N!)) (F) Omega(N^2) (G) none of the above. Answer: (G). It should be order log(n). (b) If the variables are suitably initialized and if i remains within appropriated bounds, then the following code implements stack operations Push and Pop when the stack is represented as a vector V [1,..N] and a pointer i. Push: begin V[i] = x ; i := i + 1 ; end Pop: begin i := i - 1; x := V[i] ; end (i) Which of the following gives the correct initialization for this stack implementation? (A) i := 0 (B) i := 1 (C) i := N - 1 (D) i := N (E) None of the above Answer: (B) (ii) If it is assumed that suitable changes in the initialization code were also made, which of the following changes to Push and Pop would yield a correct implementation of stacks? I. Replace the code for Push with that for Pop and vice versa. II. Make Push decrement i and Pop increment i. III. Reverse the order of the statements in both Push and Pop. 11
(A) I only (B) II only (C) III only (D) I and II (E) II and III Answer: (E) 7. Quick Sort Applying the Quick Sort with the median of three methods that you have learnt from the lecture to the given array A[]. Please complete the full sorting process with the help of the following table. You must fill in the table with the contents of A[] and the final positions of the pivot(s) by marking it with p in the space provided. Note: The content of A[] must be in ascending order. Below is the pseudocode of quicksort: (swapping is indicated in line 14) void quicksort(a[], p, r){ int pivot = median_of_three(a[],p,r); // begin partitioning int i = p; int j = r-2; for(;;){ while(a[i] < pivot){ i++; while(a[j] > pivot){ j--; if(i < j) swap(a[i], A[j]); else break; swap(a[i], A[r-1]); <------***** swapping **** // recursive sort each partition quicksort(a, p, i-1); // Sort left partition quicksort(a, i+1, r); // Sort right partition Answer: 12
Note: If the student finishes the trace using either the routine in given above, or the lecture notes, then the student is considered right. Solution 1 - median of three Index 0 1 2 3 4 5 6 7 8 A[] 89 19 65 19 55 38 19 42 38 A[] after 1st swap of pivot 38 19 19 19 42 38 55 65 89 P A[] after 2nd swap of pivot 19 19 19 38 42 38 55 65 89 P P A[] after 3rd swap of pivot 19 19 19 38 38 42 55 65 89 P P A[] after 4th swap of pivot A[] after 5th swap of pivot A[] after 6th swap of pivot A[] after 7th swap of pivot 13
Solution 2 - original Index 0 1 2 3 4 5 6 7 8 A[] 89 19 65 19 55 38 19 42 38 A[] after 1st swap of pivot 19 19 19 38 55 38 42 89 65 P A[] after 2nd swap of pivot 19 19 19 38 55 38 42 65 89 P P A[] after 3rd swap of pivot 19 19 19 38 38 42 55 65 89 P A[] after 4th swap of pivot A[] after 5th swap of pivot A[] after 6th swap of pivot A[] after 7th swap of pivot 14