《算法导论》第五章-第3节_练习(参考答案)

来源:互联网 发布:swot矩阵分析图 编辑:程序博客网 时间:2024/05/17 09:26

算法导论(第三版)参考答案:练习5.3-1,练习5.3-2,练习5.3-3,练习5.3-4,练习5.3-5,练习5.3-6,练习5.3-7

Exercise 5.3-1

Professor Marceau objects to the loop invariant used in the proof of Lemma 5.5. He questions whether it is true prior to the first iteration. He reasons that we could just as easily declare that an empty subarray contains no 0-permutations. Therefore, the probability that an empty subarray contains a 0-permutation should be 0, thus invalidating the loop invariant prior to the first iteration. Rewrite the procedure RANDOMIZE-IN-PLACE so that its associated loop invariant applies to a nonempty subarray prior to the first iteration, and modify the proof of Lemma 5.5 for your procedure.

Pseudocode

RANDOMIZE-IN-PLACE02(A)  n = A.length  swap A[1] with A[RANDOM(1,n)]  for i = 2 to n    swap A[i] with A[RANDOM(i,n)]

在证明中,初始化部分不再从空数组开始,而是从A[i]开始。根据循环不变式可知,初始化部分成立。

Exercise 5.3-2

Professor Kelp decides to write a procedure that produces at random any permutation besides the identity permutation. He proposes the following procedure:

PERMUTE-WITHOUT-IDENTITY(A)n = A.lengthfor i = 1 to n - 1    swap A[i] with A[RANDOM(i + 1, n)]

Does this code do what Professor Kelp intends?

(中英文版的题意貌似不一样,beside?/ except?,这里参考英文原版)

不能,因为每次置换都不可能出现第i个元素在相同的位置上,只会产生 (n1)! 种不同的情况。

Exercise 5.3-3

Suppose that instead of swapping element A[i] with a random element from the subarray A[in], we swapped it with a random element from anywhere in the array:

PERMUTE-WITH-ALL(A)n = A.lengthfor i = 1 to n    swap A[i] with A[RANDOM(1,n)]

Does this code produce a uniform random permutation? Why or why not?

不能。有 nn 种方式放置元素,同时又有 n! 种排列。获得等同排列的概率是 1nnn! ,不是 1n!

拓展阅读 from Coding Horror

Exercise 5.3-4

Professor Armstrong suggests the following procedure for generating a uniform random permutation:

n = A.lengthlet B[1..n] be a new arrayoffset = RANDOM(1, n)for i = 1 to n    dest = i + offset    if dest > n        dest = dest - n    B[dest] = A[i]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.

很容易理解,A 中每一个元素出现在 B 中的任何位置的概率是 1n, 因为 P{dest}=P{offset}=1/n

这样做只是获得了一个在原数组 A 上的循环置换元素后的数组 B ,并不是均匀随机排列。

Exercise 5.3-5

⋆ Prove that in the array P in procedure PERMUTE-BY-SORTING, the probability that all elements are unique is at least 11/n.

P{j} 代表索引为 j 的元素是唯一的。根据Pseudocode可知:P{j}=1j1n3,于是所有的元素都唯一

P{123}=P{1}P{2|1}P{3|12}=1(11n3)(12n3)(13n3)1(1nn3)(1nn3)(1nn3)(11n2)n11n(1x)n1nx

Exercise 5.3-6

Explain how to implement the algorithm PERMUTE-BY-SORTING to handle the case in which two or more priorities are identical. That is, your algorithm should produce a uniform random permutation, even if two or more priorities are identical.

只要对优先级相同情况,生成一个新优先级就行了。

Exercise 5.3-7

Suppose we want to create a random sample of the set 1,2,3,,n, that is, an m-element subset S, where 0mn, such that each m-subset is equally likely to be created. One way would be to set A[i]=i for i=1,2,3,,n, call RANDOMIZE-IN-PLACE(A), and then take just the first m array elements. This method would make n calls to the RANDOM procedure. If n is much larger than m, we can create a random sample with fewer calls to RANDOM. Show that the following recursive procedure returns a random m-subset S of 1,2,,n, in which each m-subset is equally likely, while making only m calls to RANDOM:

RANDOM-SAMPLE(m,n)if m == 0    returnelse    S = RANDOM-SAMPLE(m-1, n-1)    i = RANDOM(1,n)    if i ∈ S        S = S ∪ {n}    else        S = S ∪ {i}    return S

归纳法证明:

RANDOM-SAMPLE(m,n) 返回一个含有 m 个元素的随机样本。即一个均匀随机组合,每个组合出现概率为 1/Cmn

首先在 m=0 or 1 时,RANDOM-SAMPLE(0, n-m)RANDOM-SAMPLE(1, n-m+1) 显然成立。假设在 m1 时,RANDOM-SAMPLE(m-1,n-1) 依然成立。那么在 m 时,分两种情况:新随机样本中是否包含元素 n

包含 n 的情况:

mn(1/Cm1n1)=1/Cmn(1)

不包含 n 的情况:
nmnC1mnm(1/Cm1n1)=1/Cmn(2)

关于式 (1),(2) 的解释:求组合概率过程中,如果设定或约定第 m 个元素不存在 n1 次组合的搜索集合中,则就可以向公式 (1) 一样求解,即P(Cmn;e in Cmn)=P{e appears at the this time}P(1/Cm1n1)

公式 (2) P(Cmn;i in Cmn)=nmnC1mnm(1/Cm1n1) ,其中 1nm 是最后一次剩下元素中 i 出现的概率;C1m 表示同样的组合还有 C1m 中情况。因为最后一次进入样本的 i 元素存在 n1 次组合的搜索集合中。(可以举个例子试试,如:{1,2,3,4,5} 中调用 RANDOM-SAMPLE(2,5)


一般性证明:

给了思路:可以先从排列出发,然后在对排列进行讨论出现的次数。(因为看作排列,概率很好计算且每种排列概率都相同)

阅读全文
0 0
原创粉丝点击