1 / 6

Hashing

Hashing . Chapter 10 . Comparison of keys was the main operation used by the previous discussed searching methods . There is a different way of searching by calculates the position of the key based on the value of the key. So, the search time is reduced to O(1) from O(n) or from O(log n).

waneta
Télécharger la présentation

Hashing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Hashing Chapter 10

  2. Comparison of keys was the main operation used by the previous discussed searching methods . • There is a different way of searching by calculates the position of the key based on the value of the key. • So, the search time is reduced to O(1) from O(n) or from O(log n). • We need to find a function h that can transfer a key K (string, number, record, etc..) into an index the a table used for storing items of the same type as K. • This function is called hash function.

  3. Example: Suppose we want to store a sequence of randomly generated numbers, keys: 5, 17, 37, 20, 42, 3. The array A, the hash table, where we want to store the numbers: 0 1 2 3 4 5 6 7 8 | | | | | | | | | | We need a way of mapping the numbers to the array indexes, a hash function, that will let us store the numbers and later recompute the index when we want to retrieve them. There is a natural choice for this.

  4. Our hashtable has 9 fields and the mod function, which sends every integer to its remainder modulo 9, will map an integer to a number between 0 and 8. 5 mod 9 = 5 17 mod 9 = 8 37 mod 9 = 1 20 mod 9 = 2 42 mod 9 = 6 3 mod 9 = 3 We store the values: | | 37 | 20 | 3 | | 5 | 42 | | 17 | In this case, computing the hash value of the number n to be stored: n mod 9, costs a constant amount of time. And so does the actual storage, because n is stored directly in an array field.

  5. 10.1 Hash Functions • Division • A hash function must guarantee that the number it returns is a valid index to one of the table entries. • The simplest way is to use division modulo. • Folding • The key is divided into several parts. These parts are combined together and are usually transformed in a certain way to create the target address. • It is simple and fast especially when bit patterns are used instead of numerical values. • There are two types of folding • Shift folding • Boundary folding

  6. 10.1 Hash Functions(cont’) • Mid-Square function • The key is squared and the middle or mid part of the result is used as the address. • Extraction • Only a part of the key is used to compute the address. • Radix transformation • The key K is transformed into another number base; K is expressed in a numerical system using a different radix. • Collisions can not be avoided.

More Related