In Java Language
Write a recurrence equation expressing the time complexity of the following algorithm. Explain your answer.
Assume that n is a power of 2.
Algorithm rec(n)
Input: Integer value n ≥ 0
if n = 0 then return 1
else {
c ← 0
For i ← 0 to n−1 do c ← c + i
c ← c + rec(n/2)
return c
}
Base Case
F(0)=C0
In else part,for loop is executed n times.Hence C1N.Outside loop without recusrvide it performs C2 constant operations.For recursive call it uses N/2.hence it is F(N/2).
Total recurrance relation:
F(0)=1
F(N) = C1N + C2 + F(N/2) where N>0
==========================================================
package snippet;
public class FrequntLetter {
public static int rec(int n)
{
if(n==0)
{
return 1;
}
else
{
int c=0;
for(int
i=0;i<n-1;i++)
{
c=c+i;
}
c=c+rec(n/2);
return c;
}
}
public static void main(String[] args) {
System.out.println(rec(4));
}
}
=====================================================
package snippet;
public class FrequntLetter {
public static int rec(int n)
{
if(n==0)
{
return 1;
}
else
{
int c=0;
for(int
i=0;i<n-1;i++)
{
c=c+i;
}
c=c+rec(n/2);
return c;
}
}
public static void main(String[] args) {
System.out.println(rec(64));
}
}
===================================================
Output:
2548
In Java Language Write a recurrence equation expressing the time complexity of the following algorithm. Explain...
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?
What is the time-complexity of the algorithm abc? Procedure abc(n: integer) s := 0 i :=1 while i ≤ n s := s+1 i := 2*i return s consider the following algorithm: Procedure foo(n: integer) m := 1 for i := 1 to n for j :=1 to i2m:=m*1 return m c.) Find a formula that describes the number of operations the algorithm foo takes for every input n? d.)Express the running time complexity of foo using big-O/big-
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...
Analyze the time complexity of the following algorithm. You may assume that the floor function in line 2 takes Theta (1) time. Please show your work. Input: data: array of integers Input: n: size of data Output: median of data 1 Algorithm: MedianSelect 2 lim = [n/2] + 1 3 min = - infinity 4 for i = 1 to lim do 5 prev = min 6 min = infinity 7 for j = 1 to n do 8 if...
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...
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...
3. Calculate the time complexity of the following algorithm: 1. Initialize sum to 0. I 2. Input the value of n. 3. While i = 0 to n-1 do a. sum=sumti; ; b. Increment i
pleas answer asap 3. (20 points) Algorithm Analysis and Recurrence There is a mystery function called Mystery(n) and the pseudocode of the algorithm own as below. Assume that n 3* for some positive integer k21. Mystery (n) if n<4 3 for i1 to 9 5 for i-1 to n 2 return 1 Mystery (n/3) Print "hello" 6 (1) (5 points) Please analyze the worst-case asymptotic execution time of this algorithm. Express the execution time as a function of the input...
Java question Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A itf ATO] + A[1] + +A[iI-1] Ali+1]+ Ali+2] +... + A[n-1]; where 0 <i< n-1 Similarly, 0 is an stability index if (A[1] A[2]A[n-1]) 0 and n-1 is an stability index if (A[0] A[1]+... A[n-21) 0 Example:...