Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
-
Upload
osborne-bradford -
Category
Documents
-
view
223 -
download
4
Transcript of Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
![Page 1: Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.](https://reader035.fdocuments.net/reader035/viewer/2022062803/56649f3d5503460f94c5d30f/html5/thumbnails/1.jpg)
Hashing - 2
Designing Hash Tables
Sections 5.3, 5.4, 5.4, 5.6
![Page 2: Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.](https://reader035.fdocuments.net/reader035/viewer/2022062803/56649f3d5503460f94c5d30f/html5/thumbnails/2.jpg)
Designing a hash table
Hash function: establishing a key with an indexed location in a hash table.
Index = hash(key) % table_size;
Resolve conflicts: Need to handle multiple keys that may be mapped to
the same index. Two representative solutions
Linear probe open addressing (will discuss more later) Chaining with separate lists.
![Page 3: Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.](https://reader035.fdocuments.net/reader035/viewer/2022062803/56649f3d5503460f94c5d30f/html5/thumbnails/3.jpg)
Separate Chaining
Each table entry stores a list of items
So we don’t need to worry about multiple keys mapped to the same entry.
![Page 4: Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.](https://reader035.fdocuments.net/reader035/viewer/2022062803/56649f3d5503460f94c5d30f/html5/thumbnails/4.jpg)
Separate Chaining (contd.)
Type Declaration forSeparate ChainingHash Table
![Page 5: Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.](https://reader035.fdocuments.net/reader035/viewer/2022062803/56649f3d5503460f94c5d30f/html5/thumbnails/5.jpg)
Separate Chaining (contd.)
![Page 6: Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.](https://reader035.fdocuments.net/reader035/viewer/2022062803/56649f3d5503460f94c5d30f/html5/thumbnails/6.jpg)
Separate Chaining (contd.)
![Page 7: Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.](https://reader035.fdocuments.net/reader035/viewer/2022062803/56649f3d5503460f94c5d30f/html5/thumbnails/7.jpg)
Separate Chaining (contd.)
![Page 8: Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.](https://reader035.fdocuments.net/reader035/viewer/2022062803/56649f3d5503460f94c5d30f/html5/thumbnails/8.jpg)
Separate Chaining (contd.)
![Page 9: Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.](https://reader035.fdocuments.net/reader035/viewer/2022062803/56649f3d5503460f94c5d30f/html5/thumbnails/9.jpg)
Hash Tables Without Chaining
Try to avoid buckets with separate lists
How use Probing Hash Tables If collision occurs, try another cell in the hash
table. More formally, try cells h0(x), h1(x), h2(x), h3(x)… in succession until a free cell is found.
hi(x) = (hash(x) + f(i)) And f(0) = 0
![Page 10: Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.](https://reader035.fdocuments.net/reader035/viewer/2022062803/56649f3d5503460f94c5d30f/html5/thumbnails/10.jpg)
Insert(k,x) // assume unique keys1. index = hash(key) % table_size;2. if (table[index]== NULL)
table[index]=new key_value_pair(key,
x);3. Else {
• index++;• index = index % table_size;• goto 2;}
Linear Probing: f(i) = i
![Page 11: Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.](https://reader035.fdocuments.net/reader035/viewer/2022062803/56649f3d5503460f94c5d30f/html5/thumbnails/11.jpg)
Search (key)
1. Index = hash(key) % table_size;
2. If (table[index]==NULL)
return –1; // Item not found
3. Else if (table[index].key == key)
return index;
4. Else {
• Index ++;
• index = index % table_size;
• goto 2;
5. }
Linear Probing: Search
![Page 12: Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.](https://reader035.fdocuments.net/reader035/viewer/2022062803/56649f3d5503460f94c5d30f/html5/thumbnails/12.jpg)
Linear Probing Example
Insert 89, 18, 49, 58, 69
![Page 13: Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.](https://reader035.fdocuments.net/reader035/viewer/2022062803/56649f3d5503460f94c5d30f/html5/thumbnails/13.jpg)
Linear Probing: Delete
Can be tricky ... How to maintain the consistency of the
hash tableWhat is the simplest deletion strategy you
can think of?
![Page 14: Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.](https://reader035.fdocuments.net/reader035/viewer/2022062803/56649f3d5503460f94c5d30f/html5/thumbnails/14.jpg)
Quadratic Probing
f(i) = i2
![Page 15: Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.](https://reader035.fdocuments.net/reader035/viewer/2022062803/56649f3d5503460f94c5d30f/html5/thumbnails/15.jpg)
Double Hashing
f(i) = i*hash2(x)
E.g.: hash2(x) = 7 – (x % 7)
What if hash2(x) == 0 for some x?
![Page 16: Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.](https://reader035.fdocuments.net/reader035/viewer/2022062803/56649f3d5503460f94c5d30f/html5/thumbnails/16.jpg)
Rehashing
Hash Table may get full No more insertions possible
Hash table may get too full Insertions, deletions, search take longer time
Solution: Rehash Build another table that is twice as big and has a new hash function Move all elements from smaller table to bigger table
Cost of Rehashing = O(N) But happens only when table is close to full Close to full = table is X percent full, where X is a tunable parameter
![Page 17: Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.](https://reader035.fdocuments.net/reader035/viewer/2022062803/56649f3d5503460f94c5d30f/html5/thumbnails/17.jpg)
Rehashing Example
After RehashingOriginal Hash Table
After Inserting 23
![Page 18: Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.](https://reader035.fdocuments.net/reader035/viewer/2022062803/56649f3d5503460f94c5d30f/html5/thumbnails/18.jpg)
Rehashing Implementation