算法导论 课后题答案

算法导论 课后题答案
算法导论 课后题答案

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

shao_wen_buaa@https://www.360docs.net/doc/a318384256.html,

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

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<8ln 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

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)

1for j ← 2 to length*A+

2do key ← A*j+

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

4i ← j – 1

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

6d o A*i+1+ ← A*i+

7i ← j – 1

8A*i+1+ ← key

图解:

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

T n=

2if n=2

2T

n

+n if n=2k,for k>1

is T(n)=nlgn.

由题意得:

根据数学归纳法:

给出基本条件(base case):

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

给出假设:

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

那么,

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

= 2T(2t)+2t+1

= 2(2t lg2t)+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)

9for j ←2 to length[a]

10do key ←a[j]

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

12high ←j–1

13low ←1

14while low < high

15mid = ( low + high ) / 2

16If key == A[mid] then

17break

18if key < A[mid] then

19high ←mid - 1

20if key > A[mid] then

21Low ←mid + 1

22for i ←mid to j – 1

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

24A[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

×Θ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

+cn, n>k

其中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) A′1≤A′2≤?≤A′n

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.

https://www.360docs.net/doc/a318384256.html,ing 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)都是渐近非负的函数,所以根据定义有:存在N f,N g,使得:当n>N f时,f(n)≥0,同时,当n> N g时,g(n)≥0.所以,我们取N0=max{N f,N g},此时,当n>N0时,同时有f(n)≥0,g(n)≥0.下面我们取C1=1/2,C2=1,根据f(n),g(n)的非负性保证,当n>N0时,有:

f n+g(n)

≤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) 因为2n+1= 2×2n≤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时有:C0g n≤f n≤C1g(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≤C1g(n),同样地,存在N2, C0使得:当n> N2时有C0g n≤f n,此时,我取N0=max(N1, N2),则有如下事实成立:当n> N0时有:C0g n≤f n≤C1g(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 (f(n)/g(n))=0

lim

n→∞

But the definition ofω, we have

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

lim

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

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

) + 1

2

= 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

=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 T n=2T n+1by 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(2m/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

2

n+(

3

2

)2n+?…+(

3

2

)lgnn=n(

3

2

)i

lgn

i=0

=n

1?(32)lgn

1?

3

2

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

= 2·3lgn-2n

=Θ(n lg3)

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

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. V erify 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) =Θ(n2lg 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)= Θ(n lg7),我们分类讨论a的取值:

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

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

a>16时,T’(n)= Θ(n lg 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.)

解:n log b 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(n) + 1.

Θ

a)()3n

Θ

b)()n

c) ()2lg n n Θ d) ()2n Θ

e) ()

7

2log n Θ

f) )

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) = n T( n ) + n.

d).(lg )n n θ f).(),8n c θ≥

i).

3

2(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

我们下面来分析一下条件:对于往证的不等式我们需要证明:当1≤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

所以,得证!*/

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

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 个)

故其运行时间为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(x i) = 1/6 * (1 + 2 + 3 + 4 + 5 + 6) = 7/2

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

n

E X=E X i

= n × 7/2 = 7n/2

i=1

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 X i=I{The event that people get the proper hat}

Then I{A} is the indicator random variable.

Make X=X1+X2+...+X n

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

E[X i]=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← l i[j]

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

if j = n

then i← l*

print "line" i ", station" j

else i← l i[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), r i (j) = 1 = 2 n-n 成立

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

当j = k – 1时

r i (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.

原来f i[j]含有2n个表项,l i[j]含有2n-2个表项,l i[j]保持不变,而将f i[j]改为四个表项,两个记录f i[j - 1],两个记录f i[j ]。因为每次求f i[j ]只需知道上一步的f i 值,没有必要将所有i的f i 值都记录。这样,空间需求便缩减到了2n+2,而仍能输出通过工厂的最快路线上的所有装配站。

Exercises 15.1-5

Professor Canty conjectures that there might exist some ei, ai,j, and ti,j values for which FASTEST-WA Y 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

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

两式相加:t2,j-1 +t2,j-1<0.题目中说,t i,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))

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

P n = P k P n ?k ≥ c n ?1

k=1

n ?1

k =1

2k c2n ?k =c 2(n ?1)2n

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

c +1,使得当n> N 0时,有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) ≥2

n-1

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

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

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

使用枚举法时,计算其全部括号的重数。设P(n)表示一串n 个矩阵可能的加全部括号的方案数。当n=1 时,只有一个矩阵,因此只有一种方式来加全部括号矩阵乘积。当n ≥2 时,一个加全部括号的矩阵乘积,就是两个加全部括号的矩阵子乘积的乘积,而且这两个子乘积之间的分裂可能发生在第k 个和第(k+1)个矩阵之间,其中k=1,2,…,n-1。因此可得递归式

P n = 1 n =1

P k P n ?k n ≥2

n ?1

k =1

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

n 32

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

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

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

T n =C 0+ T k +T n ?k +C 1 n ≥2n ?1

k =1

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

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

)

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

要证明此上界我们需要一个比较强的假设条件: T(n) ≤C m (3n -1)/2, 其中C m =max{C 0,C 1}

1. T(1)=C 0C m (31-1)/2=C m , 成立!

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

T(n) ≤Cm + (T k +T n ?k +C m )n ?1

k=1

≤Cm + Cm (3k ?1+(3n ?k ?1)

+ 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

=C m 1?3n

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

成立!

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

n 32).

注意到:

算法导论 第三版 第21章 答案 英

Chapter21 Michelle Bodnar,Andrew Lohr April12,2016 Exercise21.1-1 EdgeP rocessed initial{a}{b}{c}{d}{e}{f}{g}{h}{i}{j}{k} (d,i){a}{b}{c}{d,i}{e}{f}{g}{h}{j}{k} (f,k){a}{b}{c}{d,i}{e}{f,k}{g}{h}{j} (g,i){a}{b}{c}{d,i,g}{e}{f,k}{h}{j} (b,g){a}{b,d,i,g}{c}{e}{f,k}{h}{j} (a,h){a,h}{b,d,i,g}{c}{e}{f,k}{j} (i,j){a,h}{b,d,i,g,j}{c}{e}{f,k} (d,k){a,h}{b,d,i,g,j,f,k}{c}{e} (b,j){a,h}{b,d,i,g,j,f,k}{c}{e} (d,f){a,h}{b,d,i,g,j,f,k}{c}{e} (g,j){a,h}{b,d,i,g,j,f,k}{c}{e} (a,e){a,h,e}{b,d,i,g,j,f,k}{c} So,the connected that we are left with are{a,h,e},{b,d,i,g,j,f,k}, and{c}. Exercise21.1-2 First suppose that two vertices are in the same connected component.Then there exists a path of edges connecting them.If two vertices are connected by a single edge,then they are put into the same set when that edge is processed. At some point during the algorithm every edge of the path will be processed,so all vertices on the path will be in the same set,including the endpoints.Now suppose two vertices u and v wind up in the same set.Since every vertex starts o?in its own set,some sequence of edges in G must have resulted in eventually combining the sets containing u and v.From among these,there must be a path of edges from u to v,implying that u and v are in the same connected component. Exercise21.1-3 Find set is called twice on line4,this is run once per edge in the graph,so, we have that?nd set is run2|E|times.Since we start with|V|sets,at the end 1

算法导论第二章答案

第二章算法入门 由于时间问题有些问题没有写的很仔细,而且估计这里会存在不少不恰当之处。另,思考题2-3 关于霍纳规则,有些部分没有完成,故没把解答写上去,我对其 c 问题有疑问,请有解答方法者提供个意见。 给出的代码目前也仅仅为解决问题,没有做优化,请见谅,等有时间了我再好好修改。 插入排序算法伪代码 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 ←i ? 1 8 A[i+1]←key C#对揑入排序算法的实现: public static void InsertionSort(T[] Input) where T:IComparable { T key; int i; for (int j = 1; j < Input.Length; j++) { key = Input[j]; i = j - 1; for (; i >= 0 && Input[i].CompareTo(key)>0;i-- ) Input[i + 1] = Input[i]; Input[i+1]=key; } } 揑入算法的设计使用的是增量(incremental)方法:在排好子数组A[1..j-1]后,将元素A[ j]揑入,形成排好序的子数组A[1..j] 这里需要注意的是由于大部分编程语言的数组都是从0开始算起,这个不伪代码认为的数组的数是第1个有所丌同,一般要注意有几个关键值要比伪代码的小1. 如果按照大部分计算机编程语言的思路,修改为: INSERTION-SORT(A) 1 for j ← 1 to length[A] 2 do key ←A[j] 3 i ←j-1

算法导论 第三版 第六章 答案 英

Chapter6 Michelle Bodnar,Andrew Lohr December31,2016 Exercise6.1-1 At least2h and at most2h+1?1.Can be seen because a complete binary tree of depth h?1hasΣh?1 i=02i=2h?1elements,and the number of elements in a heap of depth h is between the number for a complete binary tree of depth h?1exclusive and the number in a complete binary tree of depth h inclusive. Exercise6.1-2 Write n=2m?1+k where m is as large as possible.Then the heap consists of a complete binary tree of height m?1,along with k additional leaves along the bottom.The height of the root is the length of the longest simple path to one of these k leaves,which must have length m.It is clear from the way we de?ned m that m= lg n . Exercise6.1-3 If there largest element in the subtee were somewhere other than the root, it has a parent that is in the subtree.So,it is larger than it’s parent,so,the heap property is violated at the parent of the maximum element in the subtree Exercise6.1-4 The smallest element must be a a leaf node.Suppose that node x contains the smallest element and x is not a leaf.Let y denote a child node of x.By the max-heap property,the value of x is greater than or equal to the value of y.Since the elements of the heap are distinct,the inequality is strict.This contradicts the assumption that x contains the smallest element in the heap. Exercise6.1-5 Yes,it is.The index of a child is always greater than the index of the parent, so the heap property is satis?ed at each vertex. 1

算法导论 第三版 第七章 答案 英

Chapter7 Michelle Bodnar,Andrew Lohr April12,2016 Exercise7.1-1 13199512874212611 13199512874212611 13199512874212611 91913512874212611 95131912874212611 95131912874212611 95819121374212611 95871213194212611 95874131912212611 95874131912212611 95874219122113611 95874261221131911 95874261121131912 Exercise7.1-2 If all elements in the array have the same value,PARTITION returns r.To make PARTITION return q= (p+r)/2 when all elements have the same value,modify line4of the algorithm to say this:if A[j]≤x and j(mod2)= (p+1)(mod2).This causes the algorithm to treat half of the instances of the same value to count as less than,and the other half to count as greater than. Exercise7.1-3 The for loop makes exactly r?p iterations,each of which takes at most constant time.The part outside the for loop takes at most constant time.Since r?p is the size of the subarray,PARTITION takes at most time proportional to the size of the subarray it is called on. Exercise7.1-4 To modify QUICKSORT to run in non-increasing order we need only modify line4of PARTITION,changing≤to≥. 1

算法导论 第三版 第二章 答案 英

Chapter2 Michelle Bodnar,Andrew Lohr April12,2016 Exercise2.1-1 314159264158 314159264158 314159264158 263141594158 263141415958 263141415859 Exercise2.1-2 Algorithm1Non-increasing Insertion-Sort(A) 1:for j=2to A.length do 2:key=A[j] 3://Insert A[j]into the sorted sequence A[1..j?1]. 4:i=j?1 5:while i>0and A[i]

算法导论 第三版 第24章 答案 英

Chapter24 Michelle Bodnar,Andrew Lohr April12,2016 Exercise24.1-1 If we change our source to z and use the same ordering of edges to decide what to relax,the d values after successive iterations of relaxation are: s t x y z ∞∞∞∞0 2∞7∞0 25790 25690 24690 Theπvalues are: s t x y z NIL NIL NIL NIL NIL z NIL z NIL NIL z x z s NIL z x y s NIL z x y s NIL Now,if we change the weight of edge(z,x)to4and rerun with s as the source,we have that the d values after successive iterations of relaxation are: s t x y z 0∞∞∞∞ 06∞7∞ 06472 02472 0247?2 Theπvalues are: s t x y z NIL NIL NIL NIL NIL NIL s NIL s NIL NIL s y s t NIL x y s t NIL x y s t 1

Note that these values are exactly the same as in the worked example.The di?erence that changing this edge will cause is that there is now a negative weight cycle,which will be detected when it considers the edge(z,x)in the for loop on line5.Since x.d=4>?2+4=z.d+w(z,x),it will return false on line7. Exercise24.1-2 Suppose there is a path from s to v.Then there must be a shortest such path of lengthδ(s,v).It must have?nite length since it contains at most|V|?1 edges and each edge has?nite length.By Lemma24.2,v.d=δ(s,v)<∞upon termination.On the other hand,suppose v.d<∞when BELLMAN-FORD ter-minates.Recall that v.d is monotonically decreasing throughout the algorithm, and RELAX will update v.d only if u.d+w(u,v)

《算法导论2版》课后答案_加强版2

1 算法导论第三次习题课 2008.12.17

2 19.1-1 如果x 是根节点:degree[sibling[x]] > sibling[x] 如果x 不是根节点:degree[sibling[x]] = sibling[x] –119.1-3 略

3 19.2-2 过程略( union 操作) 19.2-3 (1)decrease-key (2)extract-min 过程略 19.2-6 算法思想:找到堆中最小值min ,用min-1代替-∞. [遍历堆中树的根节点]

4 15.1-1 15.1-2 略P195 或P329 15.1-4 f i [j]值只依赖f i [j-1]的值,从而可以从2n 压缩为2个。再加上f*、l*、l i [j]。 Print-station(l, I, j ) //i 为线路,j 为station if j>1 then Print-station(l, l i [j], j-1 ) print “line”I, “station”j;

5 15.2-1 略(见课本) 15.2-2 15.2-4 略 MATRIX-CHAIN-MULTIPLY(A, s, i, j) if j>i x= MATRIX-CHAIN-MULTIPLY(A, s, s(i,j), j) y= MATRIX-CHAIN-MULTIPLY(A, s, s(i,j)+1,j) return MATRIX-MULTIPLY(x, y) else return A i

6 15.3-1 (归纳法)递归调用 枚举15.3-2 没有效果,没有重叠子问题 15.3-4 略 (3)n Ο3/2(4/) n n Θ

算法导论 第三版 第十九章 答案 英

Chapter19 Michelle Bodnar,Andrew Lohr April12,2016 Exercise19.2-1 First,we take the subtrees rooted at24,17,and23and add them to the root list.Then,we set H.min to18.Then,we run consolidate.First this has its degree2set to the subtree rooted at18.Then the degree1is the subtree rooted at38.Then,we get a repeated subtree of degree2when we consider the one rooted at24.So,we make it a subheap by placing the24node under18. Then,we consider the heap rooted at17.This is a repeat for heaps of degree1, so we place the heap rooted https://www.360docs.net/doc/a318384256.html,stly we consider the heap rooted at23,and then we have that all the di?erent heaps have distinct degrees and are done,setting H.min to the smallest,that is,the one rooted at17. The three heaps that we end up with in our root list are: 23 17 38 30 41 and 1

算法导论习题答案

Chapter2 Getting Start 2.1 Insertion sort 2.1.2 将Insertion-Sort 重写为按非递减顺序排序 2.1.3 计算两个n 位的二进制数组之和 2.2 Analyzing algorithms 当前n-1个元素排好序后,第n 个元素已经是最大的元素了. 最好时间和最坏时间均为2()n Θ 2.3 Designing algorithms 2.3.3 计算递归方程的解 22()2(/2)2,1k if n T n T n n if n for k =?=?+ = >? (1) 当1k =时,2n =,显然有()lg T n n n = (2) 假设当k i =时公式成立,即()lg 2lg 22i i i T n n n i ===?, 则当1k i =+,即12i n +=时, 2.3.4 给出insertion sort 的递归版本的递归式 2.3-6 使用二分查找来替代insertion-sort 中while 循环内的线性扫描,是否可以将算法的时间提高到(lg )n n Θ? 虽然用二分查找法可以将查找正确位置的时间复杂度降下来,但

是移位操作的复杂度并没有减少,所以最坏情况下该算法的时间复杂度依然是2()n Θ 2.3-7 给出一个算法,使得其能在(lg )n n Θ的时间内找出在一个n 元素的整数数组内,是否存在两个元素之和为x 首先利用快速排序将数组排序,时间(lg )n n Θ,然后再进行查找: Search(A,n,x) QuickSort(A,n); i←1; j←n; while A[i]+A[j]≠x and i,()()b b n a n +=Θ 0a >时,()()2b b b b n a n n n +<+= 对于121,2b c c ==,12()b b b c n n a c n <+< 0a <时,()b b n a n +<

Ch10算法导论 第三版 第十章 答案 英

Chapter10 Michelle Bodnar,Andrew Lohr April12,2016 Exercise10.1-1 4 41 413 41 418 41 Exercise10.1-2 We will call the stacks T and R.Initially,set T.top=0and R.top=n+1. Essentially,stack T uses the?rst part of the array and stack R uses the last part of the array.In stack T,the top is the rightmost element of T.In stack R, the top is the leftmost element of R. Algorithm1PUSH(S,x) 1:if S==T then 2:if T.top+1==R.top then 3:error“over?ow” 4:else 5:T.top=T.top+1 6:T[T.top]=x 7:end if 8:end if 9:if S==R then 10:if R.top?1==T.top then 11:error“over?ow” 12:else 13:R.top=R.top?1 14:T[T.top]=x 15:end if 16:end if 1

Algorithm2POP(S) if S==T then if T.top==0then error“under?ow” else T.top=T.top?1. return T[T.top+1] end if end if if S==R then if R.top==n+1then error“under?ow” else R.top=R.top+1. return R[R.top?1] end if end if Exercise10.1-3 4 41 413 13 138 38 Exercise10.1-4 Algorithm3ENQUEUE if Q.head==Q.tail+1,or Q.head==1and Q.tail==Q.length then error“over?ow” end if Q[Q.tail]=x if Q.tail==Q.length then Q.tail=1 else Q.tail=Q.head+1 end if Exercise10.1-5 As in the example code given in the section,we will neglect to check for over?ow and under?ow errors. 2

算法导论 第三版 第35章 答案 英

Chapter35 Michelle Bodnar,Andrew Lohr April12,2016 Exercise35.1-1 We could select the graph that consists of only two vertices and a single edge between them.Then,the approximation algorithm will always select both of the vertices,whereas the minimum vertex cover is only one vertex.more generally,we could pick our graph to be k edges on a graph with2k vertices so that each connected component only has a single edge.In these examples,we will have that the approximate solution is o?by a factor of two from the exact one. Exercise35.1-2 It is clear that the edges picked in line4form a matching,since we can only pick edges from E ,and the edges in E are precisely those which don’t share an endpoint with any vertex already in C,and hence with any already-picked edge. Moreover,this matching is maximal because the only edges we don’t include are the ones we removed from E .We did this because they shared an endpoint with an edge we already picked,so if we added it to the matching it would no longer be a matching. Exercise35.1-3 We will construct a bipartite graph with V=R∪L.We will try to construct it so that R is uniform,not that R is a vertex cover.However,we will make it so that the heuristic that the professor(professor who?)suggests will cause us to select all the vertices in L,and show that|L|>2|R|. Initially start o?with|R|=n?xed,and L empty.Then,for each i from 2up to n,we do the following.Let k= n i .Select S a subset of the vertices of R of size ki,and so that all the vertices in R?S have a greater or equal degree.Then,we will add k vertices to L,each of degree i,so that the union of their neighborhoods is S.Note that throughout this process,the furthest apart the degrees of the vertices in R can be is1,because each time we are picking the smallest degree vertices and increasing their degrees by1.So,once this has been done for i=n,we can pick a subset of R whose degree is one less than the rest of R(or all of R if the degrees are all equal),and for each vertex in 1

算法导论 课后题答案

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 shao_wen_buaa@https://www.360docs.net/doc/a318384256.html, 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

算法导论 第三版 第十七章 答案 英

Chapter17 Michelle Bodnar,Andrew Lohr April12,2016 Exercise17.1-1 It woudn’t because we could make an arbitrary sequence of MULT IP USH(k),MULT IP OP(k). The cost of each will beΘ(k),so the average runtime of each will beΘ(k)not O(1). Exercise17.1-2 Suppose the input is a1followed by k?1zeros.If we call DECREMENT we must change k entries.If we then call INCREMENT on this it reverses these k changes.Thus,by calling them alternately n times,the total time isΘ(nk). Exercise17.1-3 Note that this setup is similar to the dynamic tables discussed in section 17.4.Let n be arbitrary,and have the cost of operation i be c(i).Then, n i=1c(i)= lg(n) i=1 2i+ i≤n not a power of2 1≤ lg(n) i=1 2i+n=21+ lg(n) ?1+n≤4n?1+n≤5n∈O(n) So,since to?nd the average,we divide by n,the average runtime of each com-mand is O(1). Exercise17.2-1 To every stack operation,we charge twice.First we charge the actual cost of the stack operation.Second we charge the cost of copying an element later on.Since we have the size of the stack never exceed k,and there are always k operations between backups,we always overpay by at least enough.So,the ammortized cost of the operation is constant.So,the cost of the n operation is O(n). Exercise17.2-2 1

算法导论 第八章答案

8.2-4 :在O(1)的时间内,回答出输入的整数中有多少个落在区间[a...b]内。给出的算法的预处理时间为O(n+k) 算法思想:利用计数排序,由于在计数排序中有一个存储数值个数的临时存储区C[0...k],利用这个数组即可。 #include using namespace std; //通过改编计数排序而来,因为有些部分需要注释掉 void counting_sort(int*&a, int length, int k, int*&b, int*&c); int main() { const int LEN =100; int*a =newint[LEN]; for(int i =0; i < LEN; i++) a[i] = (i -50)*(i -50) +4; int* b =new int[LEN]; const int k =2504; int* c =new int[k +1]; counting_sort(a, LEN, k, b, c); //这里需要注释掉 //for(int i = 0; i < LEN; i++) //cout<>m>>n) { if(m >n) cout<<"区间输入不对"< k && m >0) cout<<"个数为"< k && m <=0) cout<<"个数为"<= 0; i--) { b[c[a[i]] - 1] = a[i]; c[a[i]]--; }*/ } PS:计数排序的总时间为O(k+n),在实践中,如果当k = O(n)时,我们常常采用计数排序,

相关文档
最新文档