What is the goal of Hashing?
Do faster than O(LogN) time complexity for: lookup, insert, and remove operations. To achieve O(1)
What kind of Collection is Hashing?
value-orientated.
How does Hashing work?
hash Table:
An array that stores a collection of items.
Table Size(TS)
The Array’s Length
Load Factor (LF)
Number of items/Table size. For instance, a load factor of 1 = 100% of the items are used.
Key
Information in items that is used to determine where the item goes into the table.
Hash Function
A function that takes in the key to compute a specific Hash Index.
What would the Perfect Hash Function be?
Each Key maps to an unique Hash Index.
How do you delete a value within the hash table?
You just set Table[hash(Key)] = null
How do you look up a value within the hash table?
return Table[Hash(key)];
How do you insert a value within the hash table?
Table[Hash(key)]=data;
What is the worst case time complexity for: Insert, lookup, and delete, for hash functions?
O(1)
If we have UW-Madison student ID’s, and we wanted the ideal hash functions, how would we do it, and why would there be a problem
-> We’d simply count each one as an index-> Hash table would be huge.
Collisions:
When the Hash Function returns the same index for different keys.
Good Hash Function qualities:
Extraction:
Breaking keys into parts and using the parts that uniquely identify with the item. 379452 = 394121267 = 112
Folding:
Combining parts of the key using operations like + and bitwise operations such as exclusive or.Key: 123456789 123 456 789 — 1368 ( 1 is discarded)
Weighting:
Emphasizing some parts of the key over another.
Compressing:
Ensuring the hash code is a valid index for the table size.
The more items a table can hold, the () likely a collision will happen.
less
Load Factor
Approximately how it’s full… 0.7-0.8.
Why do we use prime numbers for table size?
We mod often, and prime numbers give us the most unique numbers. (2*ts+1)
Steps to resizing: