建立并求解递归函数的递归关系?
我正在学习java递归的过程中,但我陷入了以下问题。
void f(int n) {
if (n<=1) return;
f(n/2);
System.out.writeln("still continuing...");
f(n/2);
f(n/2);
}
我对此有两个问题。
如果我们说 T(n) 是程序打印的行数,n 是输入,那么 T(n) 的递推公式是什么?
如何在不使用主定理的情况下解决问题 1 的递推式?
干杯
I am in the process of learning java recurrence but am stuck on the following question.
void f(int n) {
if (n<=1) return;
f(n/2);
System.out.writeln("still continuing...");
f(n/2);
f(n/2);
}
I have two questions about this.
if we say that T(n) is the number of lines that the program prints and n is the input, what would be the recurrence formula for T(n)?
How do I go about solving the recurrence from question 1 without using master theorem?
cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们从 T(n) 值的公式开始。我们知道以下内容:
因此,我们可以得到以下递推式:
请注意,我在这里使用“+ 1”项而不是“+ O(1)” “ 学期。这在数学上是不确定的,但由于我们正在寻找以大 O 表示法表示的最终结果,所以这不会是太大的问题。
现在,我们将如何尝试解决这个问题?一种选择是为 n 插入一些任意值,看看会发生什么。我们首先(假设 n > 1)
现在,让我们考虑一下对 T(n / 2) 的调用。如果n/2> 1,然后我们就得到了
现在,让我们将其扩展为增益:
此时,我们可以看到一种模式正在出现。经过 i 次递归迭代后,我们完成的总工作是
这个过程当 n / 2i ≤ 1 时终止,即当 i ≈ lg n 时发生。如果我们插入 lg n,我们得到
现在,3lg n = 3(log3 n / log3 2) = 3log3 n1 / log3 2 = n1 / log3 2,所以整个事情是
使用几何级数和的公式,可得最后一项是 (3lg n - 1) / 2,最终扩展到 O(n1 / log3 2),所以总的来说,这个表达式是O(n1 / log3 2)。
但这个公式实在是太难看了。我们可以简化一下吗?好吧,我们确实有这个:
由此可知运行时间为 O(nlg 3),大约为 O(n 1.58)。
希望这有帮助!
Let's begin with a formula for the value of T(n). We know the following:
Consequently, we can get the following recurrence:
Notice that I'm using a "+ 1" term here instead of a "+ O(1)" term. This is mathematically iffy, but since we're looking for a final result expressed in big-O notation anyway, this will not be too much of a problem.
Now, how would we go about trying to solve this? One option would be to plug in some arbitrary value for n and see what happens. We begin with (assuming n > 1) that
Now, let's think about those calls to T(n / 2). If n / 2 > 1, then we get that
Now, let's expand this out a gain:
At this point, we can see a pattern emerging. After i iterations of the recursion, we have that the total work done is
This process terminates when n / 2i ≤ 1, which occurs when i ≈ lg n. If we plug in lg n, we get
Now, 3lg n = 3(log3 n / log3 2) = 3log3 n1 / log3 2 = n1 / log3 2, so this entire thing is
Using the formula for sums of geometric series, this last term is (3lg n - 1) / 2, which ends up expanding out to O(n1 / log3 2), so overall this expression is O(n1 / log3 2).
But this formula is really ugly. Can we simplify it? Well, we do have this:
Which gives us that the runtime is O(nlg 3), which is about O(n1.58).
Hope this helps!
正如定理所示,答案应该是 O(n^(lg 3))。
更详细的内容可以参考Cormen等人的Introduction to Algorithm,见第4章。
递归方程的求解相当复杂。但通常的方法是先猜测,然后用代入法证明。
As the theorem indicates, the answer should be O(n^(lg 3)).
For more details, you can refer to the Introduction to Algorithm by Cormen et, see chapter 4.
The solving of recurrance equations are quite complicated. But normally the method is first guess, then prove using substitution.