简体   繁体   中英

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.

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.

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(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(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

You cannot simply replace T(1) and claim it the best-case complexity. Especially using the Big-O notation to denote that

T(n) = O(logn)

to be correct you would use Ω(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.

In your case, you do not have a concrete algorithm but rather a function (represented as a recurrence). 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

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)

your rational is not correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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