Intro to Algorithms Course Homework #3 Chatikyan Elen 1. a) First, I’ll implement the SELECT algorithm which helps to find the i-th smallest element from the array (I will give n-i+1 as a parameter, and we’ll get the i-th largest element id as a result of the algorithm), and then I will sort those i elements with MergeSort or HeapSort algorithms in order to return the sorted array as mentioned in the problem. ο· Deterministic Partition 1. Divide the n elements of Array into floor (n/5) groups of 5 elements (at most 1 group can be made up of the remaining nmod5 elements). 2. Find median of each group (with the help of insertion sort) and pick the median from each group 3. Recursively Select the median of the medians 4. Partition the array around the median of medians Algorithm will return the sorted array of the i largest elements The pseudocode of the algorithm is the following: 1. Deterministic_Select(Array, start, end, i) //O(n) 2. p = Deterministic_Partition(Array, start, end) //selects a good pivot and partitions the given array around it using median of medians algorithm 3. if p == i 4. return p // return the index of the i-th smallest element from the array // A [p...end] array contains the n-i+1 largest elements in the array 5. else if i<p 6. return Deterministic_Select(A, start, p-1, i) 7. else if p<i 8. return Deterministic_Select(A, p+1, end, i-p) 9. HelpMaggie(Array, i) //O(iLogi) 10. n = Array.Length 11. if n < i 13. return -∞ 12. //we should find the n-i+1-th smallest element, which will be i-th largest element 14. index = Deterministic_Select(Array, 1, n, n-i+1) 15. return HeapSort(Array, index, end) //Or we can also use MergeSort to sort the array b) Deterministic SELECT has running time of O(n), and the HeapSort/MergeSort Algorithms have O(nLogn) time complexity, as we sort i elements - the time complexity of sorting part is O(iLogi), so we have the total Time Complexity T(n, i)= O(n+iLogi) 2. a) A is unsorted array with n distinct integers, k-th largest and k-th smallest elements are removed from the array, count the average value of array after removing elements Deterministic_Select algorithm is implemented in problem 1 1. CountAvarage(Array, k) 2. n = Array. Length 3. if n - 2k <=0 4. return 0 5. sum = 0 //find the k-th smallest element and take the other part of the array 6. smallest_k_Index = Deterministic_Select(Array, 1, n, k+1) // O(n) //find the n-k-th smallest element in the remaining array 7. largest_k_Index = Deterministic_Select(Array, smallest_k_Index+1, n, n-k) O(n-k) //count the average value for the array, without taking the k smallest and k largest elements 8. for i= smallest_k_Index to largest_k_Index // O(n-2k) 9. sum+=Array[i] 10. return = floor(sum/(n-2k)) b) lines 6,7 call the Deterministic_Select function which has O(n) time complexity, so those lines have time complexity of O(n + n-k). In order to count the average of n-2k elements we traverse the array(line 8-9 for loop), this takes O(n-2k) time. As k<=n, the total time complexity is T(n) = O(n+n-k+n-2k) ~=O(n) 3. Given set of the functions πΉπ (β), for each i-th role MAX {πΉπ (β), MAX 0≤π≤β−1 (π[π − 1][β − π] + π[π][π])} a) T[i][h] = { 0 πΌπΉ β = 0 ππ π = 0 b) T[i][h] represents the maximum total score that Firgus can get if he prepares for i roles and has h hours. The following is the table for dynamic solution 0 1 2 ... h ... H 0 items 0 0 0 0 1 (only item1) 2 (role1, role2) i (role1, …, rolei) ... 0 0 0 0 0 n (role 1, 0 i\h ... rolen) 0 0 0 T[n][H] T[n][H] is the maximum Score that Firgus can get if he prepares for n roles, and has H hours, which is what we need to find. There are the following cases when filling every cell of the table. Take the score that Firgus can get if 1. 2. 3. 4. 5. 6. 7. 8. prepares rolei for h hours, 0 hours for other roles in this case The score will be= πΉπ (β), Prepare rolei for h-1 hours and take the max score for preparing role1, …, rolei-1 roles for 1 hours, Prepare rolei for h-2 hours and take the max score for preparing role1, …, rolei-1 for 2 hours, Prepare rolei for h-3 hours and take the max score for preparing role1, …, rolei-1 roles for 3 hours, … Prepare rolei for h-k hours and take the max score for preparing role1, …, rolei-1 roles for k hours, … Prepare rolei for 0 hours and take the maximum score for preparing i-1 roles for h hours. We should write in T[i][h] cell the maximum score value that we get in the mentioned cases. c) The Bottom-Up dynamic Programming algorithm 1. HelpFirgus(F_1,F_2,..,F_n, n, H) 2. T[n+1][H+1]//0...n, 0...H 3. for i = 0 to n //O(n) 4. for h = 0 to H //O(H) 5. max = -infinity 6. if i==0 or h==0 7. T[i][h] = 0 8. else 9. for k = 0 to h-1 //O(~H) 10. if max < T[i-1][h-k]+T[i][k] 11. max = T[i-1][h-k]+T[i][k] 12. if max < F_i(h) 13. max = F_i(h) 14. T[i][h] = max 15. //T[n][H] contains the max score that Firgus can get when preparing for n roles in H hours c) Time complexity of the algorithm is O(n*H*H), as the algorithm has 3 nested for loops with n, H, ~H iterations, so the Total Time Complexity is O(max(n,H)^3) Space Complexity is O((n+1)*(H+1)) as the algorithm uses a table with n*H dimensions, so the Space Complexity is O(max(n, H)^2) 4. The weight for ππ‘πππ is π€π and the price is π£π , maximum weight is W MAX{π[π − 1][π], π£π + π[π − 1][π − π€π ]} πΌπΉ π€π ≤ π a) T[i][j] = {0 πΌπΉ π = 0 ππ π = 0 π[π − 1][π€π ] ππ‘βπππ€ππ π b) T[i][j] represents the maximum total value we can have with j items if the backpack’s maximum weight capacity is = i. The following is the table for dynamic solution 0 1 2 ... j ... W 0 items 0 0 0 0 1 (only item1) 2 (item1, item2) 0 0 0 0 0 i\j i (item1, …, itemi) ... 0 0 0 n (item1, ... itemn) 0 T[n][W] T[n][W] is the maximum amount of value that we can have with n items, when the max capacity is W, which is what we need to find. There are 2 cases when filling every cell of the table. 1. Include the ππ‘πππ – in this case we should write in T[i][j] cell = π£π + T[i-1][j-π€π ] T [i-1][j-π€π ] – this is the maximum value that we can get without ππ‘πππ when we have j-π€π weight capacity 2. Not include the ππ‘πππ – in this case we should write in T[i][j] cell = T[i-1][j] T [i-1][j] is the maximum value that we can get without ππ‘πππ when we have j weight capacity We should take the maximum value of the above mentioned 2 cases. c) The Bottom-Up dynamic Programming algorithm 1. HelpGeralt(itemsArray, Weights, Values, W) 2. n = itemsArray.Length 3. T[n+1][W+1] //0...n, 0..W 4. for i = 0 to n 5. for j = 0 to W 6. if i == 0 || j == 0 7. T[i][j] = 0 8. else if Weights[i] <= W 9. T[i][j] = max{T[i-1][j], Values[i] + T[i-1][j-Weight[i]]} 10. else 11. T[i][j] = T[i-1][j] 12. 13. //T[n][W] contains the maximum value 14. 15. resultArray[n]// max value could be n if all the items are participating 16. //get the items 17. while i > 0 && j > 0 //O(max(n,m)) 18. if(T[i][j] == T[i-1][j]) //this means item-i is not included 19. i-20. Else // this means item-i is included, add it in the resultArray 21. resultArray.Add(itemsArray[i]) 22. i-23. j-=Weights[i] 24. 26. return resultArray d) Time Complexity is O((n+1)*(W+1) + max(n, W) = O (n*W) as we have nested loops with n and W iterations and a while loop which in worst case has max(n, W) iterations. Total time Complexity is O(n^2) // suppose max(n, W) = n Space Complexity is O((n+1)*(W+1) + n), as we have created a table T[n+1][W+1] with dimensions:(n+1)X(W+1) , so the total time complexity is O(n^2) // suppose max(n, W) = n