Analysis of Algorithms (Recurrences) - GeeksforGeeks

21 May, 2019

T(n) = T(n/4) + T(n/2) + cn2
T(1) = c
T(0) = 0
           cn^2
         /      \
     T(n/4)     T(n/2)

               cn^2
           /           \      
       c (n^2)/16       c(n^2)/4
      /      \          /     \
  T(n/16)     T(n/8)  T(n/8)    T(n/4) 

                 cn^2
            /             \      
       c(n^2)/16           c(n^2)/4
       /      \            /      \
c(n^2)/256  c(n^2)/64  c(n^2)/64    c(n^2)/16
 /    \      /    \    /    \         /    \    
 


           C1
       /       \
    T(n-1)     T(n-1) 


                    C1
                /       \
              C1           C1
           /     \        /    \
      T(n-2)  T(n-2)   T(n-2)  T(n-2)

                    C1
                /       \
              C1           C1
           /     \        /    \
          C1     C1      C1     C1
        /   \   /  \    /  \   /  \

       
If we sum the above tree level by level, we get the following series
T(n) = C1 + 2C1 + 4C1 + 8C1 + ...
The above series is Geometrical progression and there will be n terms in it.
So T(n) = O(2^n)    

  Let n = 2^m
  T(2^m) = T(2^(m/2)) + 1
  Let T(2^m) =  S(m)
  S(m)  = 2S(m/2) + 1  

S(m)  = (m)  
      = (logn)  /* Since n = 2^m */

  T(n)  = T(2^m) = S(m)
                 = (Logn)

    if  n <= 3  then   T(n) = n
    else T(n) = T(n/3) + cn

T(n) = cn + T(n/3)
     = cn + cn/3 + T(n/9)
     = cn + cn/3 + cn/9 + T(n/27)
Taking the sum of infinite GP series. The value of T(n) will
be less than this sum.
T(n) <= cn(1/(1-1/3))
     <= 3cn/2

or we can say 
cn <= T(n) <= 3cn/2
Therefore T(n) = (n)

  Procedure A(n)  
  If n <= 2 return(1) else return A();

What is the time complexity of the following recursive function:  
c
(A) (n) (B) (nlogn)(C) (logn)(D) (loglogn) 
C
D
B
A
Recursive relation for the DoSomething() is  
  T(n) =  T() + C1 if n > 2  
We have ignored the floor() part as it doesn't matter here if it's a floor or ceiling.  
  Let n = 2^m,  T(n) = T(2^m)
  Let T(2^m) =  S(m)

  From the above two, T(n) = S(m)

  S(m) = S(m/2) + C1  /* This is simply binary search recursion*/
  S(m)  = O(logm)      
          = O(loglogn)  /* Since n = 2^m */
  
  Now, let us go back to the original recursive function T(n) 
  T(n)  = S(m) 
          = O(LogLogn)
 
  T(n) = 2T(n-1) + c
  T(1) = c1.
  T(n) = 2(2T(n-2) + c) + c        = 4T(n-2) + 3c
  T(n) = 8T(n-3) + 6c + c          =  8T(n-3) + 7c
  T(n) = 16T(n-4) + 14c + c        =  16T(n-4) + 15c
  ............................................................
  .............................................................
  T(n) = (2^(n-1))T(1) +  (2^(n-1) - 1)c

  T(n) = O(2^n)
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Comments
Old Comments
Difference between sh and bash
What is "network ID" and "host ID" in IP Addresses?
Primer CSS Flexbox Flex Direction
Converting an image to a Torch Tensor in Python
Prime CSS Marketing Buttons Animated Arrow Symbol
How to Calculate Cosine Similarity in Python?
Python Program for Breadth First Search or BFS for a Graph
10 Best C and C++ Books For Beginners & Advanced Programmers
Show all columns of Pandas DataFrame in Jupyter Notebook
How to split a Dataset into Train and Test Sets using Python