Complexity of factorial recursive algorithm

不问归期 提交于 2019-11-30 03:17:58
templatetypedef

Let's start off with the analysis of this algorithm. We can write a recurrence relation for the total amount of work done. As a base case, you do one unit of work when the algorithm is run on an input of size 1, so

T(1) = 1

For an input of size n + 1, your algorithm does one unit of work within the function itself, then makes a call to the same function on an input of size n. Therefore

T(n + 1) = T(n) + 1

If you expand out the terms of the recurrence, you get that

  • T(1) = 1
  • T(2) = T(1) + 1 = 2
  • T(3) = T(2) + 1 = 3
  • T(4) = T(3) + 1 = 4
  • ...
  • T(n) = n

So in general this algorithm will require n units of work to complete (i.e. T(n) = n).

The next thing your teacher said was that

T(n) = n = 2log2 n

This statement is true, because 2log2x = x for any nonzero x, since exponentiation and logarithms are inverse operations of one another.

So the question is: is this polynomial time or exponential time? I'd classify this as pseudopolynomial time. If you treat the input x as a number, then the runtime is indeed a polynomial in x. However, polynomial time is formally defined such that the runtime of the algorithm must be a polynomial with respect to the number of bits used to specify the input to the problem. Here, the number x can be specified in only Θ(log x) bits, so the runtime of 2log x is technically considered exponential time. I wrote about this as length in this earlier answer, and I'd recommend looking at it for a more thorough explanation.

Hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!