Complexity of the recursion: T(n) = T(n-1) + T(n-2) + C

前端 未结 5 1528
北恋
北恋 2021-01-12 11:12

I want to understand how to arrive at the complexity of the below recurrence relation.

T(n) = T(n-1) + T(n-2) + C Given T(1) = C and

相关标签:
5条回答
  • 2021-01-12 11:57

    You can use this general approach described here.Please ask if you have more questions.

    0 讨论(0)
  • 2021-01-12 11:59

    If you were also interested in finding an explicit formula for T(n) this may help.

    We know that T(1) = c and T(2) = 2c and T(n) = T(n-1) + T(n-2) + c.

    So just write T(n) and start expanding...

    T(n) = T(n-1) + T(n-2) + c
    T(n) = 2*T(n-2) + T(n-m) + 2c
    T(n) = 3*T(n-3) + 2*T(n-4) + 4c
    T(n) = 5*T(n-4) + 3*T(n-5) + 7c
    etc ...
    

    You see the coefficients are Fibonacci numbers themselves!

    Call F(n) the nth Fibonacci number. F(n) = (phi^n + psi^n)/sqrt(5) where phi = (1+sqrt(5))/2 and psi = -1/phi, then we have:

    T(n) = F(n)*2c + F(n-1)*c + (F(n+1)-1)*c
    

    Here is some quick code to demonstrate:

    def fib_gen(n):
        """generates fib numbers to avoid rounding errors"""
        fibs=[1,1]
        for i in xrange(n-2):
            fibs.append(fibs[i]+fibs[i+1])
        return fibs
    
    F = fib_gen(50) #just an example.
    c=1
    
    def T(n):
        """the recursive definiton"""
        if n == 1:
            return c
        if n == 2:
            return 2*c
        return T(n-1) + T(n-2) + c
    
    def our_T(n): 
        n=n-2 #just because your intials were T(1) and T(2), sorry this is ugly!
        """our found relation"""
        return F[n]*2*c + F[n-1]*c + (F[n+1]-1)*c
    

    and

    >>> T(24)
    121392
    >>> our_T(24)
    121392
    
    0 讨论(0)
  • 2021-01-12 12:01

    Is "worse than exponential" accurate enough for your purposes? The special case C=0 defines http://en.wikipedia.org/wiki/Fibonacci_number, which you can see from the article is exponential. Assuming C is positive, your series will be growing faster than this. In fact, your series will lie between the Fibonacci series and a variant of the Fibonacci series in which the golden ratio is replaced by something very slightly larger.

    0 讨论(0)
  • 2021-01-12 12:02

    This type of recurrences are called: non-homogeneous recurrence relations and you have to solve in the beginning homogeneous recurrence (the one without a constant at the end). If you are interested, read the math behind it.

    I will show you an easy way. Just type your equation in wolfram-alpha and you will get:

    So the complexity grows in the same way as either Lucas or Fibonacci number (the bigger of them).

    But both of them have the same growth rate:

    and therefore your growth rate is an exponential of the golden ratio: O(phi^n)

    0 讨论(0)
  • 2021-01-12 12:09

    The complexity is related to input-size, where each call produce a binary-tree of calls

    Where T(n) make 2n calls in total ..

    T(n) = T(n-1) + T(n-2) + C

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

    O(2n)

    In the same fashion, you can generalize your recursive function, as a Fibonacci number

    T(n) = F(n) + ( C * 2n)

    Next you can use a direct formula instead of recursive way

    Using a complex method known as Binet's Formula

    0 讨论(0)
提交回复
热议问题