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...
For the following recursive function: Derive a recurrence relation, solve the relation, prove the solution is correct, and find its big-O notation. String f2(String s, char c) { if (s.length() == 0) return s; if (s.charAt(0) == c) return f2(s.substring(1), c); else return s.charAt(0) + f2(s.substring(1), c); }
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...
Find a) overall run time T(n) and b) worst case run timeO(N) of this recursive function. int mystery (int n) { if (n<= 1) {return 1 } else { return mystery(n-1) + mystery(n-2); } }
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(...