算法原理 第五章
来源:互联网 发布:金税盘网络连接失败 编辑:程序博客网 时间:2024/06/05 13:05
RANDOMIZE-IN-PLACE
n = A.lengthfor i=1 to n swap A[i] with A[RANDOM(i,n)]
RESERVIOR SAMPLING
The algorithm creates a “reservoir” array of size k and populates it with the first k items of S. It then iterates through the remaining elements of S until S is exhausted. At the ith element of S, the algorithm generates a random number j between 1 and i. If j is less than or equal to k, the jth element of the reservoir array is replaced with the ith element of S. In effect, for all i, the ith element of S is chosen to be included in the reservoir with probability k/i. Similarly, at each iteration the jth element of the reservoir array is chosen to be replaced with probability 1/k * k/i, which simplifies to 1/i. It can be shown that when the algorithm has finished executing, each item in S has equal probability (i.e. k/length(S)) of being chosen for the reservoir. To see this, consider the following proof by induction. After the (i-1)th round, let us assume, the probability of a number being in the reservoir array is k/(i-1). Since the probability of the number being replaced in the ith round is 1/i, the probability that it survives the ith round is (i-1)/i. Thus, the probability that a given number is in the reservoir after the ith round is the product of these two probabilities, i.e. the probability of being in the reservoir after the (i-1)th round, and surviving replacement in the ith round. This is (k/(i-1)) * ((i-1)/i)=k/i. Hence, the result holds for i, and is therefore true by induction.
// S has items to sample, R will contain the resultReservoirSample(S[1..n], R[1..k]) // fill the reservoir array for i = 1 to k R[i] := S[i] // replace elements with gradually decreasing probability for i = k+1 to n j := random(1, i) // important: inclusive range if j <= k R[j] := S[i]
- 算法原理 第五章
- 算法导论: 第五章
- 第五章-算法入门
- 第五章 路由算法
- 算法导论第五章
- 算法导论第五章总结
- 算法导论读书笔记 第五章
- 第五章 基础算法 pending
- 第五章 KMP匹配算法
- 牛客算法--第五章
- 算法导论第五章5.3随机算法
- 算法导论 第五章:随机算法
- 第五章 BF算法和KMP算法
- 数据库原理与设计第五章作业
- 【计算机网络原理】第五章 网络层
- 计算机网络原理知识点(第五章:运输层)
- 【连载】计算机组成原理 --- 第五章 中央处理器
- GZIP压缩原理分析(10)——第五章 Deflate算法详解(五01) 章前语
- 模板方法模式
- 算法进行时--单链表(二)常见题型01
- java文件大小格式化输出
- 如何在Unity中实现逐字打印UI中的Text文字
- git常用命令
- 算法原理 第五章
- 从手动添加jar包到使用Maven管理
- ssm框架基础配置
- hadoop 关闭datanode节点时发生异常:no datanode to stop
- 中英文对照 —— 体育与健身
- keras学习随笔01——准备知识和keras安装
- 决定将APP开发深入学习研究
- 模板方法模式-用模板方法排序
- JavaSE_28th_数组——数3退1游戏