According to my evaluation the overall asymptotic running time of the below algorithm is O(n)
,since x
(number of recursions) is 1, and y
is ( the number of splits) is 2 , and finally z
( the power of of amount of work done outside of the recursion call) is 1, hence x<y^{d}, but my answer is wrong . Why?
FastPower(a,b) :
if b = 1
return a
else
c := a*a
ans := FastPower(c,[b/2])
if b is odd
return a*ans
else return ans
end
So, first of all, it's not immediately clear what you mean by n
, but I'm guessing |b|
would be the most natural (as a
doesn't affect run-time). Your analysis was mostly correct, but the mistake was saying z=1. There is constant work being done outside of the recursion, but that doesn't mean z, "the power of the amount of work done outside of the recursion call", is 1. To get z, you express that work as a polynomial function of n: f(n)=n^z=1, so z=0.
So that means x = y^d (1 = 2^0) rather than x < y^d, changing the case of the Master Theorem you apply. Instead of T(n) = O(n), you get T(n) = n^0*log(n) = O(log(n)), which should be what you expect as the problem is being divided in half at every recursive call, with only constant working being done in each call.
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.