Presentation is loading. Please wait.

Presentation is loading. Please wait.

CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.

Similar presentations


Presentation on theme: "CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and."โ€” Presentation transcript:

1 CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount (Wiley 2004) and slides from Nancy M. Amato And Jory Denny 1 1

2 Hash Tables Sometimes a key can be interpreted or transformed into an address. In this case, we can use an implementation called a hash table for the Map ADT. Hash tables Essentially an array ๐ด of size ๐‘ (either to an element itself or to a โ€œbucketโ€) A Hash function โ„Ž ๐‘˜ โ†’[0,๐‘โˆ’1], โ„Ž(๐‘˜) is referred to as the hash value Example - โ„Ž ๐‘˜ =๐‘˜ mod ๐‘ Goal is to store elements ๐‘˜, ๐‘ฃ at index ๐‘–=โ„Ž ๐‘˜

3 Issues with Hash Tables
Collisions - some keys will map to the same index of H (otherwise we have a Direct Address Table). Chaining - put values that hash to same location in a linked list (or a โ€œbucketโ€) Open addressing - if a collision occurs, have a method to select another location in the table. Load factor Rehashing

4 Example We design a hash table for a Map storing items (SSN, Name), where SSN (social security number) is a nine-digit positive integer Our hash table uses an array of size ๐‘=10,000 and the hash function โ„Ž ๐‘˜ = last four digits of ๐‘˜ ๏ƒ† 1 2 3 4 9997 9998 9999 โ€ฆ

5 Hash Functions A hash function is usually specified as the composition of two functions: Hash code: โ„Ž 1 : keys ๏‚ฎ integers Compression function: โ„Ž 2 : integers ๏‚ฎ [0, ๐‘โˆ’1] The hash code is applied first, and the compression function is applied next on the result, i.e., โ„Ž ๐‘˜ = โ„Ž 2 โ„Ž 1 ๐‘˜ The goal of the hash function is to โ€œdisperseโ€ the keys in an apparently random way

6 Hash Codes Memory address: Component sum: Integer cast:
We reinterpret the memory address of the key object as an integer Good in general, except for numeric and string keys Integer cast: We reinterpret the bits of the key as an integer Suitable for keys of length less than or equal to the number of bits of the integer type (e.g., byte, short, int and float in C++) Component sum: We partition the bits of the key into components of fixed length (e.g., 16 or 32 bits) and we sum the components (ignoring overflows) Suitable for numeric keys of fixed length greater than or equal to the number of bits of the integer type (e.g., long and double in C++)

7 Hash Codes Polynomial accumulation: Cyclic Shift:
We partition the bits of the key into a sequence of components of fixed length (e.g., 8, 16 or 32 bits) ๐‘Ž 0 ๐‘Ž 1 โ€ฆ ๐‘Ž ๐‘›โˆ’1 We evaluate the polynomial ๐‘ ๐‘ง = ๐‘Ž 0 + ๐‘Ž 1 ๐‘ง+ ๐‘Ž 2 ๐‘ง 2 +โ€ฆ+ ๐‘Ž ๐‘›โˆ’1 ๐‘ง ๐‘›โˆ’1 at a fixed value z, ignoring overflows Especially suitable for strings (e.g., the choice z = 33 gives at most 6 collisions on a set of 50,000 English words) Cyclic Shift: Like polynomial accumulation except use bit shifts instead of multiplications and bitwise or instead of addition Can be used on floating point numbers as well by converting the number to an array of characters

8 Compression Functions
Division: โ„Ž 2 ๐‘˜ = ๐‘˜ mod ๐‘ The size N of the hash table is usually chosen to be a prime (based on number theory principles and modular arithmetic) Multiply, Add and Divide (MAD): โ„Ž 2 ๐‘˜ = ๐‘Ž๐‘˜+๐‘ mod ๐‘ ๐‘Ž and ๐‘ are nonnegative integers such that ๐‘Ž mod ๐‘โ‰ 0 Otherwise, every integer would map to the same value ๐‘

9 Collision Resolution with Separate Chaining
Collisions occur when different elements are mapped to the same cell Separate Chaining: let each cell in the table point to a linked list of entries that map there Chaining is simple, but requires additional memory outside the table ๏ƒ† 1 2 3 4

10 Exercise Separate chaining
Assume you have a hash table ๐ป with ๐‘=9 slots (๐ด[0โˆ’8]) and let the hash function be โ„Ž ๐‘˜ =๐‘˜ mod ๐‘ Demonstrate (by picture) the insertion of the following keys into a hash table with collisions resolved by chaining 5, 28, 19, 15, 20, 33, 12, 17, 10

11 Collision Resolution With Open Addressing - Linear Probing
Example: โ„Ž ๐‘˜ =๐‘˜ mod 13 Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in this order In Open addressing the colliding item is placed in a different cell of the table Linear probing handles collisions by placing the colliding item in the next (circularly) available table cell. So the ๐‘–th cell checked is: โ„Ž ๐‘˜, ๐‘– = โ„Ž ๐‘˜ +๐‘– mod ๐‘ Each table cell inspected is referred to as a โ€œprobeโ€ Colliding items lump together, causing future collisions to cause a longer probe sequence 1 2 3 4 5 6 7 8 9 10 11 12 41 18 44 59 32 22 31 73

12 Search with Linear Probing
Algorithm find ๐‘˜ ๐‘–โ†โ„Ž ๐‘˜ ๐‘โ†0 repeat ๐‘โ†๐ด ๐‘– if ๐‘โ‰ โˆ… return ๐‘›๐‘ข๐‘™๐‘™ else if ๐‘.๐‘˜๐‘’๐‘ฆ()=๐‘˜ return ๐‘ else ๐‘–โ† ๐‘–+1 mod ๐‘ ๐‘โ†๐‘+1 until ๐‘=๐‘ Consider a hash table A that uses linear probing find ๐‘˜ We start at cell โ„Ž ๐‘˜ We probe consecutive locations until one of the following occurs An item with key ๐‘˜ is found, or An empty cell is found, or ๐‘ cells have been unsuccessfully probed

13 Updates with Linear Probing
To handle insertions and deletions, we introduce a special object, called AVAILABLE, which replaces deleted elements erase ๐‘˜ We search for an item with key ๐‘˜ If such an item ๐‘˜, ๐‘ฃ is found, we replace it with the special item AVAILABLE put ๐‘˜, ๐‘ฃ We start at cell โ„Ž(๐‘˜) We probe consecutive cells until one of the following occurs A cell ๐‘– is found that is either empty or stores AVAILABLE, or ๐‘ cells have been unsuccessfully probed

14 Exercise Open Addressing โ€“ Linear Probing
Assume you have a hash table ๐ป with ๐‘=11 slots (๐ด[0โˆ’10]) and let the hash function be โ„Ž ๐‘˜ =๐‘˜ mod ๐‘ Demonstrate (by picture) the insertion of the following keys into a hash table with collisions resolved by linear probing. 10, 22, 31, 4, 15, 28, 17, 88, 59

15 Collision Resolution with Open Addressing โ€“ Quadratic Probing
Linear probing has an issue with clustering Another strategy called quadratic probing uses a hash function โ„Ž ๐‘˜,๐‘– = โ„Ž ๐‘˜ + ๐‘– 2 mod ๐‘ for ๐‘–=0, 1, โ€ฆ, ๐‘โˆ’1 This can still cause secondary clustering

16 Collision Resolution with Open Addressing - Double Hashing
Double hashing uses a secondary hash function โ„Ž 2 (๐‘˜) and handles collisions by placing an item in the first available cell of the series โ„Ž ๐‘˜, ๐‘– = โ„Ž 1 ๐‘˜ +๐‘– โ„Ž 2 ๐‘˜ mod ๐‘ for ๐‘–=0, 1, โ€ฆ, ๐‘โˆ’1 The secondary hash function โ„Ž 2 ๐‘˜ cannot have zero values The table size ๐‘ must be a prime to allow probing of all the cells Common choice of compression map for the secondary hash function: โ„Ž 2 ๐‘˜ =๐‘žโˆ’ ๐‘˜ mod ๐‘ž where ๐‘ž<๐‘ ๐‘ž is a prime The possible values for โ„Ž 2 ๐‘˜ are 1, 2, โ€ฆ, ๐‘ž

17 Performance of Hashing
In the worst case, searches, insertions and removals on a hash table take ๐‘‚ ๐‘› time The worst case occurs when all the keys inserted into the map collide The load factor ๐œ†= ๐‘› ๐‘ affects the performance of a hash table Assuming that the hash values are like random numbers, it can be shown that the expected number of probes for an insertion with open addressing is 1 1โˆ’๐œ† = 1 1โˆ’ ๐‘› ๐‘ = 1 ๐‘โˆ’๐‘› ๐‘ = ๐‘ ๐‘โˆ’๐‘› The expected running time of all the Map ADT operations in a hash table is ๐‘‚ 1 In practice, hashing is very fast provided the load factor is not close to 100% Applications of hash tables Small databases Compilers Browser caches

18 Uniform Hashing Assumption
The probe sequence of a key ๐‘˜ is the sequence of slots probed when looking for ๐‘˜ In open addressing, the probe sequence is โ„Ž ๐‘˜, 0 , โ„Ž ๐‘˜,1 , โ€ฆ, โ„Ž ๐‘˜, ๐‘โˆ’1 Uniform Hashing Assumption Each key is equally likely to have any one of the ๐‘! permutations of {0, 1, โ€ฆ, ๐‘โˆ’1} as is probe sequence Note: Linear probing and double hashing are far from achieving Uniform Hashing Linear probing: ๐‘ distinct probe sequences Double Hashing: ๐‘ 2 distinct probe sequences

19 Performance of Uniform Hashing
Theorem: Assuming uniform hashing and an open-address hash table with load factor ๐œ†= ๐‘› ๐‘ <1, the expected number of probes in an unsuccessful search is at most 1 1โˆ’๐œ† . Exercise: compute the expected number of probes in an unsuccessful search in an open address hash table with ๐œ† = 1 2 , ๐œ†= 3 4 , and ๐œ†=

20 On Rehashing Keeping the load factor low is vital for performance
When resizing the table: Reallocate space for the array Design a new hash function (new parameters) for the new array size For each item you reinsert it into the table

21 Summary Maps/Dictionaries (so far)
put(๐‘˜, ๐‘ฃ) find(๐‘˜) Space Log File ๐‘‚(1) ๐‘‚(๐‘›) Direct Address Table (map only) ๐‘‚(๐‘) Lookup Table (ordered map/dictionary) ๐‘‚( log ๐‘› ) Hashing (chaining) ๐‘‚(๐‘›/๐‘) ๐‘‚(๐‘›+๐‘) (open addressing) ๐‘‚ 1 1โˆ’ ๐‘› ๐‘


Download ppt "CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and."

Similar presentations


Ads by Google