Exam1B_Solutions

pdf

School

Pennsylvania State University *

*We aren’t endorsed by this school

Course

465

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

11

Uploaded by MinisterBadgerMaster464

Report
Pennsylvania State University — CMPSC465 : Data Structures & Algorithms Exam 1 Lecturers: Antonio Blanca and Yana Safonova September 22, 2023 Exam 1 - B Name: Penn State access ID (xyz1234) in the following box: Student ID number (9XXXXXXXX): TA and/or section time: Instructions: Answer all questions. Read them carefully first. Be precise and concise. Handwriting needs to be neat. Box numerical final answers. Please clearly write your name and your PSU access ID (i.e., xyz1234) in the box on top of every page . Write in only the space provided. You may use the back of the pages only as scratch paper. Do not write your solutions in the back of pages! Do not write outside of the black bounding box : the scanner will not be able to read anything outside of this box. We are providing two extra page at the end if you need extra space. Make sure you mention in the space provided that your answer continues there. Good luck! 1
Name: PSU Access ID (xyz1234): Multiple choice questions (27 points) For each of the following questions, select the right answer by filling the corresponding grading bubble grading bubbles. 1. Consider the min heap below. Suppose the number 1 is inserted into the heap. 3 4 7 8 5 Which one of the following is the resulting min heap? 3 4 7 8 5 1 (a) 1 4 7 8 3 5 (b) 1 3 5 7 4 8 (c) 1 4 5 8 3 5 (d) 3 4 7 8 1 5 (e) Answer. (a) '! (b) (c) (d) 2
Name: PSU Access ID: 2. Consider the min heap on the left below. Suppose that first the element at position 3 of the array is removed from the heap (remember that we use 0 indexed arrays), and after that the element at position 1 is removed from the heap. 4 6 7 10 12 8 14 12 5 9 16 10 7 Which one of the following is the resulting min heap? 4 8 10 12 12 14 5 9 16 10 7 (a) 4 16 10 10 12 8 14 12 5 9 7 (b) 4 8 10 10 12 12 14 16 5 9 7 (c) 4 8 10 16 12 10 14 12 5 9 7 (d) Answer. (a) (b) '! (c) (d) (e) 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Name: PSU Access ID: 3. Run Build-Heap on the array [3 , 5 , 4 , 8 , 6 , 2 , 9 , 1 , 8 , 11] to construct a min heap. Select the resulting min heap. 2 3 5 8 9 8 6 4 1 11 (a) 3 5 8 8 6 11 2 4 9 1 (b) 1 2 6 8 9 5 11 3 4 8 (c) 1 2 6 11 8 5 8 3 9 4 (d) 1 3 5 8 8 6 11 2 4 9 (e) 1 3 5 4 9 6 11 2 8 8 (f) Answer. (a) (b) (c) (d) '! (e) (f) 4
Name: PSU Access ID: 4. The array [ 2 , 4 , 3 , 8 , 9 , 12 , 13] corresponds to a min heap. In the space provided below, write down the resulting heap (as an array) after the first two iterations of the heapsort algorithm. Answer. [ 2 , 3 , 4 , 8 , 9 , 12 , 13] [ 8 , 9 , 12 , 13 , 4 , 3 , 2] [ 2 , 4 , 3 , 8 , 9 , 12 , 13] '! [ 4 , 8 , 12 , 13 , 9 , 3 , 2] [ 12 , 4 , 13 , 8 , 9 , 3 , 2] [ 13 , 4 , 12 , 8 , 9 , 3 , 2] 5. How many integer multiplications does Strassen’s algorithm need to do to multiply two 8 × 8 matrices of integers? Answer. 49 256 64 '! 343 16 7 6. Suppose that lim n →∞ f ( n ) g ( n ) = . Which of the following must be true? Answer. f ( n ) = O ( g ( n )) '! f ( n ) = Ω( g ( n )) f ( n ) = Θ( g ( n )) None of the above. 7. What is the running time of Insertion Sort (for sorting in ascending order) on a list with the first n −⌈ n 3 / 4 elements sorted in ascending order? Answer. O ( n ) O ( n log n ) O ( n 2 ) O ( n 3 / 2 ) '! O ( n 7 / 4 ) 5
Name: PSU Access ID: 8. If T ( n ) = 5 T ( n 5 ) + O ( n ), which of the following is true? Answer. T ( n ) = O ( n log 5 2 ) T ( n ) = O ( n log 2 5 ) T ( n ) = O ( n ) '! T ( n ) = O ( n log n ) None of the above 9. If T ( n ) = 14 T ( n 7 ) + O ( n 2 ), which of the following is true? Answer. '! T ( n ) = O ( n 2 ) T ( n ) = O ( n 2 log n ) T ( n ) = O ( n log n ) T ( n ) = O ( n log 7 12 ) None of the above 6
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Name: PSU Access ID (xyz1234): True/False (20 points) True or false? Fill in the correct bubble. No justification is needed. No points will be subtracted for wrong answers, so it is in your best interest to guess all you want. T F n 0 . 75 = O ((log n ) 7 ). False 3 n = O (4 n ). True n log n = O (1 . 5 n ). True If f ( n ) and g ( n ) are non-negative functions, then either f ( n ) = O ( g ( n )) or g ( n ) = O ( f ( n )). False Explanation: Consider f ( n ) = 1 when n is even and n 2 when n is odd. Let g ( n ) = n . Then, we can see that f ( n ) ̸∈ O ( g ( n )) and g ( n ) ̸∈ O ( f ( n )). In a min heap, the largest element will be stored in the last position of the array. False In a min heap, the left child of the root is smaller than the right child of the root. False Explanation: The min-heap property does not specify a relationship between the left and right children. Checking whether a min heap with n elements contains a certain element takes O (log n ) time. False Explanation: There is no order between the left and right children of each node. n i =1 3 i = O (3 n ). True If f ( n ) = O ( g ( n )), then there is some value of n where f ( n ) g ( n ). False Explanation: Consider f ( n ) = n and g ( n ) = n + 1. Clearly f ( n ) = O ( g ( n )). But there is no value of n for which f ( n ) g ( n ). The procedure Heapify-Down could make O (1) swaps only. True Explanation: This asks about a possible case and not a general case. 7
Name: PSU Access ID: Recurrences (12 points) Each of the following scenarios outlines a divide-and-conquer algorithm. In each case, write down the appropriate recurrence relation for the running time as a function of the input size n and give its solution. Giving your solution in O -notation suffices. You need not give a full derivation of your solution, but you should indicate how you arrived at it (e.g., by using the Master Theorem). You may assume that n is of some special form (e.g., a power of two or of some other number), and that the recurrence has a convenient base case with cost Θ(1). (a) An input of size n is broken down into 4 subproblems, each of size n/ 4. The time taken to construct the subproblems, and to combine their solutions, is Θ( n 2 ). Solution: In this case, T ( n ) = 4 T ( n/ 4) + O ( n 2 ). Using the Master Theorem ( a = 4 , b = 4 , d = 2), we get log b a = log 4 4 = 1 < d , so T ( n ) = O ( n 2 ). (b) An input of size n is broken down into n subproblems, each of size n . The time taken to construct the subproblems, and to combine their solutions, is n . Solution: T ( n ) = nT ( n ) + n . This was essentially Problem 5j from Homework 3, and if you remembered the solution, it was fine to justify as “this was a HW problem”. Alternatively, you can note by unfolding that T ( n ) = n 1 2 + 1 4 + ··· + 1 2 k + kn = T ( n 1 2 k ) + kn Now if we let k = log 2 log 2 n , we have n 1 2 k = 2 which is constant. Since 1 2 1 2 + 1 4 + · · · + 1 2 k 1, we have T ( n ) = Θ( n log log n ) . 8
Name: PSU Access ID: Algorithms. (15 points) Given an array of n distinct integers and two numbers i and j such that 1 i j n , design an algorithm that finds the sum of all elements between the i -th and the j -th smallest elements of the array. For example, if the array contains the numbers 20 , 8 , 22 , 4 , 12 , 10 , 14, i = 3 and j = 6, then the output should be 56 (the third smallest integer in the array is 10 and the sixth smallest is 20, so 10 + 12 + 14 + 20 = 56). Design and algorithm for this task with O ( n ) running time. You may assume that j = O ( n ). For full credit, make sure to include an explanation of how and why your algorithm works and a run time analysis. Solution: The central idea of the solution is to use a min-heap to retrieve the smallest numbers from the input array. This idea was also used in Worksheet 3 Problem 5. As discussed in class, the Build-Heap operation to create a min-heap from the input array runs in O ( n ). Deleting the minimum element from this min-heap is an O (log n ) operation, and we are given j = O ( n ). Therefore, deleting the j smallest elements takes O ( n log n ). We can split these j deletions into a first group of i 1 deletions (the elements that are too small to be included in the final sum) and a second group of j i +1) deletions (the elements we include in the final sum). We keep a variable for the running sum of every element in the second group, which will be our output. Elements greater than the j smallest simply remain in the heap and are not considered. Data: An array of n distinct integers A Result: The sum of all elements between the i -th and j -th smallest Build-Heap( A ); for k from 1 to i-1 do Delete( A , 0); end s = 0; for k from i to j do s = s +GetMin( A ); Delete( A , 0); end return s; This is just one possible way to write the idea of using a heap in pseudocode. The running time for the above algorithm is O ( n ) + O ( n log n ) = O ( n ). An equivalent way to phrase the solution is to build the min-heap, then run the first j iterations of Heapsort. This will sort the j smallest elements in descending order and place them at the end of the array, after which we iterate between elements n j and n i to calculate the final sum. Note: It is even possible to solve this problem in O ( n ) if we remove the j = O ( n ) constraint. Though the heap approach would be too slow in this case, there is an O ( n ) algorithm for calculating the k -th smallest number in an unsorted array called the Median-of-Medians algorithm. Using this algorithm to find the i -th and j -th smallest numbers, the sum could then be calculated by iterating over the entire array and adding any value between them to the final sum. 9
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Name: PSU Access ID (xyz1234): 10
Name: PSU Access ID (xyz1234): 11