如何求解:T(n) = T(n/2) + T(n/4) + T(n/8) + (n)

发布于 2024-10-31 19:36:02 字数 127 浏览 4 评论 0原文

我知道如何为仅调用自身一次的算法建立递归关系,但我不确定如何执行一次多次调用自身的操作。

例如:

T(n) = T(n/2) + T(n/4) + T(n/8) + (n)

I know how to do recurrence relations for algorithms that only call itself once, but I'm not sure how to do something that calls itself multiple times in one occurrence.

For example:

T(n) = T(n/2) + T(n/4) + T(n/8) + (n)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

萌能量女王 2024-11-07 19:36:02

使用递归树。请参阅 CLRS“算法简介”中递归树的最后一个示例。

T(n) = T(n/2) + T(n/4) + T(n/8) + n。根将是 n(cost) &分为3次递归。所以递归树如下所示:

T(n) = n = n
T(n/2)T(n/4)T(n/8) (n/2) (n/4) (n/8)
T(n/4)T(n/8)T(n/16) T(n/8)T(n/16)T(n/32) T(n/16)T(n/32)T( n/64)

                                             n---------------------------------> n      

                             (n/2)         (n/4)           (n/8)--------------> (7/8)n

                         n/4 n/8 n/16  n/8 n/16 n/32  n/16 n/32 n/64)--------> (49/64)n
                                            ...         

最长路径:最左边的左分支 = n -> n/2-> n/4-> ...-> 1

最短分支:最右边的分支 = n -> n/8-> n->64-> ...-> 1

叶子数(l): 3^log_8(n) < l < 3^log_2(n) => n^0.5< l < n^1.585

查看树 - 直到 log_8(n) 层树已满,然后当我们向下时,更多 &缺少更多内部节点。通过这个理论,我们可以给出界限,

T(n) = Big-Oh (Summation j=0 to log_2(n)-1 (7/8)^jn) = ... => T(n) = O(n)。
T(n) = Big-Omega(求和 j=0 到 log_8(n)-1 (7/8)^jn) = ... => T(n) = Big-Omega(n)。

因此,T(n) = Theta(n)。

这里要点是:
T(n/2) 路径的长度最大...

这一定不是完整的三叉树...高度 = n 的以 2 为底的对数 & # 叶子必须小于 n ...

希望,这样你就可以解决问题。

Use Recursion Tree. See the last example of Recursion tree at CLRS "Intro to Algorithm".

T(n) = T(n/2) + T(n/4) + T(n/8) + n. The root will be n(cost) & divided into 3 recursions. So the recursion tree looks like as follows:

T(n) = n = n
T(n/2)T(n/4)T(n/8) (n/2) (n/4) (n/8)
T(n/4)T(n/8)T(n/16) T(n/8)T(n/16)T(n/32) T(n/16)T(n/32)T(n/64)

                                             n---------------------------------> n      

                             (n/2)         (n/4)           (n/8)--------------> (7/8)n

                         n/4 n/8 n/16  n/8 n/16 n/32  n/16 n/32 n/64)--------> (49/64)n
                                            ...         

Longest path: the leftmost left branch = n -> n/2 -> n/4 -> ... -> 1

Shortest branch: the rightmost right branch = n -> n/8 -> n->64 -> ... -> 1

The number of leaves (l): 3^log_8(n) < l < 3^log_2(n) => n^0.5 < l < n^1.585

Look at the tree - upto log_8(n) levels the tree is full, and then as we go down, more & more internal nodes are absent. By this theory we can give the bound,

T(n) = Big-Oh (Summation j=0 to log_2(n)-1 (7/8)^j n) = ... => T(n) = O(n).
T(n) = Big-Omega (Summation j=0 to log_8(n)-1 (7/8)^j n) = ... => T(n) = Big-Omega(n).

Therefore, T(n) = Theta(n).

Here the points are:
T(n/2) path has the highest length...

This must not be a complete ternary tree ... height = log base 2 of n & # of leaves must be less than n ...

Hope, likely this way u can solve the prob.

戒ㄋ 2024-11-07 19:36:02

请参阅图片以获得更好的解释 -

在此处输入图像描述

树的高度:我们采用 log(n)(base 2) 因为 n/2 构成树与 n/4 相比更长,并且n/8。我们的 GP 系列将一直持续到 k=logn(base)。

See the Image for a better explanation-

enter image description here

Height of Tree: We took log(n)(base 2) because n/2 make the tree longer in comparison to n/4, and n/8. And our GP series will go till k=logn(base).

浮云落日 2024-11-07 19:36:02

有两种方法可以解决这个问题。一是展开递归并找到相似之处,这可能需要创造性并且可能非常困难。另一种方法是使用 Akra-Bazzi 方法

在本例中,g(x) = na1 = a2 = a3 = 1b1 = 1/2b2 = 1/4,b3 = 1/8。解方程

“在此处输入图像描述”

,即 1/2^p + 1/4^p + 1/8^p = 1,您将得到 p = 0.87915。求解积分,您将得到 在此处输入图像描述,表示复杂度为:O(n)

There are two ways of solving this. One is unrolling recursion and finding similarities which can require inventiveness and can be really hard. Another way is to use Akra-Bazzi method.

In this case g(x) = n, a1 = a2 = a3 = 1 and b1 = 1/2, b2 = 1/4, b3 = 1/8. Solving the equation

enter image description here

which is 1/2^p + 1/4^p + 1/8^p = 1 you get p = 0.87915. Solving the integral you will get enter image description here, which means that the complexity is: O(n)

奈何桥上唱咆哮 2024-11-07 19:36:02

就像编码斐波那契数列(困难的方法)一样,您只需输入以下内容:

长 fib(长 n){
 if(n <= 1) 返回 n; 
 否则返回 fib(n-1) + fib(n-2);
}     

或者,更好的是,使用全局变量来记忆它,以使其更快。再次使用斐波那契数列:

static ArrayListfib_global = new ArrayList(1000); 
  //delcare 一个可以追加的全局变量
长 fib(长 n){
   if(n >= fib_global.length)fib_global.add(fib(n-1) + fib(n-2));
   返回 fib_global.get(n);
}

该代码一次只会执行其中一个调用,并且很可能按照您输入的从左到右的顺序执行,这样您就不必真正担心需要执行的次数。打电话给某事。

Just like coding the Fibonacci Sequence (the hard way) as an example, you would simply type something along the lines of:

long fib(long n){
 if(n <= 1) return n; 
 else return fib(n-1) + fib(n-2);
}     

Or, better yet, memoize it using a global variable to make it much quicker. Once again, with the Fibonacci Sequence:

static ArrayList<Long>fib_global = new ArrayList(1000); 
  //delcare a global variable that can be appended to
long fib(long n){
   if(n >= fib_global.length)fib_global.add(fib(n-1) + fib(n-2));
   return fib_global.get(n);
}

The code will only execute one of these calls at a time, and most likely in the left-to-right order you typed them in, making it so that you only don't really need to worry about the amount of times you needed to call something.

流年里的时光 2024-11-07 19:36:02

正如CLRS所说,通过数学归纳法可以将T(n)替换为cn。这种归纳假设适用于 n 以下的数字。如上所述,我们需要证明的是参数值为n。因此,如下:
假设:对于 n 下面的数字,T(n) <= cn
结论:

T(n) = T(n/2) + T(n/4) + T(n/8) + n
    <= c/2*n + c/4*n + c/8*n + n
     = (7/8*c + 1) * n
    <= cn (when c >= 8)

仅此而已。

As CLRS has said, T(n) can be replaced by cn by mathematical induction. This inductive assumption works for the number below n. As mentioned above, what we need to prove is that the parameter value is n. Therefore, as follows:
assume: T(n) <= cn for the number below n;
conclude:

T(n) = T(n/2) + T(n/4) + T(n/8) + n
    <= c/2*n + c/4*n + c/8*n + n
     = (7/8*c + 1) * n
    <= cn (when c >= 8)

that's all.

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