《算法导论》第五章-第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)]
在证明中,初始化部分不再从空数组开始,而是从
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?,这里参考英文原版)
不能,因为每次置换都不可能出现第
Exercise 5.3-3
Suppose that instead of swapping element
A[i] with a random element from the subarrayA[i…n] , 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?
不能。有
拓展阅读 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 a1/n probability of winding up in any particular position inB . Then show that Professor Armstrong is mistaken by showing that the resulting permutation is not uniformly random.
很容易理解,
这样做只是获得了一个在原数组
Exercise 5.3-5
⋆ Prove that in the array
P in procedurePERMUTE-BY-SORTING
, the probability that all elements are unique is at least1−1/n .
令
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, where0≤m≤n , such that each m-subset is equally likely to be created. One way would be to setA[i]=i fori=1,2,3,…,n , callRANDOMIZE-IN-PLACE(A)
, and then take just the firstm array elements. This method would maken calls to theRANDOM
procedure. Ifn is much larger thanm , we can create a random sample with fewer calls toRANDOM
. Show that the following recursive procedure returns a random m-subsetS of1,2,…,n , in which eachm -subset is equally likely, while making only m calls toRANDOM
:
RANDOM-SAMPLE(m,n)if m == 0 return ∅else 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
首先在 RANDOM-SAMPLE(0, n-m)
和 RANDOM-SAMPLE(1, n-m+1)
显然成立。假设在 RANDOM-SAMPLE(m-1,n-1)
依然成立。那么在
包含
不包含
关于式
公式 RANDOM-SAMPLE(2,5)
)
一般性证明:
给了思路:可以先从排列出发,然后在对排列进行讨论出现的次数。(因为看作排列,概率很好计算且每种排列概率都相同)
- 《算法导论》第五章-第3节_练习(参考答案)
- 《算法导论》第五章-第1节_练习(参考答案)
- 《算法导论》第五章-第2节_练习(参考答案)
- 《算法导论》第五章-第4节_练习(参考答案)
- 《算法导论》第二章-第3节_练习(参考答案)
- 《算法导论》第四章-第3节_练习(参考答案)
- 《算法导论》第一章-第1节_练习(参考答案)
- 《算法导论》第一章-第2节_练习(参考答案)
- 《算法导论》第二章-第1节_练习(参考答案)
- 《算法导论》第二章-第2节_练习(参考答案)
- 《算法导论》第三章-第1节_练习(参考答案)
- 《算法导论》第三章-第2节_练习(参考答案)
- 《算法导论》第四章-第1节_练习(参考答案)
- 《算法导论》第四章-第2节_练习(参考答案)
- 《算法导论》第四章-第4节_练习(参考答案)
- 《算法导论》第四章-第5节_练习(参考答案)
- 《算法导论》第四章-第6节_练习(参考答案)
- 《算法导论》第五章-思考题(参考答案)
- 树结构练习——排序二叉树的中序遍历
- 使用ReactNative开发的仿微信客户端
- js转码加密的方法
- 第七篇:Javac编译与JIT编译
- (转)POJ 1062 昂贵的聘礼 (最短路,dijktra算法)
- 《算法导论》第五章-第3节_练习(参考答案)
- 58到家数据库30条军规解读
- js随机颜色
- vim配置
- [ZJOI2004]沼泽鳄鱼
- php 使用redis锁限制并发访问类
- 带好团队的7个步骤,不然就自己干到死
- VS2017专业版和企业版激活密钥
- NYOJ171