简体   繁体   English

如何计算递归函数的上限时间复杂度(“大 O”)?

[英]How to calculate the upper bound time complexity (`“big O`”) of a recursive function?

Suppose I have a recursive function T , and I want to calculate the upper bound timer complexity of this function.假设我有一个递归函数T ,我想计算这个函数的上限计时器复杂度。

T(1) = 3 T(1) = 3

T(n) = 3T(n/3) + 3. T(n) = 3T(n/3) + 3。

How can I find the upper bound of the time complexity of T(n)?如何找到 T(n) 的时间复杂度的上限?

Use the master theorem case使用主定理案例公式where a=3, b=3, c=0.其中 a=3,b=3,c=0。

解决方案

I highly recommend the MIT lectures on Algorithms.我强烈推荐麻省理工学院关于算法的讲座。 You can learn more about the Master theorem in lecture 2您可以在第2 课中了解有关 Master 定理的更多信息

assume that, n = 3^k假设,n = 3^k

F(0) = 3 F(0) = 3

F(k) = 3 * F(k-1) + 3 F(k) = 3 * F(k-1) + 3

 = 3^2 * F(k-2) + 3^2 + 3

 = ...

 = 3^k * F(0) + 3^k + 3^(k-1) + ... + 3

 = 3^(k+1) + 3^k + ... + 3^2 + 3

 = [3^(k+2) - 3] / 2

T(n = 3^k) = F(k) = (9 * n - 3) / 2 = O(n) T(n = 3^k) = F(k) = (9 * n - 3) / 2 = O(n)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM