Complexity formula of this algorithm - algorithm

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).

Related

What could we the complexity proof of it?

In an online assessment I got one coding challenge and I wrote one recursive code for it.
The question was -
Given an integer n, return all the reversible numbers that are of length n.
A reversible number is a number that looks the same when rotated 180 degrees (looked at upside down).
Example:
Input: n = 2
Output: ["11","69","88","96"]
I wrote some kind of recursive approach and it passed.
vector<string> recursion(int n, int m) {
if (n == 0) {
vector<string> res = {""};
return res;
}
if (n == 1) {
vector<string> res = {"0", "1", "8"};
return res;
}
vector<string> ans = recursion(n - 2, m);
vector<string> res;
for (auto subAns: ans) {
// We can only append 0's if it is not first digit.
if (n != m) {
res.push_back('0' + subAns + '0');
}
res.push_back('1' + subAns + '1');
res.push_back('6' + subAns + '9');
res.push_back('8' + subAns + '8');
res.push_back('9' + subAns + '6');
}
return res;
}
vector<string> getAllNumbers(int n) {
return recursion(n, n);
}
I thought because we are calling 5 recursion it is something like 5^N but I want to do exact space and time complexity analysis for it.
Can anyone help me out what could be the exact solution, it is very tricky for me to figure out the exact space time complextiy for recursive approaches
Observe first that there are Θ(5n/2) valid numbers of length
n. Given the recurrence
C(−2) = 0
C(−1) = 0
C(0) = 1
C(1) = 3
∀n ≥ 2, C(n) = 5 N(n−2),
there are C(n) − C(n−2) numbers. If n = 2k where k is an integer, then
C(n) = 5k. If n = 2k + 1, then C(n) = 3 (5k).
The running time is Θ(5n/2 n). We can write a recurrence
T(0) = O(1)
T(1) = O(1)
∀n ≥ 2, T(n) = T(n−2) + Θ(5n/2 n),
where the latter term counts the cost of constructing Θ(5n/2)
numbers each of length n. This is not a terribly interesting recurrence;
we end up with a sum whose terms decrease faster than geometrically, so
it's Θ of its largest term.
Space usage will be asymptotically the same since space usage is bounded
above by time and below by the total size of the output, which are the
same Θ.

What is the time complexity of recursive function?

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.

Computing expected time complexity of recursive program

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.

complexity algorithm recurrence relation

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.

What is the recurrence if the base case is O(n)?

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).

Resources