(20 points) Recall the following deterministic select algorithm: (a) Divide the n elements into groups of size 5. So n/5 groups. (b) Find the median of each group. (c) Find the median x of the n/5 medians by a recursive call to Select. (d) Call Partition with x as the pivot. (e) Make a recursive call to Select either on the smaller elements or larger elements (de- pending on the situation); if the pivot is the answer we are done. Then, the recurrence for the running time was T(n) = T(1/5n) + T(7/10n) + O(n) (Can you explain why?) Now suppose we change the algorithm as follows. (a) Divide the n elements into groups of size 9. So n/9 groups. (b) Find the median of each group. (c) Find the median x of the n/9 medians by a recursive call to Select. (d) Call Partition with x as the pivot. (e) Make a recursive call to Select either on the smaller elements or larger elements (de- pending on the situation); if the pivot is the answer we are done.
Now what is the recurrence for the running time? Also explain how much time it takes to perform each of the five steps.
Time Taken each of the Five steps
Step 1
Dividing the n elements into n/5 group will take constant time i.e)(1)
Step 2
To find median of n elements we need O(n ) time
Sort the above created ⌈n/5⌉ groups and find median of all groups. Create an auxiliary array ‘median[]’ and store medians of all ⌈n/5⌉ groups in this median array.
So here we have groups of only 5 elements so time needed is constant i.e O(1)
so In all Step 1 and Step 2 will take O(n) time
Step 3
We recursively call to find median of median
so Time taken = T(n/5)
Step 4
Is normal Partition algorithm that takes O(n) time
Step 5
What is the worst case size of these recursive calls?
The answer is maximum number of elements greater than median (obtained in step 3) or maximum number of elements smaller than median
At least half of the medians found in step 2 are greater than or equal to median. Thus, at least half of the n/5 groups contribute 3 elements that are greater than median, except for the one group that has fewer than 5 elements. Therefore, the number of elements greater than median is at least.
So In the worst case, the function recurs for at most n – (3n/10 – 6) which is 7n/10 + 6 elements.
So time needed is T(7n/10 +6) = T(7n+10)
So total Recurrence Relation is
T(n) = T(n/5) + T(7n/10) + O(n)
After solving this relation the time we get is O(n) in worst case
Now change in the algorithm
1. This time we are dividing into groups of 9 elements in O(1) time
2.To find median of each group of n elements take O(n) but here we have 9 elements only so O(1) time
so In all Step 1 and Step 2 will take O(n) time
3.We will recursively call to find median of all groups
So time is T(n/9)
4. Normal Partition Algotihm takes O(n) time
5. Same reason as above bt makes changes accortding to 9 elements now so this time atlest half will be 5 instead of 3 so calculation will be :--->
the number of elements that are less than medOfMed is at least 5n/18 -20
5 ( 1/2 ( n/9 ) - 4 ) >= 5n /18 -20
So In the worst case, the function recurs for at most n – (5n/18 – 20) which is 13n/18 + 20 elements.
so total time = T( 13n/18 )
So total Recurrence Relation is
T(n) = T(n/9) + T(13n/18) + O(n)
After solving this relation the time we get is O(n) in worst case
Thank You
If u like the answer please upvote it
(20 points) Recall the following deterministic select algorithm: (a) Divide the n elements into groups of...
JAVA For the code in which I implemented most of the quick select algorithm. Quick select is a O(n) time algorithm to find the nth smallest value in an (unordered list). The following recursive algorithm finds thenth smallest element with an index bewtween left and right in list: Code: Integer QuickSelect(list, left, right, n) { if left = right // If we only have one index available return list[left] // return the element at that index endif pivotIndex ⇐ partition(list,...
Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size n of a problem into 5 sub-instances of size n/3, and the dividing and combining steps take a time in Θ(n n). Write a recurrence equation for the running time T (n) , and solve the equation for T (n) 2. Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size n of a problem into 5 sub-instances of size n/3, and the dividing...
And the related algorithms: (20 points) Consider the following strategy for choosing a pivot element for the Partition subroutine of QuickSort, applied to an array A. .Let n be the number of elements of the array A. If n 24, perform an Insertion Sort of A and return. Otherwise: Choose 2n/2)| elements at random from n; let S be the new list with the chosen elements. Sort the list S using Insertion Sort and use the median m of S...
Suppose the following is a divide-and-conquer algorithm for some problem. "Make the input of size n into 3 subproblems of sizes n/2 , n/4 , n/8 , respectively with O(n) time; Recursively call on these subproblems; and then combine the results in O(n) time. The recursive call returns when the problems become of size 1 and the time in this case is constant." (a) Let T(n) denote the worst-case running time of this approach on the problem of size n....
(13 pts) Given an array AlI,2,. .. ,n] integers, design and analyze an efficient Divide-and-Conquer algorithm to find some i and j, where j > 1, such that A[j]-Ali] is maximized. For example, given A 6, 1,3,8,4,5, 12,6], the maximum value of AL] - Ali] for j > i is 12-1 11 where j -7 and i 2. Give the underlying recurrence relation for your algorithm and analyze its running time. You should carefully state all details of your algorithm:...
program in python Randomness can be used to improve the performance of deterministic algorithms which need to make many choices. Rather than repeatedly making fixed, hard-coded choices, a pseudorandom number generator can be used to make dynamic, unbiased choices. If the benefits of "good" choices outweigh the costs of "bad" choices, a random selection of good and bad choices can improve the performance of an algorithm Let us explore this with the QUICKSELECT algorithm. Discovered by the influential computer science...
(a) Give the pseudo-code for a recursive algorithm called Find_Smallest(A, n) that returns the value of the smallest element in an array of n integers called A. Assume the elements in the array are at locations A[1]..A[n]. (b) Give a recurrence T(n) for the running time of your algorithm. (c) Solve the recurrence in part (b)
I want you to now remember the following partitioning problem we considered a while ago. You are given a list L of length n and asked to partition the elements of L into two sublists L_1 and L_2 such that (i) n/3 lessthanorequalto |L_1|, |L_2| lessthanorequalto 2n/3 and (ii) all elements in L_1 are less than or equal to all elements in L_2. We designed a simple, randomized (Las Vegas) algorithm for this problem that ran in O(n) expected time....
I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is not limited to finding only the median, but can generally find the ith element with 0≤i <n. Implement this generic version of the median algorithm by creating a class selector in the ads.set2.select package and implementing the following method: /** * Returns the...
pleas answer asap 3. (20 points) Algorithm Analysis and Recurrence There is a mystery function called Mystery(n) and the pseudocode of the algorithm own as below. Assume that n 3* for some positive integer k21. Mystery (n) if n<4 3 for i1 to 9 5 for i-1 to n 2 return 1 Mystery (n/3) Print "hello" 6 (1) (5 points) Please analyze the worst-case asymptotic execution time of this algorithm. Express the execution time as a function of the input...