[英]Solving the similar recurrence: T(n) = 3T(n/3) + n/3
Given..鉴于...
T(0) = 3 for n <= 1
T(n) = 3T(n/3) + n/3 for n > 1
So the answer's suppose to be O(nlogn)
.. Here's how I did it and it's not giving me the right answer:所以答案应该是
O(nlogn)
.. 这就是我是怎么做的,它没有给我正确的答案:
T(n) = 3T(n/3) + n/3
T(n/3) = 3T(n/3^2) + n/3^2
Subbing this into T(n) gives..将其代入 T(n) 给出..
T(n) = 3(3T(n/3^2) + n/3^2) + n/3
T(n/3^2) = 3(3(3T(n/3^3) + n/3^3) + n/3^2) + n/3
Eventually it'll look like..最终它会看起来像..
T(n) = 3^k (T(n/3^k)) + cn/3^k
Setting k = lgn..
设置
k = lgn..
T(n) = 3^lgn * (T(n/3^lgn)) + cn/3^lgn
T(n) = n * T(0) + c
T(n) = 3n + c
The answer's O(n)
though..What is wrong with my steps?答案是
O(n)
..我的步骤有什么问题?
Eventually it'll look like..
T(n) = 3^k (T(n/3^k)) + cn/3^k
最终它看起来像..
T(n) = 3^k (T(n/3^k)) + cn/3^k
No. Eventually it'll look like不。最终它会看起来像
T(n) = 3^k * T(n/3^k) + k*n/3
You've opened the parenthesis inaccurately.你打开的括号不准确。
T(n) = 3T(n/3) + n/3
T(n/3) = 3T(n/9) + n/9
T(n) = 3(3T(n/9) + n/9) + n/3
= 9T(n/9) + 2*n/3 //statement 1
T(n/9)= 3T(n/27) + n/27
T(n) = 9 (3T(n/27)+n/27) + 2*n/3 // replacing T(n/9) in statement 1
= 27 T (n/27) + 3*(n/3)
T(n) = 3^k* T(n/3^k) + k* (n/3) // eventually
replace k with log n to the base 3.用 log n 替换 k 到基数 3。
T(n) = n T(1) + (log n) (n/3);
// T(1) = 3
T(n) = 3*n + (log n) (n/3);
Hence , O (n* logn)
These types of problems are easily solved using the masters theorem .使用大师定理很容易解决这些类型的问题。 In your case
a = b = 3
, c = log3(3) = 1
and because n^c
grows with the same rate as your f(n) = n/3
, you fall in the second case.在您的情况下
a = b = 3
, c = log3(3) = 1
并且因为n^c
以与f(n) = n/3
相同的速率增长,所以您属于第二种情况。
Here you have your k=1
and therefore the answer is O(n log(n))
这里你有你的
k=1
,因此答案是O(n log(n))
This question can be solved by Master Theorem:这个问题可以通过主定理来解决:
In a recursion form :以递归形式:
where a>=1, b>1, k >=0 and p is a real number, then:其中 a>=1, b>1, k >=0 且 p 为实数,则:
b.) if p = -1, then
c.) if p < -1, then
3. if a < b k 3. 如果 a < b k
a.) if p >=0, then a.) 如果 p >=0,则
b.) if p<0, then T(n) = O(n k ) b.) 如果 p<0,则 T(n) = O(n k )
So, the above equation所以,上面的方程
T(n) = 3T(n/3) + n/3
a = 3, b = 3, k =1, p =0
so it fall into 2.a case, where a = b k所以它属于 2.a 情况,其中 a = b k
So answer will be所以答案将是
O(n⋅log(n))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.