简体   繁体   中英

How do you solve this recurrence?

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

How do we solve this recurrence? When I use the substitution method I get the runtime to be O(n), but the answer is O(nlogn). Why? How can we use the substitution method here?

We make a guess for the solution and then we use mathematical induction to prove the the guess is correct or incorrect. We guess the solution as T(n) = O(nLogn) . Now we use induction to prove our guess.

We need to prove that T(n) <= cnLogn . We can assume that it is true for values smaller than n .

T(n) = n + T(3n/4) + 2T(n/8)
    <= n + cn(3/4)log(3n/4) + 2cn/8 log(n/8)
    <= n + cnlogn  + cnlog3- cnlog4 + cnlogn - cnlog8
    <= n + cnlogn + cnlogn + c
    <= cnlogn

Hence

T(n) = O(n logn)

Here's an (incorrect) proof that T=O(n) by showing that T(n)≤cn :

T(n) = n + T(3n/4) + 2T(n/8)
     ≤ n + c3n/4   + 2cn/8
     = n + cn
     = (c+1)n
     = c2n

The problem is at the end: although we can safely ignore constants because this is a big O estimate, we can't do that when showing an upper bound. To give a more explicit example, let's suppose we had the recurrence: S(n) = 1 + 64 S(n/2) and show that S=O(1) by showing S(n)≤c :

S(n) = 1 + 64 S(n/2)
     ≤ 1 + 64 c
     = c2

On the other hand, the Master theorem tells us that S=Θ(n^6) . (Basically, changing constants in the proof means that you don't care at all about the constants (3/4, 2, 1/8, 64, 1/2) in the recurrence, and only about the inhomogeneous term ( n+ and 1+ ).)

Here's a correct proof that T=O(n log n) by showing that T(n)≤cnlog n :

T(n) = n + T(3n/4) + 2T(n/8)
     ≤ n + c(3n/4)log(3n/4) + 2c(n/8)log(n/8)
     = n + 3cn/4 log n + 3cn/4 log(3/4) + 2cn/8 log n + 2cn/8 log(1/8)
     = n + cn log n - 3cn/4 log(4/3) - cn/4 log(8)
     = cn log n + n [ 1 - 3c/4 log(4/3) - c/4 log(8) ]
     ≤ cn log n

as long as 1 ≤ c[3/4 log(4/3) + 1/4 log(8)] , that is, c≥1/[3/4 log(4/3)+1/4 log(8)] (the exact value of this depends on the base of the logarithm). The moral is that we are free to make c as large as we want at the beginning of the proof, but we can't change it in the middle.

Unfortunately, this still leaves us with the problem of figuring out that we should guess T=O(n log n) in the first place. If we don't mind the overkill, we can use the Akra–Bazzi method . Because 1(3/4)^p+2(1/8)^p=1 when p=1 , T=Θ(x(1+∫ x/x² dx))=Θ(x(1+log x))=Θ(x log x) .

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