简体   繁体   English

T(n) = 2T(n/2) +O(1) 的时间复杂度是多少

[英]what the Time Complexity of T(n) = 2T(n/2) +O(1)

i want to know what the Time Complexity of my recursion method : T(n) = 2T(n/2) + O(1) i saw a result that says it is O(n) but i don't know why , i solved it like this :我想知道我的递归方法的时间复杂度是多少:T(n) = 2T(n/2) + O(1) 我看到一个结果说它是 O(n) 但我不知道为什么,我解决它是这样的:

T(n) = 2T(n/2) + 1
T(n-1) = 4T(n-1/4) + 3
 T(n-2) = 8T(n-2/8) + 7
......  …………..  ..
T(n) = 2^n+1 T (n/2^n+1) + (2^n+1 - 1) 

Consider that n=2^m , which allows you to write考虑n=2^m ,它允许你写

T(2^m)=2T(2^(m-1))+O(1)

or by denoting S(m):= T(2^m) ,或通过表示S(m):= T(2^m)

S(m)=2 S(m-1) + O(1),

2^m S(m)=2 2^(m-1)S(m-1) + 2^(m-1) O(1)

and finally,最后,

R(m) = R(m-1) + 2^(m-1) O(1).

Now by induction,现在通过归纳,

R(m) = R(0) + (2^m-1) O(1),

T(n) = S(m) = 2^(1-m) T(2^m) + (2 - 2^(m-1)) O(1) = 2/n T(n) + (2 - n/2) O(1).

I think you have got the wrong idea about recursive relations.我认为您对递归关系有错误的看法。 You can think as follows:你可以这样思考:

If T(n) represents the value of function T() at input = n then the relation says that output is one more double the value at half of the current input.如果 T(n) 表示函数 T() 在 input = n 处的值,则该关系表示输出是当前输入一半处的值的两倍。 So for input = n-1 output ie T(n-1) will be one more than double the value at half of this input, that is T(n-1) = 2*T((n-1)/2) + 1因此,对于输入 = n-1 输出,即 T(n-1) 将是该输入一半处的值的两倍多,即 T(n-1) = 2*T((n-1)/2) + 1

The above kind of recursive relation should be solved as answered by Yves Daoust.上面那种递归关系应该按照 Yves Daoust 的回答来解决。 For more examples on recursive relations, you can refer this有关递归关系的更多示例,您可以参考这个

There are a couple of rules that you might need to remember.您可能需要记住一些规则。 If you can remember these easy rules then Master Theorem is very easy to solve recurrence equations.如果你能记住这些简单的规则,那么主定理很容易解决递推方程。 The following are the basic rules which needs to be remembered以下是需要记住的基本规则

case 1) If n^(log b base a) << f(n) then T(n) = f(n)
case 2) If n^(log b base a) = f(n) then T(n) = f(n) * log n
case 3) 1) If n^(log b base a) >> f(n) then T(n) = n^(log b base a)

Now, lets solve the recurrence using the above equations.现在,让我们使用上述方程求解递归。

a = 2, b = 2, f(n) = O(1)
n^(log b base a) = n = O(n)

This is case 3) in the above equations.这是上述等式中的情况 3)。 Hence T(n) = n^(log b base a) = O(n).因此 T(n) = n^(log b base a) = O(n)。

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

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