From 3d8e44eef9b96fa24eb1d05263159aaf12ad9ac3 Mon Sep 17 00:00:00 2001 From: bt3gl <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Tue, 8 Aug 2023 17:46:27 -0700 Subject: [PATCH] Update README.md --- sets/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sets/README.md b/sets/README.md index 2259368..96412ee 100644 --- a/sets/README.md +++ b/sets/README.md @@ -10,15 +10,15 @@ * a set structure where we would implement `insert`, `delete`, and `get_random` at `O(1)` time. -* this type of structure widely used in statistical algorithms such as markov chain monte carlo and metropolis-hastings algorithms, which needs sampling from a probability distribution when it's difficult to compute the distribution itself. +* this type of structure is widely used in statistical algorithms such as markov chain monte carlo and metropolis-hastings algorithms, which needs sampling from a probability distribution when it's difficult to compute the distribution itself. * candidates for `O(1)` average insert time are: - * **hashmaps (or hashsets)**: to be able to implement `get_random` at `O(1)` (choose a random index and to retrieve narandom element), we would have to convert hashmap keys in a list, which is `O(N)`. a solution is to build a list of keys aside and use this list to compute `get_random` in `O(1)`. + * **hashmaps (or hashsets)**: to be able to implement `get_random` at `O(1)` (choose a random index and retrieve a random element), we would have to convert hashmap keys in a list, which is `O(N)`. a solution is to build a list of keys aside and use this list to compute `get_random` in `O(1)`. * **array lists**: we would have `O(N)` with `delete`. the solution would be delete the last value (first swap the element to delete with the last one, then pop the last element out). for that, we need to compute an index of each element in `O(N)`, and we need a hashmap that stores `element -> index`. * either way, we need the same combination of data structures: a hashmap and an array. * an array keeps the values appended in order. `delete` always replace elements to the end. - * an dictionary maps the values (key) to the corresponding length of the array (their index) so it guarantees `O(1)` lookup and provide a list for `random.choice()`. + * an dictionary maps the values (key) to the corresponding length of the array (their index) so it guarantees `O(1)` lookup and provides a list for `random.choice()`.