Download Hash Tables, Hashing Functions - Data Structures | CS 310 and more Study notes Data Structures and Algorithms in PDF only on Docsity!
Hash Tables
key value
Hashing Function
key value
CS 310 Hash Tables, Page 2 ' $
- The hash-table data structure achieves (near) constant time searching by “wasting” memory space.
- the size of the memory we reserve for a hash table is typically much large than the number of data stored in it.
- Each item in the table comprises a key and a value.
- In each GMU student record, we could use GMU ID as the key and all other data combined as the value.
- We use a hashing function to determine where to store items in table.
- Given an entry x, it will be stored at location h(x.key), where h is the hashing function.
- The verb “to hash” means to chop something up or to make a mess out of it (it/something being the key).
Example
- The table contains 80 entries.
- The key of each entry is an integer.
- We use the hash function h(key) = key % 80.
- An entry with key 140 will be stored at location 140 % 80 = 60 in the table.
- An entry with key 425 will be stored at location 425 % 80 = 25
- An entry with key 825 will be also stored at location 825 % 80 = 25!
CS 310 Hash Tables, Page 4 ' $
Key-to-Index Mapping
- A collision occurs when two keys are “hashed” to the same location in the table.
- Birthday paradox: If 23 people are present in a room, chances are good (> 0 .5) that two of them will have the same month/day birthday.
- If we randomly maps 23 keys into a table of size 365, the probability that no two keys map to the same location is less than 0.5.
- A complete key-to-index mapping consists of
- a hashing function that minimizes collisions, and
- a technique for handling collisions.
Hashing Function III: Multiplication
- The key is multiplied by a constant less than one.
- The hash function returns the first r digits of the the results.
- Again, CAPACITY is set to 2r.
CS 310 Hash Tables, Page 8 ' $
How to Handle Keys of Type String
- Just treat a string as a long binary number. Example: Suppose the key is the string ’KEY’ In ASCII, this is 1001011 1000101 1011001. Treat this as a binary number and use previous hashing functions.
- Folding. Suppose we are given a key comprising 11 letters a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10 a 11.
a 1 a 2 a 3 a 4
- a 5 a 6 a 7 a 8
- a 9 a 10 a 11
Collision Resolution Method I: Chaining
For chaining, we put entries hashed to the same index into a linked list. The advantages of chaining are:
- It is easy to locate a key when there are collisions.
- The hash table needs only space for pointers.
- Deletion of an item is straight-forward. The disadvantage of chaining is that we need additional space for pointers along with the data.
CS 310 Hash Tables, Page 10 ' $
Example: Suppose h(key) = key % 100 Keys: 4203 3606 1200 1302 8802 4106
99
98
00 01 02 03 04 05 06
M = 10
Keys: 211, 143, 160, 821, 223, 221
CS 310 Hash Tables, Page 14 ' $
Quadratic Probing
- hi = (h 0 + i^2 ) % CAPACITY
- That is, we try h 0 + 1, h 0 + 4, h 0 + 9,... until an empty slot is found.
- Typically, quadratic probing will not probe all locations.
- For prime CAPACITY values, (CAPACITY+1)/2 probes will bring you back to the starting point; when that happens, there is no point in going further. (See pages 402 to 403 of Kruise for a proof.)
Example
CAPACITY=7, key = 702 h 0 = 2 h 1 = (h 0 + 1^2 ) % 7 = 3 h 2 = (h 0 + 2^2 ) % 7 = 6 h 3 = (h 0 + 3^2 ) % 7 = 4 h 4 = (h 0 + 4^2 ) % 7 = 4 h 5 = (h 0 + 5^2 ) % 7 = 6 h 6 = (h 0 + 6^2 ) % 7 = 3 h 7 = (h 0 + 7^2 ) % 7 = 2 We have completed a cycle, and half of the table entries are not probed.
CS 310 Hash Tables, Page 16 ' $
- consider inserting 2, 72, 702, 7002, and 70002 to an empty, size-7 hash table that uses quadratic probing
- for all these keys, h 0 = 2, h 1 = 3, h 2 = 6, and h 3 = 4. 0 1 2 h 0 3 h 1 4 h 3 5 6 h 2
- This is typically acceptable with hash tables, for “probing half of the table” most probably indicates flaws in system design.
- a hash table is typically large to minimize the chances of collisions.
Implementing Open-Address Hashing
with Linear Probing
template class Table { public: static const int CAPACITY = 811; Table(); ~Table();
void insert (const Entry& entry); void remove (int key); bool is_present (int key) const; void find (int key, bool& found, Entry& result) const; int size() const {return used;}
CS 310 Hash Tables, Page 20 ' $
private: static const int NEVER_USED = -1; static const int PREVIOUSLY_USED = -2;
Entry data[CAPACITY]; int used; int hash (int key) const; void find (int key, bool& found, int& index) const; }
template void Table::find (int key, bool& found, int& i) const { int count = 0; int i = hash (key); while (count < CAPACITY && data[i].key != key && data[i].key != NEVER_USED) { count++; i = (i+1) % CAPACITY; } found = data[i].key == key; }
CS 310 Hash Tables, Page 22 ' $ template void Table::insert (const Entry& entry) const { bool already_present; int i; find (entry.key, already_present, i);
if (!already_present) { i = hash(entry.key); while (data[i].key != NEVER_USED || data[i].key != PREVIOUSLY_USED) i = (i+1) % CAPACITY; used++; data[i] = entry; } }
- In open-address hashing with double hashing, a nonfull hash table, and no deletions, the average number of table entries examined in a successful search is approximately: − ln(1 − α) α Given a table with CAPACITY=811 and 611 entries occupied, we expect successful searches to examine −^ ln(1 0 .− 8 0 .8)= 2 slots.
- In chained hashing, the average number of table entries examined in a successful search is approximately:
1 +
α 2 Note that, with chaining the table can be overloaded (that is, α ≥ 1).
CS 310 Hash Tables, Page 26 ' $
An Empirical Comparison
Load factor 0.1 0.5 0.8 0.9 0.99 2. Successful search, average # of probes Chaining 1.04 1.2 1.4 1.4 1.5 2. Open, quadratic probes 1.04 1.5 2.1 2.7 5.2 NA Open, linear probes 1.05 1.6 3.4 6.2 21.3 NA Unsuccessful search, average # of probes Chaining 1.1 1.5 1.8 1.9 1.99 2. Open, quadratic probes 1.13 2.2 5.2 11.9 126 NA Open, linear probes 1.13 2.7 15.4 59.8 430 NA