https://github.com/coloquinte/roulette
Weighted random selection
https://github.com/coloquinte/roulette
Last synced: 11 months ago
JSON representation
Weighted random selection
- Host: GitHub
- URL: https://github.com/coloquinte/roulette
- Owner: Coloquinte
- License: mit
- Created: 2017-09-27T20:06:49.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-09-28T20:48:27.000Z (almost 9 years ago)
- Last Synced: 2025-08-03T11:54:48.499Z (11 months ago)
- Language: C++
- Size: 7.81 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Roulette tree
Random weighted selection, dynamic: select an element with probability proportional to its weight.
It is an important step in many randomized algorithms. This datastructure provides efficient update and query (O(log n)).
It is inspired by a [pointer-based implementation](https://github.com/hyPiRion/roulette-tree).
Compared to it, it will have much lower memory usage (~3x), and may have better performance (not benchmarked).
It is still a work in progress, and will still be improved over time.
## Implementation
The datastructure is a balanced binary tree with the elements as leaves. Each node stores the total weight of its subtree.
The selection procedure rolls a number between 0 and the sum of the weights, and the corresponding element is obtained by dichotomy.
Like a typical binary heap, the tree is stored in an array and doesn't have to use any pointer: the datastructure uses contiguous memory space for at most 3n weights.