DSA - Hash Tables Flashcards
What is a Hash Function?.
hash(key) computes the location from a KEY
Does it matter what Data Type is when we use the hash function?
NO
How Does a Hash Table Work?
Take the key calculate some number without referencing the key or record, use the number for a index in the array.
Get Us Immediately in 1 step where we want to be
Where do you store KEY?
Store it in Linked List/ Tree/ Hash Table
Describe assignments in Hash Table ?
-It is O(1) time complexity
- Very Memory Inefficient
==> Need an Array of size 10^7, if we only use 500 spaces it
still keeps the rest thus very inefficient.
Define Hash/Data Collison:
2 Different Keys with same number of finding remainder to place in that index
- Increasing array length may cause an decrease in the
likeness of collisions. (Does Not Fully Remove Them) - Require a robust & reliable solution to resolve collisions
What Does Hash Table Consists of:
- Array arr for storing the values
- hash function hash(KEY) => turns Key into index of array
- Mechanism for dealing with the collisions
What are the 2 Types of solutions of hash Collisions?
-Chaining Strategy
- Open Addressing Strategy
What does Chaining Strategy do?
Use another data structure on each position of the hash tables
-Only Chaining Strategy is Direct Chaining
- Use Linked List to store the values with the
same hash(code)
What does Addressing Strategy do?
GIVE THE 2 EXAMPLES ?
Finding a different address in the same array for the colliding value to be stored
(ADDRESS MUST BE UNUSED or OPEN)
- Two Strategy are Linear Probing & Double Hashing
How to Insert with Direct Chaining?
- Always check if the key which we are inserting is in the LL on position hash(key)
- If isn’t insert key at BEG of list
// DOES NOT NEED TO INSERT KEY IN BEG, BUT IN PRATICE IT IS IDEAL AS THE KEY IS MORE LIKELY TO BE ACCESSED SOON
TAKE O(n) = need to go through all elements already stored in hash table
How to Delete with Direct Chaining?
To delete(key) we delete key from LL stored in Position hash(key)
Takes O(n)
How to do Lookup in Direct Chaining?
lookup(key) return TRUE/FALSE, depending if key is stored in the list on position hash(key)
What is a Bad Hash Function?
-All entries have the same hash value
- 1 LARGE Linked List (LL)
Why is a Bad Hash Function BAD?
Has LARGE linear search through those links
What makes a GOOD Hash Function?
- Uniformly distributes keys among positions
- Given a Random key it has to have the same probability of being stored on every position
- location given by hash has the expected number of entries stored there equal to n/T.
What is the Load Factor?
AVG. NO. of entries stored on a Location
n/T
n = total number of store entries
T = size of hash table
REPRESENTS HOW FULL THE HASH TABLE IS
What is Unsuccessful lookup of a Key?
- Key is not in the table
- Location hash(key) stores n/T entries on AVG.
(Have TRAVERSE them all, LINEAR SEARCH on
n/T entries )
What is a Successful Lookup for a key?
- Location hash(key) stores k = n/t entries on AVG
- AVG. a linear search in LL of k elements takes
1/k(1+2+…+k) = k(k+1)/2k = (k+1)/2 comparisons - ASSUME MAX load Factor λ that is, n/T <= λ
What is AVG case time complexity of unsuccessful lookup?
n/T <= λ comparisons O(1)
What is AVG case time complexity of successful lookup?
1/2(1+n/T) <= 1/2(1+λ) Comparisons O(1)
λ is constant
Time Complexity of insert(key)?
- Same as unsuccessful lookup
O(1) - First Check if the key is stored in the table
- Otherwise insert key at BEG of list stored on hash(key)