- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reservoir Sampling
The Reservoir sampling is a randomized algorithm. In this algorithm, k items are chosen from a list with n different items.
We can solve it by creating an array as a reservoir of size k. Then randomly pick one element from the main list and placed that item in the reservoir list. When one item is selected once, it will not be selected for next time. But his approach is not effective, we can increase the complexity by this method.
In the reservoir list, copy first k items from the list, now one by one from the (k+1)th number in the list, let the current selected item is placed at index i. Find a random index from 0 to i and store it into j, if j is in the range of 0 to k, then swap reservoir[j] with list[i].
Input and Output
Input: The list of integers: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, The value of k = 6 Output: K-Selected items in the given array: 8 2 7 9 12 6
Algorithm
chooseKItems(array, n, k)
Input: The array, number of elements in the array, number of elements to pick.
Output: Pick k elements randomly.
Begin define output array of size [k] copy k first items from array to output while i < n, do j := randomly choose one value from 0 to i if j < k, then output[j] := array[i] increase i by 1 done display output array End
Example
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; void display(int array[], int n) { for (int i = 0; i < n; i++) cout << array[i] << " "; } void chooseKItems(int array[], int n, int k) { //it will choose k items from the array int i; int output[k]; for (i = 0; i < k; i++) output[i] = array[i]; srand(time(NULL)); //use time function to get different seed value while(i < n) { int j = rand() % (i+1); //random index from 0 to i if (j < k) //copy ith element to jth element in the output array output[j] = array[i]; i++; } cout << "K-Selected items in the given array: "; display(output, k); } int main() { int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; int n = 12; int k = 6; chooseKItems(array, n, k); }
Output
K-Selected items in the given array: 8 2 7 9 12 6
- Related Articles
- Sampling
- Different Types of Sampling Techniques
- What are Sampling-Based Approaches?
- Application of Sampling Distribution Methods in Psychology
- Updating and sampling of catalog statistics for DB2 tablespace
- If two pipes function simultaneously, a reservoir will be filled in 12 hours. One pipe fills the reservoir 10 hours faster than the other. How many hours will the second pipe take to fill the reservoir?
- How to change the row index after sampling an R data frame?
- A reservoir in the form of the frustum of a right circular cone contains ( 44 times 10^{7} ) litres of water which fills it completely. The radii of the bottom and top of the reservoir are 50 metres and 100 metres respectively. Find the depth of water and the lateral surface area of the reservoir. (Take: ( pi=22 / 7) )
- The hump of the camel is a reservoir of(a) fatty tissue(b) water(c) milk(d) all of above
- What kind of energy is possessed by the following?(a) A stone kept on roof-top.(b) A running car.(c) Water stored in the reservoir of a dam.(d) A compressed spring.(e) A stretched rubber band.
- Water in a rectangular reservoir having base $80 m$ by $60 m$ is $6.5 m$ deep. In what time can the water be emptied by a pipe of which the cross-section is a square of side $20 cm$, if the water runs through the pipe at the rate of $15 km/hr$.
- The Tehri dam is the highest dam in India and one of the highest in the World. The Tehri dam withholds a reservoir of capacity 4.0 km3 and surface area 52 km2. It is used for irrigation, municipal water supply, and the generation of 1000 MW of hydroelectricity.The Tehri Dam has been the object of protests. Environment activist Shri Sunder Lal Bahuguna led the “Anti Tehri Dam Movement” from the 1980s to 2014. The protest was against the displacement of town inhabitants and environmental consequences of the weak ecosystem. The relocation of more than 1,00,000 people from the area has led to protracted legal battles over resettlement rights and ultimately resulted in the delayed completion of the project.(a) How is hydropower harnessed?(b) Define 1 MW.(c) Mention two disadvantages of constructing the Tehri Dam.(d) What happens when water from great heights is made to fall on the blades of turbine?
