Data Structures 1 Flashcards
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?
- you have a key for the item.2. the item’s key gets churned within the hash function to form the Hash index.3. The hash index can be applied to the data array, and so, the specific data is found.
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:
- Must be deterministic:-> Key must ALWAYS generate the same Hash Index (excluding rehashing).2. Must achieve uniformity-> Keys should be distributed evenly across hash table.3. FAST/EASY to compute-> only use parts of the key that DISTINGUISH THE ITEMS FROM EACH OTHER4. Minimize collisions:
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:
- Double table size to nearest prime number2. Re-hash items from old table into the new table.