Design and Analysis of Algorithms Hash Tables Haidong Xue Summer 2012, at GSU
Dictionary operations INSERT DELETE SEARCH O(1) “A hash table is an effective data structure for implementing dictionaries” – textbook page 253 Very likelyWorst case O(1)
Direct-address tables Direct-address table: SEARCH(S, 6) INSERT(S, ) DELETE(S, ) 7 4 O(1) What’s the problem here? When the range of element is in [1, 30000]….. Direct-addressing: use keys as addresses
Hash tables Can we have O(1) INSERT, DELETE AND SEARCH with less storage? Hash Table: Hash Function: h(x) = x mod 3 h(2) = 2 mod 3 = 2 h(3) = 3 mod 3 = 0 h(6) = 6 mod 3 = 0 h(1) = 1 mod 3 = 1 h(7) = 7 mod 3 = 1 h(5) = 5 mod 3 = 2 Multiple elements in one slot Collision! Yes!
Hash tables 0 12 Hash Table: SEARCH(S, 6) INSERT(S, ) DELETE(S, ) 7 4 O(1)+2 DELETE in 1-linked-list SEARCH in 0-linked-list INSERT in 1-linked-list O(1)+O(1) = O(1) (2 is the length of the linked-list) h(6)=6 mod 3=0 h(4)=4 mod 3=1 h(7)=7 mod 3=1 A common method is to put them into a linked-list, i.e. chaining What is the upper bound length? What is the average length?
Analysis of hash tables 0 12 Hash Table: 34 …….. n m m-1 … … …… … … Uniform hashing “each key is equally likely to hash to any of the m slots”
Analysis of hash tables …….. m-1 … … …… … … Therorem11.1 Unsuccessful search: Therorem11.2 Successful search: How to get uniform hashing? With the assumption of uniform hashing
Hash functions How to get uniform hashing? Uniform hashing “each key is equally likely to hash to any of the m slots” Division hashing Multiplication hashing Universal hashing To achieve this goal, many hashing methods are proposed:
Hash functions – division hashing h(k) = k mod m where k is value of key, m is the number of slots E.g.: – Final grades of all my students with a hash table of 10 slots – Items in grocery stores with a hash table of 10 slots 99 cents, large soda $1.99, ground beef $6.99, lamb What’s the problem here? What if we still use 10 slots?
Hash functions – division hashing What’s the problem here? e.g.: 99 mod 7 = mod 7 = mod 7 = 6
Hash functions – multiplication hashing h(k) = floor(m(kA mod 1)) where m is the number of slots and A is a constant number in (0, 1) E.g.: A=0.123, m=10 – 99*0.123= – 199*0.123= – 699*0.123= h(99)=floor(10*0.177)=1 h(199)=floor(10*0.477)=4 h(699)=floor(10*0.977)=9
Hash functions – universal hashing Theorem 11.3
Another method to deal with collisions: Open Address
Open addressing: 3612 Another method to deal with collisions: Open Address 3612 h(2, 0)=((2 mod 3) +0)mod 10=2 h(3, 0)=((3 mod 3) +0)mod 10=0 h(6, 0)=((6 mod 3) +0)mod 10=0 h(6, 1)=((6 mod 3) +1)mod 10=1 h(1, 0)=((1 mod 3) +0)mod 10=1 h(1, 1)=((1 mod 3) +1)mod 10=2 h(1, 2)=((1 mod 3) +2)mod 10=3