Reservior Sampling (蓄水池抽样算法)

Reservior Sampling是一种在大数据流中随机采样的算法,适用于内存限制场景。该算法保证了每个元素被选中的概率相等。本文介绍了Algorithm R的原理、数学证明,并提及其在LeetCode上的实践应用。
摘要由CSDN通过智能技术生成

Reservior Sampling (蓄水池抽样算法)


1. 蓄水池抽样问题描述

  Reservoir sampling is a family of randomized algorithms for randomly choosing a sample of k items from a list S containing n items, where n is either a very large or unknown number. Typically n is large enough that the list doesn’t fit into main memory.
  蓄水池抽样问题是,从一个长度为n的流中随机选取k个元素,使得n个元素中的每个元素都以相同的概率被采样到,通常情况下n是一个未知的很大的数目,而且无法将其载入主存中。


2. 蓄水池抽样的解法

  在Dictionary of Algorithms and Data Structures中该问题的定义与解法如下:

  • Definition: Randomly select k items from a stream of items of unknown length.
  • Solution: Save the first k items in an array of size k. For each item j, j > k, choose a random integer M from 1 to j (inclusive). If M ≤ k, replace item M of the array with item j.

该问题的解法一般被称为 Algorithm R 由 Jeffrey Vitter 在其论文“Random sampling with a reservoir” 中提出,其伪代码如下所示:

(*
  S has items to sample, R will contain the result
 *)
ReservoirSample(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]

3. Algorithm R 算法的数学证明

采用数学归纳法进行证明:假设我们要从 n 个元素中随机抽取 k 个元素( 0kn ),使得每个元素被采样的概率等于 kn

kmn
m=k 时,从 m 个元素随机采样 k 个,则

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值