int function(int n){
if (n<=1)
return 1;
else
return (2*function(n/2));
}
What is the recurrence relation T(n) for running time , and why ?
The complexity-function of this algorithm would be
T(n) = T(n / 2) + 1
T(1) = 1
Applying the master-theorem, we would get
a = 1
b = 2
c = 0 (1 = n^0)
log b(A) = log2(1) = 0 = 0 c, thus case 2
apply values and the result is O(log n).
As #guillaume already correctly stated, this can be solved a lot easier by using a linear function though.
You can calculate directly: it is the nearest 2^n, largest or equal.
You calculate L=log2(n), and you take 2^L, or 2^(L+1)
Complexity is O(log2 N) : log2 N operations.
Related
I have a recursive function. And i’m looking for what is the time complexity ?
Here is the function
public static int f7(int N){
if (N==1) return 0;
return 1 + f7(N/2);
}
First, we come up with a recurrence for this function:
T(1) = 1
T(n) = T(n/2) + 1
This is a recurrence that we can plug into the master theorem, which will give us Θ(log n) as an answer.
Assume that when N=1, the call takes a time units, and when N is a power of 2, it takes b time units, not counting the recursive call.
Then
T(1) = a
T(2^n) = T(2^(n-1)) + b.
This can be seen as an ordinary linear recurrence
S(0) = a
S(n) = S(n-1) + b = S(n-2) + 2b = … = S(0) + nb = a + nb,
or
T(N) = a + Lg(N) b
where Lg denotes the base-2 logarithm.
When N is not a power of 2, the time is the same as for the nearest inferior power of 2.
The exact formula for all N is
T(N) = a + [Lg(N)] b.
Brackets denote the floor function.
I wish to determine the average processing time T(n) of the recursive algorithm:
int myTest( int n ) {
if ( n <= 0 ) {
return 0;
}
else {
int i = random( n - 1 );
return myTest( i ) + myTest( n - 1 - i );
}
}
provided that the algorithm random( int n ) spends one time unit to return
a random integer value uniformly distributed in the range [0, n] whereas
all other instructions spend a negligibly small time (e.g., T(0) = 0).
This is certainly not of the simpler form T(n) = a * T(n/b) + c so I am lost at how to write it. I can't figure out how to write it due to the fact that I am taking a random number each time from 0 to n-1 range and supplying it twice to the function and asking for the sum of those two calls.
The recurrence relations are:
T(0) = 0
T(n) = 1 + sum(T(i) + T(n-1-i) for i = 0..n-1) / n
The second can be simplified to:
T(n) = 1 + 2*sum(T(i) for i = 0..n-1) / n
Multiplying by n:
n T(n) = n + 2*sum(T(i) for i = 0..n-1)
Noting that (n-1) T(n-1) = n-1 + 2*sum(T(i) for i = 0..n-2), we get:
n T(n) = (n-1) T(n-1) + 1 + 2T(n-1)
= (n+1) T(n-1) + 1
Or:
T(n) = ((n+1)T(n-1) + 1) / n
This has the solution T(n) = n, which you can derive by telescoping the series, or by guessing the solution and then substituting it in to prove it works.
I have this algorithm (golden ratio):
public static float golden(int n){
float res;
if(n == 0) {
res = 1;
} else {
res = (float) (1.0 + (1.0 / golden(n-1)));
}
return res;
}
I suppose The T(n) formula is T(n-1). I can get the complexity following this formula.
T(n) = aT(n - b) + c^n p(n)
and this one:
What's p(n) and c?
For each n > 0 the call to golden(n) results in a constant number of arithmetic operations and one recursive call, therefore you can write the recurrence for the time complexity function T(n) as
T(n) = T(n-1) + k, n>0
where k is the number of operations in each call. This is the recurrence relation for sequential search.
Now you can apply the formula that you mention in the question. Recall that d in the formula is the degree of the polynomial p(n). Here
a = 1, c = 1, b = 1, and p(n) = k,
therefore d = 0, now applying your formula you get a=c^b, and therefore the second case applies, so
T(n) = Theta(n^{d+1}c^n) = Theta(n).
This gives the final answer T(n) = Theta(n).
Consider the following code :
def f(int A):
if A<256 :
return A^(1/2)
B = A^(1/2)
C = B^(1/2)
return (f(B)+f(C))mod 16
Let T(n) denote the time complexity of this Algorithm. Time complexity is :
a) T(n) = T(n/2) + T(n/4) + O(1)
b) T(n) = T(n^(1/2)) + T(n^(1/4)) +O(1)
according to me, the answer should be (b) but it is given as (a). Why is this the answer, and where am I going wrong?
We usually talk about the complexity of an algorithm with respect to it's input size (i.e. the length of it's binary representation), not the actual numerical value of the input.
So in this case the input size is n = log(A), and the binary representations of B and C are n / 2 = log(A) / 2 = log(A^(1/2)) = log(B) and n / 4 = log(C) respectively. This gives us the relation in (a).
We have to create an algorithm and find and solve its recurrence. Finding the recurrence has me stumped..
foo(A, C)
if (C.Length = 0)
Sum(A)
else
t = C.Pop()
A.Push(t)
foo(A,C)
foo(A,C)
Initially A is empty and C.Length = n. I can't give the real algorithm because that's not allowed.
My instructor told me that I might try to use 2 variables. This is what I came up with:
T(n, i) = { n if i = 0
2*T(n, i-1) + C if i != 0
I couldn't solve it, so I also tried to solve a recurrence with just one variable:
T(n) = { n0 if n = 0
2*T(n-1) + C if n != 0
Where n0 is the initial value of n.
How do you form a recurrence from an algorithm where the complexity of the base case is O(n)?
Let f(n) be the complexity if C is of size n. Let N be the original size of C.
Then f(0) = N and f(n) = 2 * f(n - 1) + c.
This has the solution f(n) = N * 2^n + (2^n - 1) * c, and so f(N) = O(N * 2^N).