Question

3a. Runtime Analysis: For each of the following recursive algorithms, state the recurrence equation that is the runtime of th

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

Here is the solution to above problem. Please read the explanation for more information

GIVE A THUMBS UP!!!

1)

Product(array,P)
{
   //base case takes only O(1) time
   if(p>n)
   {
       return 1; //O(1)
   }
   if(array[p]!=0) //O(1)
       //recursive call to Product(N-1)
       return array[p] *Product(array,p+1);
   else
   //recursive call to Product(N-1)
       return Product(array,p+1);   
}

Explanation

In the above problem base condition and if condition are present other than that a recursive call to Product(P+1) is done if the Size of Array is N than the recurrence relation is

T(N) = T(N-1) + C where C is constant which represents all the constant time statement in program and C has time complexity O(1)

2)

modMerge(A,p,r)
{
   if(p<r)
   {
       //we divide the merge sort into 4 parts
       div=floor(r-p)/4;
       //4 intervals each of equal size
       q1=p+div;
       q2=p+div*2;
       q3=p+div*3;
       //We divide this modified MergeSort into 4 equal parts unlike th
       //traditional merge sort with 2 equal parts
       modMerge(A,p,q1);
       modMerge(A,q1+1,q2);
       modMerge(A,q2+1,q3);
       modMerge(A,q3+1,r);
       //here we call the merge function which will Take maximum
       //O(N) time were N is the size of the array
       Merge(A,p,r,q1,q2,q3);
      
   }
}

Explanation

In this modified version of the merge sort unlike the normal one , We divide the merge sort into 4 equal parts , Hence we get 4 recurrence relation and Still the Merge function only takes N time since all part combined together have max size O(N) where N is the size of array

T(N) = 4T(N/4) + N

Add a comment
Know the answer?
Add Answer to:
3a. Runtime Analysis: For each of the following recursive algorithms, state the recurrence equation that is...
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
  • For each of the following recursive methods available on the class handout, derive a worst-case recurrence...

    For each of the following recursive methods available on the class handout, derive a worst-case recurrence relation along with initial condition(s) and solve the relation to analyze the time complexity of the method. The time complexity must be given in a big-O notation. 1. digitSum(int n) - summing the digits of integer: int digitSum(int n) {                 if (n < 10)                                 return n;                 return (digitSum(n/10) + n%10); } 2. void reverseA(int l, int r) - reversing array: void...

  • Write a recurrence relation describing the worst case running time of each of the following algorithms,...

    Write a recurrence relation describing the worst case running time of each of the following algorithms, and determine the asymptotic complexity of the function defined by the recurrence relation. Justify your solution by using substitution or a recursion tree. You may NOT use the Master Theorem. Simplify your answers, expressing them in a form such as O(nk) or (nklog n) whenever possible. If the algorithm takes exponential time, then just give an exponential lower bound using the 2 notation. function...

  • Hello, I would like to get help with the following algorithms and their respective analysis of...

    Hello, I would like to get help with the following algorithms and their respective analysis of runtime along with their recurrence equations, thanks in advance. 1.       Analyze each of the following algorithms by providing a tight big-Oh bound on the run time with respect to the value n. Create a recurrence equation. a.     void padawan(int n) {       if( n <= 10 )             time++;       else       {             for(int i=0; i<n; i++)                         time++;             padawan(n/3);             padawan(n/3);             padawan(n/3);       } } b.    void nerfHerder(int n)...

  • Your task is to design algorithms to solve the following problems. For full credit, your algorithm...

    Your task is to design algorithms to solve the following problems. For full credit, your algorithm must run in logarithmic time. Given a number n greaterthanorequalto 1 and a (user-specified) error tolerance e, you want to approximate the squareroot of n to within error tolerance e. Specifically, you want to return an x = Squareroot n that satisfies |x^2 - n| greaterthanorequalto e. For example, to compute the squareroot of n = 2 with e = 0.01, an acceptable answer...

  • Question 2. (24 points. Recall that in Assignment 1, we developed recursive algorithms for implementing the...

    Question 2. (24 points. Recall that in Assignment 1, we developed recursive algorithms for implementing the specification Given a non-decreasing array Aſ1..n], and given a value v, return a number r such that • ifr=0 then v does not occur in A[1..n] • ifr #0 then 1 <r<n and A[r] = v. In this question, we shall implement this specification by an iterative algorithm, build around a while loop of the form (with AA, BB, CC and CC2 to be...

  • For each of the following problems write a recurrence relation describing the running time of eac...

    For each of the following problems write a recurrence relation describing the running time of each of the following algorithms and determine the asymptotic complexity of the function defined by the recurrence relation. Justify your solution using substitution and carefully computing lower and upper bounds for the sums. Simplify and express your answer as Θ(n k ) or Θ(n k (log n)) wherever possible. If the algorithm takes exponential time, then just give exponential lower bounds. 5. func5 (A,n) /*...

  • And the related algorithms: (20 points) Consider the following strategy for choosing a pivot element for...

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

  • you will analyse two algorithms for finding the median of an array of integers. You will...

    you will analyse two algorithms for finding the median of an array of integers. You will compare both algorithms in terms of timing, and hopefully design a hybrid algorithm that uses both, depending on input size. You will write a report describing your experiments and results. the following Java program implements two algorithms for finding the median of an array of integers. The first uses merge sort, and the other implements the recursive linear time selection algorithm, the task is...

  • Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array...

    Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array of integers q ≤ r and q,r ∈ N. Postcondition: Returns the smallest element of A[q, ... , r]. 1: function Smallest (A , q , r) 2: if q = r then 3: return A[q] 4: else 5: mid <--- [q+r/2] 6: return min (Smallest(A, q, mid), Smallest (A, mid + 1, r)) 7: end if 8: end function (a) Write a recurrence...

  • Mergesort3: Your friend suggests the following variation of Mergesort: instead of splitting the list into two...

    Mergesort3: Your friend suggests the following variation of Mergesort: instead of splitting the list into two halves, we split it into three thirds. Then we recursively sort each third and merge them. Mergesort3 (A[1...n]):    If n <= 1, then return A[1..n]    Let k = n/3 and m = 2n/3    Mergesort3(A[1..k])    Mergesort3(A[k+1..m])    Mergesort3(A[m+1..n) Merge3(A[1..k], A[k+1,..m], A[m+1..n]) Return A[1..m]. Merge3(L0, L1, L2):    Return Merge(L0, Merge(L1,L2)). Assume that you have a function Merge that merges two sorted...

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