0

Im trying to calculate the complexity of a recursive call inside a loop:

Calculate(n,m){
for(i=1; i<n; i++){
    if(n==1) return 1;
    Calculate(n-1,m);
 }
for(j=1; i<m; i++){
    Calculate(n,m-1);
    if(m==1) return 1;
} 
}

here is what i did to calculate it :

C(m,n) = 1 + C(m-1,n) + C(m-2,n) + .... + C(1,n) 
           + C(m,n-1) + C(m,n-2) + .... + C(m,1)

is the complexity = 2^(m+n) ??

1 Answer 1

1

You are correct: to reduce either m or n by 1, you need to make 2 calls; since you need to reduce both of them to zero, you need to go through O(m+n) 'levels'.

Thus the number of calls is O(2^(m+n)).

Sign up to request clarification or add additional context in comments.

10 Comments

i'm not sure but to reduce m or n by 1 we need to make m-1 + n-1 call .aren't we ??
Yes, but when we talk about complexity, we are only concerned about the highest-order terms. O(m+n-2) is the same as O(m+n).
Ok thanks but i still didn't really understand why it's 2^??
Because every recursion makes 2 calls, so you're going to have 1+2+4+8+...+2^(m+n-2) calls, which is equal to 2^(m+n-1).
There is two calls each one in loop then it will not be only two calls
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.