Solving recurrence T(n) = 2T(n/2) + Θ(1) by substitution

好久不见. 提交于 2019-12-22 10:11:13

问题


So I am pretty sure it is O(n) (but it might not be?), but how do you solve it with substitution?

If you assume T(n) <= c * n, what is the induction steps?


回答1:


First of all,I'd like to assume clearly that Θ(1)=k,some constant.

Next,proceeding using substitution method, we get

 T(n)=2T(n/2)+Θ(1)
     =2T(n/2)+k
     =2{2T(n/4)+k)+k
     =4T(n/4)+3k
     =...
     =n.T(1)+(n-1)k
     =n.k+(n-1)k
     =2nk-k
     =O(n).

If you assume it as T(n) <= c * n,you should start with T(1) and assume for T(n) to be correct,and then proceed to show that it is correct for T(n+1) by using the assumption for T(n).

BTW,your assumption was right!




回答2:


For simplicity, let's assume that the O(1) term hides some constant c, so the recurrence is really

T(n) = 2T(n/2) + c

Let's also assume for simplicity that T(1) = c.

You're venturing a (correct) guess that

T(n) <= an + b

For some constants a and b. Let's try to prove this inductively.

For n = 1, we need a and b to be such that

c <= a + b

For the inductive step, we want

T(n) <= 2T(n/2) + c

Substituting our guess gives

T(n) <= 2(an / 2 + b) + c

= an + 2b + c

Notice that if 2b + c = b, the left-hand side of this expression would be an + b, the upper bound we need. Thus we need to choose a and b such that

c <= a + b

2b + c = c

One possible choice that works here is a = 2c and b = -c, giving that T(n) <= 2cn - c = O(n).

Hope this helps!



来源:https://stackoverflow.com/questions/25858500/solving-recurrence-tn-2tn-2-%ce%981-by-substitution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!