《算法导论》第四章-思考题(参考答案)

来源:互联网 发布:蓝月翅膀升级数据 编辑:程序博客网 时间:2024/05/17 21:45

算法导论(第三版)参考答案:思考题4.1,思考题4.2,思考题4.3,思考题4.4,思考题4.5,思考题4.6

Problem 4.1 (Recurrence examples)

Give asymptotic upper and lower bound for T(n) in each of the following recurrences. Assume that T(n) is constant for n2. Make your bounds as tight as possible, and justify your answers.

  1. T(n)=2T(n/2)+n4
  2. T(n)=T(7n/10)+n
  3. T(n)=16T(n/4)+n2
  4. T(n)=7T(n/3)+n2
  5. T(n)=7T(n/2)+n2
  6. T(n)=2T(n/4)+n
  7. T(n)=T(n2)+n2
  1. 主方法情况三,T(n)=Θ(n4)
  2. 主方法情况三,T(n)=Θ(n)
  3. 主方法情况二,T(n)=Θ(n2lgn)
  4. 主方法情况一,T(n)=Θ(n2)
  5. 主方法情况一,T(n)=Θ(nlg27)
  6. 主方法情况二,T(n)=Θ(nlgn)
  7. 递归树法,假设n为偶数,T(n)=n2+(n2)2+(n4)2++T(2)=n21i=0(n2i)2+T(2)=Θ(n3)

Problem 4.2 (Parameter-passing costs)

Throughout this book, we assume that parameter passing during procedure calls takes constant time, even if an N-element array is being passed. This assumption is valid in most systems because a pointer to the array is passed, not the array itself. This problem examines the implications of three parameter-passing strategies:

  1. An array is passed by pointer. Time =Θ(1)
  2. An array is passed by copying. Time =Θ(N), where N is the size of the array.
  3. An array is passed by copying only the subrage that might be accessed by the called procedure. Time =Θ(qp+1) if the subarray A[pq] is passed.

So:

  1. Consider the recursive binary search algorithm for finding a number in a sorted array (see Exercise 2.3-5). Give recurrences for the worst-case running times of binary search when arrays are passed using each of the three methods above, and give good upper bounds on the solutions of the recurrences. Let N be the size of the original problems and n be the size of a subproblem.
  2. Redo part (a) for the MERGE-SORT algorithm from Section 2.3.1.
  1. 二分查找

    a.

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

    b.

    T(n)=T(n/2)+cN=2cN+T(n/4)=3cN+T(n/8)=lgn1i=0(2icN/2i)=cNlgn=Θ(nlgn) c.

    T(n)=T(n/2)+cn=Θ(n)

  2. 归并排序

    a.

    T(n)=2T(n/2)+cn=Θ(nlgn)

    b.

    T(n)=2T(n/2)+cn+2N=i=0lgn1(cn+2iN)=i=0lgn1cn+Ni=0lgn12i=cnlgn+N2lgn121=cnlgn+nNN=Θ(nN)=Θ(n2)

    c.

    T(n)=2T(n/2)+cn+2n/2=2T(n/2)+(c+1)n=Θ(nlgn)

Problem 4.3 (More recurrence examples)

Give asymptotic upper and lower bounds for T(n) in each of the following recurrences. Assume that T(n) is constant for sufficiently small n. Make your bounds as tight as possible, and justify your answers.

  1. T(n)=4T(n/3)+nlgn
  2. T(n)=3T(n/3)+n/lgn
  3. T(n)=4T(n/2)+n2n
  4. T(n)=3T(n/32)+n/2
  5. T(n)=2T(n/2)+n/lgn
  6. T(n)=T(n/2)+T(n/4)+T(n/8)+n
  7. T(n)=T(n1)+1/n
  8. T(n)=T(n1)+lgn
  9. T(n)=T(n2)+1/lgn
  10. T(n)=nT(n)+n
  1. 主方法情况一,T(n)=Θ(nlog34)

  2. 利用积分求和的近似

    T(n)=3T(n/3)+nlgn=nΘ(1)+i=0log3n1nlgni=nΘ(1)+ni=1+lgnlog3nlgn1i=Θ(nlglgn)

  3. 主方法情况三,T(n)=Θ(n2n)

  4. n 足够大时,可以忽略-1。所以应用主方法情况二,T(n)=Θ(nlgn)

  5. 通过积分求和的近似,类似第2问

T(n)=2T(n/2)+nlgn=nΘ(1)+i=0lgn1nlgni=nΘ(1)+ni=1lgn1i=Θ(nlglgn)

  1. 类似于练习4.4-6。T(n)=Θ(n)
  2. 通过积分求和近似。

T(n)=T(n1)+1/n=1n+1n1+T(n2)=i=0n11ni+Θ(1)=i=1n1i+Θ(1)=Θ(lgn)

  1. 递归树结构类似第7问

T(n)=lgn+T(n1)=i=0n1lg(ni)=i=1nlgi+Θ(1)=Θ(lg(n!))=Θ(nlgn)

  1. 同样利用到积分求和的近似

T(n)=1lgn+1lg(n2)++Θ(1)=i=0n/211lg(n2i)=i=2n1lgi=i=1lgn1i=Θ(lglgn)

  1. S(n)=T(n)n,则 S(n)=S(n)+1。考虑 n=2m 的情况,有 S(2m)=S(2m/2)+1。令 P(m)=S(2m),得 P(m)=P(m/2)+1。 根据主定理情况二,P(m)=Θ(lgm)。 则

T(n)=nS(n)=nS(2m)=nP(m)=Θ(nlgm)=Θ(nlglgn)

Problem 4.4 (Fibonacci numbers)

This problem develops properties of the Fibonacci numbers, which are defined by recurrence (3.22). We shall use the technique of generating functions to solve the Fibonacci recurrence. Define the generating function (or formal power series) F as

>>F(z)>=i=0Fizi=0+z+z2+2z3+3z4+5z5+8z6+13z7+21z8+,>>

where Fi is the ith Fibonacci number.

  1. Show that F(z)=z+zF(z)+z2F.
  2. Show that

>>F(z)>>=z1zz2=z(1ϕz)(1ϕ^z)=15(11ϕz11ϕ^z)>>

where

>ϕ=1+52=1.61803>ϕ^=152=0.61803>

  1. Show that

>F(z)=i=015(ϕiϕ^i)zi>

  1. Use part (c) to prove that Fi=ϕi/5 for i>0, rounded to the nearest integer. (Hint: Observe that |ϕ^|<1.)

z+zF(z)+z2F(Z)==z+zi=0Fizi+z2i=0Fizi=z+i=1Fi1zi+i=2Fi2zi=z+0+i=2(Fi1+Fi2)zi=0+z+i=2Fizi=F(z)

  1. 根据第1问,可得

F(z)=z1zz2=z(1ϕz)(1ϕ^z)=15(11ϕz11ϕ^z)(ϕ=1+52,ϕ^=152)

  1. 根据几何级数的性质 11x=k=0xk |x|<1

F(n)=15(11ϕz11ϕ^z)=15(i=0ϕizii=0ϕ^izi)=i=015(ϕiϕ^i)zi

  1. Fi=ϕiϕ^i5=ϕi5ϕ^i5 ,又 Fi={0,1,2,3,5,8,13...} 。当 i>0 , |ϕ^|<1, 得 |ϕ^i5|<0.5。所以对一个整数,加/减去一个小于0.5的数,对所得结果舍入到最近的整数即可。

Problem 4.5 (Chip testing)

Professor Diogenes has n supposedly identical integrated-circuit chips that in principle are capable of testing each other. The professor’s test jig accomodates two chips at a time. When the jig is loaded, each chip tests the other and reports whether it is good or bad. A good chip always reports accurately whether the other chip is good or bad, but the professor cannot trust the answer of a bad chip. Thus, the four possible outcomes of a test are as follows.

Chip A says Chip B says Conclusion B is good A is good both are good, or both are bad B is good A is bad at least one is bad B is bad A is good at least one is bad B is bad A is bad at least one is bad

1. Show that if more than n/2 chips are bad, the professor cannot necessarily determine which chips are good using any strategy based on this kind of pairwise test. Assume that the bad chips can conspire to fool the professor.
2. Consider the problem of finding a single good chip from among n chips, assuming that more than n/2 of the chips are good. Show that n/2 pairwise tests are sufficient to reduce the problem to one of nearly half the size.
3. Show that the good chips can be identified with Θ(n) pairwise tests, assuming that more than n/2 chips are good. Give and solve the recurrence that describes the number of tests.

  1. 因为好芯片检测准确,而坏芯片检测不确定(可能准确,也可能错误)。如果好芯片数量少于n/2,则就会有同样数量的坏芯片,假设跟好芯片表现的一样(总能准确报告芯片的好坏)。按照这种策略,永远也没法区分这两类的好坏性了。
  2. 一种策略:从中取出两类,每类个n/2。对这两类一一对应地比较,只保留满足情况一(都报告对方为好芯片)的芯片。也就是说,剔除出去的至少有一个坏的。剩下来的每对只挑选一个,加上可能为配对的芯片,组成一个新集合。这个集合仍然满足:超过一半的好芯片,同时规模减半。
  3. 第2问的策略递归式:

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

满足主方法情况三,T(n)=Θ(n)

Python code

import randomclass GoodChip:    def good(self):        return True    def check(self, other):        return other.good()class BadChip:    def good(self):        return False    def check(self, other):        return [True, False][random.randint(0, 1)]def jig(a, b):    return [a.check(b), b.check(a)]def diogenes(chips, verbose = False):    def find_single(chips):        if len(chips) <= 2:            return chips[0]        else:            halfpoint = len(chips) // 2            pairs     = zip(chips[0:halfpoint], chips[halfpoint:halfpoint * 2])            kept      = [a for (a, b) in pairs if jig(a, b) == [True, True]]            if len(chips) % 2 == 1:                kept.append(chips[-1])            return find_single(kept)    good = find_single(chips)    return [chip for chip in chips if jig(good, chip) == [True, True]]

Problem 4.6 (Monge arrays)

An m×n array A of real numbers is a Monge array if for all i,j,k, and l such that 1i<km and 1j<ln, we have

>A[i,j]+a[k,l]A[i,l]+A[k,j]>

In other words, whenever we pick two rows and two columns of a Monge array and consider the four elements at the intersections of the rows and columns, the sum of the upper-left and lower-right elements is less than or equal to the sum of the lower-left and upper-right elements. For example, the following array is Monge:
>>10>17>24>11>45>36>7517222813443366131622632195128293417372153232324723634>>

  1. Prove that an array is Monge if and only i for all i=1,2,,m1, and j=1,2,,n1 we have

>A[i,j]+A[i+1,j+1]A[i,j+1]+A[i+1,j]>

(Hint: For the “if” part, use induction seperately on rows and columns)

  1. The following array is not Monge. Change one element in order to make it Monge. (Hint: Use part (a).)

>>37>21>53>32>43>2363413212273091532103168>

  1. Let f(i) be the index of the column containing the leftmost minimum element of the row i. Prove that f(1)f(2)f(m) for any m×n Monge array.
  2. Here is a description of a divide-and-conquer algorithm that computes the leftmost minimum element in each row of an m×n Monge array A:

Construct a submatrix A' of A consisting of the even-numbered rows of A. Recursively determine the leftmost minimum for each row in A'. Then compute the leftmost minimum in the odd-numbered rows of A.

Explain how to compute the leftmost minimum in the odd-numbered rows of A (given that the leftmost minimum of the even-numbered rows is known) in O(m+n) time.

  1. Write the recurrence describing the running time of the algorithm described in part (d). Show that its solution is O(m+nlogm).
  1. “仅当”证明,根据定义可以方便地得到。对于“当”的部分

A[i,j]+A[i+1,j+1]A[i,j+1]+A[i+1,j]A[i,j]+A[k,l]A[i,l]+A[k,j]

对行使用归纳法,证明:

A[i,j]+A[i+1,j+1]A[i,j+1]+A[i+1,j]A[i,j]+A[k,j+1]A[i,j+1]+A[k,j]


A[i,j]+A[k,j+1]A[i,j+1]+A[k,j]()A[k,j]+A[k+1,j+1]A[k,j+1]+A[k+1,j]()A[i,j]+A[k,j+1]+A[k,j]+A[k+1,j+1]A[i,j+1]+A[k,j]+A[k,j+1]+A[k+1,j]A[i,j]+A[k+1,j+1]A[i,j+1]+A[k+1,j]

在此基础上,同理对列使用归纳法。得证

  1. 将第一行第三列元素改为24。

37215332432363413212473091532103168

  1. 反证法。若 f(i)>f(j)(i<j) ,则

A[i,f(i)]+A[j,f(j)]<A[i,f(j)]+A[j,f(i)]A[i,f(j)]+A[j,f(i)]A[i,f(i)]+A[j,f(j)]

所以当 i<jf(i)f(j)

  1. 利用上述性质

T(m,n)=i=0m/21(μ2i+2μ2i+1)=i=0m/21μ2i+2i=0m/21μ2i+m/2=i=1m/2μ2ii=0m/21μ2i+m/2=μmμ0+m/2n+m/2=O(m+n)

T(m)=T(m/2)+cn+m/2=cn+dm/2+cn+dm/4+cn+dm/8+=i=0lgm1cn+i=0lgmdm2i+1=cnlgm+dmi=0lgm112i+1<cnlgm+dmi=012i=cnlgm+2dm=O(nlgm+m)

C code

typedef struct array {    int m;    int n;    int step;    int *data;} array;int get(array A, int i, int j) {    return A.data[((i + 1) * A.step - 1) * A.n + j];}array half(array a) {    array result = { a.m, a.n, a.step * 2, a.data };    return result;}int height(array array) {    return array.m / array.step;}int min_index(array A, int row, int left, int right) {    int min = left;    for (int i = left; i < right; i++) {        if (get(A, row, i) < get(A, row, min)) {            min = i;        }    }    return min;}void find_minimums(array A, int *mins) {    if (height(A) == 1) {        mins[0] = min_index(A, 0, 0, A.n);    } else {        array evens = half(A);        int even_minimums[height(evens)];        find_minimums(evens, even_minimums);        int leftmost = 0;        for (int i = 0; i < height(evens); i++) {            leftmost = min_index(A, 2 * i, leftmost, even_minimums[i] + 1);            mins[2 * i]     = leftmost;            mins[2 * i + 1] = even_minimums[i];        }        if (height(A) % 2) {            mins[height(A) - 1] = min_index(A, height(A) - 1, leftmost, A.n);        }    }}
阅读全文
0 0