Analyzing an algorithm with recurrence T(n) = T(n - 1) + T(n - 2) + T(n -3)?

后端 未结 3 1499
野趣味
野趣味 2021-02-02 03:42

So, someone posted this question earlier, but essentially no effort was put into it, it was poorly tagged and then closed. Nonetheless, I think it could have been a good questi

3条回答
  •  粉色の甜心
    2021-02-02 04:07

    This is a cool method that I learned, so I thought I'll share it with you.It's really simple to estimate the time complexity. Looking at the recurrence we guess that the time complexity is exponential.

    Lets say:

    T(N)=x^n
    

    The given recurrence is

    T(N) = T(N-1) + T(N-2) + T(N-3)
    

    Substituting

     x^n = x^n-1  + x^n-2  + x^n-3
    Dividing throughout by x^n-3
     x^3 = x^2    + x^1    + 1
    Rearranging
     x^3 - x^2 - x - 1=0
    

    You can find out it's cubic roots here.

    This cubic equation has one real root( 1.8392867552141612) and two complex roots(of magnitude 0.7373527).

    Thus asymptotically our algorithm's running time is bounded by T(N)=1.839^n.

提交回复
热议问题