企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## B.4 Extending Results for *n*, a Power of a Positive Constant *b*, to *n* in General It is assumed in the material that follows that you are familiar with the material in [Chapter 1](LiB0008.html#16). In the case of some recursive algorithms, we can readily determine the exact time complexity only when *n* is a power of some base *b*, where *b* is a positive constant. Often the base *b* is 2. This is true in particular for many divide-and-conquer algorithms (see [Chapter 2](LiB0014.html#141)). Intuitively, it seems that a result that holds for *n* a power of *b* should approximately hold for *n* in general. For example, if for some algorithm we establish that ![](https://box.kancloud.cn/8ab6af1fdb2ff9cf59689931caa63145_119x21.jpg) for *n* a power of 2, it seems that for *n* in general we should be able to conclude that ![](https://box.kancloud.cn/97965aa991e739a2ea8b8f24ea1edfa1_145x22.jpg) It turns out that usually we can draw such a conclusion. Next we discuss situations in which this is the case. First we need some definitions. These definitions apply to arbitrary functions whose domains and ranges are any subsets of the real numbers, but we state them for complexity functions (that is, functions that map the positive integers to the positive reals) because these are the functions that interest us here. Definition A complexity function *f* (*n*) is called ***strictly increasing*** if *f* (*n*) always gets larger as *n* gets larger. That is, if *n*1 > *n*2, then ![](https://box.kancloud.cn/c2a2ba6c39ae9390133395e871bce0a5_127x23.jpg) The function shown in [Figure B.1(a)](#ap-bfig01) is strictly increasing. (For clarity, the domains of the functions in [Figure B.1](#ap-bfig01) are all the nonnegative reals.) Many of the functions we encounter in algorithm analysis are strictly increasing for nonnegative values of *n*. For example, lg *n, n, n*, lg *n, n*2 and 2*n* are all strictly increasing as long as *n* is nonnegative. [![Click To expand](https://box.kancloud.cn/852862e97349fb11fbfeef3ff87c3844_350x384.jpg)](figap-b-1_0.jpg) Figure B.1: Four functions. Definition A complexity function *f* (*n*) is called ***nondecreasing*** if *f* (*n*) never gets smaller as *n* gets larger. That is, if *n*1 > *n*2, then ![](https://box.kancloud.cn/c2a2ba6c39ae9390133395e871bce0a5_127x23.jpg) Any strictly increasing function is nondecreasing, but a function that can level out is nondereasing without being strictly increasing. The function shown in [Figure B.1(b)](#ap-bfig01) is an example of such a function. The function in [Figure B.1 (c)](#ap-bfig01) is not nondecreasing. The time (or memory) complexities of most algorithms are ordinarily nondecreasing because the time it takes to process an input usually does not decrease as the input size becomes larger. Looking at [Figure B.1](#ap-bfig01), it seems that we should be able to extend an analysis for *n* a power of *b* to *n* in general as long as the function is nondecreasing. For example, suppose we have determined the values of *f*(*n*) for *n* a power of 2. In case of the function in Figure B.1(c), anything can happen between, say, 23 = 8 and 24 = 16. Therefore, nothing can be concluded about the behavior of the function between 8 and 16 from the values at 8 and 16. However, in the case of a nondecreasing function *f* (*n*), if 8 ≤ *n* ≤ 16 then ![](https://box.kancloud.cn/7bd3091a643e4803c1afcd4c9f09170c_179x20.jpg) So it seems that we should be able to determine the order of *f* (*n* from the values of *f* (*n*) for *n* a power of 2. What seems to be true intuitively can indeed be proven for a large class of functions. Before giving a theorem stating this, we recall that order has to do only with long-range behavior. Because initial values of a function are unimportant, the theorem requires only that the function be eventually nondecreasing. We have the following difinition: Definition A complexity function *f* (*n*) is called ***eventually nondecreasing*** if for all *n* past some point the function never gets smaller as *n* gets larger. That is, there exists an *N* such that if *n*1 > *n*2*N*, then ![](https://box.kancloud.cn/a9143c322b1b03d7f3fee71baac488f0_127x22.jpg) Any nondecreasing function is eventually nondecreasing. The function shown in [Figure 3.1(d)](LiB0025.html#260) is an example of an eventually nondecreasing function that is not nondecreasing. We need the following definition before we give the theorem for extending the results for *n* a power of *b*: Definition A complexity function *f* (*n*) is called ***smooth*** if *f* (*n*) is eventually nondecreasing and if ![](https://box.kancloud.cn/a572d3f0fd9274ead38fa0a569dd4d27_150x22.jpg) Example B.23**![Start example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)**The functions lg *n*, *n* lg *n*, and *nk*, where *k* ≥ 0, are all smooth. We show this for lg *n*. In the exercises you are asked to show it for the other functions. We have already noted that lg *n* is eventually nondecreasing. As to the second condition, we have ![](https://box.kancloud.cn/fdaf8afa563589e78726dd0398c0adaa_241x22.jpg) **![End example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)** Example B.24**![Start example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)**The function 2*n* is not smooth, because the Properties of Order in [Section 1.4.2](LiB0011.html#85) in [Chapter 1](LiB0008.html#16) imply that ![](https://box.kancloud.cn/8afa317a1833bf5235411f8ba84d9e62_95x21.jpg) Therefore, ![](https://box.kancloud.cn/6a35836cd60b69fbd886053aa3ac4fec_205x23.jpg) **![End example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)** We now state the theorem that enables us to generalize results obtained for *n* a power of *b*. The proof appears near the end of this appendix. Theorem B.4**![Start example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)** Let *b* ≥ 2 be an integer, let *f* (*n*) be a smooth complexity function, and let *T* (*n*) be an eventually nondecreasing complexity function. If ![](https://box.kancloud.cn/cb6598d8f2605fc31a08ecea08347bfe_318x21.jpg) then ![](https://box.kancloud.cn/443a8e71976b15597068d8ee424c0e4c_142x22.jpg) Furthermore, the same implication holds if Θ is replaced by "big *O*," Ω, or "small *o*." **![End example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)** By "*T* (*n*) ∊ Θ (*f* (*n*)) for *n* a power of *b*," we mean that the usual conditins for Θ are known to hold when *n* is restricted to being a power of *b*. Notice in [Theorem B.4](#ap-bex29) the additional requirement that *f* (*n*) be smooth. Next we apply [Theorem B.4](#ap-bex29) Example B.25**![Start example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)**![](https://box.kancloud.cn/485bc67a7beadb016ac9d44f235f6e03_27x27.jpg) Suppose for some comlexity function we establish that ![](https://box.kancloud.cn/eb35ca7315f23cfa7b59e382962062d8_301x82.jpg) When *n* is a power of 2, we have the recurrence in [Example B.18](LiB0103.html#1417). Therefore, by that example, ![](https://box.kancloud.cn/b909e57dfdfe928eb81f7db45b2915ee_390x21.jpg) Because lg *n* is smooth, we need only show that *T* (*n*) is eventually nondecreasing in order to apply [Theorem B.4](#ap-bex29) to conclude that ![](https://box.kancloud.cn/e780eb73e466200fa0e981eb615ff90f_133x22.jpg) One might be tempted to conclude that *T* (*n*) is eventually nondecreasing from the fact that lg *n* + 1 is eventually nondecreasing. However, we cannot do this because we know only that *T* (*n*) = lg *n* + 1 when *n* is a power of 2. Given only this fact, *T* (*n*) could exhibit any possible behavior in between powers of 2. We show that *T* (*n*) is eventually nondecreasing by using induction to establish for *n* ≥ 2 that if 1 ≤ *k* < *n*, then ![](https://box.kancloud.cn/c6efa0ecf8fca8e0089e1fd87258be7f_115x21.jpg) Induction base: For *n* = 2, ![](https://box.kancloud.cn/9d85874509cd630f0e9a86f4f2191a0c_372x106.jpg) Therefore, ![](https://box.kancloud.cn/bfad96f75f4f3dbe5f006ca0c9519e52_111x22.jpg) Induction hypothesis: One way to make the induction hypothesis is to assume that the statement is true for all *m* ≤ *n*. Then, as usual, we show that it is true for *n* + 1. This is the way we need it to be stated here. Let *n* be an arbitrary integer greater than or equal to 2. Assume for all *m* ≤ *n* that if *k* < *m*, than ![](https://box.kancloud.cn/f77b8fa218c705b72d2006160004c1c2_120x21.jpg) Induction step: Because in the induction hypothesis we assumed for *k < n* that ![](https://box.kancloud.cn/e86e4d7b1745f5ce6a902bea7a3af3dd_115x22.jpg) we need only show that ![](https://box.kancloud.cn/3e7a1012b96c227a63bcef2c83e0476a_146x22.jpg) To that end, it is not hard to see that if *n* ≥ 1, then ![](https://box.kancloud.cn/5540bace56781d6a1607321473d00a22_162x47.jpg) Therefore, by the induction hypothesis, ![](https://box.kancloud.cn/51705bc7e565af7d832eb28044833e90_215x47.jpg) Using the recurrence, we have ![](https://box.kancloud.cn/8c3bd12e9e8b4101b88e95945c160c59_400x41.jpg) and we are done. **![End example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)** Finally, we develop a general method for determining the order of some common recurrences. Theorem B.5**![Start example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)**Suppose a complexity function *T* (*n*) is eventually nondecreasing and satisfies ![](https://box.kancloud.cn/62e90af0d6bc1311973cfcc07a7b39ed_400x78.jpg) where *b* ≥ 2 and *k* ≥ 0 are constant integers, and *a*, *c*, and *d* are constants such that *a* > 0, *c* > 0, and *d* ≥ 0. Then ![](https://box.kancloud.cn/beae257871de0b8feacec626cb55a52b_400x73.jpg) Furthermore, if, in the statement of the recurrence, ![](https://box.kancloud.cn/4c636c38f18b4e198d7f0ff664e6bd5d_178x36.jpg) is replaced by ![](https://box.kancloud.cn/2971fbdd182c87dbe14396cf404f9b4a_400x36.jpg) then Result B.5 holds with "big *O*" or Ω, respectively, replacing Θ. **![End example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)** We can prove this theorem by solving the general recurrence using the characteristic equation and then applying [Theorem B.4](#ap-bex29). Example applications of [Theorem B.5](#ap-bex31) follow. Example B.26**![Start example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)**Suppose that *T* (*n*) is eventually nondecreasing and satisfies ![](https://box.kancloud.cn/8c7481c0265b4b6f23ce09314cf8e6c1_400x100.jpg) By [Theorem B.5](#ap-bex31), because 8 42, ![](https://box.kancloud.cn/4a17ecb687e5e6407ee5d691bd911dcd_126x25.jpg) **![End example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)** Example B.27**![Start example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)**Suppose that *T* (*n*) is eventually nondecreasing and satisfies ![](https://box.kancloud.cn/5f84ee5b114a1b3b15aab4af0fb79f62_400x99.jpg) By [Theorem B.5](#ap-bex31), because 9 > 31, ![](https://box.kancloud.cn/e5cee67b955565c8218c053c42ea0620_229x26.jpg) **![End example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)** [Theorem B.5](#ap-bex31) was stated in order to introduce an important theorem as simply as possible. It is actually the special case, in which the constant *s* equals 1, of the following theorem. Theorem B.6**![Start example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)**Suppose that a complexity function *T* (*n*) is eventually nondecreasing and satisfies ![](https://box.kancloud.cn/c0865fab241b611cd68b15407f85d8b3_400x76.jpg) where *s* is a constant that is a power of *b*, *b* ≥ 2 and *k* ≥ 0 are constant integers, and *a*, *c*, and *d* are constants such that *a* > 0, *c* > 0, and *d* ≥0. Then the results in [Theorem B.5](#ap-bex31) still hold. **![End example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)** Example B.28**![Start example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)**Suppose that *T* (*n*) is eventually nondecreasing and satisfies ![](https://box.kancloud.cn/619e9cf47559093aa82940fbfb63a398_400x96.jpg) By [Theorem B.6](#ap-bex34), because 8 = 23, ![](https://box.kancloud.cn/63c59537400399314eacba80d484faf1_156x25.jpg) **![End example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)** This concludes our discussion of techniques for solving recurrences. Another technique is to use "generating functions" to solve recurrences. This technique is discussed in Sahni (1988). Bentley, Haken, and Sax (1980) provide a general method for solving recurrences arising from the analysis of divide-and-conquer algorithms ([see Chapter 2](LiB0014.html#141)).