Arjun Thakur
Arjun Thakur

Reputation: 345

Calculating time complexity of recursive algorithm.

I have just started solving Topcoder algorithm problems and have written this algorithm for SRM 466 Div 2 LotteryTicket problem in Java.

As I am not good with time complexity so if somebody could explain me how to calculate time complexity for this algorithm step by step.

public static String buy1(int price,int...b){
    int sum=0; String stat="IMPOSSIBLE";

    for(int i=0;i<b.length;i++)
        sum=sum+b[i];

    if(sum==price)
        return "POSSIBLE";

    if(b.length>1){
        stat=buy1(price,Arrays.copyOfRange(b,0,b.length-1));
        stat=buy1(price,Arrays.copyOfRange(b,1,b.length));
    }

    return stat;
}

Upvotes: 1

Views: 3514

Answers (3)

Imposter
Imposter

Reputation: 2686

For your case , The recurrence relation is (let b.length() be bn)

                     ___________buy1(p,bn-1) (as (b,0,b.length-1) equivalent is bn-1 in number )
                    /
    buy1(p,bn) ____/
                   \
                    \___________ buy1(p,bn-1)  (as (b,1,b.length) equivalent is bn-1 in number )

So our problem for n = two sub-problem of n-1 hence our time function T(n) is as follows

   T(n)=2T(n-1)+c (For convenience lets eliminate c as it is very less compared to T(n) for this instance )
   T(n)=2[2(T(n-2))]
   T(n)=2{2[2(T(n-3))]} ===> 2poweri(T(n-i))  -------- equation(1)

The recurrence ends when it meets base condition . Lets say T(0)=c(be base condition) that means t(n-i)=t(0) for base condition .so i=n

Substituting i value in equation(1) we get 2power(n){t(0)}

So our Time function value will be 2power(n) and our complexity of program is equal to bigoh(2power(n))

Upvotes: 5

Gloomcore
Gloomcore

Reputation: 950

Interesting question. Lets calcluate it correctly ;) So we will check the worst situation when condition (sum == price) will never appear.

First let's check complecity when b.length = 1. Then you should use only only one "=" operation inside the cycle:

for(int i=0;i<b.length;i++)

And 2 inside initialization:

int sum=0; String stat="IMPOSSIBLE";

Next step. Lets calculate this task for N. First you need to do N "=" operations inside first the cycle, 2 inside initialization and 2 operations inside if.

    stat=buy1(price,Arrays.copyOfRange(b,0,b.length-1));
    stat=buy1(price,Arrays.copyOfRange(b,1,b.length));

Another operations are made inside recursive step. So we can use recurrent formula for this situation, which equals:

f(n) = 4 + n + 2*f(n-1), f(1) = 3

Solution of this equation is: f(n) = -6+5 * 2^n-n

So complecity of your algo is exponential. O(2^n) I am ignoring all another operations, except "=" because they will not change asymptotic complexity.

Upvotes: 2

Ajay George
Ajay George

Reputation: 11875

You can use the recursion tree method and master method to find the complexity.

Check this out for more ideas on how to approach this problem.

As an additional exercise try computing the complexity of merge sort using this.

Upvotes: 2

Related Questions