4. Divide-and-Conquer
Exercises
4.1-1
Available in the latest revision of the IM.
4.1-2
Available in the latest revision of the IM.
4.1-3
Available in the latest revision of the IM.
4.1-4
Assume that n is an exact power of 2, otherwise employ the "trick" from Exercise 4.1-1. The pseudocode calculates C=C+A+B by recursively finding the following sums:
Matrix-Add-Recursive(A, B, C, n)
1 if n == 1
2 // Base case.
3 c11 = c11 + a11 + b11
4 return
5 // Divide.
6 partition A, B, and C into n/2 x n/2 submatrices
A11, A12, A21, A22; B11, B12, B21, B22;
and C11, C12, C21, C22; respectively
7 // Conquer.
8 Matrix-Add-Recursive(A11, B11, C11, n/2)
9 Matrix-Add-Recursive(A12, B12, C12, n/2)
10 Matrix-Add-Recursive(A21, B21, C21, n/2)
11 Matrix-Add-Recursive(A22, B22, C22, n/2)The recurrence is T(n)=4T(n/2)+Θ(1), so T(n)=Θ(n2) (case 1 of the master theorem).
If we copy submatrices instead of performing index calculations, then the recurrence changes into T(n)=4T(n/2)+Θ(n2), thus T(n)=Θ(n2lgn) (case 2 of the master theorem).
4.2-1
Available in the latest revision of the IM.
4.2-2
Available in the latest revision of the IM.
4.2-3
Available in the latest revision of the IM.
4.2-4
Available in the latest revision of the IM.
4.2-5
Available in the latest revision of the IM.
An alternative solution is presented below, using the same total number of operations as the approach shown in the IM. The primary advantage of this method is that it avoids the need to construct a potentially large product (a+b)(c+d).
4.2-6
Available in the latest revision of the IM.
4.3-1
Available in the latest revision of the IM.
4.3-2
Available in the latest revision of the IM.
4.3-3
Available in the latest revision of the IM.
4.4-1
Available in the latest revision of the IM.
4.4-2
Available in the latest revision of the IM.
4.4-3
Available in the latest revision of the IM.
4.4-4 🌟
Justifies the guess T(n)=Θ(nlgn) as the solution to the recurrence T(n)=T(αn)+T((1–α)n)+Θ(n), where α is a constant in the range 0 < α < 1.
Available in the latest revision of the IM.
4.5-1
Available in the latest revision of the IM.
4.5-2
Available in the latest revision of the IM.
4.5-3
Available in the latest revision of the IM.
4.5-4
Available in the latest revision of the IM.
4.5-5
Available in the latest revision of the IM.
4.6-1
★ 4.6-2
Performing j=⌊logb(n/n0)⌋ iterations of the inequality af(n/b)≤cf(n) yields ajf(n/bj)≤cjf(n). Notice that n/bj≥n0 and n/bj+1<n0, so n/bj∈[n0,bn0). We have
Let μ=[n0,bn0)inff, which can be regarded as a positive constant in the context of algorithmic analysis. We have two cases depending on the ratio (a/c) that comprises the base of an exponential in the formula above.
Since logbc<0, let ϵ=−logbc>0. We get f(n)≥d⋅nlogba+ϵ=Ω(nlogba+ϵ).
★ 4.6-3
In principle, the statement can be included into Lemma 4.3 as an extension to case 2. Of course, we must tolerate some lack of rigor, as f(n) is not defined for n=1, which is assumed to be true in Lemma 4.3. Since f(n)=Θ(nlogba/lgn) we have that f(n/bj)=Θ((n/bj)logba/lg(n/bj)). Notice that n/bj=1 when logbn is an integer, so we will sum up to ⌊logbn⌋−1 to avoid potential division by zero error. The rest of the steps are rather mechanical.
The summation within the Θ-notation can be bounded from above as follows:
The corresponding lower bound is
Theorem 3.1 implies that g(n)=Θ(nlogbalglgn). Following the outline of the proof of Theorem 4.4 for case 2 from the book, we have
★ 4.7-1 🌟
Shows that we can drop the asymptotics on a driving function in any Akra-Bazzi recurrence without affecting its asymptotic solution.
Let the initial conditions be T′(n)=cT(n) for 0<n<n0, where n0>0 is the corresponding threshold value. This is possible, since the implicit initial conditions of T(n) are given. We now prove by induction on n that we can maintain the relationship T′(n)=cT(n) for all n>0.
For 0<n<n0 (base case) the equality holds by design. For the inductive step, assume that n≥n0
Choosing the initial conditions such that T′(n)=cT(n) for 0<n<n0, the recurrence implies T′(n)=cT(n) for all n≥n0. Therefore, T′(n)=cT(n) for all n>0.
If T(n) has Θ(g(n)) as a solution, then the solution of T′(n) is c⋅Θ(g(n))=Θ(g(n)). Thus, scaling the driving function by a constant factor does not alter the asymptotic growth of the solution in the Akra-Bazzi recurrence.
4.7-2
Choose d=ϕ2+1>1, such that
This proves that f(n)=n2 satisfies the polynomial-growth condition.
Select any ϕ>1, so that for ψ=2 we have
This proves that f(n)=2n does not satisfy the polynomial-growth condition.
4.7-3
Let n0=n^>0 to reconcile an inconsistency, at the time of writing this text, between the problem statement and the definition of the polynomial-growth condition from the book.
A trivial nonnegative function f(n)=0 for all n≥n0 satisfies the polynomial-growth condition, but it cannot represent the driving function in any realistic scenario. So, assume that f(n) denotes a proper cost function in equation (4.22).
Suppose, for the sake of contradiction, that there exists some n≥n0 where f(n)=0. Then, for any ψ∈[1,ϕ] the polynomial-growth inequality becomes 0≤f(ψn)≤0, which implies f(ψn)=0. Since ϕ can be chosen arbitrarily large, this means that f(n) must be identically zero on [n0,∞). But this contradicts our initial assumption that f(n) is nontrivial above some threshold. Therefore, f(n) must be positive for all sufficiently large n.
★ 4.7-4
Read my post on StackExchange for Mathematics. It also provides an example of a polynomially bounded function that does not satisfy the PGC.
4.7-5
To solve math problems, you can use a tool such as WolframAlpha. Each subproblem includes executable commands for WolframAlpha along with their corresponding responses, which are substituted into equation (4.23).
a.
We get that p=1. We can now calculate the required definite integral.
The result is ln4ln2n, hence T(n)=Θ(nlg2n).
b.
We get that p≈1.86<2. Notice that the integral becomes I(n)=∫1nlgxx1−pdx, so it does not converge due to division by zero. This is a fine example, that you may run into technical difficulties in leveraging the Akra-Bazzi method. At any rate, the Akra-Bazzi theorem states that the lower limit of integration (which defaults to 1) can be replaced by any sufficiently large constant c without affecting the asymptotic solution. This is because:
You can calculate the integral using the following Sage code:
The output is
The Ei is the exponential integral function, thus
This gives T(n)=Θ(n2/lgn).
Escaping the vagaries of calculus is perhaps a better path. In this case, we may immediately decipher the lower bound based on the driving function, and simply use it as a candidate for an upper bound. The latter can be easily proven using the substitution method.
c.
Evidently p=0, since a1+a2=1. Issuing Integrate[Log[2,x]/x,{x,1,n}], we get T(n)=Θ(lg2n).
d.
We can immediately see that p=−1. The definite integral is lnn. Thus, T(n)=Θ(lgn/n).
e.
We get that p=3. The definite integral is −1/n. Thus, T(n)=Θ(n3).
★ 4.7-6
The master method is a special case of the Akra-Bazzi framework, where k=1, a1=a, and b1=b. Therefore, a/bp=1⟹p=lgba. Also, let n0≥1 such that for all n≥n0 a driving function is defined and nonnegative. Notice that the integral I0=∫1n0xp+1f(x)dx is some nonnegative value, due to f(n) being nonnegative. We use this fact in all subproblems.
Case 1
Suppose f(n)=O(np−ϵ) for some constant ϵ>0, so 0≤f(n)≤cnp−ϵ.
The upper bound is
Since the integral I is always positive, if we exclude an identically zero driving function from the picture, we get that I=Θ(1). By equation (4.23), we get that T(n)=Θ(np(1+I))=Θ(np)=Θ(nlogba).
Case 2
Suppose f(n)=Θ(nplgkn) for some constant k≥0, so 0≤c1nplgkn≤f(n)≤c2nplgkn.
We first need to solve the indefinite integral ∫xlgkxdx, for example, by using WolframAlpha.
We get that it equals (ln2)k(k+1)lnk+1n+C=k+1(ln2)lgk+1x+C.
The lower bound is
The upper bound is
Theorem 3.1 implies that I=Θ(lgk+1n). Finally, we have
Case 3
Read my post on StackExchange for Mathematics.
Problems
4-1 Recurrence examples
Available in the latest revision of the IM.
4-2 Parameter-passing costs
a.
Ta1(N,n)=Ta1(N,n/2)+Θ(1) resolves to Ta1(N,n)=Θ(1)⋅Θ(lgn)=Θ(lgn) (see Exercise 4.5-3).
Ta2(N,n)=Ta2(N,n/2)+Θ(N) resolves to Ta2(N,n)=Θ(N)⋅Θ(lgn)=Θ(Nlgn).
Ta3(N,n)=Ta3(N,n/2)+Θ(n) resolves to Ta3(N,n)=Θ(n) according to case 3 of the master theorem.
b.
Tb1(N,n)=2Tb1(N,n/2)+Θ(n) resolves to Tb1(N,n)=Θ(nlgn).
Tb2(N,n)=2Tb2(N,n/2)+Θ(n+N) resolves to Tb2(N,n)=Θ(n(lgn+N))=Θ(nN). Recall that N≥n. We have to be careful with that extra Θ(N) term, since it is a cost that occurs at each recursive call; we have a branching factor of 2, therefore, we perform array copying 2lgn=n times. This is fundamentally different situation than with a binary search from the previous subproblem.
Tb3(N,n)=2Tb3(N,n/2)+Θ(n) resolves to Tb3(N,n)=Θ(nlgn).
c.
Tc1(N,n)=8Tc1(N,n/2)+Θ(1) resolves to Tc1(N,n)=Θ(n3).
Tc2(N,n)=8Tc2(N,n/2)+Θ(N2) resolves to Tc2(N,n)=Θ(n3N2) (see the comment in part (b)).
Tc3(N,n)=8Tc3(N,n/2)+Θ(n2) resolves to Tc3(N,n)=Θ(n3) (see Exercise 4.1-3).
4-3 Solving recurrences with a change of variables
Available in the latest revision of the IM.
4-4 More recurrence examples
Available in the latest revision of the IM.
The solutions in the latest IM are based on the previous edition of the book. Despite being correct, they could be solved more efficiently using the newly added chapters 4.6 and 4.7.
Parts (b) and (e) are belonging to the extended case 2 of the master theorem (see Exercise 4.6-3).
Part (d) is covered by the Akra-Bazzi method. Based on Exercise 4.6-1, we can ignore the scaling factor of ½ on a driving function and take that f(n)=n. It trivially satisfies the polynomial-growth condition. Furthermore, the perturbation factor hi(n)=−2 surely satisfies ∣hi(n)∣=2=O(n/lg1+ϵ) for some constant ϵ>0, so it can be also ignored. In this problem p=1, so the definite integral becomes trivial.
Part (f) is also easily solved using the Akra-Bazzi method with 0<p<1 (actually p≈0.88).
Part (j) can be easily solved with a change of variables. Let S(n)=T(n)/n=S(n)+1 and m=lgn. In terms of m, we have R(m)=R(m/2)+1 that resolves to R(m)=Θ(lgm). Thus, S(n)=R(m)=Θ(lglgn) and finally T(n)=nS(n)=Θ(nlglgn).
4-5 Fibonacci numbers
Available in the latest revision of the IM.
4-6 Chip testing
Available in the latest revision of the IM.
4-7 Monge arrays
a.
The "only if" direction is trivial. Just substitute k=i+1 and l=j+1 into the main definition. You can find the proof of the "if" part here (lookup Lemma 1.1).
b.
We should ensure that A[1,3]∈[24,29].
c.
Suppose for the sake of contradiction that there exists a row 1≤i<m such that f(i)>f(i+1). We have A[i,f(i+1)]>A[i,f(i)] and A[i+1,f(i)]≥A[i+1,f(i+1)] implying A[i,f(i+1)]+A[i+1,f(i)]>A[i,f(i)]+A[i+1,f(i+1)]. But this contradicts the definition of an array being Monge for coordinates j=f(i+1), k=i+1, and l=f(i).
d.
To calculate f(i) for odd i we use values of f in neighboring rows. By part (c) we know that f(i−1)≤f(i)≤f(i+1), so the index of a minimum element must be inside an interval [f(i−1),f(i+1)]. The size of this interval is f(i+1)−f(i−1)+1. The total number of comparisons is (assume f(0)=1 and f(m+1)=n)
e.
The algorithmic recurrence is T(m,n)=T(⌊m/2⌋,n)+O(m+n) for m>1.
Let c>0 and n0>1 represent the constants hidden by the O-notation. Assume n≥n0. We use the substitution method and induction on m to prove that T(m,n)≤c′(m+nlgm) for some c′>0.
We have
when −c′(m/2+n)+c(m+n)≤0, which is true if c′≥2c.
Last updated