26
Partial Solutions for Introduction to algorithms second edition Professor: Song You TA: Shao Wen

Partial Solutions for Introduction to algorithms … Solutions for Introduction to algorithms ... pseudocode, either iterative ... Observe that the while loop of lines 5

Embed Size (px)

Citation preview

Partial Solutions for

Introduction to algorithms

second edition

Professor: Song You

TA: Shao Wen

ACKNOWLEDGEMENT

CLASS ONE: JINZI

CLASS TWO: LIUHAO, SONGDINMIN, SUNBOSHAN, SUNYANG

CLASS FOUR:DONGYANHAO, FANSHENGBO, LULU, XIAODONG,

CLASS FIVE:GAOCHEN, WANGXIAOCHUAN, LIUZHENHUA, WANGJIAN, YINGYING

CLASS SIX: ZHANGZHAOYU, XUXIAOPENG, PENGYUN, HOULAN

CLASS: LIKANG,JIANGZHOU, ANONYMITY

The collator of this Answer Set, SHAOWen, takes absolutely no

responsibility for the contents. This is merely a vague suggestion to a solution to

some of the exercises posed in the book Introduction to algorithms by Cormen,

Leiserson and Rivest. It is very likely that there are many errors and that the

solutions are wrong. If you have found an error, have a better solution or wish to

contribute in some constructive way please send an Email to

[email protected]

It is important that you try hard to solve the exercises on your own. Use this

document only as a last resort or to check if your instructor got it all wrong.

Have fun with your algorithms and get a satisfactory result in this course.

Best regards,

SHAOWen

Chapter one Exercises 1.1-2

Other than speed, what other measures of efficiency might one use in a real-world setting?

空间,硬件资源等

Exercises 1.1-4 (class two 刘浩)

How are the shortest-path and traveling-salesman problems given above similar? How are

they different?

相似点:找出最短路径

不同点:shortest-path 不一定经过所有点,而 traveling-salesman 得经过所有点

Exercises 1.2-2

Suppose we are comparing implementations of insertion sort and merge sort on the same

machine. For inputs of size n, insertion sort runs in 8n2 steps, while merge sort runs in 64n

lg n steps. For which values of n does insertion sort beat merge sort?

插入排序的性能优于合并排序也就是:插入排序所用的步数少:

所以有:8n2 ≤ 64nlgn ⇒ n < 8𝑙𝑛n 需要解一下这个超越方程,

编个程序很容易得到:

2 ≤ n ≤ 43

Exercises 1.2-3

What is the smallest value of n such that an algorithm whose running time is 100n2 runs

faster than an algorithm whose running time is 2n on the same machine?

原理同上题,可列出如下不等式:

100n2 ≤ 2n 解这个不等式(代数法),可求出最小的整数 n=15

Problems 1-1: Comparison of running times

For each function f(n) and time t in the following table, determine the largest size n of a

problem that can be solved in time t, assuming that the algorithm to solve the problem

takes f(n) microseconds.

1

second

1

minute

1

hour

1

day

1

month

1

year

1

century

lg n 2106 26·107 236·108

∞ ∞ ∞ ∞

n 1012 36·1014 1296·1016 ∞ ∞ ∞ ∞

n 106 6·107 36·108 ∞ ∞ ∞ ∞

n lg n 62746 2801417 ?? ?? ?? ?? ??

n2 103

24494897 6·104

293938 1609968 30758413 307584134

n3 102 391 1532 4420 13736 98169 455661

2n 19 25 31 36 41 49 56

n! 9 11 12 13 15 17 18

Chapter two

Exercises 2.1-1 (class four 匿名)

Using Figure 2.2 as a model, illustrate the operation of INSERTION-SORT on the array A =

〈31, 41, 59, 26, 41, 58〉.

代码:

INSERTION-SORT(A)

1 for j ← 2 to length*A+

2 do key ← A*j+

3 Insert A[j] into the sorted sequence A[1..j-1]

4 i ← j – 1

5 While I > 0 and A[i] > key

6 do A*i+1+ ← A*i+

7 i ← j – 1

8 A*i+1+ ← key

图解:

31 41 59 26 41 58

31 41 59 26 41 58

31 41 59 26 41 58

26 31 41 59 41 58

26 31 41 41 59 58

26 31 41 41 58 59

Exercises 2.3-1 (class five 高晨)

Using Figure 2.4 as a model, illustrate the operation of merge sort on the array A = 〈3, 41,

52, 26, 38, 57, 9, 49〉.

如下所示:

3,9,26,38,41,49,52,57

3,26,41,52 9,38,49,57

3,41 26,52 38,57 9,49

3 41 52 26 38 57 9 49

Exercises 2.3-3 (class six 侯岚)

Use mathematical induction to show that when n is an exact power of 2, the solution of

the recurrence

𝐓 𝐧 = 𝟐 𝐢𝐟 𝐧 = 𝟐

𝟐𝐓 𝐧

𝟐 + 𝐧 𝐢𝐟 𝐧 = 𝟐𝐤 ,𝐟𝐨𝐫 𝐤 > 1

is T(n)=nlgn.

由题意得:

根据数学归纳法:

给出基本条件(base case):

当 n=2 时,T(2)= 2 lg2 = 2。符合条件。

给出假设:

当 n=2t 时,T(2t)= 2t lg2 t成立。

那么,

当 n=2t+1 时,T(2t+1)= 2 T(2t+1 /2)+ 2t+1

= 2T(2t)+2

t+1

= 2(2tlg2t)+2t+1

= 2t+1lg2t+1

所以,得证!

Exercises 2.3-5

Referring back to the searching problem (see Exercise 2.1-3), observe that if the sequence

A is sorted, we can check the midpoint of the sequence against v and eliminate half of the

sequence from further consideration. Binary search is an algorithm that repeats this

procedure, halving the size of the remaining portion of the sequence each time. Write

pseudocode, either iterative or recursive, for binary search. Argue that the worst-case

running time of binary search is Θ(lg n).

给出一个递归算法. 显然最坏情况的运行时间为(lg n).

BINARY-SEARCH(A, v, p, r)

if p ≥ r and v ≠ A[p] then

return nil

end if⌊n/2⌋

j←A[⌊ (r - p)/2⌋]

if v = A[j] then

return j

else

if v < A[j] then

return BINARY-SEARCH(A; v; p; j)

else

return BINARY-SEARCH(A; v; j; r)

endif

end if

Exercises 2.3-6 (李康)

Observe that the while loop of lines 5 - 7 of the INSERTION-SORT procedure in Section 2.1

uses a linear search to scan (backward) through the sorted subarray A[1 ‥ j - 1]. Can we

use a binary search (see Exercise 2.3-5) instead to improve the overall worst-case running

time of insertion sort to Θ(n lg n)?

将插入排序的顺序查找改为二分查找的算法:

INSERTION-SORT(A)

9 for j ← 2 to length[a]

10 do key ← a[j]

11 insert a[j] into the sorted sequence a[1..j-1]

12 high ← j–1

13 low ← 1

14 while low < high

15 mid = ( low + high ) / 2

16 If key == A[mid] then

17 break

18 if key < A[mid] then

19 high ← mid - 1

20 if key > A[mid] then

21 Low ← mid + 1

22 for i ← mid to j – 1

23 A[i+1] ← a[i]

24 A[mid] ← key

在最坏情况下,二分查找的时间复杂度是 nlgn,但插入时数组移动的时间复杂度仍是 n2。

故总体运行时间不能改善为 Θ(n lg n)。(但若排序中采用链表的数据结构,则可改善。)

Problems 2-1: Insertion sort on small arrays in merge sort (class two 刘浩)

Although merge sort runs in Θ(n lg n) worst-case time and insertion sort runs in Θ(n2)

worst-case time, the constant factors in insertion sort make it faster for small n. Thus, it

makes sense to use insertion sort within merge sort when subProblems become

sufficiently small. Consider a modification to merge sort in which n/k sublists of length k

are sorted using insertion sort and then merged using the standard merging mechanism,

where k is a value to be determined.

a. Show that the n/k sublists, each of length k, can be sorted by insertion sort in Θ(nk)

worst-case time.

在最坏情况下,插入排序的运行时间为 Θ(n2),由于每个字列表的长度为 k,则对每个

字列表进行插入排序需要的时间为 Θ(k2),而一共有 n/k 个字列表,则所需总时间为

n

k× Θ k2 = Θ(nk)

b. Show that the sublists can be merged in Θ(n lg (n/k) worst-case time.

在最坏情况下,按照条件:

T n = ck, n = k

2T n

2 + cn, n > 𝑘

其中k为一常数

画出递归树, 递归树每层的代价都为cn,其中最后一层有n

k个规模为ck的节点,而递

归树一共有lg(n

k)+1层。所以总代价为cnlg(

n

k)+cn,为Θ(n lg(n/k))

Problems 2-2: Correctness of bubblesort (class five 董亮)

Bubblesort is a popular sorting algorithm. It works by repeatedly swapping adjacent

elements that are out of order.

BUBBLESORT(A)

1 for i ← 1 to length*A+

2 do for j ← length*A+ downto i + 1

3 do if A[j] < A[j - 1]

4 then exchange A*j+ ↔ A*j - 1]

a. Let A′ denote the output of BUBBLESORT(A). To prove that BUBBLESORT is

correct, we need to prove that it terminates and that

(2.3) 𝐀′ 𝟏 ≤ 𝐀′ 𝟐 ≤ ⋯ ≤ 𝐀′ 𝐧

b. where n = length[A]. What else must be proved to show that BUBBLESORT actually

sorts?

The next two parts will prove inequality (2.3).

b. State precisely a loop invariant for the for loop in lines 2-4, and prove that this loop

invariant holds. Your proof should use the structure of the loop invariant proof

presented in this chapter.

c. Using the termination condition of the loop invariant proved in part (b), state a

loop invariant for the for loop in lines 1-4 that will allow you to prove inequality

(2.3). Your proof should use the structure of the loop invariant proof presented in

this chapter.

d. What is the worst-case running time of bubblesort? How does it compare to the

running time of insertion sort?

b. 循环不变式:在第 2~4 行 for 循环每一轮迭代的开始,A[j]是 A[j„„n]中最小的元素

下面给出这个循环不变式的证明:

初始化:在循环第一轮迭代开始之前,有 j=n,故 A[j]是 A[j„„j]中最小元素;

保持:由 A[j]是 A[j„„n]中的最小元素,3~4 行可以保证 A[j-1]是 A[j]、A[j-1]中较小的,那

么 A[j-1]是 A[j-1„„n]中最小的元素;

终止:循环结束时 j=i+1,此时 A[i]是 A[i„„n]中的最小元素

c. 循环不变式在在第 1~4 行 for 循环每一轮迭代的开始,子数组 A[1„„i-1]是 A[1„„n]中

最小的 i-1 个元素的集合,并且已排好序

下面给出这个循环不变式的证明:

初始化:在循环第一轮迭代开始之前,有 i=1,此时子数组 A[1„„i-1]是空的;

保持:前次迭代的结果为子数组 A[1„„i-1]是 A[1„„n]中最小的 i-1 个元素的集合,并且已

排好序,并由 2~4 行内层循环不变式的结果可知,A[i]是 A[i„„n]中的最小元素,那么在

A[1„„i]中必有 A[i]最大,且子数组 A[1„„i]是 A[1„„n]中最小的 i 个元素的集合;

终止:循环结束时 i=n,此时 A[1„„n]已被排好序。

d. 冒泡排序法最坏运行时间是 O(n2),与插入排序相同,但冒泡排序在最佳情况下的运行时间

比插入排序略长。

Chapter three

Exercises 3.1-1

Let f(n) and g(n) be asymptotically nonnegative functions. Using the basic definition of

Θ-notation, prove that max(f(n), g(n)) = Θ(f(n) + g(n)).

因为 f(n)和 g(n)都是渐近非负的函数,所以根据定义有:存在 Nf,Ng,使得:当 n>Nf时,f(n)≥0,

同时,当 n> Ng时,g(n) ≥0.所以,我们取 N0=maxNf,Ng,此时,当 n>N0 时,同时有 f(n)≥0,g(n) ≥0.

下面我们取 C1=1/2,C2=1,根据 f(n),g(n)的非负性保证,当 n>N0 时,有:

f n + g(n)

2≤ max f n , g n ≤ f n + g(n)

.所以,得证!

Exercises 3.1-4

Is 2n+1 = O(2n)? Is 22n = O(2n)?

2n+1

= O(2n) 因为 2

n+1 = 2×2

n≤2×2n, 所以成立

22n = O(2n) 不成立。用反证法:如果有 22n = O(2n)成立,根据定义需要有:22n≤c2n,由此需

要:n≤lgc.矛盾!对于任意大的 n 是不可能成立的,因为 c 是一个常数。

Exercises 3.1-5 (class five 高晨)

Prove Theorem 3.1.

首先证明充分性:即由 f(n)= Θ(g(n))推出 f(n)= O(g(n))且 f(n)= Ω (g(n))

根据 Θ 定义:有存在 N0,C0, , 使得:当 n> N0 时有:C0 g n ≤ f n ≤ C1 g(n),在根据 O,和 Ω

定义有 f(n)= O(g(n))且 f(n)= Ω (g(n)).

在证明必要性:即由 f(n)= O(g(n))且 f(n)= Ω (g(n))推出 f(n)= Θ(g(n))

根据 O,和 Ω 定义有:存在 N1, C1 使得:当 n> N1 时有f n ≤ C1 g(n),同样地,存在 N2, C0

使得:当 n> N2 时有C0 g n ≤ f n ,此时,我取 N0=max(N1, N2),则有如下事实成立:当 n> N0

时有:C0 g n ≤ f n ≤ C1 g(n),即 f(n)= Θ(g(n)).所以,得证!

Exercises 3.1-7 (class six 彭运)

Prove that o(g(n)) ∩ ω(g(n)) is the empty set.

Solution: Let f∈o (g(n))∩ ω(g(n)). That means f= o (g(n))= ω(g(n)). By the definition of o, we have

limn→∞

(f(n)/g(n)) = 0

But the definition ofω, we have

limn→∞

(f(n)/g(n)) = ∞

Because of 0≠∞, we have a obvious contradiction.

Problems 3-2: Relative asymptotic growths

Indicate, for each pair of expressions (A, B) in the table below, whether A is O, o, Ω, ω, or Θ

of B. Assume that k ≥ 1, 𝛆> 0, and c > 1 are constants. Your answer should be in the form of

the table with "yes" or "no" written in each box.

A B O o Ω ω Θ

a. lgk n nε n n y y n

b. nk cn n n y y n

c. n nsin n n n n n n

d. 2n 2

n/2 y y n n n

e. nlg c clg n y n y n y

f. lg(n!) lg(nn) n n y y n

Chapter four Exercises 4.1-1

Show that the solution of T (n) = T (⌈n/2⌉) + 1 is O(lg n).

因为包含上取整函数,又要找一个上界,所以我们要用一些技巧:

假设:T(n)≤c lg(n - b),对⌈n/2⌉成立,进而,我们有:

T(n)≤c lg(⌈n/2⌉-b) + 1

≤c lg(n/2 - b + 1) + 1

= c lg(n − 2b + 2

2) + 1

= c lg(n - 2b + 2) - c lg 2 + 1

≤c lg(n - b)

最后一个不等号成立需要,b≥ 2,c≥1

所以,得证!

Exercises 4.1-2 (class two 刘浩)

We saw that the solution of T (n) = 2T (⌈n/2⌉) + n is O(n lg n). Show that the solution of this

recurrence is also Ω(n lg n). Conclude that the solution is Θ(n lg n).

当 n = 1 时,T(n) = 1, T(n) ≥ cn lg n = c × 1 × lg 1 = 0

假设 T(n) ≥ cn lg n 对 n/2 成立

T(n) ≥ 2(⌈cn/2⌉ lg(⌈n/2⌉)) + n ≥ cn lg(n/2) + n

= cn lg n – cn lg 2 + n = cn lg n – cn lg 2 + n

= cn lg n – cn + n

≥cn lg n ( 当 c < 1 时)

综上所述,能取到一个小于 1 的 c, 使得 T(n) ≥ cn lg n, 则 T(n) = Ω(n lgn)

又 T(n) = O(n lgn) ,所以 T(n) = Θ(n lg n)

Exercises 4.1-5 (class four 董延昊)

Show that the solution to T (n) = 2T (⌊ n/2⌋ + 17) + n is O(n lg n).

往证:存在常数N0,C0,使得:当n>N0时 T(n) ≤C0nlgn.

假设:T(⌊n/2⌋+ 17)≤C0 (⌊n/2⌋+ 17)lg(⌊n/2⌋+ 17)

我们有:

T(n) ≤2C0(⌊n/2⌋+ 17)lg(⌊n/2⌋+ 17)+n

≤2C0(n/2+ 17)lg(n/2+ 17)+n

=C0(n+34)[lg(n+34)-1]+n

=C0nlg(n+34)-C0n+34C0lg(n+34)-34C0+n „„(*1)

我们取C0≥2

则有(*1) ≤C0nlg(n+34)+34C0lg(n+34)-n -34C0

<C0nlg(n+34)+34C0lg(n+34)-n

=C0nlgn+ C0nlg(n+34) –C0nlgn + 34C0lg(n+34) –n „„(*2)

下面我们只需证明存在常数N0,C0≥2 使得当n>N0时,有

C0nlg(n+34) –C0nlgn + 34C0lg(n+34) –n≤0

此时即有:(*2)<=C0nlgn (即我们往证的结论)

C0nlg(n+34) –C0nlgn + 34C0lg(n+34) –n

=C0n(lg(n+34)-lgn)+34C0lg(n+34)-n

=C0n(34ln2/ξ) +34C0lg(n+34)-n (n ≤ ξ ≤ 34 + n)

≤34C0nln2/(34+n)+34C0lg(n+34)-n

此时只需取C0=3,N0=100,即可使当n>N0时,有

C0nlg(n+34) –C0nlgn + 34C0lg(n+34) –n≤0

所以,得证!

Exercises 4.1-6 (class five 高晨)

Solve the recurrence 𝐓 𝐧 = 𝟐𝐓 𝐧 + 𝟏 by making a change of variables. Your solution

should be asymptotically tight. Do not worry about whether values are integral.

T(n)=2T( n)+1

令 m=lgn,则有 n=2m, n=2m/2,则原式可以化为 T(2m)=2T(2 m/2)+1,再令 S(m)=T(2m),则有

S(m)=2S(m/2)+1 。利用递归树或者主方法可得: S(m)=O(m), 再利用代换可得:

T(n)=T(2m)=S(m)=O(m)=O(lgn)

Exercises 4.2-1 (class five 应莹)

Use a recursion tree to determine a good asymptotic upper bound on the recurrence T(n) =

3T(⌊n/2⌋) + n. Use the substitution method to verify your answer.

证明: 画出递归树,我们发现对于第i层,每个节点的规模不超过n/2i,(这句话针对式子T(n) =

3T(⌊n/2⌋) + n中的第一项来说) 所以,每一层的贡献最多为:n(3/2)i(这句话针对式子T(n) =

3T(⌊n/2⌋) + n中的第二项来说),而在最后一层中,我们假设解决T(1)=1,(规定为常数c,更具

有普遍意义,这里做了简化,不影响一般性),所以最后一层的贡献为n(3/2)lgn

所以,我们有 :

T(n)=3T(⌊n/2⌋)+n

≤ n +3

2n + (

3

2)2n + ⋯… + (

3

2)lgnn = n (

3

2)i

lgn

i=0

= n1 − (

32

)lgn

1 −32

= 2n(2/3)lgn − 2n

= 2·3lgn

-2n

= Θ(nlg 3)

以上是我们的猜测,我们可以用替换法给出一个证明,但我们可以用主方法直接得出答案。

Exercises 4.2-3(class five 应莹)

Draw the recursion tree for T (n) = 4T (⌊ n/2⌋ )+cn, where c is a constant, and provide a tight

asymptotic bound on its solution. Verify your bound by the substitution method.

此题和上一题的思路完全一样,只是数字不用而已,这里不在赘述。套用上题的过程,即可

得出答案,Θ(n2 )

Exercises 4.3-1

Use the master method to give tight asymptotic bounds for the following recurrences.

a. T (n) = 4T(n/2) + n.

b. T (n) = 4T(n/2) + n2.

c. T (n) = 4T(n/2) + n3.

a. T(n) = 4T(n/2) + n. 因为 n = O(n2−ε) ,第一种情况,所以有 T(n) = Θ(n2).

b. T(n) = 4T(n/2) +n2 . 因为 n2= Θ(n2) ,所以有 T(n) =Θ(n2 lg n).

c. T(n) = 4T(n/2) + n3 . 因为 n3 = Ω(n2+ε ) 第三种情况,所以有 T(n) = Θ(n3).

Exercises 4.3-2 (class six 彭运)

The recurrence T(n) = 7T (n/2)+n2 describes the running time of an algorithm A. A

competing algorithm A′ has a running time of T′(n) = aT′(n/4) + n2. What is the

largest integer value for a such that A′ is asymptotically faster than A?

解: (在此题中,具体套用主定理的过程省略)应用主定理我们知道算法 A 的时间复杂度为

T(n)= Θ(nlg7),我们分类讨论 a 的取值:

a<16 时, T’(n)= Θ(n2) 此时,A’比算法 A 更快

a=16 时,T’(n)= Θ(n2lgn) 此时,A’比算法 A 更快

a>16 时,T’(n)= Θ(nlg a) 此时,若要 A’比算法 A 更快,则需要lg a<7, 由此,a<49,所以,a

的最大整数解为 48

Exercises 4.3-3

Use the master method to show that the solution to the binary-search recurrence T(n) = T

(n/2) + Θ(1) is T(n) = Θ(lg n). (See Exercise 2.3-5 for a description of binary search.)

解:nlogb

a =1. 与 Θ(1)同阶,所以是 Θ(lg n). (直接套公式)

Problems 4-1: Recurrence examples (class two 孙博山)

Give asymptotic upper and lower bounds 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.

a. T(n) = 2T(n/2) + n3.

b. T (n) = 7T(n/3) + n2.

c. T(n) = T(n - 1) + n.

d. T(n) = T( 𝐧) + 1.

a) 3n

b) n

c) 2 lgn n

d) 2n

e) 72logn

f) lgn n

g) n

h) lg n

Problems 4-4: More recurrence examples (class two 孙博山)

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.

d)T(n) = 3T(n/3 + 5) + n/2.

f)T(n) = T(n/2) + T(n/4) + T(n/8) + n.

i)T(n) = T(n - 2) + 2 lg n.

j)T(n) = 𝐧T( 𝐧) + n.

d). ( lg )n n

f). ( ), 8n c

i).32

( lg ),n n c

j). ( lg )n n

Problems 4-6: VLSI chip testing

Professor Diogenes has n supposedly identical VLSI chips that in principle are capable of

testing each other. The professor's test jig accommodates 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 answer of a bad

chip cannot be trusted. 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

a. 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.

b. 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.

c. Show that the good chips can be identified with Θ(n) pairwise tests, assuming that

more than n/2 of the chips are good. Give and solve the recurrence that describes

the number of tests.

a) 实际上仔细想来,只有以下几种情况(在解题中可能用不上,但我还是给写上了)

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 A is bad and B is good or both are bad

B is bad A is good B is bad and A is good or both are bad

B is bad A is bad A is bad and B is good or B is bad and A is good both are bad

下面来证明第一问:我们主要来构造坏的芯片如何“联合起来欺骗教授”。给出一种构造方

法:我们取任何一个整数 m(m≤n)个芯片来测试,(即取总芯片的一个子集来测试),下面的

假设都可能成立:

我们假设:在任何这样的子集中,坏芯片的个数总是多于好芯片的个数。坏的芯片总是说坏

的芯片是好的,而说好的芯片是坏的,即坏的芯片总是说同类是好的,而异类是坏的。另一

方面,好的芯片会说好的芯片是好的,而坏的芯片是坏的,即好的芯片总是说同类是好的,

而异类是坏的。由此可以看出,无论好芯片还是坏芯片都说同类是好的,而异类是坏的,这

样没有任何区别,也就不可能分出好坏芯片了。

需要另外说明的是:以上我们做的是一种假设,在实际情况下不一定发生!但关键问题是:

在未知好坏芯片的情况下,做成对测试时,没有理由排除以上假设!既然排除不了以上假设,

就意味着无论使用任何策略都不能确定哪个芯片是好的。所以,得证!

b) 证明:这个问题有点复杂:我们分两种情况来讨论:

1) n 是偶数:注意到:由多于 n/2 的芯片是好的。即有严格大于 n/2 个芯片是好的,那

就意味着好的芯片只少比坏的芯片多两个。我们把这 n 个芯片两两分组进行测试,

其结果分别有以下四种情况:”GG” (A,B 都说对方是好的,下面类似), ”GB”, ”BG”, ”BB”.

其中,”GG”的情况最多有 n/2 个,最少有 1 个。(可以简单地用反证法来证明)。我

们丢掉”GB”, ”BG”, ”BB”这三种情况的芯片,只留下”GG”情况的芯片,此时,剩下的

芯片数最多有 n 个最少有 2 个。之后,我们再在每组中选出一个来作为最后的结果(选

哪个芯片都一样,因为在”GG”组中两个都是好的,或者两个都是坏的)。此时,最后

结果的芯片总数最多有 n/2 个,最少有 1 个,并且在最后结果中,好芯片的数目总

是严格的大于 n/2,和原问题相同。因为我们在去掉”GB”, ”BG”, ”BB”芯片的时候,每

组中至少有一个芯片是坏的!此时,经过了 n/2 次比较,便可以把原问题规模降至

最大为原来的一半。进一步可知,如果在最后结果中只剩下一个芯片,那么这一个

芯片肯定是好的。

2) n 是奇数:如果 n 是奇数先孤立一个按照 1)的步骤进行,然后再把剩下的一个放入

到最后结果中即可。

c) 基于 b)中给出的算法,我们有以下递归式成立:

T(n)=T(n/2)+n/2,(忽略上下取整,因为上下取整不影响阶数)

根据主定理可得:T(n)= Θ(n)

Problems 4-7: Monge arrays (class five 高晨)

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

m and 1 ≤ j < l ≤ n, 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 the columns, the sum of

the upper-left and lower-right elements is less or equal to the sum of the lower-left and

upper-right elements. For example, the following array is Monge:

10 17 13 28 23

17 22 16 29 23

24 28 22 34 24

11 13 6 17 7

45 44 32 37 23

36 33 19 21 6

75 66 51 53 34

a. Prove that an array is Monge if and only if for all i = 1, 2, …, m - 1 and j = 1, 2,…, n -

1, we have

A*i, j+ + A*i + 1, j + 1+ ≤ A*i, j + 1+ + A*i + 1, j+.

Note (For the "only if" part, use induction separately on rows and columns.)

b. (*)The following array is not Monge. Change one element in order to make it

Monge. (Hint: Use part (a).)

37 23 22 32

21 6 7 10

53 34 30 31

32 13 9 6

43 21 15 8

c. (*)Let f(i) be the index of the column containing the leftmost minimum element of

row i. Prove that f(1) ≤ f(2) ≤ ··· ≤ f(m) for any m × n Monge array.

d. (*)Here is a description of a divide-and-conquer algorithm that computes the

left-most minimum element in each row of an m × n Monge array A:

o Construct a submatrix A′ of A consisting of the even-numbered rows of A.

Recursively determine the leftmost minimum for each row of 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.

e. (*)Write the recurrence describing the running time of the algorithm described in

part (d). Show that its solution is O(m + n log m).

a. 充分条件:

显 然 , 如 果 一 个 矩 阵 为 Monge 矩 阵 , 那 么 由 定 义 可 以 得 到 :

A[i,j]+A[i+1,j+1]<=A[i,j+1]+A[i+1,j]

必要条件:

现在对行和列分别进行归纳证明。先对行进行归纳。

当 m=1 时,有 A[i,j]+A[i+1,j+1]<=A[i,j+1]+A[i+1,j]成立。

设当 m=k 时,有 A[i,j]+A[i+k,j+1]<=A[i,j+1]+A[i+k,j]成立,则当 m=k+1 时,有

A[i,j]+A[i+1,j+1]<=A[i,j+1]+A[i+1,j] (1)

A[i,j]+A[i+k,j+1]<=A[i,j+1]+A[i+k,j] (2)

两式成立。

将两式相加并化简记可以得到:A[i,j]+A[i+k+1,j+1]<=A[i,j+1]+A[i+k+1,j] (3)

下面对列进行归纳:

当 n=1 时,有(3)式成立。设当 n=l 时,有 A[i,j]+A[k,l]<=A[i,l]+A[k,j]成立。

则当 n=l+1 时,有

A[i,l]+A[k,l]<=A[I,l+1]+A[k,j] (4)

A[i,j]+A[k,l]<=A[i,l]+A[k,j] (5)

两式成立。

两式相加,既有 A[i,j]+A[k,l+1]<=A[i,l+1]+A[k,j]成立

固由以上归纳证明可知,结论成立。

/*以下另一个同学的证明,仅供大家参考

a. 充分条件:

显然,如果一个矩阵为 Monge 矩阵,那么由定义可以得到:A[i,j]+A[i+1,j+1]≤A[i,j+1]+A[i+1,j]

必要条件:

[为了讨论方便,我们称 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].为定义条件组]

用归纳法来证明:首先根据定义,Monge 矩阵的最小规模也是一个 2×2 的矩阵。所以,对于

基本情况,即 m=2,n=2 时,如果有递归条件组成立,即 A[i, j] + A[i + 1, j + 1] ≤ A[i, j + 1] + A[i + 1, j]

成立,此时,递归条件组中只有一个不等式,即 A[1, 1] + A[2,2] ≤ A[1, 2] + A[2, 1]成立,根据定义,

此时有递归条件组成立,所以在基本情况中,满足递归条件组的矩阵是一个 Monge 矩阵。

我们再对行和列分别进行归纳证明。(在这里我们只对行进行归纳,列的归纳证明完全相同,

这里不再赘述)

假设:当 m=p,n=q 时,即在一个 p×q 规模的矩阵下,满足递归条件组的矩阵也满足定义条件

组,即在一个 p×q 规模的矩阵下,满足递归条件组的矩阵是一个 Monge 矩阵。

此时需要我们证明:在 p+1 ×q 规模的矩阵下,满足递归条件组的矩阵也满足定义条件组,即

在一个 p+1 ×q 规模的矩阵下,满足递归条件组的矩阵是一个 Monge 矩阵。

我们用数学语言表述一下:

往证:对所有的 i,j,k ,l 1≤i<k≤p+1, 1≤j<l<q 有 A[i, j] + A[k, l] ≤ A[i, l] + A[k, j]成立。我们拥有的条件

是:对所有的 i,j,k ,l 1≤i<k≤p, 1≤j<l<q 有 A[i, j] + A[k, l] ≤ A[i, l] + A[k, j]成立(记为条件 1),并且,

对 i=1,2,„„p,1≤j<l<q,都有 A[i, j] + A[i + 1, l] ≤ A[i, l] + A[i + 1, j]成立。(记为条件 2)

我们下面来分析一下条件:对于往证的不等式我们需要证明:当 1≤j<l<q,i=1, 2≤k≤p+1,且 i=2,

3≤k≤p+1„„且 i=p,k=p+1 时,A[i, j] + A[k, l] ≤ A[i, l] + A[k, j]成立。而在条件 1 中,提供了,当 1≤j<l<q,

i=1, 2≤k≤p,且 i=2 , 3≤k≤p„„且 i=p-1,k=p 时,A[i, j] + A[k, l] ≤ A[i, l] + A[k, j]成立。所以,进一步地,

我们只需证明,当 1≤j<l<q,1≤i≤p, k=p+1 时,A[i, j] + A[k, l] ≤ A[i, l] + A[k, j]成立即可。下面给出证

明:

1. 当 i=p 时,根据条件 2,我们有 A[p, j] +A[p + 1 , l] ≤ A[p, l] + A[p + 1, j]

2. 当 1≤i≤p-1,k=p 时,根据条件 1,我们有 A[i, j] + A[p, l] ≤ A[i, l] + A[p, j] 将此式与 1 中不等

式相加得:A[i, j] +A[p + 1, l] ≤ A[i, l] + A[p + 1, j]

根据以上两条,即可证明:当 1≤j<l<q,1≤i≤p, k=p+1 时,A[i, j] + A[k, l] ≤ A[i, l] + A[k, j]成立。

所以,得证!*/

b. 可以将第一行第三列的 22 改为 24。

c. 假设对于第 i 行(i≥2)的最左端最小值的列号小于 i-1 的列号,则有

A[i-1,f(i)]+A[I,f(i-1)]> A[i-1,f(i-1)]+ A[i,f(i)]成立,这是因为 A[i-1,f(i-1)]+ A[i,f(i)]都是本行最小的

元素,故其与 Monge 矩阵相矛盾。

因此对于任意的 i(1≤i<m)有 f(i) ≤f(i+1),故有结论成立。

d. 由 c 中可知,对于每一行最左端的最小值的列号有 f(1) ≤f(2) ≤……≤f(m)

若已知 f(2k),f(2k+2),则只要搜索两者之间的列即可得到 2k+1 的最小值。搜索的总次数为

(f(2)-0+1)+(f(4)-f(2)+1)+……+(f(m)-f(m-2)+1)=m+n-2 (m 为奇数)

(f(2)-0+1)+(f(4)-f(2)+1)+……+(n-f(m)+1)=m+n-2 (m 为偶数)

故得证

e.其运行时间的递归式为:T(m)=T(m/2)+O(m+n)

T(m) ≤T(m/2)+O(m+n) ≤T(m/4)+ O(m/2+n)+ O(m+n)≤T(m/8)+ O(m/4+n) +O(m/2+n)+

O(m+n) ≤ …… ≤ O(m+n)+ O(m/2+n) +O(m/4+n)+……+ O(m+n)( 共 有 了 lgm

个)<k1m+k2n*lgm

故其运行时间为 O(m+nlgm)

Exercises 5.2-1 (江周)

In HIRE-ASSISTANT, assuming that the candidates are presented in a random order, what is

the probability that you will hire exactly one time? What is the probability that you will

hire exactly n times?

第一种想法:由于已经假设应聘者以随机的顺序出现,所以前 i 个也是以随机的顺序出现。

这些前 i 个应聘者中的任何一个都等可能地是目前最有资格。应聘者 i 比应聘者 1 到 i-1 都没

资格的概率是 i-1/i,因此也以 1/i 的概率被雇用。

所以正好雇一个的概率为:1*1/2*2/3*3/4*„„*n-1/n=1/n.

而正好雇用 n 个的概率为:1*1/2*1/3*„„*1/n=1/n!.

另一想法:n 个人随即出现,所以对他们进行全排,有 n!种排法,而我们需要只雇佣一次,

所以把最好那个排第一,其余 n-1 个人进行全排,共有(n-1)!种排法,所以只雇一次的概率

为(n-1)!/n! 得 1/n。

而雇用 n 次需要各个应聘者严格地按照水平递增的顺序排列,这种情况只占所有 n!种情况

的 1 种,所以是 1/n!.

Exercises 5.2-3 (class two 刘浩)

Use indicator random variables to compute the expected value of the sum of n dice.

每一次投骰子:E(xi) = 1/6 * (1 + 2 + 3 + 4 + 5 + 6) = 7/2

则投 n 次骰子总和的期望值为:

E X = E Xi

n

i=1

= n × 7/2 = 7n/2

Exercises 5.2-4 (class five 王晓川)

Use indicator random variables to solve the following problem, which is known as the

hat-check problem. Each of n customers gives a hat to a hat-check person at a restaurant.

The hat-check person gives the hats back to the customers in a random order. What is the

expected number of customers that get back their own hat?

Let Xi=IThe event that people get the proper hat

Then IA is the indicator random variable.

Make X=X1+X2+...+Xn

E X = E Xi = E Xi ; i = 1,2, . . . n

E[Xi]=1/n

Thus E[X]=n×1/n=1,the average number is 1.

Exercises 5.3-4 (class five 高晨)

Professor Armstrong suggests the following procedure for generating a uniform random

permutation:

PERMUTE-BY-CYCLIC(A)

1 n ← length*A+

2 offset ← RANDOM(1, n)

3 for i ← 1 to n

4 do dest ← i + offset

5 if dest > n

6 then dest ← dest -n

7 B*dest+ ← A*i+

8 return B

Show that each element A[i] has a 1/n probability of winding up in any particular position

in B. Then show that Professor Armstrong is mistaken by showing that the resulting

permutation is not uniformly random.

在从 1 到 n 的数之中,dest与 offest 一一对应。

由于 offset在 1 到 n之中任意数 i的概率是 P(i)=1/n,因此 dest在 1 到 n中取任意数的概

率是 1/n,所以对于 i 确定的 A[i],A[i]在 B 中的任意位置的概率是 1/n.但是这种算法产生

的 B 仅有 n 中可能的序列,每种序列的概率是 1/n,而不是 1/n!,这显然不是随机序列。

Chapter fifteen Exercises 15.1-1

Show how to modify the PRINT-STATIONS procedure to print out the stations in increasing

order of station number. (Hint: Use recursion.)

PRINT-STATIONS(l, j, n)

i ← l*

if j=0

then return

else

if(j>1) i ← li[j]

PRINT-STATION(l, j-1, n)

if j = n

then i ← l*

print "line" i ", station" j

else i ← li[j+1]

print "line" i ", station" j

Exercises 15.1-2 (class two 刘浩)

Use equations (15.8) and (15.9) and the substitution method to show that ri(j), the number

of references made to fi[j] in a recursive algorithm, equals 2n - j.

1.j = n 时,由(15.8), ri (j) = 1 = 2 n-n 成立

2.假设当 j = k 时, ri (k) = 2 n-k

当 j = k – 1 时

ri (k-1) = r1 (k) + r2(k) = 2 n-k + 2 n-k = 2 n-(k-1) 成立

3.综上,得证

Exercises 15.1-4 (class two 刘浩)

Together, the tables containing fi[j] and li[j] values contain a total of 4n - 2 entries. Show

how to reduce the space requirements to a total of 2n + 2 entries, while still computing f*

and still being able to print all the stations on a fastest way through the factory.

原来 fi[j]含有 2n 个表项 ,li[j]含有 2n-2 个表项,li[j]保持不变,而将 fi[j]改为四个表项,两个

记录 fi[j - 1],两个记录 fi[j ]。因为每次求 fi[j ]只需知道上一步的 fi 值,没有必要将所有 i 的

fi 值都记录。这样,空间需求便缩减到了 2n+2,而仍能输出通过工厂的最快路线上的所有

装配站。

Exercises 15.1-5

Professor Canty conjectures that there might exist some ei, ai,j, and ti,j values for which

FASTEST-WAY produces li[j] values such that l1[j] = 2 and l2[j] = 1 for some station number j.

Assuming that all transfer costs ti,j are nonnegative, show that the professor is wrong.

证明:如果 l1[j]=2,则有 f2(j-1)+t2,j-1+a1,j<f1(j-1)+a1,j

同样地,如果 l2[j]=1,则有 f1(j-1) +t2,j-1+a2,j< f2 (j-1) +a2,j

两式相加:t2,j-1 +t2,j-1<0.题目中说,ti,j都非负,所以矛盾,得证!

Exercises 15.2-1

Find an optimal parenthesization of a matrix-chain product whose sequence of dimensions

is 〈5, 10, 3, 12, 5, 50, 6〉.

大家看这两个表格应该就明白了:最后答案是(AB)((CD)(EF))

1 2 3 4 5 6

1 0 150 330 405 1655 2010

2 0 360 330 2430 1950

3 0 180 930 1770

4 0 3000 1860

5 0 1500

6 0

1 2 3 4 5 6

1 0 1 2 2 4 2

2 0 2 2 2 2

3 0 3 4 4

4 0 4 4

5 0 5

6 0

Exercises 15.2-3 (class five 高晨)

Use the substitution method to show that the solution to the recurrence (15.11) is Ω(2n).

当 n=1 时,有 P(n)=1 成立

现在假设当任意的 k<n 都成立,即 P(k)≥c2k, 则有

P n = P k P n − k ≥ c

n−1

k=1

n−1

k =1

2k c2n−k = c2 (n− 1)2n

由此,对于任意给定的 c 总存在一个 N0=1

c+ 1,使得当 n> N0时,有P n ≥ c2n

所以,得证

Exercises 15.3-1 (class six 徐晓鹏)

Which is a more efficient way to determine the optimal number of multiplications in a

matrix-chain multiplication problem: enumerating all the ways of parenthesizing the

product and computing the number of multiplications for each, or running

RECURSIVE-MATRIX-CHAIN? Justify your answer.

用枚举法和运行 RECURSIVE-MATRIX-CHAIN 来计算矩阵列乘法中的最优次数的效率是一样的。

首先,使用枚举法时,计算其全部括号的重数。设 P(n)表示一串 n 个矩阵可能的加全部括号

的方案数。当 n=1 时,只有一个矩阵,因此只有一种方式来加全部括号矩阵乘积。当 n≥2 时,

一个加全部括号的矩阵乘积,就是两个加全部括号的矩阵子乘积的乘积,而且这两个子乘积

之间的分裂可能发生在第 k 个和第(k+1)个矩阵之间,其中 k=1,2,„,n-1。因此可得

递归式

P(n)=

1 if n=1

∑P(k)+P(n-k) if n≥2

可以计算出,解的个数是 n 的指数形式。所以用枚举法时,计算效率是 n 的指数形式。

其次,运行 RECURSIVE-MATRIX-CHAIN 来计算时, RECURSIVE-MATRIX-CHAIN 重复调用子

问题,实际上与枚举法相比并未提高计算效率,在计算所有可能性的过程中,重复计算了

子问题。所以得到的递归式为:

T(1) ≥1 T(n) ≥1+∑(T(k)+T(n-k)+1) for n>1

根据计算容易证明:

T(n) ≥2n-1

所以调用 RECURSIVE-MATRIX-CHAIN(p,1,n)的工作总量至少为 n 的指数形式。

综上所述,使用两种方法的工作量都是 n 的指数形式的。所以两种算法的效率相同,都是

低效率的算法。

/*以下是另一个同学的答案,仅供参考

使用枚举法时,计算其全部括号的重数。设 P(n)表示一串 n 个矩阵可能的加全部括号的方

案数。当 n=1 时,只有一个矩阵,因此只有一种方式来加全部括号矩阵乘积。当 n≥2 时,

一个加全部括号的矩阵乘积,就是两个加全部括号的矩阵子乘积的乘积,而且这两个子乘积

之间的分裂可能发生在第 k 个和第(k+1)个矩阵之间,其中 k=1,2,„,n-1。因此可得递

归式

𝑃 𝑛 =

1 𝑛 = 1

𝑃 𝑘 𝑃 𝑛 − 𝑘 𝑛 ≥ 2

𝑛−1

𝑘=1

书上给出了这个算法的一个下界:𝛺(4𝑛

𝑛3

2

) (见书:中文版 198 页,英文版 333 页)

另一方面:用递归的方法时:有如下递归公式:

T(1)=C0 (C0 是一个常数,对应英文版书上 345 页算法中第 1,2 行)

𝑇 𝑛 = 𝐶0 + 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 + 𝐶1 𝑛 ≥ 2

𝑛−1

𝑘=1

(其中,C1 是算法中 6,7 行所需要的常数时间)

书上给出了它的一个下界 T(n)= 𝛺(2𝑛 ),在这里,我们给出它的一个上界:T(n)=O(3n)

我们用替换法给出他的一个简单的证明:

要证明此上界我们需要一个比较强的假设条件:

T(n) ≤Cm(3n-1)/2, 其中 Cm=maxC0,C1

1. T(1)=C0 Cm(31 -1)/2=Cm, 成立!

2. 假设 T(s) ≤Cm(3s-1)/2,对任意 s≤n,都成立,则有:

T(n) ≤ Cm + (T k + T n− k + Cm )

n−1

k=1

≤ Cm + Cm (3k − 1

2+

(3n−k − 1)

2+ 1)

n−1

k=1

≤ Cm(1 +1

2 (3k + 3n−k )

n−1

k=1

) = Cm(1 + 3k

n−1

k=1

) = Cm 3k

n−1

k =0

= Cm

1 − 3n

1 − 3= Cm(3n− 1)/2

成立!

综上,我们证明了用递归的方法所用时间的上界:T(n)=O(3n),同时,穷举法所用时间的下

界为:𝛺(4𝑛

𝑛32

).

注意到:

𝑙𝑖𝑚𝑛→∞

3𝑛

4𝑛

𝑛32

= 0

, 由此就说明了递归方法的效率高!

*/

Exercises 15.3-3 (class four 肖咚)

Consider a variant of the matrix-chain multiplication problem in which the goal is to

parenthesize the sequence of matrices so as to maximize, rather than minimize, the

number of scalar multiplications. Does this problem exhibit optimal substructure?

这个问题同样具有最优子结构。下面进行分析:

采用动态规划法的第一步就是寻找最优子结构,然后利用这一子结构,就可以根据子问题的

最优解结构造出原问题的一个最优解。

用记号 Ai…j表示对乘积 AiAi+1„A j的求积结果,其中 i<j.

这个问题的最优子结构如下:假设 AiAi+1„Aj的一个最优加全部括号的乘法把乘积 Ak 与 Ak+1

之间分开,则对 AiAi+1„Aj 最优加全部括号的“前缀”子链 AiA i+1„Ak 的加全部括号必须是

AiAi+1„AK 的一个最优加全部括号,因为如果对 AiAi+1„Ak有一个代价更大的加全部括号,那

么把它替换到 AiAi+1„Aj的最优加全部括号中就会产生 AiAi+1„Aj的另一种加全部括号,而它

会具有更大的代价,产生矛盾。所以判断这个问题同样具有最优子结构。

详细的解法可以参考书上 P199 的分析和算法设计。

Exercises 15.3-4 (class two 刘浩 class five 王健)

Describe how assembly-line scheduling has overlapping subproblems.

[刘浩:] 由(15.8)和(15.9)可知,在装配线调度问题的递归算法中,对 fi [j]的引用次数为 2 n-j ,由

此可知每个子问题的重复的次数.当计算一个子问题 fi [j]时, 将引用 fi [m](m < j)的所有子问题,

这样一来,装配线调度问题自然就有了重叠子问题,而且很严重.

[王健:] 适用于动态规划求解的最优化问题其子问题的空间要很小,也就是说用来解原问题

的递归算法可反复地解同样的子问题,而不是总在产生新的子问题。当一个递归算法不断地

调用同一个问题时,我们说该最优问题包含重叠子问题。

我们要求解到达 S1, j 最快路线,必须先知道到达 S1, j-1 和 S2, j-1 的最快路线,以此类推,

要使到达某一个装配站的路线最快,必须使到达它前一个装配站的路线最快,所以在求解到

达每个装配站的最快路线时,必须重复求解到达某些装配站的最快路线,这就是重复子问题。

如果我们在求解装配线调度问题时采用开销比较大的递归算法,就会在递归树中看到,某些

值被求解了多次,也正是这个原因使得此种算法拥有指数级的时间复杂度。

所以装配线调度问题比较适合用动态规划求解,即将重复子问题的解存于表中以便后面调用,

避免重复计算。

Exercises 15.4-1 (class six 彭运)

Determine an LCS of 〈1, 0, 0, 1, 0, 1, 0, 1〉 and 〈0, 1, 0, 1, 1, 0, 1, 1, 0〉.

The LCS is <1,0,0,1,1,0>

(注:没有标箭头的数字,就是对应书中指向左上角的箭头。红色的数字是 LCS。)

Yi 0 1 0 1 1 0 1 1 0

Xi 0 0 0 0 0 0 0 0 0 0

1 0 ↑0 1 ←1 1 1 ←1 1 1 ←1

0 0 1 ↑1 2 ←2 ←2 2 ←2 ←2 2

0 0 1 ↑1 2 ↑2 ↑2 3 ←3 ←3 3

1 0 ↑1 2 ↑2 3 3 ↑3 4 4 ←4

0 0 1 ↑2 3 ↑3 ↑3 4 ↑4 ↑4 5

1 0 ↑1 2 ↑3 4 4 ↑4 5 5 ↑5

0 0 1 2 3 ↑4 ↑4 5 ↑5 ↑5 6

1 0 ↑1 2 ↑3 4 5 ↑6 6 6 ↑6

Exercises 15.4-5 (class two 刘浩)

Give an O(n2)-time algorithm to find the longest monotonically increasing subsequence of

a sequence of n numbers.

设原序列为 A,首先将序列 A 进行递增排序,可在Θ (n lg n)时间完成,得到序列 B,然后求

A 和 B 的最长公共子序列 C,可在 O(n2)时间完成,而 C 即为 A 的最长的单调递增子序列。

Exercises 15.5-1(class four 董延昊)

Write pseudocode for the procedure CONSTRUCT-OPTIMAL-BST(root) which, given the

table root, outputs the structure of an optimal binary search tree. For the example in

Figure 15.8, your procedure should print out the structure

k2 is the root

k1 is the left child of k2

d0 is the left child of k1

d1 is the right child of k1

k5 is the right child of k2

k4 is the left child of k5

k3 is the left child of k4

d2 is the left child of k3

d3 is the right child of k3

d4 is the right child of k4

d5 is the right child of k5

corresponding to the optimal binary search tree shown in Figure 15.7(b).

可给出如下算法:

CONSTRUCT-OPTIAML-BST(root, i, j)

if(i==1&&j==n)

print root[i, j] is the root

if (i<j)

print root[i, root[i,j]-1] is the left child of Kroot[i, j]

CONSTRUCT-OPTIAML-BST(root, i, root[i, j]-1)

print root[root[i,j]+1, j] is the right child of K root[i,j]

CONSTRUCT-OPTIAML-BST(root, root[i, j], j)

if(i==j)

print di-1 is the left child of K i

print di is the right child of K i

if(i>j)

print dj is the right child of K j

Chapter sixteen

Exercises 16.1-2 (class six 徐晓鹏)

Suppose that instead of always selecting the first activity to finish, we instead select the

last activity to start that is compatible with all previously selected activities. Describe how

this approach is a greedy algorithm, and prove that it yields an optimal solution.

证明:对于任意非空子问题 Sij,设 am 是 Sij 中具有最迟开始时间的活动

sm=maxSk,ak∈Sij

那么,1)活动 am在 Si j 的某最大兼容子活动中被使用

2)子问题 Smj 为空,所以选择 am 将使子问题 Sim 为唯一可能非空的问题

首先,证明 2)。假设 Smj 非空,则有活动 ak 将满足

Sm<fm<=Sk<fk<=Sj

ak 同时也在 Sij 中,且具有比 am 更晚的开始时间,这与 am 的选择相矛盾,所以推出 Smj 为空。

然后,证明 1)。设 Aij 为 Si j 的最大兼容活动子集,且将 Ai j 中的活动按开始时间单调递增排

序,又设 ak 为 Aij 的最后一个活动,如果 ak=am,则得到结论。如果 ak≠am,则构造子集

A'ij=Aij-ak∪am,因为在 Aij中 ak是最后一个活动,而 sm>=sk,所以 A’ij中的活动是

不相交的,此时 Aij 与 A’ij 的活动数目相同,因此 A’ij 是包含 am的 Si j 的最大兼容活动的

集合。

Exercises 16.2-2 (class two 刘浩)

Give a dynamic-programming solution to the 0-1 knapsack problem that runs in O(n W)

time, where n is number of items and W is the maximum weight of items that the thief can

put in his knapsack.

Optimal substructure: 背包的最大负荷 W,有 n 件物品,最优解 S 中的最大项数为 i ,则

S’=S-i必定是最大负荷为 W-wi,物品项为 1,…,i-1 的最优解,且 v(S)=v(S’)+vi

c[i,w]: 物品项 1,…,i,最大负荷 w 时的最优值

输入:W, n, v = <v1, v2, . . . , vn>, w = <w1, w2, . . . ,wn>

储存 c[i, j ] 在表 c[0 . . n, 0 . .W]中(行优先), c[n,W] 为原始最大值

0 if 0 or 0,

[ , ] [ 1, ] if ,

max( [ 1, ], [ 1, ]) if 0 and .

i

i i i

i w

c i w c i w w w

v c i w w c i w i w w

Tracing 最优解:If c[i,w]=c[i-1,w], item i is !∈最优解,继续 tracing with c[i-1,w], else item i ∈

最优解,继续 tracing with c[i-1,w-wi].

Exercises 16.3-2 (Class four 陆璐)

What is an optimal Huffman code for the following set of frequencies, based on the first 8

Fibonacci numbers?

a:1 b:1 c:2 d:3 e:5 f:8 g:13 h:21

Can you generalize your answer to find the optimal code when the frequencies are the first

n Fibonacci numbers?

可根据课本算法做出下图

命题:对任何的连续 Fibonacci 数列总可以构造出如下的哈夫曼编码树

a:1111111

b:1111110

c:111110

d:11110

e:1110

f:110

g:10

h:0

b : 1 a : 1

2 c : 2

d : 3 4

e : 5 7

f : 8 12

g : 13 20

23

44

h : 21

下面可以对其可推广性进行证明:

我们只需要证明:对任意的 n≥1,总有下列式子成立:

1 𝑓 𝑛 + 1 ≤ 𝑓 𝑖 ≤ 𝑓 𝑛 + 2

𝑛

𝑖=1

2 f i = f n + 2 − 1

n

i=1

我在这里只证明第一个不等式,第二个同理可以证明。

证明:用数学归纳法证明。对 n=1 时,显然 f(2)≤f(1) ≤f(3)

假设,当 n=k(k>1)时,(1)式成立, 即

𝑓 𝑘 + 1 ≤ 𝑓 𝑖 ≤ 𝑓 𝑘 + 2

𝑘

𝑖=1

成立,那么当 n = k + 1 时

𝑓 𝑘 + 2 = 𝑓 𝑘 + 𝑓 𝑘 + 1 ≤ 𝑓 𝑘 + 1 + 𝑓 𝑘 + 1

≤ 𝑓 𝑖 + 𝑓 𝑘 + 1 ≤ 𝑓 𝑘 + 2 + 𝑓 𝑘 + 1

𝑘

𝑖=1

= 𝑓 𝑘 + 3

由此,便证明了

𝑓 𝑘 + 2 ≤ 𝑓 𝑖 ≤

𝑘+1

𝑖=1

𝑓 𝑘 + 3

所以,得证!

Chapter thirty

Exercises 30.1-2 (class five 高晨)

Evaluating a polynomial A(x) of degree-bound n at a given point x0 can also be done by

dividing A(x) by the polynomial (x - x0) to obtain a quotient polynomial q(x) of

degree-bound n - 1 and a remainder r, such that

A(x) = q(x)(x - x0) + r.

Clearly, A(x0) = r. Show how to compute the remainder r and the coefficients of q(x) in time

Θ(n) from x0 and the coefficients of A.

解:这个题乍一看比较绕,其实还是很简单的,下面来分析一下:

既然已经知道了 x0 和 A 的系数,那么不妨设 A(x)=a0+a1×x+a2×x2+……+an-1×xn-1; 同理,我们

可以设 q(x)=b0+b1 ×x+b2×x2+……+bn-2×xn-2(显然 q(x)的最高次项系数只有 xn-2).将 q(x)与(x-x0)相

乘,然后合并同类项就可以很简单地得到以下关系式:

bn-2=an-1 ------(1)

bn-3-bn-2×x0=an-2 -------(2)

bn-4-bn-3×x0=an-3 ------(3)

……

r-b0×x0=a0 ------(n)

需要说明的是,r 是一个数而不是一个表达式,这个可以通过多项式的除法的性质得到,因

为其因子是(x-x0),是一次项的,所以 r 肯定是一个数。题中也说了 A(x0)=r,可见 r 是一个数。

将这 n 个式子从上向下一次计算就可以计算出 r 及 q(x)中各项的系数。最后来分析验证一下

这个算法需要的时间复杂度:

将 q(x)=b0+b1 ×x+b2×x2+……+bn-2×xn-2 与(x-x0)相乘共需要 2×(n-1)次;计算式子(1)到式子(n)共

需要 n 次加法和 n-1 次乘法,合起来是 4n-3 次,所以总的时间复杂度是Θ (n)的,满足题目

要求,此问题得解。

Chapter thirty-four

要会判断合取范式的可满足性