Hash Map Flashcards
What are the 3 requirements for a good hash function?
- Consistent
- Efficient to compute and not ignore siginficant numbers
- Should uniformly distribute
Why should array size M be prime?
If not, not all bits of key will play role and will therefore lose a chance to disperse evvenly and avoid collisions
How do we hash integers, floating-pont numbers, strings?
Use modular hashing
* Integers: Calculation with modular hashing to get index
* Floats: Binary representation of key
* Strings: Treat as large integers
* Compound keys: Mix together in a similar way to strings
How do we convert hashCode()
into an array index?
Combine with modular hashing by converting 32-bit to 31-bit then compute remainder
What is separate Chaining?
Build a linked list or symbol table for each bucket that contains a collision. Should be kept short.
How do we implement symbol tables into hash table?
Build for each array index a symbol table of keys that hash to that index
Therefore maintain an array of ST objects that implements operations. This is done by computing Hash Function to get index of object that contain key, then using get() and put() to delegate the operation to that table.
What is the average length of the list?
N/M
What is the probability that a list has more than t x a keys?
(a e/y)^t e ^(-a)
What is linear probing?
Store N key-value pairs in hash table of size M > N, relying on empty entries for collision
What are the 3 cases for linear probing?
- Key equal to search key
- Search reaches null
- Key not equal, try next with iteration
What is probing?
Operation of determining whether a given table entry holds an item whose key is equal to the search
How do we delete a key-value pair from a linear probing table?
- Check if key exists
- Find index by hashing
- Find index where key matches given key, set both the key and value at index to null
- Rehash the subsequent key-value pairs re-inserting key-value pairs into hash table using put
What is the load factor, a?
Percentage of table entries that are occupied. Can never be 1