From 185bbbc341d96e0e8679b06cc505b0aaa4f6f68c Mon Sep 17 00:00:00 2001 From: Marina <138340846+bt3gl-google@users.noreply.github.com> Date: Sun, 30 Jul 2023 11:04:07 -0700 Subject: [PATCH] Update README.md --- arrays_and_strings/README.md | 53 ++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/arrays_and_strings/README.md b/arrays_and_strings/README.md index ae10b57..782445a 100644 --- a/arrays_and_strings/README.md +++ b/arrays_and_strings/README.md @@ -11,21 +11,64 @@ * if the hash function is not a 1-1 mapping, collisions must be handled: - how to organize the values in the same bucket? - what if too many values are assigned to the same bucket? - - hwo to search for a target value in a specific bucket? + - how to search for a target value in a specific bucket?
+
+
+ +#### implementing a hash set
+* to implement a HashSet data structure, you need to implement: + - a hash function (to assign an address to store a given value), and + - a collision handling (since the nature of a hash function is to map a value from a space A to a corresponding smaller space B). + + +* overall, there are several strategies to resolve the collisions: + - separate chaining: for value with the same hash key, we keep them in a bucket, and each bucket is independent of each other. + - open addressing: whenever there is a collision, we keep on probing the main space with certain strategy until a free slot is found. + - 2-choices hashing: we use two hash functions rather than one, and pick the generated address with fewer collisions. + +* if we were to implement separate chaining, the primary storage underneath a hashset is a continuous memory as array, where each element in this array corresponds to a bucket that store the actual values. + * given a value, first we generate a key for the value via the hash function (the generated key serves as the index to locate the bucket). + * once the bucket is located, we then perform the desired operation on the bucket (such as add, remove, and contain). + * use a prime number as the base of the module to reduce collisions. + +
+ +#### buckets as linked lists + +
+ +* a good choice for buckets is linked lists, as their time complexity for insertion and deletion is constant (once the position to be updated is located). +* time complexicity for search is O(N/K) where N is the number of all possible values and K is the number of predefined buckets (the average size of bucket is N/K). +* space complexity is O(K+M), where K is the number of predefined buckets, and M is the number of unique values that have been inserted in the HashSet. +* lastly, to optimize search, we could maintain the buckets as sorted lists (and obtain O(logN) time complexity for the lookup operation). however, insert and delete are linear time (as elements would need to be shifted). + +
+ +#### buckets as binary search trees + +
+ +* another option for a bucket is a binary search tree, with O(logN) time complexity for search, insert, and delete. +* time complexity for search is O(logN/K), where N is the number of all possible values and K is the number of predefined buckets. +* space complexity is O(K+M) where K is the number of predefined buckets, and M is the number of unique values in the HashSet.
--- -### `is_palindrome.py` +### examples + +
+ +#### `is_palindrome.py`
@@ -39,7 +82,7 @@ Is helllo there a palindrone?: False
-### `playing_with_strings.py` +#### `playing_with_strings.py`
@@ -53,7 +96,7 @@ Reversed: [5, 4, 3, 2, 1]
-### `anagram.py` +#### `anagram.py`
@@ -66,7 +109,7 @@ Is listen an anagram of silent?: True
-### `permutation.py` +#### `permutation.py`