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

后端 未结 3 1496
野趣味
野趣味 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:02

    As a few people have noticed, this recurrence is different than the original recurrence T(N) = T(N-1) + T(N-2) - T(N-3). I prefer the approach of assuming T(N)=x^N given by @Aravind. With this recurrence, you get the characteristic equation x^3-x^2-x+1=(x-1)^2(x+1). (This will be the characteristic equation for the matrix approach of @templatetypedef, and the denominator of the generating function if you took that approach.)

    The repeated root causes all sorts of difficulties. The matrix isn't diagonalizable. The generating function has a repeated denominator when you factor it. When you assume T(N)=x^N, you only get two linearly independent solutions, and need a third.

    In general, when you assume T(N)=x^N and get a double root r, that means the linearly independent solutions are r^N and N*r^N (a triple root would introduce N^2*r^N). So in our case, the three linearly independent solutions to the recurrence are (-1)^N, 1^N=1, and N*1^N=N. This means that a general solution is T(N)=A(-1)^N+B+C*N, and you use the initial conditions to determine A, B, and C. If C!=0, then T(N)=Θ(N), otherwise T(N)=Θ(1). Which is probably not that realistic for an algorithm.

提交回复
热议问题