算法导论(第三版)参考答案:思考题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 n≤2 . Make your bounds as tight as possible, and justify your answers.
- T(n)=2T(n/2)+n4
- T(n)=T(7n/10)+n
- T(n)=16T(n/4)+n2
- T(n)=7T(n/3)+n2
- T(n)=7T(n/2)+n2
- T(n)=2T(n/4)+n√
- T(n)=T(n−2)+n2
- 主方法情况三, T(n)=Θ(n4)
- 主方法情况三, T(n)=Θ(n)
- 主方法情况二, T(n)=Θ(n2lgn)
- 主方法情况一, T(n)=Θ(n2)
- 主方法情况一, T(n)=Θ(nlg27)
- 主方法情况二, T(n)=Θ(n√lgn)
- 递归树法,假设n为偶数, T(n)=n2+(n−2)2+(n−4)2+⋯+T(2)=∑n2−1i=0(n−2i)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:
- An array is passed by pointer. Time =Θ(1)
- An array is passed by copying. Time =Θ(N) , where N is the size of the array.
- An array is passed by copying only the subrage that might be accessed by the called procedure. Time
=Θ(q−p+1) if the subarray A[p…q] is passed.So:
- 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.
- Redo part (a) for the MERGE-SORT algorithm from Section 2.3.1.
二分查找
a.
T(n)=T(n/2)+c=Θ(lgn) b.
T(n)=T(n/2)+cN=2cN+T(n/4)=3cN+T(n/8)=∑lgn−1i=0(2icN/2i)=cNlgn=Θ(nlgn) c.
T(n)=T(n/2)+cn=Θ(n)
归并排序
a.
T(n)=2T(n/2)+cn=Θ(nlgn)
b.
T(n)=2T(n/2)+cn+2N=∑i=0lgn−1(cn+2iN)=∑i=0lgn−1cn+N∑i=0lgn−12i=cnlgn+N2lgn−12−1=cnlgn+nN−N=Θ(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.
- T(n)=4T(n/3)+nlgn
- T(n)=3T(n/3)+n/lgn
- T(n)=4T(n/2)+n2n√
- T(n)=3T(n/3−2)+n/2
- T(n)=2T(n/2)+n/lgn
- T(n)=T(n/2)+T(n/4)+T(n/8)+n
- T(n)=