Presentation is loading. Please wait.

Presentation is loading. Please wait.

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-1 Chapter 8 Hashing Introduction to Data Structure CHAPTER 8 HASHING 8.1 Symbol Table Abstract Data.

Similar presentations


Presentation on theme: "Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-1 Chapter 8 Hashing Introduction to Data Structure CHAPTER 8 HASHING 8.1 Symbol Table Abstract Data."— Presentation transcript:

1 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-1 Chapter 8 Hashing Introduction to Data Structure CHAPTER 8 HASHING 8.1 Symbol Table Abstract Data Type 8.2 Static Hashing 8.3 Dynamic Hashing

2 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-2 Chapter 8 Hashing Chapter 1 Basic Concepts Chapter 2 Arrays Chapter 3 Stacks and Queues Chapter 4 Linked Lists Chapter 5 Trees Chapter 6 Graph Chapter 7 Sorting Chapter 8 Hashing Chapter 9 Heap Structures Chapter 10 Search Structures Contents

3 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-3 Chapter 8 Hashing 8.1 Symbol Table Definition A symbol table is a set of name-attribute pairs E.g., thesaurus (name: word, attribute: a list of synonyms) data dictionary in database management General operations performed on a symbol table determine if a particular name is in the table retrieve the attribute of that name modify the attributes of that name insert a new name and its attributes delete a name and its attributes ADT of Symbol Table, p. 396 NameAttribute

4 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-4 Chapter 8 Hashing Symbol Table (cont.) Data structure for symbol table Binary search tree Worst case complexities for the operations are O(n) Best case … are O(log n) Hashing technique Can provide a good performance for search, insert, and delete Instead of using comparisons to perform search, hashing relies on a formula called the hash function Two types of hashing static hashing dynamic hashing

5 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-5 Chapter 8 Hashing 8.2 Static Hashing Hash table A fixed-size table used to store identifiers. The memory available to maintain the symbol table (hash table) is assumed to be sequential. The hash table ht consists of b buckets and each bucket contains s records. An arithmetic function f(x) used to compute the address of location (0 through b-1) of an identifier, x E.g., If the identifier is 6 alpha-numerical long, with the first one being a letter. Then, the total of distinct identifiers are

6 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-6 Chapter 8 Hashing Static Hashing (cont.) Terminology Identifier density: the identifier density of a hash table is the ratio n/T, where n: the number of identifiers in the table T: the total number of possible identifiers Loading density: the loading density or loading factor is α= n/(sb) Two identifiers, i 1, and i 2, are said to be synonyms with respect to f if f(i 1 ) = f(i 2 ) Hash overflow: an overflow occurs when a new identifier i is mapped or hashed by f into a full bucket Hash collision: a collision occurs when two non-identical identifiers are hashed into the same bucket

7 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-7 Chapter 8 Hashing Hash table: Example acosatan charceil define exp floatfloor 0 1 2 3 4 5 6 25 Slot 0 Slot 1 A hash table ht with b = 26 buckets and s = 2 # identifiers, n = 10 Loading factor  = 10/52 = 0.19 Hashing function f(x) = the first character of x

8 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-8 Chapter 8 Hashing Hashing Functions Definition A hash function, f, transforms an identifier, x, into a bucket address in the hash table. Characteristics of ideal hashing functions Easy to compute and result in very few collisions Should provide a mechanism to handle overflow Should not have bias on the use of the hash table i.e., a random x has an equal chance of hashing into any of the b buckets

9 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-9 Chapter 8 Hashing Hash Function: Mid Square Mid-Square function, f m squaring the identifier and then using an appropriate number of bits from the middle of the square to obtain the bucket address Since the middle bits of the square usually depend on all the characters in the identifier, different identifiers are expected to result in different hash addresses with high probability. The number of bits dependents on the table size e.g., if r bits are used, the range of values is 2 r Example Let x = ‘A’ = 65, x 2 = 4225 = 1000010000001 r = 5,  f(x) = 8

10 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-10 Chapter 8 Hashing Hash Function: Division Division function, f D An identifier x is divided by some number M and the remainder is used as the hash address for x. f D (x) = x % M The bucket addresses are in the range of 0 through M-1. Example Let x = ‘A’ = 65, M = 16. Then f D (x) = 65 % 16 = 1

11 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-11 Chapter 8 Hashing Hash Function: Division (cont.) Bad choice of M If M is a power of 2, then f D (x) depends only on the least significant bits of x. => identifiers with the same suffix would result in collisions Example x 1 = ‘BA’, x 2 = ‘CA’ If M is divisible by two, the odd keys are mapped to odd buckets and even keys are mapped to even buckets. => the hash table is biased Good choice of M M a prime number

12 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-12 Chapter 8 Hashing Hash Function: Folding Concept The identifier x is partitioned into several parts, all but the last being of the same length. All partitions are added together to obtain the hash address for x. Two methods Method 1: Shift folding different partitions are added together to get f(x) Method 2: Folding at the boundaries identifier is folded at the partition boundaries, and digits falling into the same position are added together to obtain f(x) similar to reversing every other partition and then adding

13 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-13 Chapter 8 Hashing Hash Function: Folding (cont.) Example x = 123|203|241|112|20 are partitioned into three decimal digits long x 1 = 123, x 2 = 203, x 3 = 241, x 4 = 112, x 5 = 20 Shift folding: Folding at the boundaries x 2 = 302, x 4 = 211

14 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-14 Chapter 8 Hashing Digit Analysis This method is useful when a static file where all the identifiers in the table are known in advance. Concept Each identifier x is transformed into a number using some radix r. The digits of each identifier are examined. Digits having most skewed distributions are deleted. Enough digits are deleted so that the number of remaining digits is small enough to give an address in the range of the hash table. Example x = ‘data’ ====> (1009711697) 10 Frequency: 0:2, 1: 3, 6:1, 7:2, 9:2 Average frequency = 2,  the most skewed digits: 1 and 6  f(x) = (009797) 10 ASCII

15 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-15 Chapter 8 Hashing Overflow Handling There are two ways to handle overflow Open addressing Linear probing Quadratic probing Rehashing Random hashing … Chaining

16 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-16 Chapter 8 Hashing Open Addressing Assumes the hash table is an array. The hash table is initialized so that each slot contains the null identifier. When a new identifier is hashed into a full bucket, find the closest unfilled bucket. This is called linear probing or linear open addressing

17 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-17 Chapter 8 Hashing Open Addressing (cont.) Example Assume 26-bucket table with one slot per bucket and the following identifiers: GA, D, A, G, L, A2, A1, A3, A4, Z, ZA, E. Let the hash function f(x) = first character of x. When entering G, G collides with GA and is entered at ht[7] instead of ht[6]. A2 A1 A A3 A4 D G ZA GA E 0 1 2 3 4 5 6 7 8 9 25

18 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-18 Chapter 8 Hashing Linear Probing When linear probing is used to handle overflows, a hash table search for identifier x proceeds as follows: compute f(x) examine identifiers at positions ht[f(x)], ht[f(x)+1], …, ht[f(x)+j], until one of the following condition happens: ht[f(x)+j]=x; in this case x is found ht[f(x)+j] is null; x is not in the table We return to the starting position f(x); the table is full and x is not in the table

19 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-19 Chapter 8 Hashing Linear Probing (cont.) Problem with linear probing Identifiers tend to cluster together. Also adjacent clusters tend to coalesce. This increases search time. Example To find ZA, you need to examine ht[25], ht[0], …, ht[8] (total of 10 comparisons). To retrieve each identifier once, a total of 39 buckets are examined (average 3.25 bucket per identifier). The expected average number of identifier comparisons, p, to look up an identifier is approximately (2 -α)/(2-2α), whereαis the loading density. α=12/26=.47 and p = 1.5. Even though the average number of probes is small, the worst case can be quite large. A2 A1 A A3 A4 D G ZA GA E Z 0 1 2 3 4 5 6 7 8 9 25

20 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-20 Chapter 8 Hashing Quadratic Probing A quadratic probing scheme improves the growth of clusters. Concept A quadratic function of i is used as the increment when searching through buckets. Perform search by examining bucket f(x), (f(x)+i 2 ) % b, (f(x)-i 2 ) % b for 1 ≤ i ≤ (b-1)/2. When b is a prime number of the form 4j+3, for j an integer, the quadratic search examine every bucket in the table.

21 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-21 Chapter 8 Hashing Chaining Motivation Linear probing perform poorly because the search for an identifier involves comparisons with identifiers that have different hash values. Unnecessary comparisons can be avoided if all the synonyms are put in the same list, where one list per bucket. As the size of the list is unknown before hand, it is best to use linked chain. Each chain has a head node. Head nodes are stored sequentially.

22 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-22 Chapter 8 Hashing Chaining: Example 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 25 10 11 ht A4 A3 A1 A2 A0 D0 E0 G GA0 L0 ZA Z0

23 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-23 Chapter 8 Hashing Chaining (cont.) The expected number of identifier comparisons can be shown to be ~ 1 +α/2, where αis the loading density n/b (b=number of head nodes). For α=0.5, it’s 1.25. And if α=1, then it’s about 1.5. Another advantage of this scheme is that only the b head nodes must be sequential and reserved at the beginning. The scheme only allocates other nodes when they are needed. This could reduce overall space requirement for some load densities, despite of links.


Download ppt "Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-1 Chapter 8 Hashing Introduction to Data Structure CHAPTER 8 HASHING 8.1 Symbol Table Abstract Data."

Similar presentations


Ads by Google