Docsity

Context for Hashing - Lecture Notes | CPSC 5115G, Study notes of Computer Science

Toggle sidebar
bg1
CONTEXT FOR HASHING
Hashing was developed in the 1950’s by researchers at IBM.
This research was in support of efforts to develop an efficient compiler for
high–level languages, such as FORTRAN.
All modern compilers, including these early ones, use multiple passes of the source code.
The first pass generates the symbol table, which includes a description of every
variable used in the program.
The goal of the research was to produce a data structure for the symbol table that
could be searched efficiently and implemented with little overhead.
Consider a collection of N symbols, where commonly 100 N 1000.
Storing the collection in an unordered linked list would yield a search time in (N).N).
Storing the collection in an ordered linked list would yield a search time in (N). log(N).N) ).
Storing the collection in a properly indexed array would yield a search time in (N).1);
that is, the search time would be constant for any number of elements.
The question now becomes one of indexing the array for efficient search.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Context for Hashing - Lecture Notes | CPSC 5115G and more Study notes Computer Science in PDF only on Docsity!

CONTEXT FOR HASHING

Hashing was developed in the 1950’s by researchers at IBM. This research was in support of efforts to develop an efficient compiler for high–level languages, such as FORTRAN. All modern compilers, including these early ones, use multiple passes of the source code. The first pass generates the symbol table , which includes a description of every variable used in the program. The goal of the research was to produce a data structure for the symbol table that could be searched efficiently and implemented with little overhead. Consider a collection of N symbols, where commonly 100  N  1000. Storing the collection in an unordered linked list would yield a search time in (N).N). Storing the collection in an ordered linked list would yield a search time in (N). log(N).N) ). Storing the collection in a properly indexed array would yield a search time in (N).1); that is, the search time would be constant for any number of elements. The question now becomes one of indexing the array for efficient search.

Variable Names in FORTRAN IV

The constraint in early FORTRAN was that a variable name could contain 6 characters.

  1. All characters were either upper case letters or digits.
  2. The first character must be an upper case letter.
  3. Embedded spaces were not legal, but would usually be ignored by the compiler. The size of the valid name space for FORTRAN variables is easily enumerated. One character names 26 = 26 Two character names 26  36 = 936 Three character names 26  36 2 = 33, Four character names 26  36 3 = 1,213, Five character names 26  36 4 = 43,670, Six character names 26  36 5 = 1,572,120, Total 1,617,038, Of this space, fewer than one entry in a million would be used by a typical program. How does one insert names from such a large space into a reasonably–sized array?

Example of Hashing M = 151 C = 301 “REORDER” The word has seven characters; S – 1 = 6 I = 0 c = ‘R’ h = (N). 0  301 + 82) mod 151 = 82 mod 151 = 82 I = 1 c = ‘E’ h = (N). 82  301 + 69) mod 151 = 24571 mod 151 = 138 I = 2 c = ‘O’ h = (N). 138  301 + 79) mod 151 = 41617 mod 151 = 92 I = 3 c = ‘R’ h = (N). 92  301 + 82) mod 151 = 27774 mod 151 = 141 I = 4 c = ‘D’ h = (N). 141  301 + 68) mod 151 = 42509 mod 151 = 78 I = 5 c = ‘E’ h = (N). 78  301 + 69) mod 151 = 23547 mod 151 = 142 I = 6 c = ‘R’ h = (N). 142  301 + 82) mod 151 = 42824 mod 151 = 91 By this calculation we have h(N).“REORDER”) = 91. Set H[91] = “REORDER”, if H[91] is not occupied. Collision K 1 ¹ K 2 but h(N).K 1 ) = = h(N).K 2 ) Suppose K 1 = “REORDER”, with h(N).K 1 ) = 91 K 2 = “ALPHABET”, with h(N).K 2 ) = 91. Try to insert K 2 into H[91], but that slot is occupied. We have a collision.

The Book’s Hash Example A FOOL AND HIS MONEY ARE SOON PARTED Let h(N).K) be as defined in the textbook. h(N).A) = 1 h(N).ARE) = 11 h(N).FOOL) = 9 h(N).SOON) = 11 h(N).AND) = 6 h(N).PARTED) = 12 h(N).HIS) = 10 h(N).MONEY) = 7 OPEN HASHING H[ 0 .. (N).M – 1) ] is an array of header nodes for linked lists. Here we have M = 13 and H[0 .. 12] Remember that the array must be 0–based. As always, the value part of the node could contain other data.

Closed hashing – one item per array entry N words in M slots Load factor a = N / M. We require that a < 1.0, and prefer that a < 0.5. Handle collision by computing a new index for the colliding key. Consider two keys K 1 and K 2 , with K 2 ¹ K 1. Let h 1 = h(N).K 1 ). Assume H[h 1 ] is empty. The algorithm places K 1 into H[h 1 ]. Try to place K 2 by computing h(N).K 2 ) and find h(N).K 2 ) = h(N).K 2 ) = h 1. Closed hashing is “first come, first served”, so the key K 2 cannot be placed in the slot already occupied by K 1. K 2 must be placed elsewhere. There are two main strategies for placing the colliding key. These can work only if a < 1.0 and will be quite slow if a  0.75 (N).approximately). Linear Probing Let h = h 1 , and repeatedly try h = (N).h + 1) Mod M until H[h] is empty Double Hashing Use a second hash function s(N).K). Initialize h = h 1. Compute h = (N).h + s(N).K)) mod M until H[h] is empty

Insertion and Deletion in Closed Hashing Insertion

  1. If a = 1.0, raise an exception.
  2. Compute h = h(N).K)
  3. If H[h] is blank, then set H[h] = K and set H[h].Deleted = FALSE
  4. If H[h] = = K, then set H[h].Deleted = FALSE
  5. If H[h] ¹ K and H[h] is not blank, compute a new hash value h (N).using either linear probing or double hashing) and go to 3. Deletion
  6. Compute h = h(N).K).
  7. If H[h] is blank, then exit the routine; key K has not been found.
  8. If H[h] = = K, then set H[h].Deleted = TRUE. The key is marked as deleted.
  9. If H[h] ¹ K and H[h] is not blank, compute a new hash value h (N).using either linear probing or double hashing) and go to 2.

Summary and a Few Examples

Hashing is the process of creating indices into a table of size M for each of a list of entries to be inserted into that table. Because modulo–S arithmetic is used in creating these indices, the table is always implemented as a 0–based array: H[0 .. (N).M – 1)]. The ideal hash function will place entries into the table in a fashion that appears to be random, almost creating a uniform distribution. One common approach to hashing creates the table with size M being a prime number. Experience shows that this helps in distributing the keys and avoiding clusters. A cluster is a sequence of contiguously occupied slots in the hash table. This presents a problem for the use of linear probing to resolve collisions. Hashing relies on two or three functions to create the index.

  1. A function, here called “MakeInt” that converts the key to a positive integer. MakeInt: Key ® Integer
  2. The primary hash function: h 1 = (N). MakeInt(N).Key) ) mod M
  3. For double hashing, a second hash function with another key is used.

Selection of the Hash Table Size

As noted above, experience indicates that setting the table size to a prime number will yield better distributions of the hashed entries. If double hashing is used to resolve collisions, then it is desirable to select the table size M such that each of (N).M – 2) and M is a prime number. These sets are called twin primes. Examples include: 11 and 13. 17 and 19 41 and 43 71 and 73 101 and 103 107 and 109 311 and 313 There are many more. One last pair 99,989 and 99, Our example will use the pair 11 and 13. The table size is 13; H[0 .. 12].

Hashing With Linear Probing

We shall insert keys in the following sequence: 119, 85, 43, 141, 72, 91, 109, 147, 38, 137, 148, and 101. At the beginning, the hash table is empty. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key Insert 119. h(N).119) = 2 Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key 119 Insert 85. h(N).85) = 7. No collisions yet. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key 119 85 Insert 43. h(N).43) = 4. No collisions yet. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key 119 43 85

Hashing With Linear Probing (Page 2)

Insert 141. h(N).141) = 11. No collisions yet. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key 119 43 85 141 Insert 72. h(N).72) = 7. 1 collision. Place after one probe. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key 119 43 85 72 141 Insert 91. h(N).91) = 0. No collision. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key 91 119 43 85 72 141 Insert 109. h(N).109) = 5. No collision. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key 91 119 43 109 85 72 141

Hashing With Linear Probing (Page 4)

Insert 148. h(N).148) = 5. There is a collision. Linear probing places this in address 10. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key 91 119 43 109 147 85 72 137 148 141 38 Note that it took six tries to place the key 148. We had to try addresses 5, 6, 7, 8, 9, and 10 before making a placement. Insert 102. h(N).201) = 10. There is a collision. Linear probing places this in address 1. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key 91 102 119 43 109 147 85 72 137 148 141 38 At this point, with a load factor of 11/13  0.846 before insertion, we should have expected quite a few probes to make the placement.

Hashing With Double Hashing

Again, we shall insert keys in the following sequence: 119, 85, 43, 141, 72, 91, 109, 147, 38, 137, 148, and 101. Nothing differs until we have a collision. At the beginning, the hash table is empty. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key Insert 119. h 1 (N).119) = 2 Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key 119 Insert 85. h 1 (N).85) = 7. No collisions yet. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key 119 85 Insert 43. h 1 (N).43) = 4. No collisions yet. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key 119 43 85

Hashing With Linear Double Hashing (Page 3)

Insert 147. h 1 (N).147) = 4. This is a collision. h 2 (N).147) = 1 + (N).147 mod 11) = 5. Generate A 2 = (N).4 + 5) mod 13 = 9 mod 13 = 9. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key 91 119 43 109 85 147 141 Insert 38. h 1 (N).38) = 12. This is a not a collision. Just place the key. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key 91 119 43 109 85 147 141 38 Insert 137. h 1 (N).137) = 7. This is a collision. h 2 (N).137) = 1 + (N).137 mod 11) = 6. Generate A 2 = (N).7 + 6) mod 13 = 13 mod 13 = 0. Still a collision. A 3 = (N).0 + 6) mod 13 = 6 mod 13 = 6. Place the key Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key 91 119 43 109 137 85 147 141 38

Hashing With Linear Double Hashing (Page 4)

Insert 148. h 1 (N).148) = 5. This is a collision. h 2 (N).148) = 1 + (N).148 mod 11) = 6. Generate A 2 = (N).5 + 6) mod 13 = 11 mod 13 = 11. Still a collision. A 3 = (N).11 + 6) mod 13 = 17 mod 13 = 4. Still a collision. A 4 = (N).4 + 6) mod 13 = 10 mod 13 = 10. Place the key. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key 91 119 43 109 137 85 147 148 141 38 Insert 101. h 1 (N).101) = 10. This is a collision. h 2 (N).101) = 1 + (N).101 mod 11) = 3. Generate A 2 = (N).10 + 3) mod 13 = 13 mod 13 = 0. Still a collision. A 3 = (N).0 + 3) mod 13 = 3 mod 13 = 3. Place the key. Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Key 91 119 101 43 109 137 85 147 148 141 38