简体   繁体   English

递归关系的最坏情况和最佳情况运行时复杂度 T(n) = 2T(n/2) + T(n-1) + 常数

[英]Worst Case and Best Case Run-time Complexity of Recurrence Relation T(n) = 2T(n/2) + T(n-1) + constant

I was looking for the worst-case and best-case run-time analysis of the following recurrence relation:我正在寻找以下递归关系的最坏情况和最佳情况运行时分析:

T(n) = 2T(n/2) + T(n-1) + 1

I couldn't find strictly the same question on Stack Overflow or on the Web.我在 Stack Overflow 或 Web 上找不到完全相同的问题。

In this case, we have three branches, and we know that T(n/2) would reach the base case faster than T(n-1) would, so from my understanding, the longest leaf to root path represents the worst-case complexity and the shortest leaf to root path represents the best-case complexity.在这种情况下,我们有三个分支,并且我们知道T(n/2)会比T(n-1)更快地到达基本情况,所以根据我的理解,最长的叶子到根路径代表最坏的情况复杂性和最短叶到根路径代表最佳情况的复杂性。

As such, we have that the best case complexity would be:因此,我们认为最好的案例复杂度是:

T(n) = log(n) * T(1)

Assuming that T(1)=1 , then we have best-case complexity假设T(1)=1 ,那么我们有最佳情况复杂度

T(n) = O(logn)

If we look at the worst case complexity, we have如果我们看一下最坏情况的复杂性,我们有

T(n) = n * T(1)

So, then we have (by assuming T(1)=1 again):因此,我们有(再次假设T(1)=1 ):

T(n) = O(n)

Could I be misunderstanding something here or is this timing analysis accurate for this recurrence relation?我可能在这里误解了一些东西,或者这个时序分析对于这种重复关系是否准确?

Assuming that T(1)=1, then we have best-case complexity假设 T(1)=1,那么我们有最好的情况复杂度

You cannot simply replace T(1) and claim it the best-case complexity.您不能简单地替换T(1)并声称它是最佳情况复杂度。 Especially using the Big-O notation to denote that特别是使用 Big-O 符号来表示

T(n) = O(logn)

to be correct you would use Ω(logn) .正确地说,您将使用Ω(logn)

For the best-case complexity, one needs to study the behavior of the algorithm when one increases the size, and analyze if there is any property of the algorithm that might cause different scenarios.对于最佳情况复杂度,需要研究算法在增加大小时的行为,并分析算法是否存在可能导致不同场景的任何属性。 For instance, searching in a BST can be constant in the best-case scenario, but you still considered it with an input 'n', and not what is the best case-scenario with a single element.例如,在最佳情况下,在 BST 中的搜索可能是恒定的,但您仍然使用输入“n”来考虑它,而不是使用单个元素的最佳情况下。

In your case, you do not have a concrete algorithm but rather a function (represented as a recurrence).在您的情况下,您没有具体的算法,而是 function (表示为重复)。 Therefore, it does not make sense to talk about best- and worst- case scenarios因此,谈论最好和最坏的情况是没有意义的

In this case, we have three branches, and we know that T(n/2) would reach the base case faster than T(n-1) would, so from my understanding, the longest leaf to root path represents the worst-case complexity在这种情况下,我们有三个分支,并且我们知道 T(n/2) 会比 T(n-1) 更快地到达基本情况,所以根据我的理解,最长的叶子到根路径代表最坏的情况复杂

When calculating the recurrence one should not only take into account the height of the recursion tree but also the number of branches.在计算递归时,不仅要考虑递归树的高度,还要考虑分支的数量。 Therefore:所以:

If we look at the worst case complexity, we have如果我们看一下最坏情况的复杂性,我们有

T(n) = n * T(1) T(n) = n * T(1)

your rational is not correct.你的理性是不正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM