CSIS-385: Algorithm Analysis Exam 2 Name ________________________ 1. What type of algorithm strategy is used in bubblesort? Answer ___________ 2. What type of algorithm strategy is used in sequential search? Answer ___________ 3. What type of algorithm strategy is used in insertion sort? Answer ___________ 4. What are the two algorithm strategies that can be used to solve the closest pair problem and what is the running time in terms of Big-O? Strategy 1 ________________________ Running Time _______________ Strategy 2 ________________________ Running Time _______________ 5. Describe the efficiency of computing an in terms of a lower bound, i.e., Omega(?). In other words, what is the efficiency of the best algorithm for computing an? Answer _______________ 6. Given an unsorted list of size n, write an algorithm to find the kth smallest value? Your algorithm must be O(n2) in the worst case. You can not use a sorting algorithm. Describe your algorithm using pseudo-code or a precise step-by-step description. Answer: 7. Given an unsorted list of size n, do you think it is possible to find the kth smallest value in O(n) time. Justify your answer with a sentence or two. Answer: 1 of 6 CSIS-385: Algorithm Analysis Exam 2 Name ________________________ 8. Given a sorted array of integers (size n), write a sequential search algorithm that terminates (i) when the item is found and (ii) when you know the item can not be found. Describe your algorithm using pseudo-code or a precise step-by-step description. a. Answer: b. In the worst case, how many comparisons are performed? Answer ____________ c. In the best case, how many comparisons are performed? Answer ____________ d. In the average case, how many comparisons are performed in the algorithm above? (Circle the most correct answer) (a) We can not answer that question unless we know the order in which the integers are arranged? (b) Assuming the target values are random, it will take n/2 comparisons on average to find the target values. Thus, we can say O(n) time in the average case. (c) Since the algorithm terminates early, it is not a brute force algorithm and we can say that it will be O(1) in the average case. (d) Since the list is sorted the algorithm will take O(log n). 2 of 6 CSIS-385: Algorithm Analysis Exam 2 Name ________________________ 9. Given the following algorithm and the following input, how many comparisons are made? a. char T[] = {NOBODYNOTICED} char P[] = {NOT} for (i =0; i < n-m; i++) { j = 0; while (j < m && P[j] == T[i+j]) j++; if (j ==m) return i return -1; } Answer ______ 10. b. Given an example of a text of length 10 and a pattern of length 5 that constitutes a worst case for the algorithm above? Pattern = ________________________ Text = _______________ c. Exactly, how many comparisons will be made given the input above? Answer ______ 11. a. Given an array with n integers, write an algorithm to determine if the array is sorted or not sorted? Describe your algorithm using pseudo-code or a precise step-by-step description. b. What is best case running time? 3 of 6 CSIS-385: Algorithm Analysis Exam 2 Name ________________________ 12. Circle the most correct statement. (a) Solving the traveling salesman problem requires an exhaustive search algorithm that computes every possible subset of a given set of vertices. (b) Solving the traveling salesman problem requires a brute force algorithm that computes every combination of vertices. (c) Solving the traveling salesman problem requires an exhaustive search of all the different permutations of the vertices starting at a given position. (d) Solving the traveling salesman problem requires sorting each edge by its weight an then traversing them in order from smallest largest. 13. Knapsack Problem: Given a capacity of 50 lbs and the following packages, Package A = 25 lbs. $30 Package B = 30 lbs. $40 Package C = 15 lbs. $20 Package D = 35 lbs. $10 Package E = 60 lbs. $80 Package F = 45 lbs. $65 a. How many possible subsets could be considered in the worst case? Answer __________ b. Considering the weights, how many subsets are actually considered? List them. Answer: c. Which subset produces the optimal solution? Answer __________ d. Does there exist a metric for ordering the packages that will lead to an optimal solution if we use an O(n) greedy strategy (i.e., we order the packages according to the metric--highest to lowest--and try to fit the highest packages first and continue until we’ve considered all the packages)? (Circle the most correct statement) (a) No such metric exists for any input. (b) Yes, but the metric is different for different sets of packages and there exists no efficient algorithm for determining the metric based on the input. (c) Yes, as the problem size reaches infinity the cost-to-weight ratio’s solution approaches the optimal solution. (d) No, if you fail to consider every subset there is no way you’ll compute the optimal solution. 4 of 6 CSIS-385: Algorithm Analysis Exam 2 Name ________________________ 14. The following recurrence relations describe the running time of four different divide-andconquer algorithms. What is the Big-O running time of each? Which one is the most efficient? (Circle one) (a) T(n) = 2T(n/4) + n2; Answer : O( ________) (b) T(n) = 8T(n/2) + n; Answer : O( ________) (c) T(n) = T(n/4) + n3 ; Answer : O( ________) (d) T(n) = 4T(n/2) + n2 ; Answer : O( ________) 15. Given a sorted linked list of size n? What is the best case running time for binary search? Answer________________ 16. Given the following array, [0] [1] [2] [3] [4] [5] [6] 4 7 9 1 3 5 2 and the following code, i = 0; j = 7; for (;;) { while while if (i pivot = 0; (a[++i] < a[pivot]); (a[--j] > a[pivot]); < j) swap( &a[i], &a[j] ); else break; } swap( &a[j], &a[pivot] ); Show the contents of the array after each swap. 5 of 6 CSIS-385: Algorithm Analysis Exam 2 Name ________________________ 17. Given the following graph, perform a depth first traversal starting at node A. To break ties, try to visit vertices in alphabetical order. Mark each node with count (i.e., the DFS number). A B F D C G E B 18. Given the following graph, perform a breadth first traversal starting at node A. To break ties, try to visit vertices in alphabetical order. Mark each node with count (i.e., the BFS number). A B F D C G E B 6 of 6