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
3a. Runtime Analysis: For each of the following recursive algorithms, state the recurrence equation that is...
Problem 5: Recurrence relations and detailed analysis of recursive algorithm efficiency g(n: non-negative integer) 1. if n ≤ 1 then return n 2. else return (5 * g(n─1) ─ 6 * g(n─2)) MergeSort divides the array to be sorted into two equal halves, calls itself recursively on each half to sort that subarray, and then calls the Merge algorithm to merge the two sorted halves in linear time. This leads to its two recurrence relations T(n)=2T(n/2)+cn, n>1;...
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, 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 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 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 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 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 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 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 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...