This action might not be possible to undo. Are you sure you want to continue?

BooksAudiobooksComicsSheet Music### Categories

### Categories

### Categories

### Publishers

Scribd Selects Books

Hand-picked favorites from

our editors

our editors

Scribd Selects Audiobooks

Hand-picked favorites from

our editors

our editors

Scribd Selects Comics

Hand-picked favorites from

our editors

our editors

Scribd Selects Sheet Music

Hand-picked favorites from

our editors

our editors

Top Books

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Audiobooks

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Comics

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Sheet Music

What's trending, bestsellers,

award-winners & more

award-winners & more

P. 1

Chapter 8- Hashing|Views: 13|Likes: 0

Publicado porSanjay Jain

See more

See less

https://pt.scribd.com/doc/61417386/Chapter-8-Hashing

08/02/2011

text

original

Concept of Hashing

a hash table, or a hash map, is a data structure that associates keys (names) with values (attributes).

.Example A small phone book as a hash table.

Dictionaries Collection of pairs. . value) Each pair has a unique key. (key.

Lookup function (Hash function) .Just An Idea Hash table : Collection of pairs.

Each bucket has many slots. A hash table is partitioned into many buckets. Each slot holds one record. A hash function f(x) transforms the identifier (key) into an address in the hash table .Hashing Key-value pairs are stored in a fixed size table called a hash table.

b buckets . . . . . . . . b-1 . . . . . .Hash table s slots 0 0 1 1 s-1 . .

Each position of this array is a bucket.Ideal Hashing Uses an array table[0:b-1]. Every dictionary pair (key. A bucket can normally hold only one dictionary pair. element) is stored in its home bucket table[f[key]]. . Uses a hash function f that converts each key k into an index in the range [0. b-1].

e) (33. b = 8 (where b is the number of positions in the hash table) Hash function f is key % b = key % 8 Where are the pairs stored? [0] [1] [2] [3] (3.e).d).c) (85.a).(72.f) (22.Ideal Hashing Example Pairs are: (22.a) [0] [1] [2] [3] [4] [5] [6] [7] 9 .c).(33.(3.d) [4] [5] [6] [7] (72.(85.f) Hash table is ht[0:7].

d) (85.c) (3.g) is already occupied by (33.c) This situation is called collision Keys that have the same home bucket are called synonyms 25 and 33 are synonyms with respect to the hash function that is in use 10 .e) (33.What Can Go Wrong? .f) (22.g) go? The home bucket for (25.a) [0] [1] [2] [3] [4] [5] [6] [7] Where does (25.Collision (72.

a) [0] [1] [2] [3] [4] [5] [6] [7] A collision occurs when the home bucket for a new pair is occupied by a pair with different key An overflow occurs when there is no space in the home bucket for the new pair When a bucket can hold only one pair.f) (22.e) (33.c) (3.What Can Go Wrong? Overflow (72. collisions and overflows occur together Need a method to handle overflows 11 .d) (85.

) Size (number of buckets) of hash table. Overflow: there is no space in the bucket for the new pair. To avoid collision (two different pairs are in the same the same bucket. Overflow handling method.Some Issues Choice of hash function. .

.Choice of Hash Function Requirements easy to compute minimal number of collisions A good hashing function distributes the key values uniformly throughout the range.

Some hash functions Division: Choose a number m(PRIME number) larger than the number n of keys in K. K=3205. 2345 & No. 7148. The Hash function H is defined by H(k) = K(mod m) E. Of Address = 100( 0 -99) Let m =97 H(3205) = 3205 mod 97 .g.

.112.203.Some hash functions Folding: Partition the key k into several parts.. and add the parts together to obtain the hash address H(k) = k1+k2+ . x=12320324111220. +kr e.g.20..241. partition k into 123. then return the address H(k)=123+203+241+112+20=699 .

g. K= 3205. 2345 . Then the Hsh function H is defined byH(k) = l Where l is obtained by deleting digits from both ends of k2 e.Some hash functions Mid Square: The key k is squared. E. k2 = 10 272 025 H(k) = 72 7148.g.

Linear probing (linear open addressing). Chain. . Quadratic probing. element) is full. Rehashing Eliminate overflows by permitting each bucket to keep a list of all pairs for which it is the home bucket.Overflow Handling An overflow occurs when the home bucket for a new pair (key. Array linear list. We may handle overflows by: Search the hash table in some systematic fashion for a bucket that is not full.

Linear Probing resolves collisions by placing the data into the next open slot in the table.Linear probing (linear open addressing) Open addressing ensures that all elements are stored directly into the hash table. . thus it attempts to resolve collisions using various methods.

0. 45 . 7. 29.Linear Probing – Get And Insert divisor = b (number of buckets) = 17. 30. 11. 28. 33. 4 6 8 23 7 12 16 28 12 29 11 30 33 0 34 0 45 • Insert pairs whose keys are 6. 23. Home bucket = key % 17. 34. 12.

key).3) void linear_insert(element item. if (i == hash_value) { fprintf(stderr.key)) { if (!strcmp(ht[i]. exit(1). exit(1). } } ht[i] = item. “The table is full\n”). hash_value. i = hash_value = hash(item.key)) { fprintf(stderr. item.Linear Probing (program 8. “Duplicate entry\n”). while(strlen(ht[i].key. element ht[]){ int i. } . } i = (i+1)%TABLE_SIZE.

Problem of Linear Probing Identifiers tend to cluster together Increase the search time .

. h+ i2 .Quadratic Probing Quadratic probing uses a quadratic function of i as the increment (H(x)+i2)%b for H(K) = h For i = 0.e... h... h+4 .. h+1.. 2 -----i.1...

Rehashing Rehashing: Try H1. If the slot is full. then a second hash function is calculated and combined with the first hash function. Double hashing is one of the best methods for dealing with collisions. Here Hi is a hash function. H(k. …. H2. Hm in sequence if collision occurs. i) = (H1(k) + i H2(k) ) % m .

Data Structure for Chaining The idea of Chaining is to combine the linked list and hash table to solve the overflow problem. .

Hashing with Chains Hash table can handle overflows using chaining Each bucket keeps a chain of all pairs for which it is the home bucket. The chain may or may not be sorted by key 25 .

45 Home bucket = key % 17.34.12. 33.29. 26 .7. 28.11.23.Hash Table with Sorted Chains Put in pairs whose keys are 6.0.30.

- Leia e imprima sem anúncios
- Download to keep your version
- Edit, email or read offline

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

CANCEL

OK

You've been reading!

NO, THANKS

OK

scribd

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->