ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RandomSampling.h

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. More...

Files

file  ADS/numeric/RandomSampling.h
 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.

Functions

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

Detailed description

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 definitions, the invariants and the costs of the area are in Theory of numerical algorithms.

Function documentation

◆ heapSampling()

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

In pseudocode (the heap of random keys of Ferragina, chapter 3, beyond the book), \( H \) a heap with the least key on top:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{heapSampling}( S , n ) \cr 2 & \quad \textbf{foreach}\ j \gets 0 , 1 , \ldots , |S| - 1\ \textbf{do} \cr 3 & \qquad r \gets \text{a uniform of } ( 0 , 1 ) \cr 4 & \qquad \textbf{if}\ |H| < n\ \textbf{then}\ \mathrm{insert}( H , ( r , j ) ) \cr 5 & \qquad \textbf{else if}\ r > \text{the least key of } H\ \textbf{then} \ \mathrm{deleteMin}( H );\ \mathrm{insert}( H , ( r , j ) ) \cr 6 & \quad \textbf{return}\ \text{the items } S[ j ] \text{ of } H \text{, by } j \end{array}} \]

Precondition
\( n \le |S| \)

◆ reservoirSampleL()

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

In pseudocode (Algorithm L of Li, beyond the book), \( U \) a fresh uniform of \( ( 0 , 1 ) \) at each use:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{reservoirSampleL}( \mathit{stream} , n ) \cr 2 & \quad R \gets \text{the first } n \text{ items};\ W \gets U^{1/n} \cr 3 & \quad \textbf{loop} \cr 4 & \qquad s \gets \lfloor \ln U / \ln( 1 - W ) \rfloor \cr 5 & \qquad \text{skip } s \text{ items};\ \textbf{if}\ \text{the stream } \text{ended}\ \textbf{then return}\ R \cr 6 & \qquad R[ \text{a uniform of } [ 0 , n ) ] \gets \text{the next item} \cr 7 & \qquad W \gets W U^{1/n} \end{array}} \]

◆ sampleByDictionary()

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

In pseudocode (the dictionary of Ferragina, chapter 3, beyond the book), the complement drawn when it is smaller:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{sampleByDictionary}( n , N ) \cr 2 & \quad m \gets \min( n , N - n );\ D \gets \text{an empty hash table} \cr 3 & \quad \textbf{while}\ |D| < m\ \textbf{do}\ D \gets D \cup \{ \text{a uniform of } [ 0 , N ) \} \cr 4 & \quad \textbf{if}\ m = n\ \textbf{then return}\ D \text{ sorted} \cr 5 & \quad \textbf{return}\ \text{the positions not in } D \text{, in } \text{order} \end{array}} \]

Precondition
\( n \le N \)
Postcondition
\( n \) distinct positions below \( N \), sorted (audit)

◆ sampleBySkips()

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

In pseudocode (Algorithms A and D of Vitter, beyond the book), \( v \) positions still to select among the \( V \) still to see:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{sampleBySkips}( n , N ) \cr 2 & \quad v \gets n;\ V \gets N;\ \mathit{cur} \gets 0 \cr 3 & \quad \textbf{while}\ v > 1\ \textbf{and}\ 13 v < V\ \textbf{do} \cr 4 & \qquad \textbf{repeat}\ X \gets V ( 1 - U^{1/v} );\ S \gets \lfloor X \rfloor\quad \triangleright\ \text{the envelope} \cr 5 & \qquad \textbf{until}\ S \text{ is accepted against } P( G = S ) \text{ (the tests D3 and D4)} \cr 6 & \qquad \text{select } \mathit{cur} + S;\ \mathit{cur} \gets \mathit{cur} + S + 1;\ V \gets V - S - 1;\ v \gets v - 1 \cr 7 & \quad \textbf{while}\ v > 0\ \textbf{do}\quad \triangleright \ \text{Algorithm A} \cr 8 & \qquad S \gets \text{the least } s \text{ with } P( G > s ) \le U \text{, by a sequential search} \cr 9 & \qquad \text{select } \mathit{cur} + S;\ \mathit{cur} \gets \mathit{cur} + S + 1;\ V \gets V - S - 1;\ v \gets v - 1 \end{array}} \]

Precondition
\( n \le N \)
Postcondition
\( n \) distinct positions below \( N \), sorted (audit)

◆ sampleBySorting()

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

In pseudocode (the sampling by sorting of Ferragina, chapter 3, beyond the book), \( D \) sorted and with no repetition:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{sampleBySorting}( n , N ) \cr 2 & \quad m \gets \min( n , N - n );\ D \gets \emptyset \cr 3 & \quad \textbf{while}\ |D| < m\ \textbf{do} \cr 4 & \qquad k \gets m - |D|;\ X \gets k \text{ uniforms of } [ 0 , N ) \cr 5 & \qquad \text{sort } X \text{ into } k \text{ buckets of width } \lceil N / k \rceil \text{, each by insertion} \cr 6 & \qquad D \gets \text{the merge of } D \text{ and } X \text{, the } \text{repetitions removed} \cr 7 & \quad \textbf{if}\ m = n\ \textbf{then return}\ D \cr 8 & \quad \textbf{return}\ \text{the positions not in } D \text{, in } \text{order} \end{array}} \]

Precondition
\( n \le N \)
Postcondition
\( n \) distinct positions below \( N \), sorted (audit)

◆ sampleBySwaps()

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

In pseudocode (the sampling by swaps of Ferragina, chapter 3, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{sampleBySwaps}( n , N ) \cr 2 & \quad P \gets ( 0 , 1 , \ldots , N - 1 ) \cr 3 & \quad \textbf{for}\ s \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad \mathrm{swap}( P[ \text{a uniform of } [ 0 , N - s ) ] , P[ N - 1 - s ] ) \cr 5 & \quad \textbf{return}\ P[ N - n .. N - 1 ] \text{ sorted} \end{array}} \]

Precondition
\( n \le N \)
Postcondition
\( n \) distinct positions below \( N \), sorted (audit)