150 likes | 468 Views
What do you get when you cross a mountain climber and a grape? ... Must accept a key and return a value. Tries for O(1) time by using arrays... ... but may ...
E N D
1. CSC 212 –Data Structures Lecture 28:
More Hash and
Dictionaries
2. Question of the Day What do you get when you cross a mountain climber and a grape?
Nothing, you cannot cross a scalar.
3. Hash Table Used to implement Map
Must accept a key and return a value
Tries for O(1) time by using arrays…
… but may end up needing O(n) time
Do not know Keys type ahead of time
Flexible design must handle anything
Must consider what we want from hash
4. Hashing Want use array to hold our table
Indices provide O(1) access times
But array index must be an int
With int Keys, could just use array from start
Not flexible at all
So, hash function serves three purposes:
Convert Key to int in repeatable manner
Limit range of int to size of the array
Spread entries out evenly across the range
5. Hash Function First thing hash must do: turn Key into int
Easy for numeric data types
For String, add value of each character
Makes “spot”, “pots”, “stop”, & “opts” equal
Improve upon this using polynomial likex0 + x1 a + x2 a2 + … + xn-1an-1 would compute
6. Compression Need to limit table size
Unused entries waste space and do not help
When z=33, “spot” = 4,293,383
“triskaidekaphobia” = too big for my calculator
To be useful, hash must be compressible
Easiest compression uses modulus (%)
Using prime number exploits splits regularity
MAD method first does multiply and add
Can help spread hash more evenly
7. Collisions Two Keys could always share hash code
Array can only hold one value per entry
Need to some way of working around this
Three commonly used handling schemes
Separate chaining: array contains Lists of entries hashed to that index
Linear probing: loop through array looking for first open array location
Double hashing: use more hash to find an empty array location
8. Separate Chaining Table is an array of List
“Chain” whenever there is a collision
Each entry at index in which it is hashed
Search List to see if a there is a matching Key
9. Linear Probing Table is array of Entrys
Normally, Entry located where key is hashed
If index used, circle through array for first empty one
Collisions cause pockets of filled slots, slowing access
Collisions means key could be in any location
But either fill removed slots or mark specially
Only search until first empty specially marked slot
10. Double Hashing Solution to bad hash is more hash!
Table is again array of Entrys
When there is a collision, re-hash key using second hash function
Re-use second hash value to probe on repeated collisions
Re-multiplier and table size should be relatively prime, so entire table will be scanned
11. Store integers using double hashing
N = 13
h(k) = k mod 13
d(k) = 7 - k mod 7
Insert keys: 18, 41, 22, 44, 59, 32, 31 Example of Double Hashing
12. Dictionary ADT dic·tion·ar·y
Reference book containing alphabetical list of words, with information given for each word
Book listing words with translations into other language
Dictionary ADT maps a key to 1 or more values
Used by Google, computer security logs, databases
Like a Map, Dictionary works with entries
But Dictionary removes an Entry and adds findAll() to return Iterator over entries with given key
13. Implementing a Dictionary Dictionary similar to a Map
Can use List or hash table
Implementation is virtually identical except:
Allow multiple entries with same key
When searching in remove, must match key and value
Add findAll() to return instance of class implementing Iterator
Class could just have array to which you add entries with key matching search key
14. Your Turn Get back into groups and do activity
15. Before Next Lecture… Keep up with your reading!
Complete Week #10 Assignment
Review Programming Assignment #3