递归关系 T(n) = T(3/4 * n) +复杂度(1)

发布于 2024-10-06 11:05:49 字数 174 浏览 3 评论 0原文

我正在计算递推关系

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

み格子的夏天 2024-10-13 11:05:49

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)

小兔几 2024-10-13 11:05:49

尝试将 T(n) = c*nT(n) = c * log n 代入方程并求解。这两个方程之一将是可解的。

您还可以通过评估不同 n 值的函数来检查您的答案。

-- Define T in your preferred language
t n | n <= 1 = 1 | otherwise = t (3/4 * n) + 1

-- If it's O(log n), then T(n)/log(n) should be asymptotically constant
-- If it's O(n), then T(n)/n should be asymptotically constant
check1 n = t n / log n
check2 n = t n / n

print [check1 1e10, check1 1e11, check1 1e12, check1 1e13]
print [check2 1e10, check2 1e11, check2 1e12, check2 1e13]

其中一个将收敛到一个小的正数,另一个将趋于零或无穷大。

Try substituting T(n) = c*n or T(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.

-- Define T in your preferred language
t n | n <= 1 = 1 | otherwise = t (3/4 * n) + 1

-- If it's O(log n), then T(n)/log(n) should be asymptotically constant
-- If it's O(n), then T(n)/n should be asymptotically constant
check1 n = t n / log n
check2 n = t n / n

print [check1 1e10, check1 1e11, check1 1e12, check1 1e13]
print [check2 1e10, check2 1e11, check2 1e12, check2 1e13]

One of these will converge to a small positive number, the other will go to zero or infinity.

撧情箌佬 2024-10-13 11:05:49

给出的答案并未显示解决此类递归问题的正确方法。您的案例很容易通过 大师定理 解决,在您的案例中 a=1< /code> 和 b=4/3。这意味着c = logb(a) = 0

因为您的 f(n) 是一个常量,所以您属于第二种情况,即 k=0。所以解决方案是 在此处输入图像描述,其中 c = 0k = 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 and b=4/3. This means that c = logb(a) = 0.

Because your f(n) is a constant, you fall in the second case bucket and where k=0. So the solution is enter image description here, where c = 0 and k = 0. This means that:


O(log(n)) is a correct answer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文