Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design and Analysis of Algorithms Hash Tables Haidong Xue Summer 2012, at GSU.

Similar presentations


Presentation on theme: "Design and Analysis of Algorithms Hash Tables Haidong Xue Summer 2012, at GSU."— Presentation transcript:

1 Design and Analysis of Algorithms Hash Tables Haidong Xue Summer 2012, at GSU

2 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)

3 51 23 4 6 7 8910 Direct-address tables 23617 5 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

4 0 12 23617 5 Hash tables Can we have O(1) INSERT, DELETE AND SEARCH with less storage? 23617 5 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!

5 Hash tables 0 12 Hash Table: 3 1 7 5 2 6 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?

6 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”

7 Analysis of hash tables 0 12 34 …….. m-1 … … …… … … Therorem11.1 Unsuccessful search: Therorem11.2 Successful search: How to get uniform hashing? With the assumption of uniform hashing

8 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:

9 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?

10 Hash functions – division hashing What’s the problem here? e.g.: 99 mod 7 = 1 199 mod 7 = 3 699 mod 7 = 6

11 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=12.177 – 199*0.123=24.477 – 699*0.123= 85.977 h(99)=floor(10*0.177)=1 h(199)=floor(10*0.477)=4 h(699)=floor(10*0.977)=9

12 Hash functions – universal hashing Theorem 11.3

13 Another method to deal with collisions: Open Address

14 4 0 12 3 5 6789 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


Download ppt "Design and Analysis of Algorithms Hash Tables Haidong Xue Summer 2012, at GSU."

Similar presentations


Ads by Google