Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplifying a T(n) runtime

Given the following function

int g(int y) {
  if (y <= 0) {
    return 1;
  } 
  else {
    return g(y-1) + g(y-2) + g(y-3);
  }
}

We need to find the T(n) run time. Now, I know that you can write

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

I'm just not sure if you can simplify this any further, such as T(n) = 3T(n-1) + 1?

like image 942
Nick Nicolini Avatar asked Jan 16 '23 07:01

Nick Nicolini


1 Answers

Let S(n) = T(n) + 1/2, then S(n) = S(n-1) + S(n-2) + S(n-3).

Then T(n) should be c1 x1n + c2 x2n + c3 x3n - 1/2, where xi are roots of equation x3 - x2 - x - 1 = 0 and ci are specific coefficients.

The accurate solution of T(n) is a bit complex. Actually x1 = 1.84, x2,x3 = -0.42 ± 0.61i (yes, they are not real numbers).

However, if T(n) can be simplified to form like T(n) = 3T(n-1) + 1, then T(n) must be like c1 xn + c0. Therefore, you cannot simplify it any further.

like image 129
Wangfan Fu Avatar answered Jan 22 '23 05:01

Wangfan Fu