Download presentation

Presentation is loading. Please wait.

Published byFelicity Axe Modified about 1 year ago

1

2

3
HASH TABLE

4
Hashing is the transformation of a string of characters into a usually shorter fixed-length value or key that represents the original string. Example a group of people could be arranged in a database like this: Allen, JaneMoore, SarahSmith, Dan

5
Allen, Jane Moore, Sarah Smith, Dan HASH VALUES

6
Hash Table stores things and allows 3 operations: insert, search and delete. associated with a set of records

7
H 5 John Smith Bob Miller 34 Sally Wood 21 John Smith 29

8
Allen, Jane7864 BUCKET INDEX Each slot of a hash table is called a bucket and hash values are called bucket indices.

9
Mapping of the keys to indices of a hash table 2 compositions HASH FUNCTION Hash code map: key Compression map: integer [0, N-1]

10
DIVISION Map a key k into one of m slots by using this function: h(k) = k mod m Example: If table size m = 12 key k = 100 than h(100) = 100 mod 12 = 4

11
MID-SQUARE FUNCTION The key is squared and the mid part is used as the address. Ex. k=3121 then = thus h(3121)= 406

12
Folding Key is divided into several parts 2 types 1. shift folding 2. boundary folding

13
Shift Folding Ex. (SSN) Divide into 3 parts: 123, 456 and Add them = h(k)=k mod M where M = 1000 h(1368) = 1368 mod 1000 = Divide into five parts: 12, 34, 56, 78 and Add them = h(k)=k mod M where M = 1000 h(189) = 189 mod 1000 = 189

14
Extraction Only a part of the key is used to compute the address. Ex. (SSN) st 4 digits = 1234 Last 4 digits = st 2 combined with the last 2 = 1289(address)

15
Hash Method : Folding Chopping the Key in Two Parts Add the Two Parts to Generate the Hash Leading Digit will be Ignored Example Key Parts H(x) OptionRotate the Second Digit Parts H(x)825577

16
Radix Transformation K is transformed into another number base =255 9 M = 100 H(k) = k mod M H(255) = 255 mod 100 = 55

17
divide 212 by 9. 9 divides into times with remainder = 9(23)+ 5 9 divides into 23 twice with remainder 5. 23= 9(2)+5 212= 9(9(2)+ 5)+ 5 = 2(9 2 )+ 5(9)+ 5.

18
Hash Collision different keys happen to have same hash value

19
Bob Miller 34 Sally Wood 21 John Smith 29 Jane Depp 18 2 Collision!

20
Collision Resolution There are two kinds of collision resolution: 1 – Chaining makes each entry a linked list so that when a collision occurs the new entry is added to the end of the list. 2 – Open Addressing uses probing to discover an empty spot.

21
Collision Resolution – Open Addressing the table is probed for an open slot when the first one already has an element. Linear probing in which the interval between probes is fixed — often at 1. Quadratic probing in which the interval between probes increases linearly (hence, the indices are described by a quadratic function). Double hashing in which the interval between probes is fixed for each record but is computed by another hash function.

22
Linear Probing is a scheme in resolving hash collisions of values of hash functions by sequentially searching the hash table for a free location two values - one as a starting value and one as an interval between successive values newLocation = (startingValue + stepSize) % arraySize H(x,i) = (H(x) + i)(mod M)

23
empty Insert 15, 17, 8 empty H(15)=15 mod 10 = 5H(17)=17 mod 10 = 7H(8)=8 mod 10 = 8 Linear Probing - Example

24
Insert 35 H(35)=35 mod 10 = 5 empty Insert H(1,5)=(1 + 5) mod 10 = 6H(25)=25 mod 10 = 5H(1,5)=(1 + 5) mod 10 = 6H(1,6)=(1 + 6) mod 10 = 7H(1,7)=(1 + 7) mod 10 = 8H(1,8)=(1 + 8) mod 10 = 9 Insert 75 H(75)=75 mod 10 = 5H(1,5)=(1+5) mod 10 = 6H(1,6)=(1+6) mod 10 = 7H(1,7)=(1+7) mod 10 = 8H(1,8)=(1+8) mod 10 = 9H(1,9)=(1+9) mod 10 = 0 75

25
Has anyone spotted the flaw in the linear probing technique? Think about this: what would happen if we now inserted 85, then 95, then 55?

26
Each one would probe exactly the same positions as its predecessors. This is known as clustering. It leads to inefficient operations, because it causes the number of collisions to be much greater than it need be.

27
Quadratic Probing eliminates primary clustering p(K, i) = c 1 i 2 + c 2 i + c 3 p(K, i) = i 2 (i.e., c 1 = 1, c 2 = 0, and c 3 = 0)

28
27 Quadratic Probing - Example Example: Table Size is 11 (0..10) Hash Function: h(x) = x mod 11 Insert keys: 20 mod 11 = 9 30 mod 11 = 8 2 mod 11 = 2 13 mod 11 = 2 =3 25 mod 11 = 3 =4 24 mod 11 = 2 2+1 2, =6 10 mod 11 = 10 9 mod 11 = 9 9+1 2, mod 11, mod 11 =

29
not all hash table slots will be on the probe sequence Using p(K, i) = i 2 gives particularly inconsistent results If all slots on that cycle happen to be full, this means that the record cannot be inserted at all!

30
Double Hashing increment P, not by a constant but by an amount that depends on the Key. P = (1 + P) mod TABLE_SIZE P = (P + INCREMENT(Key)) mod TABLE_SIZE

31
Double Hashing - Example P = (P + INCR(Key)) mod TABLE_SIZE Suppose INCR(Key) = 1 + (Key mod 7) Adding 1 guarantees it is never 0! Insert 15, 17, 8:

32
Insert 35: P = H(35) = 5. P = (5 + ( mod 7)) mod 10 = 6. Insert 25:P = H(25) = 5. P = (5 + ( mod 7)) mod 10 = 0

33
Let’s try! Insert 75: P = (P + INCR(Key)) mod TABLE_SIZE Suppose INCR(Key) = 1 + (Key mod 7)

34
Chaining/Separate Chaining uses an array as the primary hash table an array of lists of entries

35
nil 5 : HASHMAX Key: name: tom score: 73 One way to handle collision is to store the collided records in a linked list. The array now stores pointers to such lists. If no key maps to a certain hash value, that array entry points to nil.

36

37

38
is a collision resolution method that uses pointers to connect the elements of a synonym chain. Coalesced Hashing A hybrid of separate chaining and open addressing. Linked lists within the hash table handle collisions. This strategy is effective, efficient and very easy to implement.

39
A5, A2, A3 A5 A2 A3 A5 A2 A3 A5 A2 A3 B5, A9, B2B9, C2 A9 B5 B2 A9 B5 B2 C2 B9

40
A5, A2, A3 A5 A2 A3 A5 A2 A3 B5, A9,B2 B5 B2 A9 A5 A2 A3 B5 B2 A9 B9,C2 B9 C2

41
Bucket Addressing A bucket can be defined as a block of space that can be used to store multiple elements that hash to the same position.

42
A5, A2, A3, B5, A9, B2, B9 A5 A2 A3 B5 A9 B2 B9

43
DELETION Deleting a record must not hinder later searches. The search process must still pass through the newly emptied slot to reach records whose probe sequence passed through this slot. It should not mark the slot as empty. Freed slot should be available to a future insertion. TOMBSTONE

44
2535 Insert: Collision Probing Sequence: Delete: 75 Match Found! TOMBSTONE

45
A1, A4, A2,B4,B1 B4 A1 A4 A2 B1 A4A2 B1 B4

46
Perfect Hash Functions Quick to compute Distributes keys uniformly throughout the table Very rare(birthday paradox) No collisions Perfect hash functions are rare.

47
46 A Perfect Hash Function for Strings R. J. Cichelli gave an algorithm for finding perfect hash functions for strings. He proposes the hash function: h(s)=size+g(s.charAt(0))+ g(s.charAt(size-1))%n where size = s.length(). The function g is to be constructed so that h(s) is unique for each string s.

48

49

50

51

52
Example 1: Illustrating Perfect Hashing Use Cichelli's algorithm to build a minimal perfect hash function for the following nine strings: DO DOWNTO ELSE END IF IN TYPE VAR WITH

53
Example 1: Solution For Step 1 in the algorithm, we find the frequencies of the first and last letter of each word to find: D O E I F N T V R W H Next we find the sum of the first and last letter of each word: DO=5(D+0=3+2), DOWNTO=5, ELSE = 8, END=7, IF=3, IN=3, TYPE=5, VAR=2,WITH=2 Sorting the keywords in decreasing frequency yields: ELSE END DOWNTO DO TYPE IN IF VAR WITH We are now at step 5 of the algorithm, the heart of the algorithm. We try the words in frequency order:

54
Example 1: Cichelli's Method (cont'd) s = ELSE g(E)=0 h(s)= s.length()+g(E)+g(E)=4 s = END g(D)=0 h(s)= s.length()+g(E)+g(D)=3 s = DOWNTO g(O)=0 h(s)= s.length()+g(D)+g(O)=6 s = DO h(s)= s.length()+g(D)+g(O)=2 s = TYPE g(T)=0 h(s)= s.length()+g(T)+g(E)=4* s = TYPE g(T)=1 h(s)= s.length()+g(T)+g(E)=5 s = IN g(I)=0,g(N)=0 h(s)=s.length()+g(I)+g(N)=2* s = IN g(I)=1,g(N)=0 h(s)=s.length()+g(I)+g(N)=3* s = IN g(I)=2,g(N)=0 h(s)=s.length()+g(I)+g(N)=4* s = IN g(I)=3,g(N)=0 h(s)=s.length()+g(I)+g(N)=5* s = IN g(I)=3,g(N)=1 h(s)=s.length()+g(I)+g(N)=6* s = IN g(I)=3,g(N)=2 h(s)=s.length()+g(I)+g(N)=7 s = IF g(F)=0 h(s)=s.length()+g(I)+g(F)=5* s = IF g(F)=1 h(s)=s.length()+g(I)+g(F)=6* s = IF g(F)=2 hash(s)=s.length()+g(I)+g(F)=7* s = IF g(F)=3 h(s)=s.length()+g(I)+g(F)=8

55
Example 1: Cichelli's Algorithm (cont'd) The hash table above is fully occupied with empty slots. Note that if there are empty slots or there is a collision, then the g-value assignments are in error WITHDOIFINENDVARTYPR DOWNTO ELSE

56
Extendible hashing Hashing with buckets

57
Extendible Hashing - Class Example

58
0 1 rec 1 rec 2 d 1 =0 record 3 = overflow!! splitting bucket d = 1d = 0 d 1 = local depth d = global depth rec 1 d1 = 1 rec 2 rec 3 rec 4 record 5 = overflow!! splitting bucket NEXT

59
rec 2 rec 3 rec 1 rec 4 rec 5 rec d = 2 d1 = 2 d1 = 1 11 d1 = 2 01 record 7 = overflow!! splitting bucket NEXT

60
rec 2 rec 3 rec 1 rec 4 rec 5 rec 6 d1 = d = 3 d1 = d1 = d1 = 3 rec 7 record 8 = overflow!! splitting bucket NEXT

61
record 10 = overflow!! splitting bucket NEXT rec 1 rec d = d1 = 3 d1 = 2 d1 = 3 d1 = 2 rec 8 rec 2 rec 3 rec 5 rec 6 rec 7 rec 9

62
record 13 = overflow!! splitting bucket NEXT rec 5 rec 6 rec 10 d1 = 3 rec 7 rec 9 d1 = 3 rec 1 rec d = rec 2 rec 3 d1 = 3 d1 = 2 rec 8 rec 11 rec 12

63
rec 6 d1 = 4 d1 = 3 d1 = d = d1 = 3 d1 = 4 d1 = 3 rec 10 rec 13 rec 1 rec 4 rec 2 rec 3 rec 5 rec 7 rec 8 rec 11 rec 12 rec 14 rec 15

64
Hash Table Uses driver's license record's Internet search engines telephone book databases electronic library catalogs implementing passwords for systems with multiple users. Hash Tables allow for a fast retrieval of the password which corresponds to a given username

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google