The uniform sampling of \( n \) of \( N \) items, as chapter 3 (Random Sampling) of Ferragina's Pearls of Algorithm Engineering treats it, in the three settings there: the items on the disk with \( N \) known, a stream with \( N \) known, a stream with \( N \) unknown.
More...
|
| template<class R> |
| std::vector< std::size_t > | ADS::sampleBySwaps (std::size_t n, std::size_t N, R &g) |
| | \( n \) of the positions \( 0 , \ldots , N - 1 \), sorted, by \( n \) swaps in an array of the \( N \) positions
|
| template<class R> |
| std::vector< std::size_t > | ADS::sampleByDictionary (std::size_t n, std::size_t N, R &g) |
| | \( n \) of the positions \( 0 , \ldots , N - 1 \), sorted, by a hash table of those drawn
|
| template<class R> |
| std::vector< std::size_t > | ADS::sampleBySorting (std::size_t n, std::size_t N, R &g, std::size_t *rounds=nullptr) |
| | \( n \) of the positions \( 0 , \ldots , N - 1 \), sorted, by a bucket sort of random positions; the rounds of draws go to *rounds, if given
|
| template<class R> |
| std::vector< std::size_t > | ADS::sampleBySkips (std::size_t n, std::size_t N, R &g, std::size_t *randoms=nullptr) |
| | \( n \) of the positions \( 0 , \ldots , N - 1 \), sorted, by the gaps of Vitter's Algorithm D (A when \( 13 n \ge N \)); the random numbers drawn go to *randoms, if given
|
| template<class T, class R> |
| std::vector< T > | ADS::heapSampling (const std::vector< T > &S, std::size_t n, R &g) |
| | \( n \) items of the stream \( S \), in the order of \( S \), by the \( n \) largest of random keys, as if the length of \( S \) were unknown
|
| template<std::input_iterator I, std::sentinel_for< I > S, class R> |
| std::vector< std::iter_value_t< I > > | ADS::reservoirSampleL (I first, S last, std::size_t n, R &g) |
| | \( n \) items of \( [ \mathit{first} , \mathit{last} ) \), a stream of unknown length, by the skips of Algorithm L, in no particular order; all of them, in their order, when the stream has at most \( n \) items
|
The uniform sampling of \( n \) of \( N \) items, as chapter 3 (Random Sampling) of Ferragina's Pearls of Algorithm Engineering treats it, in the three settings there: the items on the disk with \( N \) known, a stream with \( N \) known, a stream with \( N \) unknown.
The samplings of positions return them sorted, so that the items are then read by one pass, at most \( \min\{ n , \lceil N / B \rceil \} \) I/Os [see DiskReader::at() of TwoLevelMemory.h].
- sampleBySwaps() is the first algorithm of the chapter, the first \( n \) steps of the shuffle of Fisher and Yates [see shuffle() of RandomNumbers.h] on an array of the \( N \) positions: \( n \) random numbers, but \( \Theta( N ) \) space and up to \( n \) random I/Os.
- sampleByDictionary() keeps the random positions in a hash table, the repeated ones drawn again; with \( n \leq N / 2 \) a draw repeats with probability below \( 1 / 2 \), hence \( O( n ) \) expected time and space, and for \( n > N / 2 \) the \( N - n \) positions left out are drawn instead.
- sampleBySorting() sorts the positions drawn by a bucket sort: \( n \) random positions sorted by \( n \) buckets of width \( \lceil N / n \rceil \), \( O( 1 ) \) expected items each, sorted by insertion [see InsertionSort.h], and the repetitions removed; the positions missing are drawn again and merged, instead of all \( n \) as in the chapter, which is the same distribution (the process is symmetric in the \( N \) positions) but needs no luck when \( n \) exceeds \( \sqrt{N} \) and the birthday paradox makes a repetition likely, \( 1 - e^{-n ( n - 1 ) / 2 N} \); for \( n > N / 2 \) the positions left out are drawn, as above.
- The one pass that selects the position \( j \) with probability \( ( n - s ) / ( N - j ) \), \( s \) the positions already selected, is selectionSampling() of RandomNumbers.h (Algorithm S of Knuth), \( N \) random numbers. sampleBySkips() is the method of Vitter that draws the gaps instead, \( P( G = g ) = \binom{V - g - 1}{v - 1} / \binom{V}{v} \) for \( v \) positions still to select among \( V \): Algorithm D, by rejection with a continuous envelope, about \( n \) random numbers and \( O( n ) \) expected time, falling back to Algorithm A (the gap by the sequential search of its distribution, \( O( N ) \) time but \( n \) random numbers) once \( n \) exceeds \( N / 13 \), where A is faster.
- heapSampling() is the sampling for a stream of unknown length: a random key per item, the \( n \) largest keys kept in a heap [see DaryHeap.h], \( O( N \log n ) \) time; reservoirSampling() of RandomNumbers.h is the reservoir (Algorithm R of Knuth), \( O( N ) \) time and exactly \( n \) items of space, optimal in this model.
- reservoirSampleL() is Algorithm L of Li, beyond the chapter, the reservoir for a stream of unknown length that draws the number of items to skip instead of a number per item: with a uniform key per item, the reservoir holds the \( n \) items of the smallest keys, and \( W \), the largest of them, is distributed as a product of \( n \)-th roots of uniforms, \( W \leftarrow W U^{1/n} \) at each entry; the next item whose key is below \( W \) comes after a geometric number of items, \( S = \lfloor \ln U' / \ln( 1 - W ) \rfloor \), \( P( S = s ) = ( 1 - W )^s W \), and replaces a uniform item of the reservoir. The item \( t > n \) enters with probability \( n / t \), so that \( n ( H_N - H_n ) \approx n \ln( N / n ) \) items enter, each for 3 random numbers: \( O( n ( 1 + \log( N / n ) ) ) \) of them in all and, on a random access range, as much time, the skipped items never touched.
Compared with the literature: Vitter's Algorithm D is the method of choice when \( N \) is known, Algorithm Z of the same author does the same for the reservoir as Algorithm L, with a rejection that is more complex and no faster, and the random keys of heapSampling() are the start of the weighted sampling of Efraimidis and Spirakis, where the key of an item of weight \( w \) is \( U^{1 / w} \).
The contracts [see Contract.h]: \( n \le N \); the positions are distinct, below \( N \) and sorted, an audit. The functions are defined in RandomSampling.tpp.
- See also
- P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 3 (Random Sampling), doi:10.1017/9781009128933
-
J. S. Vitter, Faster methods for random sampling, Communications of the ACM 27(7), 703-718, 1984, doi:10.1145/358105.893
-
J. S. Vitter, An efficient algorithm for sequential random sampling, ACM Transactions on Mathematical Software 13(1), 58-67, 1987, doi:10.1145/23002.23003
-
J. S. Vitter, Random sampling with a reservoir, ACM Transactions on Mathematical Software 11(1), 37-57, 1985, doi:10.1145/3147.3165
-
K.-H. Li, Reservoir-sampling algorithms of time complexity \( O( n ( 1 + \log( N / n ) ) ) \), ACM Transactions on Mathematical Software 20(4), 481-493, 1994, doi:10.1145/198429.198435
-
P. S. Efraimidis, P. G. Spirakis, Weighted random sampling with a reservoir, Information Processing Letters 97(5), 181-185, 2006, doi:10.1016/j.ipl.2005.11.003
- Author
- Donato Meoli
Dipartimento di Informatica
Università di Pisa
- Copyright
- © by Donato Meoli