What you need is to recreate the hash, and make your own hash structure like an array with 2^n elements where 0.9 * 2^n >= N where N is the number of real elements in your structure (use 90% maximum fill factor) If you use the hash as a dynamic vector with minimum 10% fill factor and max 90% then for the grtRandomElement you can choose a random value between 0 and 2^n - 1, if it's occupied output that element, else rechoose until an element is occupied. This method is O(1) since in the worst case you do approx 10 iterations.
The hash function is just x * SCRAMBLE_CONSTANT mod 2^n where x is your element to be hashed. - multiplicative hashing On Nov 3, 2011, at 1:09 PM, shady <[email protected]> wrote: > simple hash ? > can you specify the hashing function because if we use stl's map its > complexity is O(log(n)) > > On Thu, Nov 3, 2011 at 1:48 AM, Robert Badita <[email protected]> wrote: > Simple hash will do it in O(1) but with careful implementation of > getRandomElement to distribute probability evenly to each bucket / element in > the bucket > > > On Nov 2, 2011, at 5:52 PM, shady <[email protected]> wrote: > >> does anyone knows how much is the complexity of operations erase and >> pop_back in case of Vector(STL) >> what would you choose : >> >> Design a data structure where the following 3 functions are optimised: >> 1. Insert(n) >> 2. GetRandomElement() >> 3. Remove(n) >> Write a class, and implement the functions. Give complexity of each of these >> >> what would you choose, insertion can always be done in O(1), but what about >> getrandomelement().... if we use simple arrays than for those >> 1. -> O(1) >> 2. -> O(1) >> 3. -> O(n) >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
