Question

In: Computer Science

You are given a function median(A,p,r) that finds the index corresponding to the median of an...

You are given a function median(A,p,r) that finds the index corresponding to the median of an array ? with starting index p and ending index r, in worst-case complexity Θ(?) where ? is the length of ?. Making use of the given median function, write an algorithm with complexity Θ(?) to partition the array ? using its median as the pivot. You may call the functions discussed in class.

Using your algorithm in Qn 1, write an algorithm that selects the ?-th smallest element of ? in worst-case complexity ?(?) . Prove that your algorithm indeed has complexity ?(?), justifying every step clearly. Note that the select algorithm given in class has worst-case complexity ?(? 2 ).

Solutions

Expert Solution

// Java program to find 
// median of an array 
// within given range 
import java.io.*; 
import java.util.Arrays;
  
class Main  
{ 
    // function to get elements within given range 
    static int[] getElementsWithInRange(int arr[], int n, int x, int y) 
    { 
        // initialize new array
        int newLength = y - x + 1;
        int j = 0;
        int[] newArr = new int[newLength]; 
        for (int i = x; i < n && i <= y; i++) { 
      
            // get element within the range 
            newArr[j] = arr[i];
            j++;
        } 
        return newArr; 
    } 
    
    // Function for calculating median
    public static double findMedian(int a[], int n)
    {
        // First we sort the array
        Arrays.sort(a);
 
        // check for even case
        if (n % 2 != 0)
            return (double)a[n / 2];
 
        return (double)(a[(n - 1) / 2] + a[n / 2]) / 2.0;
    }
  
    // driver function 
    public static void main (String[] args) 
    { 
        int arr[] = { 1, 3, 4, 9, 10, 3 }; 
        int n = arr.length; 
  
        // Get The Array WithIn the Starting And Ending Index 
        int i = 1, j = 4; 
        int newArr[] =getElementsWithInRange(arr,n,i,j);
        System.out.println("Median = " + findMedian(newArr, newArr.length)); 
    } 
} 

Output:

Above Program is to calculate the median

Time Complexity to find median = O(n Log n) as we need to sort the array first.

Program to find the ith Smallest element:

The idea in this new method is similar to quickSelect(), we get worst-case linear time by selecting a pivot that divides array in a balanced way (there are not very few elements on one side and many on another side). After the array is divided in a balanced way, we apply the same steps as used in quickSelect() to decide whether to go left or right of the pivot.

Psuedo Code:

kthSmallest(arr[0..n-1], k)
1) Divide arr[] into ⌈n/5⌉ groups where size of each group is 5 except possibly the last group which may have less than 5 elements.

2) 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.

// Recursively call this method to find median of median[0..⌈n/5⌉-1]
3) medOfMed = kthSmallest(median[0..⌈n/5⌉-1], ⌈n/10⌉)

4) Partition arr[] around medOfMed and obtain its position.
pos = partition(arr, n, medOfMed)

5) If pos == k return medOfMed
6) If pos > k return kthSmallest(arr[l..pos-1], k)
7) If pos < k return kthSmallest(arr[pos+1..r], k-pos+l-1)

// Java implementation of worst  
// case linear time algorithm 
// to find k'th smallest element 
import java.util.*; 
  
class Main  
{ 
  
// int partition(int arr[], int l, int r, int k); 
  
// A simple function to find median of arr[]. This is called 
// only for an array of size 5 in this program. 
static int findMedian(int arr[], int i,int n) 
{ 
    if(i <= n) 
        Arrays.sort(arr, i, n); // Sort the array 
    else
        Arrays.sort(arr, n, i); 
    return arr[n/2]; // Return middle element 
} 
  
// Returns k'th smallest element 
// in arr[l..r] in worst case 
// linear time. ASSUMPTION: ALL  
// ELEMENTS IN ARR[] ARE DISTINCT 
static int kthSmallest(int arr[], int l, int r, int k) 
{ 
    // If k is smaller than  
    // number of elements in array 
    if (k > 0 && k <= r - l + 1) 
    { 
        int n = r - l + 1 ; // Number of elements in arr[l..r] 
  
        // Divide arr[] in groups of size 5,  
        // calculate median of every group 
        //  and store it in median[] array. 
        int i; 
          
         // There will be floor((n+4)/5) groups; 
        int []median = new int[(n + 4) / 5]; 
        for (i = 0; i < n/5; i++) 
            median[i] = findMedian(arr,l + i * 5, 5); 
              
        // For last group with less than 5 elements 
        if (i*5 < n)  
        { 
            median[i] = findMedian(arr,l + i * 5, n % 5);  
            i++; 
        }  
  
        // Find median of all medians using recursive call. 
        // If median[] has only one element, then no need 
        // of recursive call 
        int medOfMed = (i == 1)? median[i - 1]: 
                                kthSmallest(median, 0, i - 1, i / 2); 
  
        // Partition the array around a random element and 
        // get position of pivot element in sorted array 
        int pos = partition(arr, l, r, medOfMed); 
  
        // If position is same as k 
        if (pos-l == k - 1) 
            return arr[pos]; 
        if (pos-l > k - 1) // If position is more, recur for left 
            return kthSmallest(arr, l, pos - 1, k); 
  
        // Else recur for right subarray 
        return kthSmallest(arr, pos + 1, r, k - pos + l - 1); 
    } 
  
    // If k is more than number of elements in array 
    return Integer.MAX_VALUE; 
} 
  
static int[] swap(int []arr, int i, int j) 
{ 
    int temp = arr[i]; 
    arr[i] = arr[j]; 
    arr[j] = temp; 
    return arr; 
} 
  
// It searches for x in arr[l..r], and  
// partitions the array around x. 
static int partition(int arr[], int l, 
                        int r, int x) 
{ 
    // Search for x in arr[l..r] and move it to end 
    int i; 
    for (i = l; i < r; i++) 
        if (arr[i] == x) 
        break; 
    swap(arr, i, r); 
  
    // Standard partition algorithm 
    i = l; 
    for (int j = l; j <= r - 1; j++) 
    { 
        if (arr[j] <= x) 
        { 
            swap(arr, i, j); 
            i++; 
        } 
    } 
    swap(arr, i, r); 
    return i; 
} 
  
// Driver code 
public static void main(String[] args) 
{ 
    int arr[] = {12, 3, 5, 7, 4, 19, 26}; 
    int n = arr.length, k = 3; 
    System.out.println("K'th smallest element is "
        + kthSmallest(arr, 0, n - 1, k)); 
} 
} 

Output:

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

The steps 1) and 2) take O(n) time as finding median of an array of size 5 takes O(1) time and there are n/5 arrays of size 5.
The step 3) takes T(n/5) time. The step 4 is standard partition and takes O(n) time.
The interesting steps are 6) and 7). At most, one of them is executed. These are recursive steps. What is the worst case size of these recursive calls. The answer is maximum number of elements greater than medOfMed (obtained in step 3) or maximum number of elements smaller than medOfMed.
How many elements are greater than medOfMed and how many are smaller?
At least half of the medians found in step 2 are greater than or equal to medOfMed. Thus, at least half of the n/5 groups contribute 3 elements that are greater than medOfMed, except for the one group that has fewer than 5 elements. Therefore, the number of elements greater than medOfMed is at least.

Similarly, the number of elements that are less than medOfMed is at least 3n/10 – 6. In the worst case, the function recurs for at most n – (3n/10 – 6) which is 7n/10 + 6 elements.

Note that 7n/10 + 6 20 20 and that any input of 80 or fewer elements requires O(1) time. We can therefore obtain the recurrence

We show that the running time is linear by substitution. Assume that T(n) cn for some constant c and all n > 80. Substituting this inductive hypothesis into the right-hand side of the recurrence yields

T(n) <= cn/5 + c(7n/10 + 6) + O(n)
     <= cn/5 + c + 7cn/10 + 6c + O(n)
     <= 9cn/10 + 7c + O(n)
     <= cn, 

since we can pick c large enough so that c(n/10 – 7) is larger than the function described by the O(n) term for all n > 80. The worst-case running time of is therefore linear


Related Solutions

Imagine you are given a TC function, a MC function, and a price P for a...
Imagine you are given a TC function, a MC function, and a price P for a competitive firm. 1. Describe the steps that you would take to find the quantity Q that that firm will produce, and the profit it will make. 2. Draw a graph following the steps you’ve written in the previous question. Graph P, MC, and AC, and mark Q and profit. Draw the graph such that the firm makes a positive profit.
Given the following consumption function C=70+0.85Y i)find the corresponding savings​ function ii) What is the corresponding...
Given the following consumption function C=70+0.85Y i)find the corresponding savings​ function ii) What is the corresponding marginal propensity to save b) Consider the following national income model for an economy with no external trade. Y=C+I+G C=120+0.8Y I=70 G=40 find i) equilibrium income ii) equilibrium consumption C)An economy has the following import and exp functions m=20+0.2y x=70 m=imports,x=exports find i) The levels of income at which the economy enjoys trade balance ii) Show your solution diagrammatically
Given the demand function Qd = D(p, y0), which is a function of price p and...
Given the demand function Qd = D(p, y0), which is a function of price p and exogenous income y0, the supply function Qs = S(p). Suppose both the D,S functions are not given in specific forms but possess continuous derivatives, if we know that supply function is strictly increasing, and demand function is strictly decreasing w.r.t price but a strictly increasing w.r.t income. (a) Write the equilibrium condition in a single equation. (b) Check whether the implicit-function theorem is applicable,...
Suppose the probability mass function of a random variable X is given by ??x−1?pr(1−p)x−r, ifx=r,r+1,r+2,... f(x)...
Suppose the probability mass function of a random variable X is given by ??x−1?pr(1−p)x−r, ifx=r,r+1,r+2,... f(x) = r−1 0, otherwise If this is the case then we say X is distributed as a Negative Binomial Random Variable with parameters r and p and we write X ∼ NegBin(r, p) (a) If we set r = 1, what distribution do we get? (b) Explain what this random variable models and justify the formula. (Hint: See Section 4.8.2 in Ross.) Math 241...
8. Suppose you are a market maker in S&R index forward contracts. The S&R index spot...
8. Suppose you are a market maker in S&R index forward contracts. The S&R index spot price is 1100, the risk-free rate is 5%, and the dividend yield on the index is 0. a. What is the no-arbitrage forward price for delivery in 9 months? b. Suppose a customer wishes to enter a short index futures position. If you take the opposite position, demonstrate how you would hedge your resulting long position using the index and borrowing or lending? c....
Given the function u(p,q,r)=((p-q)/(q-r)), with p=x+y+z,q=x-y+z, and r=x+y-z, find the partial derivatives au/ax=, au/ay=, au/az=
Given the function u(p,q,r)=((p-q)/(q-r)), with p=x+y+z,q=x-y+z, and r=x+y-z, find the partial derivatives au/ax=, au/ay=, au/az=
A firms demand function for a good is given by P = 107-2Q and their total cost function is given by
A firms demand function for a good is given by P = 107-2Q and their total cost function is given by TC = 200+3Q . i). Obtain an expression for total revenue profit in terms of Q ii).  For what values of Q does the firm break even. iii). llustrate the answer to (ii) using sketches of the total cost function, the total revenue function and the profit function. iv). From the graph estimate the maximum profit and the level...
10-The demand function for a product is given by p=80-0.5Q and the supply function is p=50+0.25Q,...
10-The demand function for a product is given by p=80-0.5Q and the supply function is p=50+0.25Q, where p is the price and Q is the quantity. Suppose that the government impose a tax of $15 on every unit sold. Find equilibrium price and quantity before imposing the tax. Find price of buyer and seller and the quantity sold in the market after tax. Find the tax burden on buyer and seller. Find government revenue and deadweight loss (DWL) and the...
Given the pseudocode for Binary Search Algorithm as below: BinarySearch(A, p, r, V)    if p...
Given the pseudocode for Binary Search Algorithm as below: BinarySearch(A, p, r, V)    if p < r q = (p + r)/2 if V = A[q] return q else if V > A[q] return BinarySearch(A, q+1, r, V)    else return BinarySearch(A, p, q-1) else if p = r,    if V = A[p]    return p else return -1    return -1 end function Using this pseudocode, write a function for BinarySearch and also complete the program, by...
The power dissipated in a resistor is given by P = V 2 / R ,...
The power dissipated in a resistor is given by P = V 2 / R , which means power decreases if resistance increases. Yet this power is also given by P = I 2 R , which means power increases if resistance increases. Explain.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT