weighted selection
random sampling
weighted random selection
algorithm design
data structures

Select k random elements from a list whose elements have weights

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

In many algorithmic problems, selecting random elements from a list with specified weights is a common task. Unlike uniform random selection, weighted random selection allows for some elements to be preferred over others based on assigned weights. This technique is important in areas such as machine learning, simulation, gaming strategies, and optimization tasks where randomness with specific biases is required.

Understanding Weighted Random Selection

Concept

Weighted random selection involves using a list of weights to adjust the probability of selecting each element. Put simply, elements with higher weights have a higher probability of being chosen than elements with lower weights. The selection process can be visualized as a "weighted lottery."

Probability Distribution

Assume a list with `n` elements denoted by E=e1,e2,,enE = {e_1, e_2, \ldots, e_n}, with corresponding weights W=w1,w2,,wnW = {w_1, w_2, \ldots, w_n}. The probability of selecting element eie_i is:

P(e_i)=w_i_j=1nw_jP(e\_i) = \frac{w\_i}{\sum\_{j=1}^{n}{w\_j}}

This assures that elements are chosen based on their relative weights.

Algorithm Implementation

  1. Cumulative Weights: Calculate the cumulative sum of the weights.
  2. Random Number: Generate a random number in the range [0, total of all weights).
  3. Binary Search: Use binary search to find the largest index where the cumulative weight is greater than the generated random number. The element at this index is the selected one.

Example Code

Python is a commonly used language for implementing such algorithms due to its efficient libraries and readability. Here is an example using the `random` library for selecting a single element based on weights:

• Time Complexity: The algorithm runs in O(n+logn)O(n + \log n) time, where O(n)O(n) is for constructing the cumulative sum and O(logn)O(\log n) is for binary searching. • Space Complexity: O(n)O(n) for storing cumulative weights. • Simply repeat the single selection `k` times. • Code snippet:

• Adjust weights dynamically or use a more advanced data structure like a priority queue or reservoir sampling. • Normalization: If weights are not in the desired range, normalize them by the sum of weights. • Precision: Be mindful of floating-point precision errors especially in languages that do not handle large values smoothly. • Alternative Approaches: Algorithms like the Alias Method allow for constant time selection after an O(n)O(n) preprocessing.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms