递归关系 T(n) = T(3/4 * n) +复杂度(1)
我正在计算递推关系
T(n) = T(3/4 * n) + O(1)
它的结果是 O(log(n))
code>,但我事先被告知解决方案是O(n)
。我找不到哪里出错了 - 这看起来就像二分搜索的递归关系。有什么建议吗?
I'm working out the recurrence relation
T(n) = T(3/4 * n) + O(1)
It's coming out to be O(log(n))
, but I was told before hand that the solution is O(n)
. I can't find where I'm going wrong - this looks just like the recurrence relation for binary search. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
T(n) = T(3/4 * n) + O(1) ........................(1)
在上面的等式中如果你问这个递推式的解,T(3/4 * n) 是未知项,那么我想说我们可以解这个方程。采用替代法。
在此我们必须从主方程中找出 T(3n/4) 的值。并代入等式中。递归地。因为这种递归依赖于递归。
n=3n/4
T(3n/4)=T((3/4)^2 * n)+ c ........................(2) 符号 O 替换为常数 c。
现在将 T(3n/4) 代入 (1)
T(n)= T((3/4)^2 * n) +2c ........(3)
现在将 n=((3/4)^2 * n) 代入 (1)
T((3/4)^2 * n)=T((3/4)^3 * n)+c
代替
T(n)= T((3/4)^3 * n)+3c ........................(4)
第 k 步后 eq.将
T(n)=T((3/4)^k * n)+kc ........(5) 在这一步 n 将为 2 或 1(输入大小)
(3/4)^k * n= 1
n=(4/3)^k 两边取对数。
log(n)=k*log(4/3)
k=log(n) ........................
将值放入等式中。 (5)
T(n)=T(1)+log(n) * c ........................(6)
T(n)= O(log n)
T(n) = T(3/4 * n) + O(1) ...............(1)
in above eq. T(3/4 * n) is unknown term if you are asking about the solution of this recurrence, then i want to say that we can solve this eq. using substitution method.
in this we have to find out the value of T(3n/4) from main eq. and substitute in the eq. recursively. As this recurrence is depends on recursion.
n=3n/4
T(3n/4)=T((3/4)^2 * n)+ c ...............(2) notation O replaced by constant c.
now substitute T(3n/4) in (1)
T(n)= T((3/4)^2 * n) +2c ................(3)
now put n=((3/4)^2 * n) in (1)
T((3/4)^2 * n)=T((3/4)^3 * n)+c
Substitute
T(n)= T((3/4)^3 * n)+3c ...............(4)
After kth step eq. will be
T(n)=T((3/4)^k * n)+kc ................(5) at this step n will be 2 or 1(input size)
(3/4)^k * n= 1
n=(4/3)^k by taking log on both side.
log(n)=k*log(4/3)
k=log(n) ..............
place value in eq. (5)
T(n)=T(1)+log(n) * c ..............(6)
T(n)= O(log n)
尝试将
T(n) = c*n
或T(n) = c * log n
代入方程并求解。这两个方程之一将是可解的。您还可以通过评估不同 n 值的函数来检查您的答案。
其中一个将收敛到一个小的正数,另一个将趋于零或无穷大。
Try substituting
T(n) = c*n
orT(n) = c * log n
into the equation and solving. One of the two equations will be solvable.You can also check your answer by evaluating the function for different values of n.
One of these will converge to a small positive number, the other will go to zero or infinity.
给出的答案并未显示解决此类递归问题的正确方法。您的案例很容易通过 大师定理 解决,在您的案例中
a=1< /code> 和
b=4/3
。这意味着c = logb(a) = 0
。因为您的
f(n)
是一个常量,所以您属于第二种情况,即k=0
。所以解决方案是 ,其中c = 0
和k = 0
。这意味着:O(log(n))
是正确答案The answers that are given are not showing the correct way of solving this kind of recurrences. You case is easily solved with a Masters theorem and in your case
a=1
andb=4/3
. This means thatc = logb(a) = 0
.Because your
f(n)
is a constant, you fall in the second case bucket and wherek=0
. So the solution is , wherec = 0
andk = 0
. This means that:O(log(n))
is a correct answer