问题
I am having some issues on how to solve recurrence relations.
T(n) = T(n/2) + log2(n), T(1) = 1, where n is a power of 2
This is a homework problem, so don't just give me the answer. I was just wondering how to start the problem.
In class we went over the Master theorem. But I don't think that would be the best way to solve this particular relation.
I don't really know how to start the problem... should I just be going
T(n) = T(n/2) + log_base2(n)
T(n/2) = [T(n/4)+log_base2(n/2)]
T(n) = [T(n/4)+log_base2(n/2)] + log_base2(n)
And just keep working my way down to get something I can see makes a basic equation?
回答1:
This recurrence solves to Θ((log n)2). Here are two ways to see this.
Some Substitutions
If you know that n is a perfect power of two (that is, n = 2k), you can rewrite the recurrence as
T(2k) = T(2k-1) + k
Let's define a new recurrence S(k) = T(2k). Then we get that
S(k) = S(k - 1) + k
If we expand out this recurrence, we get that
S(k) = S(k - 1) + k
= S(k - 2) + (k - 1) + k
= S(k - 3) + (k - 2) + (k - 1) + k
= S(k - 4) + (k - 3) + (k - 2) + (k - 1) + k
...
= S(0) + 1 + 2 + 3 + ... + k
= S(0) + Θ(k2)
Assuming S(0) = 1, then this recurrence solves to Θ(k2).
Since S(k) = T(2k) = T(n), we get that T(n) = Θ(k2) = Θ(log2 n).
Iterating the Recurrence
Another option here is to expand out a few terms of the recurrence and to see if any nice patterns emerge. Here’s what we get:
T(n) = T(n / 2) + lg n
= T(n / 4) + lg (n / 2) + lg n
= T(n / 8) + lg (n / 4) + lg (n / 2) + lg n
...
Eventually, after lg n layers, this recurrence bottoms out and we’re left with this expression:
lg n + lg (n / 2) + lg (n / 4) + ... + lg (n / 2lg n)
Using properties of logarithms, we can rewrite this as
lg n + (lg n - 1) + (lg n - 2) + (lg n - 3) + ... + (lg n - lg n)
Or, written in reverse, this is the sum
0 + 1 + 2 + 3 + ... + lg n
That sum is Gauss’s sum up to lg n, which evaluates to (lg n)(lg n + 1) / 2 = Θ((log n)2).
Hope this helps!
回答2:
If n is a power of 2 then you can just expand out the recurrence and solve exactly, using that lg(a/b) = lg(a) - lg(b).
T(n) = lg(n) + lg(n/2) + lg(n/4) + ... + lg(1) + 1
= (lg(n) - 0) + (lg(n) - 1) .... + (lg(n) - lg(n)) + 1
= lg(n)*lg(n) - lg(n)*(lg(n)+1)/2 + 1
= lg(n)*lg(n)/2 - lg(n)/2 + 1
回答3:
This can be done with the Akra-Bazzi theorem. See the third example in http://people.mpi-inf.mpg.de/~mehlhorn/DatAlg2008/NewMasterTheorem.pdf.
回答4:
This can be solved with Master theorem. Your a=1
and b=2
and f(n) = log(n)
. Then c = log2(1) = 0
. Because of your c
and f(n)
you fall into the second case (where k=1
).
So the solution is Θ(log2 n)
来源:https://stackoverflow.com/questions/14679790/solving-the-recurrence-tn-tn-2-lg-n