Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seems very hard to find out the time complexity of this simple program

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 : when n is 3

(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!

like image 278
lightrek Avatar asked May 14 '17 05:05

lightrek


People also ask

How do you find the time complexity of a program?

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.

Why do we not measure complexity simply by the time it takes a program to run?

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.

How can we check the complexity of a program in C++?

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

How do you find the time complexity of a code in Python?

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.


2 Answers

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.

like image 159
zenwraight Avatar answered Oct 18 '22 02:10

zenwraight


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

enter image description here

like image 39
Colonel Panic Avatar answered Oct 18 '22 04:10

Colonel Panic