计算程序运行时间?
我正在尝试计算我的分割程序的运行时间,
void splitX(int x) {
if (x<=1) {return x;};
splitX(n/2);
System.out.println("splitting in progress");
splitX(n/2);
splitX(n/2);
splitX(n/2);
}
我对此相当陌生,这就是到目前为止所做的;
T(n) = 4T(n/2)
= 4^2T(n/2^2)
= 4^3T(n/2^3)
= 4^kT(n/2^k)
= O(log n)
我走在正确的轨道上吗,我有点困惑,你还必须考虑打印线吗?
I am trying to work out the running time of my splitting program,
void splitX(int x) {
if (x<=1) {return x;};
splitX(n/2);
System.out.println("splitting in progress");
splitX(n/2);
splitX(n/2);
splitX(n/2);
}
I am fairly new to this, this is what have done so far;
T(n) = 4T(n/2)
= 4^2T(n/2^2)
= 4^3T(n/2^3)
= 4^kT(n/2^k)
= O(log n)
Am i on the right track, im getting a little confused, also do you have to account for the printing line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
分析到最后都是正确的,解为 T(n) = O(n^2)
需要注意的是 4^kT(n/2^k) != O(log n ) 因为
4^k
不是常量。看一下分析:
为了正式证明它:我们使用归纳
基数:
T(1) = 1 = 1^2
假设每个
k <= n
都为T(n) = n^2
T(2n) = 4*T(n) =(归纳假设) 4*n^2 = (2n)^2
因此归纳假设为真,且
T(n ) = n^2
您还可以在 wolfram alpha
The analyzis is true until the end, the solution is
T(n) = O(n^2)
Note that
4^kT(n/2^k) != O(log n)
since4^k
is not a constant.Have a look at the analyzis:
To formally prove it: we use induction
base:
T(1) = 1 = 1^2
Assume
T(n) = n^2
for eachk <= n
T(2n) = 4*T(n) =(induction hypothesis) 4*n^2 = (2n)^2
Thus the induction hypothesis is true and
T(n) = n^2
You can also check this result on wolfram alpha
正如我所见,您对递归函数进行了 log(N) 次调用。将其乘以常数 - 4- 不会改变复杂性,打印行也不会改变(满足所有作业相关的需求)。
As I see it you make log(N) calls to your recursive function. multiplying this by a constant - 4- does not change complexity, nor does the printing line (for all homework related needs).
是的,用 Big-O 表示法来说,它是 O(log n) 乘以常数。
Yes, in Big-O notation it's O(log n) mulltiply by constant.
表达式的形式为
T(n)=4T(n/2) + c
现在使用 a=4、b=2 和 f(n)=c 应用主定理;
T(n)=O(n^loga) //底数2
T(n)=n^2
the expression is of form
T(n)=4T(n/2) + c
now apply master theorum using a=4, b=2 and f(n)=c;
T(n)=O(n^loga) //base 2
T(n)=n^2