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 reverseA(int l, int r) {
if (l < r) {
int t = A[l];
A[l] = A[r];
A[r] = t;
reverseA(l+1, r-1);
}
}
For each of the following recursive methods available on the class handout, derive a worst-case recurrence...
4. (35') Suppose we have the following recursive function in C++: a. (5') What is the returned value of fib(5)? b. (10') Suppose the time cost for fib(n) is T(n), write down the recurrence relation for T(n). C. (20') Rewrite this function in C++ which gives exactly the same output and make it of lower complexity. Analyze the time cost for your function in big-oh notation. int fib (int n) { if (n <= 0) return 0; if( n ==...
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...
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. 上午1:46 3月21日周四 令52%. " 5. endfor 6. return (r); function func4(A, n) *Aarray of n integers */ 1. if n s 20 then return (A[n]); 4. while (i < n/2) do 7. endwhile 8. x...
7. What is the worst-case running time complexity of an algorithm with the recurrence relation T(N) = 2T(N/4) + O(N2)? Hint: Use the Master Theorem.
FOR ALGORITHM A WORST CASE TIME COMPLEXITY IS DESCRIBED BY RECURRENCE FORMULA T(n)= n/ T (n )thi T (c)=1 if c < 100 FOR ALGORITHM B WORST TIME COMPLEXITY IS DESCRIBED BY RECURRENCE FORMULA T(n) = 2T (2/2) + n/logn ; (c) = 1 fc 2100 WHICH ALGORITHM IS ASYMPTOTICALLY FASTER? WHY?
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...
Consider the following recursive algorithm for computing the sum of the first n cubes: S(n) = 13 + 23 + … + n3. (a) Set up a recurrence relation for the number of multiplications made by this algorithm. (b) Provide an initial condition for the recurrence relation you develop at the question (a). (c) Solve the recurrence relation of the question (a) and present the time complexity as described at the question number 1. Algorithm S n) Input: A positive...
3a. Runtime Analysis: For each of the following recursive algorithms, state the recurrence equation that is the runtime of the algorithm. Briefly describe how you determined each term. You do not need to provide the runtime bound – just the equation. T(n) = Product(array, p) // n is size of array if (p>n) return 1 if (array[p] != 0) return array[p] * Product(array, p+1) else return Product(array, p+1) Describe terms: T(n) = Describe terms: // modified merge sort mod Merge(...
What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The input is an array A of size n. You may assume that n is a power of 2. (NOTE: It doesn’t matter what the algorithm does, just analyze its complexity). Assume that the non-recursive function call, bar(A1,A2,A3,n) has cost 3n. Show your work! Next to each statement show its cost when the algorithm is executed on an imput of size n abd give the...
What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The input is an array A of size n. You may assume that n is a power of 2. (NOTE: It doesn’t matter what the algorithm does, just analyze its complexity). Assume that the non-recursive function call, bar(A1,A2,A3,n) has cost 3n. Show your work! Next to each statement show its cost when the algorithm is executed on an imput of size n abd give the...