edited by
1,871 views
7 votes
7 votes

Why is recursive equation of following code $T(n)=T(n/2)+O(1)$, not $T(n)=8*T(n/2)+O(1)$?

 

int x=0;
int A(n)
{
    
    if(n==1) return 1;
    else
    {
        X+=8A(n/2)+n^3;
    }
    return X;
}
edited by

3 Answers

5 votes
5 votes

This kind of questions are a bit confusing as they trick our eyes into believing that the coefficient present in the function call should be the constant in the time complexity recurrence as well. 

When you have a recurrence of the sort of:

T(n)=aT(n/b)+c

what this actually means is that every time your function is called, it will in turn call itself 'a' more times in that very instance untill it reaches base case where it begins to die:

To explain this statement with an example, suppose I have the following function:

A(n)
{
   //The base condition where the function is finally relieved of its boredom
   if(n==1)
      return 1;
   else
   //The mighty recurrence   
     return(2A(n/2)+3A(n/2));
}

You can clearly see that I am calling the function A() two times once with a coefficient 2 and once with a coefficient 3, Since I am calling it two times, the recurrence for time complexity will be as below:

T(n)=2T(n/2)+O(1)

the question that comes now is what happens to our beloved 2 and 3 present in function calls, well they are nothing more that some numbers waiting to be multiplied. When the first A(n/2) comes back to its calling point we multiply 2 to it and when the second A(n/2) comes back we multiply 3 to it. This multiplication is a simple constant time operation (in current context), and same goes for their addition. Both these operations are covered in our mighty O(1). 


Coming back to your question, we can clearly see that each time the function makes only one recurrence call to itself:

 X+=8A(n/2)+n^3;

and 8 is simply a constant waiting to be multiplied. As for nit is simply calculable in constant time. 

therefore the recurrence should be: 

T(n)=T(n/2)+O(1)

Nice question, you made me think a lot.

0 votes
0 votes
In simple way,  the time complexity of recurrence program is how many time function call will occurs. In given problem A(n) will call A(n/2) just one time and then whatever output A(n) get from the A(n/2) ,It simply multiply with 8 and add n^3 . Multiplication and addition take constant amount of time so  ,

T(n)=T(n/2)+O(1) is time complexity of program.

If you want to find equation for value than it will be ,

T(n)=8∗T(n/2)+ (n^3)

Related questions

2 votes
2 votes
3 answers
1
sabir asked Nov 26, 2015
13,403 views
Which is right method to apply for this substitute or recursion...Can we convert it in master theorem format
9 votes
9 votes
1 answer
2
anoop yadav 2 asked Jan 8, 2018
13,461 views
How to solve above recurrence relation (With substitution method)??
2 votes
2 votes
1 answer
3
0 votes
0 votes
3 answers
4
Anmol Verma asked Jan 9, 2017
1,486 views
1) T(n)=T(n-1)+1/n.2) T(n)=T(n-1) +1/log(n)3)T(n)=3T((n/3) - 2)+n/2.4)T(n)=T(n/2)+T(n/4)+T(n/8)+n.Use Masters theorem