简体   繁体   English

大师法:分而治之

[英]Master Method: Divide and Conquer

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 .根据我的评估,以下算法的总体渐近运行时间为O(n) ,因为x (递归次数)为 1, y为(拆分次数)为 2 ,最后为z (数量的幂)在递归调用之外完成的工作)为 1,因此 x<y^{d},但我的回答是错误的。 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|所以,首先,你说的n是什么意思并不是很清楚,但我猜|b| would be the most natural (as a doesn't affect run-time).将是最自然的(因为a不影响运行时)。 Your analysis was mostly correct, but the mistake was saying z=1.你的分析大部分是正确的,但错误是说 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.在递归之外有持续的工作要做,但这并不意味着 z,“在递归调用之外完成的工作量的幂”,是 1。要得到 z,你可以将该工作表示为多项式函数n:f(n)=n^z=1,所以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.所以这意味着 x = y^d (1 = 2^0) 而不是 x < y^d,改变了你应用的主定理的情况。 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.你得到的不是 T(n) = O(n),而是 T(n) = n^0*log(n) = O(log(n)),这应该是你所期望的,因为问题被一分为二在每次递归调用中,在每次调用中只进行持续的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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