I have the code below to mimic a recursive behavior of an algorithm, because I failed to figure out the time complexity of that algorithm:
int M(int n)
{
int result = 1;
for (int i = n-1; i >= 0; --i)
{
result += M(i);
}
return result;
}
According to my understanding, I have drawn the tree below to illustrate the algorithm :
(The input n is 3 in the picture). I think the number of nodes in the tree is the complexity of the algorithm. If the input is n, what's the time complexity would be? Thanks!
In general, you can determine the time complexity by analyzing the program's statements (go line by line). However, you have to be mindful how are the statements arranged. Suppose they are inside a loop or have function calls or even recursion. All these factors affect the runtime of your code.
The time complexity of an algorithm is NOT the actual time required to execute a particular code, since that depends on other factors like programming language, operating software, processing power, etc.
The inner loop is executing (log n) times where the outer is executing n times. So for single value of i, j is executing (log n) times, for n values of i, j will loop total n*(log n) = (n log n) times. So the time complexity is O(n log n).
Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, supposing that each elementary operation takes a fixed amount of time to perform. When analyzing the time complexity of an algorithm we may find three cases: best-case, average-case and worst-case.
My background is not CS but I can provide you an easy way to look at this problem,
So I took a paper and pen and started out with different values of n.
n = 2, cycles = 4
n = 3, cycles = 8
n = 4, cycles = 16
n = 5, cycles = 32.
You can clearly see the cycles = 2^N and therefor we can conclude that time complexity of this problem is O(2^N).
Now to look at this in another way could be
We know that
f(0) = 1
f(1) = f(0) + 1 = 2
f(2) = f(1) + f(0) + 1 = 4
...
f(N) = f(N-1) + f(N-2) .. + f(0) + 1 = 2^N.
So now that you have a recurrence relation similar to how you calculate factorial, you can do maths or create a program to measure time complexity of the problem.
Hope that my answer helps you in understanding the theory of calculating time complexity.
Your tree diagram is illuminating. Can you see the line of symmetry? The tree for M(n) looks like two copies of the tree for M(n-1). Thus the number of nodes in the tree is 2**n, and the complexity of the algorithm O(2**n).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With