[英]a better guess on upper bound
It's a question from 《introduction to algorithms》whose number is 4.4-5 and is described like this: 这是来自“算法的介绍”的问题,其编号为4.4-5并且描述如下:
Use a recursion tree to determine a good asymptotic upper bound on the recurrence T(n) = T(n-1) + T(n/2) + n.Use the substitution method to verify your answer.
使用递归树确定递推T(n)= T(n-1)+ T(n / 2)+ n的良好渐近上界。使用替换方法验证您的答案。
I found it is difficult to me to calculate the recursion tree's recurrence. 我发现计算递归树的重复是很困难的。 The answer I gave
我给出的答案
Math.pow(2,n)
Math.pow(2,n)的
seems too loose.Maybe there is some better guess existed.Thanks for any help. 看起来太松了。也许有更好的猜测存在。谢谢你的帮助。
Hope I did no mistakes :) 希望我没有犯错:)
let's A(n)=T(n/2)+n
我们的
A(n)=T(n/2)+n
0. T(n)=T(n-1)+A(n)=T(n-2)+A(n-1)+A(n)=...=A(1)+A(2)+...+A(n)
T(n)=sum[1..n]A(n)
T(n)=sum[i=1..n]T(i/2)+sum[i=1..n]i
assuming n/2
is integer division, T(n/2)=T((n+1)/2)
for even n
, so the first sum consists of two equal halves: T(1)+T(1)+T(2)+T(2)+...
假设
n/2
是整数除法,对于偶数n
, T(n/2)=T((n+1)/2)
,因此第一个和由两个相等的一半组成: T(1)+T(1)+T(2)+T(2)+...
1. T(n)=2*sum[1..n/2]T(i)+n*(n-1)/2
since T(n)<=T(m) for every n<=m
因为
T(n)<=T(m) for every n<=m
2. T(n)<=n*T(n/2)+n*(n-1)/2
since T(n/2)>=n/2>=(n-1)/2
因为
T(n/2)>=n/2>=(n-1)/2
3. T(n)<=n*T(n/2)+n*T(n/2)=2*n*T(n/2)
let's consider this for only n=2^k
, since T
is monotonous: n=2^k
and U(k)=T(2^k)
让我们只考虑
n=2^k
,因为T
是单调的: n=2^k
且U(k)=T(2^k)
4. U(k)<=2*(2^k)*U(k-1)=2^(k+1)*U(k-1)
let L(k)=log2 U(k)
设
L(k)=log2 U(k)
5. L(k)<=k+1+L(k-1)
just like we did between step0 and step1 就像我们在step0和step1之间做的那样
6. L(k)<=k*(k-1)/2+k=k*k/2-k/2+k<=k*k
7. U(k)=2^L(k)<=2^squared(k)
8. T(n)=U(log2 n)<=2^squared(log2 n)
The recursion relation seems to give rise to a sub-exponential and super-linear computation-time, which means that any chosen base would work as an upper bound given a large enough n
. 递归关系似乎产生了亚指数和超线性计算时间,这意味着给定足够大的
n
,任何选定的基都将作为上限。
Your choice of 2^n
is a good answer and possibly the one they were looking for in the book. 你选择
2^n
是一个很好的答案,可能是他们在书中寻找的那个。 It is a simple solution that is valid even for quite small values of n
. 这是一个简单的解决方案,即使对于非常小的
n
值也是有效的。 (Still, I understand why you are asking the question because it does grow much faster than T(n)
even for moderately large n
.) (尽管如此,我理解你为什么要问这个问题,因为即使对于中等大小的
n
它确实比T(n)
增长得快得多。)
Given T(1) = 1
(or some other constant) the recursion equation gives us the running time as follows for the first few values of n
. 给定
T(1) = 1
(或一些其他常数),递归方程给出了n
的前几个值的运行时间如下。
T(1) = 1 n^1 = 2
T(2) = 4 n^2 = 4
T(3) = 11 n^3 = 8
T(4) = 19 n^4 = 16
T(5) = 35 n^5 = 32
T(6) = 52 n^6 = 64
T(7) = 78 n^7 = 128
T(8) = 105 n^8 = 256
T(9) = 149 n^9 = 512
We can see that the choice of 2^n
as an upper limit is valid for all values T(6)
and above. 我们可以看到选择
2^n
作为上限对于所有值T(6)
及以上都是有效的。
If you want a lower bound than 2^n
you could choose a lower base (with the trade-off that it will only be valid for higher numbers of n
). 如果你想要一个低于
2^n
的下界,你可以选择一个较低的基数(权衡它只对更高数量的n
有效)。 But I must add that it will still be basically the same solution as the one you already have. 但我必须补充一点,它仍然与你已经拥有的解决方案基本相同。
Any base larger than one would do but to be a bit more specific we could for example see that the recursion equation T(n) = T(n-1) + T(n/2) + n
is bounded by the equation T(n) = T(n-1) + T(n-2)
for n>5
. 任何大于1的基数都可以做但更具体一点我们可以看到递归方程
T(n) = T(n-1) + T(n/2) + n
由方程T(n) = T(n-1) + T(n-2)
对于n>5
T(n) = T(n-1) + T(n-2)
。
This is the same recursion relation as for the Fibonacci sequence and following the steps in the answers to this question it has a computational complexity matching the golden ratio (1+sqrt(5))/2 = 1,618
to the power of n
. 这与Fibonacci序列的递归关系相同,并且遵循该问题的答案中的步骤,其具有与黄金比率
(1+sqrt(5))/2 = 1,618
匹配的计算复杂度与n
的幂。
Plotting the actual values we can see for which n
the value of T(n)
is bounded by ((1+sqrt(5))/2)^n
. 绘制我们可以看到的实际值,其中
n
( T(n)
的值由((1+sqrt(5))/2)^n
限定。 From the figure it seems to be values n=13
and above. 从图中可以看出
n=13
及以上的值。
All this said, I have thought a bit about approximating the running time with some sub-exponential function. 所有这些说,我已经考虑了一些关于使用一些子指数函数来近似运行时间。 It doesn't seem like it can be easily done and as I said I believe you have found the expected answer.
它似乎不容易完成,正如我所说,我相信你找到了预期的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.