I often* find myself in need of a data structure which has the following properties:
- can be initialized with an array of n objects in O(n).
- one can obtain a random element in O(1), after this operation the picked element is removed from the structure. (without replacement)
- one can undo p 'picking without replacement' operations in O(p)
- one can remove a specific object (eg by id) from the structure in O(log(n))
- one can obtain an array of the objects currently in the structure in O(n).
the complexity (or even possibility) of other actions (eg insert) does not matter. Besides the complexity it should also be efficient for small numbers of n.
Can anyone give me guidelines on implementing such a structure? I currently implemented a structure having all above properties, except the picking of the element takes O(d) with d the number of past picks (since I explicitly check whether it is 'not yet picked'). I can figure out structures allowing picking in O(1), but these have higher complexities on at least one of the other operations.
BTW: note that O(1) above implies that the complexity is independent from #earlier picked elements and independent from total #elements.
*in monte carlo algorithms (iterative picks of p random elements from a 'set' of n elements).