1

Let $M$ be a collection of elements given in the form of the array such that membership of any element can be done in $O(1)$ time. Which means elements of array $M$ are $\{1,2,\cdots,n\}$ such that $1$ is stored at $M[1]$ and $2$ at $M[2]$ and so on. The goal is to sample an element from the array $M$.

Note that we have to design an algorithm multiple times (say $k$ times) in such a way that in each repetition we get a different element as a result.

Sampling means the probability of picking any element from $M$ is $\frac{1}{|M|}$.

My idea is as follows: create another array $F:=[\frac{1}{|M|},\frac{1}{|M|},\cdots,\frac{1}{|M|}]$, which means assigning equal weights to each element.

There are several existing algorithms now which can sample an element from $M$ in expected time $O(1)$ (Say Algorithm 1 Algorithm).

If I run the same algorithm 1 again then the same element may get picked so I have to update something in the array $F$ this update should be in $O(1)$ time.

I am looking for an algorithm that gives $O(k)$ time for $k$ repetitions other than constructing array $F$.

How to modify the above algorithm such that next time I get a different answer?

EDIT: For any $a\in \{1,2,...,n\}, $Pr[Output_1=a] = $\frac{1}{|M|}$ and Pr[Output_i=a|Output_i-1 =a_i-1,...,Output_1=a_1]. Output_i denotes the output of the algorithm in the $i$th iteration and Pr denotes the probability.

Rma
  • 127
  • 6
  • 2
    I fail to see how establishing an array of weights explicitly changes (or defines) sampling a subset of size $k$. – greybeard Sep 15 '22 at 05:34
  • I don't understand what you mean by "given in the form of the array such that membership of any element can be done in O(1) time." Can you explain that in another way? If you have an array of elements, testing whether a particular value is an element of the array takes O(n) time. 2. Might there be repeats in the elements of the array? 3. What do you mean by "Note that..."? Do you mean the goal is to randomly choose k elements, without replacement? Please [edit] the question so it is clearer about all of these points.
  • – D.W. Sep 15 '22 at 06:14
  • There are no duplicate elements in the given array $M$. – Rma Sep 15 '22 at 07:11
  • So your question is how to sample k elements from n elements without replacement? https://stackoverflow.com/questions/311703/algorithm-for-sampling-without-replacement – qwr Sep 15 '22 at 07:20
  • @greybeard I did this as I will apply the algorithm given in the pdf. – Rma Sep 15 '22 at 07:20