Cmsc 250 Intro to Algorithms Study guide MidTerm Test: Practice Problems You may send me the solutions of the problems for feedback A. Efficiency 1. True or False? Circle the correct answers (may be more than one correct answer) N2 + (logN)6 = O((N3logN) 3) N2 + logN = O(NlogN) , N2+logN = O(N2) log(N2) = O(N2 logN), N + logN = O(N) NlogN = O(N + logN) (log(N2))2 = O(N2 logN), (log(N2))2 = O(N2), Nlog(N2) = O(N), (log(N2))2 = O(log(N)), log(N2) = O(log(N)) 2. Fill in the blanks: 1. N +( logN)3 =( ) 2. N + NlogN =( ) 3. N2 +N4 ( logN)3 = ( ) 4. N2+logN =( ) 5. N3logN =( ) 6. log(N2) =( ) 7. (log(N2))2 =( ) 3. Determine the complexity for the following loops, using the Big-Oh notation: a. for(i = 0; i < N; i++) { for(j = i +1; j < N; j++) { if(a[j-1] > a[j]) { t = a[i]; a[i] = a[j]; a[j] = t; } } 1 } b. private void percolateDown(int hole) { int child; AnyType tmp = array[hole]; for( ; hole*2 <= currentSize; hole = child) { child = hole * 2; if( child != currentSize && array[child+1].compareTo( array[child]) < 0) child++; if( array[child].compareTo(tmp) < 0) array[hole] = array[child]; else break; // found position to insert } array[hole] = tmp; } 4. Consider the following recursive algorithms 4.1. Algorithm S(n) //Input: A positive integer n //Output: to be determined by you if n = 1 return 1 else return S(n − 1) + n ∗ n ∗ n a. What does it compute? b. Set up a recurrence relation for the run time of the algorithm and solve it. c. Determine the efficiency class of this algorithm 4.2. Algorithm Q(n) //Input: A positive integer n if n = 1 return 1 else return Q(n − 1) + 2 ∗ n − 1 a. Set up a recurrence relation for Q(n) and solve it to determine the output of the algorithm 2 b. Set up a recurrence relation for the run time of the algorithm and solve it. c. Determine the efficiency class of this algorithm 5. Write a pseudocode for a recursive algorithm for finding the position of the largest element in an array of n numbers. Set up a recurrence relation for the run time of the algorithm and solve it. 6. Write a pseudocode for a recursive algorithm that determines whether a given word is a palindrome. A given word is a palindrome if it can be read backwards, for example pop, rotor, noon. Set up and solve a recurrence relation for the run time of the algorithm and solve it. 7. Write a pseudocode for a recursive algorithm that reverses the contents of an array. Set up and solve a recurrence relation for the run time of the algorithm and solve it. 8. Write a pseudocode for a recursive algorithm that traverses a binary tree in pre-order. Set up and solve a recurrence relation for the run time of the algorithm and solve it. 9. Write a pseudocode for a recursive algorithm that counts the leaves in a binary tree. Set up and solve a recurrence relation for the run time of the algorithm and solve it. 10. Write a pseudocode for a recursive algorithm that computes the height of a binary tree. Set up and solve a recurrence relation for the run time of the algorithm and solve it. 11. Solve the following recurrence relations, with initial condition T(1) = 1 11.1 T(N) = T(N/2) + LogN 11.2 T(N) = T(N/2) + N 11.3 T(N) = 2T(N/2) + N 11.4 T(N) = 2T(N/2) + NLogN 11.5 T(N) = 2T(N/2) + N2 11.6 T(N) = T(N-1) + N 11.7 T(N) = T(N-1) + 1 11.8 T(N) = 2T(N-1) + 1 B. Sorting 12. Match the descriptions on the left with the names of sorting algorithms on the right 3 Assumes part of the array is sorted, inserts the next element there. Quicksort Splits the array into two, sorts recursively and then merges MergeSort Splits the array in two sets and a middle element: smaller to the left, bigger to the right. Then sorts recursively each set Bubble Compares and swaps adjacent elements. Several passes are needed Insertion 13. Describe briefly (but adequately) the operation of each of the following sorting algorithms: selection sort, insertion sort, bubble sort, mergesort, quick sort. 14. Selection sort is best used for (circle the correct answer): a. Small number of records b. Large number of records c. Almost sorted files d. Large records 15. Insertion sort is best used for (circle the correct answer): a. Small number of records b. Large number of records c. Almost sorted files d. Large records 16. Which algorithm is usually very fast, however in certain cases its run time may increase to O(N2), and that is why it is usually not used for military and life-supporting applications. Explain why the run time may increase to O(N2) 17. What is the role of the pivot in quicksort? Describe at least three ways to choose it. Discuss their advantages and disadvantages. 18. Which algorithm is not used for sorting in main memory and why? 4 19. Write down the recurrence relation for the worst case of quicksort and solve it. 20. Write down the recurrence relation for the best case of quicksort and solve it. Give the name of another sorting algorithm with the same recurrence relation? 21. What is the best running time (in terms of Big Oh notation) of insertion sort? When does it happen? 22. Does selection sort have best//worst-case running time? Explain your answer. 23. Does merge sort have best/ worst-case running time? Explain your answer. 24. Does bubble sort have best/ worst-case running time? Explain your answer. 25. Write down the average complexity of each algorithm using Big Oh notation. Bubble sort Insertion sort Mergesort Quick sort Selection sort 26. Next to each code segment write the name of the corresponding sorting algorithm 26.1 void method01(int [] a){ int size = a.length; int i, j, t; for(i = 0; i < size; i++) { for(j = i +1; j < size; j++) if(a[j-1] > a[j]) { t = a[i]; a[i] = a[j]; a[j] = t; } } } { 5 26.2 void method02(int [] a){ int j, p; int tmp; for ( p = 1; p < N ; p++) { tmp = a[p]; for (j = p; j > 0 && tmp < a[j-1]; j--) a[j] = a[j-1]; a[j] = tmp; } } 26.3 void method03 ( int a[], int left, int right) { if (right > left) { middle = left + (right - left)/2; method03 (a, left, middle); method03 (a, middle+1, right); help_method(a, left, middle, right); } } void help_method(int a[], int left, int middle, int right) { int b[] = new b [right – left + 1]; int i = left, j = middle+1, k = 0; while (i < middle && j <= right) { if (a[i] < a[j]) b[k++] = a[i++]; else b[k++] = a[j++]; } while(i <= middle) b[k++] = a[i++]; while(j <= right) b[k++] = a[j++]; k = 0; for (k = 0; k < right – left + 1; k++) a[left + k] = b[k]; } 6 26.4 void method04 ( int a[], int left, int right){ if( left + 10 <= right) { element = help_method(array, left, right); int i = left, j = right - 1; for ( ; ; ) { while (a[++i] < element) {} while (element < a[--j] ) {} if (i < j) swap (a[i],a[j]); else break; } swap (a[i], a[right-1]); method04 ( a, left, i-1); method04 (a, i+1, right); } else insertionsort (a, left, right); } int help_method (int[] array, int left, int right) { int middle = (left + right)/2; if (array[left] > array[middle]) swap(array, left, middle); if (array[middle] > array[right]) swap(array,middle,right); if (array[left] > array[middle]) swap (array, left, middle); swap(array, middle, right-1); return array[right -1]; } 26.5 void method05 ( int [ ] num ) { int i, j, first, temp; for ( i = num.length - 1; i > 0; i-- ) { first = 0; for(j = 1; j <= i; j ++) { if( num[ j ] < num[ first ] ) first = j; } temp = num[ first ]; num[ first ] = num[ i ]; num[ i ] = temp; } } 7 C. Trees 27. Construct a binary tree with 8 nodes and minimum number of levels and assign the letters of CAMBRIDGE to the nodes so that the name can be read in a pre-order/inorder/post-order traversal. 28. Draw the binary search tree for the letters in the word CAMBRIDGE 29. Draw an AVL tree for the letters in the word CAMBRIDGE. Indicate the type of each rotation 30. Draw 2-3-4 tree for the letters in the word CAMBRIDGE. Show the tree after insertion of each letter. 31. What is the complexity of search in Binary Search trees, provided they are kept balanced? 32. What is the worst complexity of search in Binary Search trees, provided they are not kept balanced? When does it happen? 33. Which traversal order would retrieve the elements in a Binary Search Tree in sorted order? 34. Give the relation (exact formulas) between the height M of a perfect binary tree and the number of nodes N. N= M= 35. In a well-balanced binary tree with M levels (the root is at level 0) there are O( Fill in the blank appropriately. 36. In a well-balanced binary tree with N nodes there are O( ) nodes. ) levels. Fill in appropriately 37. What is the minimal height of a binary tree with 38 nodes? What would be the maximal height? 38. How many links are there in a binary tree with N nodes? 8 39. In a perfect binary tree with N nodes how many nodes are with height 0? 40. In a perfect binary tree, how many nodes are there at level p? How to represent binary trees: Drawing a tree in WORD is difficult. Here how to represent the tree (after you have drawn it on paper): Write down the left and the right child of each node in a table. Example: rwa the binary search tree for “IOWA”, It should look like this: I / \ A O \ W Node I A O W left A - right O W - I will be able to reconstruct the tree and see if it is correct. 9