Presentation is loading. Please wait.

Presentation is loading. Please wait.

NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 8 Hashing 1.

Similar presentations


Presentation on theme: "NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 8 Hashing 1."— Presentation transcript:

1 NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 8 Hashing 1

2 NCUE CSIE Wireless Communications and Networking Laboratory Hashing Definition: In hashing the dictionary pairs are stored in a table, ht, called the hash table. The hash table is partitioned into b buckets, ht[0],…,ht[b-1]. The address or location of a pair is determined by a hash function, h, which maps keys into buckets. Thus, for any key k, h(k) is an integer in the range 0 through b-1. 2

3 NCUE CSIE Wireless Communications and Networking Laboratory Hash Table index b-1 Bucket x H(x) 3

4 NCUE CSIE Wireless Communications and Networking Laboratory Terminologies 4 Identifier density: The identifier density of a hash table is the ratio n/T, where n is the number of identifiers in the table. The loading density or loading factor of a hash table is (T: distinct possible value of identifiers. s: number of slots per bucket. b: bucket number) utilizationCollision

5 NCUE CSIE Wireless Communications and Networking Laboratory 5 Terminologies Collision: A collision occurs when the home bucket for the new pair is not empty at the time of insertion. Overflow: Since many keys typically have the same home bucket, it is possible that the home bucket for a new dictionary pair is full at the time we wish to insert this pair into the dictionary. collisionoverflow not necessarily necessarily

6 NCUE CSIE Wireless Communications and Networking Laboratory Hashing Function Hashing Function Design : (1) Easy to Compute (2) Minimize the Number of Collisions (3) Uniform Hash function 6

7 NCUE CSIE Wireless Communications and Networking Laboratory Hashing Function Hashing Function :  Mid-Square  Division (Modulus)  Folding  Digit Analysis 7

8 NCUE CSIE Wireless Communications and Networking Laboratory Mid-square The mid-square hash function determines the home bucket for a key by squaring the key and then using an appropriate number of bits from the middle of the square to obtain the bucket address. ex) We assume the key = 8125 , and hashing table has 1000 buckets. 8 so address is “156” or “015”

9 NCUE CSIE Wireless Communications and Networking Laboratory Divisions The home bucket is obtained by using the modulo (%) operator. The key x is divided by some number M, and the remainder is used as the home bucket for x. f(x) = x mod M ex) 9 prime

10 NCUE CSIE Wireless Communications and Networking Laboratory Folding In this method the key k is partitioned into several parts, all but possibly the last being of the same length. These partitons are then added together to obtain the hash address for k. There are two ways of carrying out this additon. (1) Shift (2) Boundary ex1) We assume the key = , and hashing table has 1000 buckets. 123|203|241|112|20 (1) =699 (2) =897 10

11 NCUE CSIE Wireless Communications and Networking Laboratory Folding 11 ex2) (1) Shift (2) Boundary

12 NCUE CSIE Wireless Communications and Networking Laboratory Digit Analysis All the keys in the table are known in advance. Each key is interpreted as a number using some radix r. The same radix is used for all the keys in the table. Using this radix, the digits of each key are examined. ex) 12 phone number address

13 NCUE CSIE Wireless Communications and Networking Laboratory Overflow Handling  Linear Open Addressing (Linear Probing)  Quadratic Probing  Rehashing  Chaining 13

14 NCUE CSIE Wireless Communications and Networking Laboratory Linear Probing When the overflow occurs, we search the hash table buckets in the order (H(x)+1, H(x)+2…), until the hash table is full or reaching the first unfilled bucket. ex) Insert Insert

15 NCUE CSIE Wireless Communications and Networking Laboratory Linear Probing Advantages Advantages: Simple 、 Easy to Implement 。Disadvantages: When the clustering occurs, the search time will increase rapidly 。 15

16 NCUE CSIE Wireless Communications and Networking Laboratory Quadratic Probing H(x), overflow 16 When the overflow occurs, we search the hash table buckets by using ex) Key k, hash function H 1 st search : H(k) 2 nd search : (H(k)+1 2 )%b 3 th search : (H(k)-1 2 )%b 4 th search : (H(k)+2 2 )%b 5 th search : (H(k)-2 2 )%b N th search : (H(k)±((B-1)/2) 2 )%b

17 NCUE CSIE Wireless Communications and Networking Laboratory Rehashing The rehashing method is to use a series of hash functions h 1,h 2,…,h m. Buckets h i (k), 1 ≦ i ≦ m are examined in the order. 17

18 NCUE CSIE Wireless Communications and Networking Laboratory Chaining Many of the comparisons can be saved if we maintain lists of keys, one list per bucket, each list containing all the synonyms for that bucket Insert Insert 25 25

19 NCUE CSIE Wireless Communications and Networking Laboratory Assume that a hash function has the following characteristics: keys 257 and 567 hash to 3 keys 987 and 313 hash to 6 keys 734, 189 and 575 hash to 5 keys 122 and 391 hash to 8 Assume that insertions are done in order 257, 987, 122, 575, 189, 734, 567, 313, 391 (1)Indicate the position of the data if open probe addressingis used to resolve collision (2) Indicate the position of the data if chining with separate lists is used to resolve collision Hash Table 19 Question:

20 NCUE CSIE Wireless Communications and Networking Laboratory If H(x) = x mod 7 and separate chaining resolves collisions, What does the hash table look like after the following insertions occur: 8, 10, 24, 15, 32, 17? Assume that each table item contains only a search key. 20 Question:

21 NCUE CSIE Wireless Communications and Networking Laboratory Suppose the hashing function f(x) = x mod 11 is used to hash a list of input value (in the given order) into a hash table implemented by the array bucked[0],bucket[1],…bucket[10]. The inputs are 10,100,32,45,126,3,24,200,and 53. Each bucket can hold only one number. Overflow is resolved by quadratic probing, which examines buckets f(x), (f(x)+ ) mod 11,and (f(x)- ) mod 11, i=1 to 5.Show the final contents in bucket[0] to bucket[10]. 21 Question:

22 NCUE CSIE Wireless Communications and Networking Laboratory Ans:

23 NCUE CSIE Wireless Communications and Networking Laboratory For each hash table below, show the result of inserting the following sequence of key values, in the given order, into an initially empty hash table of that type: 26,17,20,9,34,32,15,21. In both cases, assume a hash table size of 11 and a hash function h(x) = x mod 11. (1)Static hash table that uses chaining (2)Hash table that uses linear probing 23 Question:

24 NCUE CSIE Wireless Communications and Networking Laboratory (1) (2) → →26→15 5→ →20→9 10→32→

25 NCUE CSIE Wireless Communications and Networking Laboratory 25 Reference  Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed 〝 Fundamentals of Data Structures in C 〞, W. H. Freeman & Co Ltd,  Ellis Horowitz, Sartaj Sahni, and Dinesh Mehta 〝 Fundamentals of Data Structures in C++ 〞 Silicon Pr, 2006  Richard F.Gilberg, Behrouz A. Forouzan, 〝 Data Structures: A Pseudocode Approach with C 〞, S Baker & Taylor Books, 2004  Fred Buckley, and Marty Lewinter 〝 A Friendly Introduction to Graph Theory 〞 Prentice Hall, 2002  〝資料結構 - 使用 C 語言〞蘇維雅譯,松崗, 2004  〝資料結構 - 使用 C 語言〞 蔡明志編著,全華, 2004  〝資料結構 ( 含精選試題 ) 〞洪逸編著,鼎茂, 2005


Download ppt "NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 8 Hashing 1."

Similar presentations


Ads by Google