计算这个函数的时间复杂度

arrow0815 2010-09-20 11:44:47
int blah(int n)
{
int sum = 0;
int i, j;
if (n == 0)
return 1;
for(i=0; i<=n-1; i++)
for(j=0; j<=log(n); j++) {
loop-body
}
sum = blah(n/2);
sum += blah(n/2);
return sum;
}

通过递归得到运行时间的公式
T(N) = 2T(n/2) + o(nLogn)

这个递归式子对么?怎么对其化简呢?
...全文
125 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
FancyMouse 2010-09-20
  • 打赏
  • 举报
回复
T(n)=O(n(logn)^2)
arrow0815 2010-09-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 air_snake 的回复:]

直接用master theorem,case 2.推广形式。O(nlog^2n)
http://en.wikipedia.org/wiki/Master_theorem
推导的话和master theorem一样的方法。
[/Quote]

看了下,好像和算法导论上的master theorem不太一样
arrow0815 2010-09-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 michael122 的回复:]

ls正解
用递归树模型,T(n)=nlgn+nlg(n/2)+nlg(n/4)+...+nlg(1)= n*(lgn+...+lg1)
括号里的东西= lg(n^lgn/sqrt(n^(lgn+1)))=lg(n^(lgn/2))=(lgn)^2
中间过程有几步是渐进意义下相等
[/Quote]
应该是
T(n)=nlgn+(n/2)lg(n/2)+(n/4)lg(n/4)+...+(n/2^(lgn))lg(1)= n*((n/2)lgn+...+(n/2^(lgn))lg1)
吧,最后化简的确是T(n)=O(n(logn)^2)
air_snake 2010-09-20
  • 打赏
  • 举报
回复
直接用master theorem,case 2.推广形式。O(nlog^2n)
http://en.wikipedia.org/wiki/Master_theorem
推导的话和master theorem一样的方法。
michael122 2010-09-20
  • 打赏
  • 举报
回复
ls正解
用递归树模型,T(n)=nlgn+nlg(n/2)+nlg(n/4)+...+nlg(1)= n*(lgn+...+lg1)
括号里的东西= lg(n^lgn/sqrt(n^(lgn+1)))=lg(n^(lgn/2))=(lgn)^2
中间过程有几步是渐进意义下相等

33,007

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧