Question

Discrete Math. What is the running time of the deterministic Select(A,n,i) for an arbitrary statistic i,...

Discrete Math. What is the running time of the deterministic Select(A,n,i) for an arbitrary statistic i, 1 ≤ i ≤ n, and an array of n unordered keys? Give as tight an answer as you can.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Deterministic selection algorithm

Select(A,n,i) means to select the ith smallest number in the array A of n unordered keys,

one of the most naive algorithms that comes into mind is that we first sort the array and then find the ith element. The time complexity of this algorithm depends upon the sorting algorithm used, if we use bubble, insertion or selection sort, our complexity for sorting the array will be O(n^2). We can improve this by using some of the faster sorting algorithms that are merge sort, heap sort, etc, which bring down the complexity of sorting to O(nlogn), after sorting we will take the ith element and we will have the final answer. This algorithm has a worst case complexity of O(nlogn).

Another algorithm that comes into mind is that we can perform a binary search on the element we need to find. The meaning of this statement is that we define some upper and lower bound on the ith element, that is the lower bound will be the minimum element of our array and upper bound will be the maximum element of our array. Now with these H and L we find a mid which is (H+L)/2 and check whether this mid is the ith smallest element or not, for checking this we traverse the array and count the elements smaller than mid, if they are greater than or equal to i, then this mid can be the answer, but we still recursively call this binary search with H=mid, L=L and if we find that elements smaller than mid are less than i, we recursively call binary search with H=H, L=mid+1. Our binary search will converge at the ith smallest element.

Code(C++):

int smallerElements(int A[], int n, int mid){

    int ret=0;

    for(int i=0;i<n;i++){

    ret+=(mid<=A[i]);

    }

    return ret;

}

int binarySearch(int H, int L, int i, int A[], int n){

    int mid;

    int ans;    

    while(L<H){

    mid=(H+L)/2;

    if(smallerElements(A,n,mid)>=i){

    H=mid;

    ans=mid;

    }else{

    L=mid+1;

    }

    }

    return ans;

}

Let us analyse the time complexity of this algorithm, the smallerElements part takes O(n) time and the binary search will take O(log(Max(A[i])) time in worst case. So the worst case time complexity of this algorithm becomes O(n*log(max(A[i])) this better than the first case if the range of elements in A[i] is small.

Now we will see algorithms that have linear time complexity in the worst case.

For turning the expected time complexity to linear we use an algorithm that is similar to pivot selection in quick sort. We will call this optimised algorithm smartSelect. In smartSelect we will select a pivot which can be anything, the last element, the first element, or any random element, these selections may give a better time complexity on a specific test case but if we take an average over some of the random test cases, this selection has very little contribution in increasing or decreasing the time complexity. Now after selecting the pivot we will partition the array into two parts, one which will have all the elements smaller than pivot and other partition have all the elements larger than pivot. Now if we compare to quicksort algorithm, we will call the recursive function on both the parts, but in smartSelect we will call the recursive function only on that partition that will have the ith smallest element. If the partition containing the smaller element has the size greater than i, then we call the smartSelect on this partition and vice versa.  

ALGORITHM

select(A[0..n-1], i)

1) Divide the given array into n/5 groups of equal size( 5 here),

2) Sort the groups and find the median of all groups. Store the median of each group into another array(medians[]) of n/5 size,

3)Now, find the median of this medians[] using this function only ,recursively.

med1= select(medians[0..(⌈n/5⌉-1)], ⌈(n/5)/2⌉) // array median has n/5 elements

4) Partition A[] around med1 and obtain its index.

ind = partition(A, n, med1)   

// partition() divides the array about a particular element and put it at the correct index

// and return index.

5) If ind == i return med1

6) If ind > i return select(A[0..ind-1], i)

7) If ind < i return select(A[ind+1..n-1], i-ind-1)

Time Complexity:

The worst case time complexity of the above algorithm is O(n). Let us analyze all steps.

We calculate the median of an array of size 5, it will take O(5) time which is constant and since we have n/5 arrays of this type, the total time which finding the medians of all the arrays would be O(5*(n/5)) = O(n).

If we assume that we can find median of n element in T(n) time , then the next step will take T(n/5) time because the number of elements in median array is n/5.

Partitioning the array around a random element and getting position of pivot element in sorted array will take O(n) time.

Out of last 2 steps ( ind <i or ind >i) one will be executed. We need to find time complexity of these recursive steps. It will either be a maximum number of elements greater than med1 or maximum number of elements smaller than med1.

Number of elements are greater than med1 :-

At least half of the medians found in step 2 are greater than or equal to med1. Thus, at least half of the n/5 groups contribute 3 elements that are greater than med1, except for the one group that has fewer than 5 elements. Therefore, the number of elements greater than med1 is at least.

3\left ( \left [ \frac{1}{2}\left [ \frac{n}{5} \right ] \right ] -2\right ) \geq \frac{3n}{10}-6

Number of elements are less than than med1 :-

In a similar way we find at least 3n/10 – 6 will be less than med1. The function recurs for at most n – (3n/10 – 6) which is 7n/10 + 6 elements.

By solving the recurrence we get

   T(n) <= O(n) + T(n/5) + T(7n/10 +6)

So our deterministic selection algorithm takes O(n) time. The worst-case running time of is therefore linear





Add a comment
Know the answer?
Add Answer to:
Discrete Math. What is the running time of the deterministic Select(A,n,i) for an arbitrary statistic i,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In terms of n, what is the worst-case running time of countingSort on an input array...

    In terms of n, what is the worst-case running time of countingSort on an input array of n letters from the alphabet (so k = 26, and n is arbitrary)? counting Sort(A[1 ...n]): // assume each A[i] € {1,2,...,k} 1: for v :=1 to k: 2: count[v] := 0 3: for i:=1 to n: 4: count[A[i]] := count[A[i]] +1 5: i:=1 6: for v:=1 tok: 7: for t :=1 to count[0]: 8: A[i]:= 0 9: i := i +1

  • (20 points) Recall the following deterministic select algorithm: (a) Divide the n elements into groups of...

    (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....

  • discrete math (1) (15 pts) Time Complexity Analysis 1) (5 pts) What is the time complexity...

    discrete math (1) (15 pts) Time Complexity Analysis 1) (5 pts) What is the time complexity of the following code segment? Explain your answer; otherwise, you can't get full mark from this question. for(int i=1; i<n; i*=2) { sum-0; sum++; Answer: 2) (5 pts) What is the time complexity of the following code segment? Explain your answer; otherwise, you can't get full mark from this question. for(int j=0; j<n; j++){ for (int k=0; k<n; k++) { for (int =0; i<n;...

  • I need help with my discrete math problem. can you show me step by step process...

    I need help with my discrete math problem. can you show me step by step process . Thanks in advance 3. Give a big-O estimate and a pair of witnesses for the number additions used in this segment of an algorithm. t:= 0 for i:=1 ton for j := 1 to n-i t:=t+i+j

  • 6. Consider the following basic problem. You're given an array A consisting of n integers A[1],...

    6. Consider the following basic problem. You're given an array A consisting of n integers A[1], A[2], , Aln]. You'd like to output a two-dimensional n-by-n array B in which B[i, j] (for i <j) contains the sum of array entries Ali] through Aj]-that is, the sum A[i] Ai 1]+ .. +Alj]. (The value of array entry B[i. Λ is left unspecified whenever i >j, so it doesn't matter what is output for these values.) Here's a simple algorithm to...

  • Compute the total running time for each of the functions in the following code. First, compute...

    Compute the total running time for each of the functions in the following code. First, compute the running time in terms of some constants a, b, c, d, e, etc. Show your work and give the answer in the text box. Then give the tightest big-O bound you can for each. (All of the functions are O(n!), but you can give a more informative bound for each.) void f(int n) {   int i = 1;   while (i <= sqrt(n)) {...

  • Question 1. (1 marks) The following procedure has an input array A[1..n] with n > 2...

    Question 1. (1 marks) The following procedure has an input array A[1..n] with n > 2 arbitrary integers. In the pseudo-code, "return” means immediately erit the procedure and then halt. Note that the indices of array A starts at 1. NOTHING(A) 1 n = A. size 2 for i = 1 ton // i=1,2,..., n (including n) 3 for j = 1 ton // j = 1,2,...,n (including n) 4. if A[n - j +1] + j then return 5...

  • Q6) let T(n) be a running time function defined recursively as 0, n=0 n=1 3T(n -...

    Q6) let T(n) be a running time function defined recursively as 0, n=0 n=1 3T(n - 1)- 2T(n - 2), n> 1 a) Find a non-recursive formula for T(n) b) Prove by induction that your answer in part (a) is correct. c) Find a tight bound for T(n).

  • Problem 1. Select the running time of each function. void print_array (int* A, int n) for...

    Problem 1. Select the running time of each function. void print_array (int* A, int n) for (int í 0; i < n; ++i) cout << A[i] << endl; void print_array pairs (int* A, int n) for (inti 0; i < n; ++i) for (int j 0; j < n; ++j) cout << Ai] ALj]< endl; void print_array_start(int* A, int n) for (int i 0; i < 100 ; ++i) cout << A[i] << endl; void print_array_alt (int* A, int n)...

  • 1. (10 pts total) For parts (1a) and (1b), justify your answers in terms of deterministic...

    1. (10 pts total) For parts (1a) and (1b), justify your answers in terms of deterministic QuickSort, and for part (1c), refer to Randomized QuickSort. In both cases, refer to the versions of the algorithms given in the lecture notes for Week 3. (a) (3 points) What is the asymptotic running time of QuickSort when every element of the input A is identical, i.e., for 1 ≤ i,j ≤ n, A[i] = A[j]? Prove your answer is correct. (b) (3...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT