View
235
Download
5
Category
Preview:
DESCRIPTION
HW5_2009-20899
Citation preview
Homework #5
Ilyong Cho(2009-20899)
11.1-1
Suppose that a dynamic set S is represented by a direct-address table T of length m.Describe a procedure that nds the maximum element of S. What is the worst-caseperformance of your procedure?
Find the maximum elements of S by checking from T [m 1] to T [0] step by step.The rst found element is the maximum element. If there aren't any elements in thetable T , then we must check all the slots. So the worst-case running time is (n)
11.3-3
Consider a version of the division method in which h(k) = k mod m, where m = 2p 1and k is a character string interpreted in radix 2p. Show that if string x can be derivedfrom string y by permuting its characters, then x and y hash to the same value. Give anexample of an application in which this property would be undesirable in a hash function.
Suppose that string x is derived from string y by interchanging a pair of charactersin positions a and b. Let xi be the ith character in x, and similarly for yi. We can viewx as x =
Pn1i=0 xi2
ip, and y as y =Pn1
i=0 yi2ip. So h(x) = (
Pn1i=0 xi2
ip) mod (2p 1),and h(y) = (
Pn1i=0 yi2
ip) mod (2p 1).(h(x) h(y)) mod (2p 1) = 0 ) h(x) h(y) mod (2p 1) By the Euclidean
algorithm, and 0 h(x); h(y) 2p1. Thus if we show that (h(x)h(y)) mod (2p1) =0, then h(x) = h(y).
h(x) h(y) =
n1Xi=0
xi2ip
!mod (2p 1)
n1Xi=0
yi2ip
!mod (2p 1)
=
n1Xi=0
xi2ip
n1Xi=0
yi2ip
!mod (2p 1)
= ((xa2ap + xb2
bp) (ya2ap + yb2bp)) mod (2p 1)= ((xa2
ap + xb2bp) (xb2ap + xa2bp)) mod (2p 1)
= ((xa xb)2ap (xb xa)2bp) mod (2p 1)= ((xa xb)(2ap 2bp)) mod (2p 1)= ((xa xb)(2ap 2bp)) mod (2p 1)= ((xa xb)2bp(2(ab)p 1) mod (2p 1)
=
(xa xb)2bp
ab1Xi=0
2pi
!(2p 1)
!mod (2p 1)
= 0:
1
Thus h(x) = h(y).
Note that any permutation can be made by a sequence of interchanging of pairsof characters. Let x be a string derived from a string y by interchanging a pair ofcharacters, if h(x) is the same with h(y) then any permutation of y hashes to h(y) bymathematical induction.
Examples of applications
A dictionary which contains words expressed by ASCII code can be one of suchexample when each character of the dictionary is interpreted in radix 28 = 256 andm = 255. The dictionary, for instance, might have words \STOP", \TOPS", \SPOT",\POTS" .. all of which are hashed into the same slot.
A DNA dictionary also can be one of such example. As we know, a DNA is asequence of four bases, \Adenine", \Cytosin", \Guanine", \Thymine", which may beinterpreted in radix 22. So following DNAs are all hased into the same slot.\ACCTG", \GTCAC", \TCAGC", \GACCT"...
11.4-1
Consider inserting the keys 10; 22; 31; 4; 15; 28; 17; 88; 59 into a hash table of lengthm = 11 using open addressing with the auxiliary hash function h0(x) = k mod m.Illustrate the result of inserting these keys using linear probing, using quadratic probingwith c1 = 1 and c2 = 3, and using double hashing with h2(k) = 1 + (k mod (m 1)).
Linear probing
h(k; i) = (k + i) mod 11.
1. Hasing 10k = 10; i = 0, h(10; 0) = (10 + 0) mod 11 = 10
10
2. Hasing 22k = 22; i = 0, h(22; 0) = (22 + 0) mod 11 = 022 10
3. Hasing 31k = 31; i = 0, h(31; 0) = (31 + 0) mod 11 = 922 31 10
4. Hasing 4k = 4; i = 0, h(4; 0) = (4 + 0) mod 11 = 422 4 31 10
5. Hasing 15k = 15; i = 0, h(15; 0) = (15 + 0) mod 11 = 4, collision!k = 15; i = 1, h(15; 1) = (15 + 1) mod 11 = 522 4 15 31 10
6. Hasing 28k = 28; i = 0, h(28; 0) = (28 + 0) mod 11 = 622 4 15 28 31 10
2
7. Hasing 17k = 17; i = 0, h(17; 0) = (17 + 0) mod 11 = 6, collision!k = 17; i = 1, h(17; 1) = (17 + 1) mod 11 = 722 4 15 28 17 31 10
8. Hasing 88k = 88; i = 0, h(88; 0) = (88 + 0) mod 11 = 0, collision!k = 88; i = 1, h(88; 1) = (88 + 1) mod 11 = 122 88 4 15 28 17 31 10
9. Hasing 59k = 59; i = 0, h(59; 0) = (59 + 0) mod 11 = 4, collision!k = 59; i = 1, h(59; 1) = (59 + 1) mod 11 = 5, collision!k = 59; i = 2, h(59; 2) = (59 + 2) mod 11 = 6, collision!k = 59; i = 3, h(59; 3) = (59 + 3) mod 11 = 7, collision!k = 59; i = 4, h(59; 4) = (59 + 4) mod 11 = 822 88 4 15 28 17 59 31 10
Quadratic probing
h(k; i) = (k + i+ 3i2) mod 11.
1. Hasing 10k = 10; i = 0, h(10; 0) = (10 + 0 + 0) mod 11 = 10
10
2. Hasing 22k = 22; i = 0, h(22; 0) = (22 + 0 + 0) mod 11 = 022 10
3. Hasing 31k = 31; i = 0, h(31; 0) = (31 + 0 + 0) mod 11 = 922 31 10
4. Hasing 4k = 4; i = 0, h(4; 0) = (4 + 0 + 0) mod 11 = 422 4 31 10
5. Hasing 15k = 15; i = 0, h(15; 0) = (15 + 0 + 0) mod 11 = 4, collision!k = 15; i = 1, h(15; 1) = (15 + 1 + 3) mod 11 = 822 4 15 31 10
6. Hasing 28k = 28; i = 0, h(28; 0) = (28 + 0 + 0) mod 11 = 622 4 28 15 31 10
7. Hasing 17k = 17; i = 0, h(17; 0) = (17 + 0 + 0) mod 11 = 6, collision!k = 17; i = 1, h(17; 1) = (17 + 1 + 3) mod 11 = 10, collision!k = 17; i = 2, h(17; 2) = (17 + 2 + 12) mod 11 = 9, collision!k = 17; i = 3, h(17; 3) = (17 + 3 + 27) mod 11 = 322 17 4 28 15 31 10
3
8. Hasing 88k = 88; i = 0, h(88; 0) = (88 + 0 + 0) mod 11 = 0, collision!k = 88; i = 1, h(88; 1) = (88 + 1 + 3) mod 11 = 4, collision!k = 88; i = 2, h(88; 2) = (88 + 2 + 12) mod 11 = 3, collision!k = 88; i = 3, h(88; 3) = (88 + 3 + 27) mod 11 = 8, collision!k = 88; i = 4, h(88; 4) = (88 + 4 + 48) mod 11 = 8, collision!k = 88; i = 5, h(88; 5) = (88 + 5 + 75) mod 11 = 3, collision!k = 88; i = 6, h(88; 6) = (88 + 6 + 108) mod 11 = 4, collision!k = 88; i = 7, h(88; 7) = (88 + 7 + 147) mod 11 = 0, collision!k = 88; i = 8, h(88; 8) = (88 + 8 + 192) mod 11 = 222 88 17 4 28 15 31 10
9. Hasing 59k = 59; i = 0, h(59; 0) = (59 + 0 + 0) mod 11 = 4, collision!k = 59; i = 1, h(59; 1) = (59 + 1 + 3) mod 11 = 8, collision!k = 59; i = 2, h(59; 2) = (59 + 2 + 12) mod 11 = 722 88 17 4 28 59 15 31 10
Double hashing
h(k; i) = (k + i(1 + (k mod 10))) mod 11.
1. Hasing 10k = 10; i = 0, h(10; 0) = 10
10
2. Hasing 22k = 22; i = 0, h(22; 0) = 022 10
3. Hasing 31k = 31; i = 0, h(31; 0) = 922 31 10
4. Hasing 4k = 4; i = 0, h(4; 0) = 422 4 31 10
5. Hasing 15k = 15; i = 0, h(15; 0) = 4, collision!k = 15; i = 1, h(15; 1) = 10, collision!k = 15; i = 2, h(15; 2) = 5, collision!22 4 15 31 10
6. Hasing 28k = 28; i = 0, h(28; 0) = 622 4 15 28 31 10
7. Hasing 17k = 17; i = 0, h(17; 0) = 6, collision!k = 17; i = 1, h(17; 1) = 322 17 4 15 28 31 10
8. Hasing 88k = 88; i = 0, h(88; 0) = 0, collision!
4
k = 88; i = 1, h(88; 1) = 9, collision!k = 88; i = 2, h(88; 2) = 722 17 4 15 28 88 31 10
9. Hasing 59k = 59; i = 0, h(59; 0) = 4, collision!k = 59; i = 1, h(59; 1) = 3, collision!k = 59; i = 2, h(59; 2) = 222 59 17 4 15 28 88 31 10
5
Recommended