Sei sulla pagina 1di 5

Name: Khanh Huynh

Student ID: 0761882


Date: 06/10/2013
2IL65 Algorithms
Assignment 5

Exercise 1:
Linear probing: h(k) = (h(k) + i) mod m
- Insert key 10:
0 1 2 3 4 5 6 7 8 9 10
T 10
0 unsuccessful probe before inserting key.
- Insert key 22:
0 1 2 3 4 5 6 7 8 9 10
T 22 10
0 unsuccessful probe before inserting key.
- Insert key 31:
0 1 2 3 4 5 6 7 8 9 10
T 22 31 10
0 unsuccessful probe before inserting key.
- Insert key 4:
0 1 2 3 4 5 6 7 8 9 10
T 22 4 31 10
0 unsuccessful probe before inserting key.
- Insert key 15:
0 1 2 3 4 5 6 7 8 9 10
T 22 4 15 31 10
1 unsuccessful probe before inserting key.
- Insert key 28:
0 1 2 3 4 5 6 7 8 9 10
T 22 4 15 28 31 10
0 unsuccessful probe before inserting key.
- Insert key 17:
0 1 2 3 4 5 6 7 8 9 10
T 22 4 15 28 17 31 10
1 unsuccessful probe before inserting key.



- Insert key 88:
0 1 2 3 4 5 6 7 8 9 10
T 22 88 4 15 28 17 31 10
1 unsuccessful probe before inserting key.
- Insert key 59:
0 1 2 3 4 5 6 7 8 9 10
T 22 88 4 15 28 17 59 31 10
4 unsuccessful probes before inserting key.
The total number of unsuccessful probes for hashing with linear probing is 7.

Quadratic probing: h(k) = (h(k) + i + 3i
2
) mod m
- Insert key 10:
0 1 2 3 4 5 6 7 8 9 10
T 10
0 unsuccessful probe before inserting key.
- Insert key 22:
0 1 2 3 4 5 6 7 8 9 10
T 22 10
0 unsuccessful probe before inserting key.
- Insert key 31:
0 1 2 3 4 5 6 7 8 9 10
T 22 31 10
0 unsuccessful probe before inserting key.
- Insert key 4:
0 1 2 3 4 5 6 7 8 9 10
T 22 4 31 10
0 unsuccessful probe before inserting key.
- Insert key 15:
0 1 2 3 4 5 6 7 8 9 10
T 22 4 15 31 10
1 unsuccessful probe before inserting key.
- Insert key 28:
0 1 2 3 4 5 6 7 8 9 10
T 22 4 28 15 31 10
0 unsuccessful probe before inserting key.
- Insert key 17:
0 1 2 3 4 5 6 7 8 9 10
T 22 17 4 28 15 31 10
3 unsuccessful probes before inserting key.
- Insert key 88:
0 1 2 3 4 5 6 7 8 9 10
T 22 88 17 4 28 15 31 10
8 unsuccessful probes before inserting key.
- Insert key 59:
0 1 2 3 4 5 6 7 8 9 10
T 22 88 17 4 28 59 15 31 10
2 unsuccessful probes before inserting key.
The total number of unsuccessful probes for hashing with quadratic probing is 14.

Exercise 2:
a) A = /2
h(k) = m(kA mod 1)
h(61) = 1000(61A mod 1) = 827
h(62) = 1000(62A mod 1) = 693
h(63) = 1000(63A mod 1) = 559
h(64) = 1000(64A mod 1) = 425
h(65) = 1000(65A mod 1) = 291

b) h(k) = (h(k) + i + i
2
) mod m = (k mod m + i + i
2
) mod m
For the quadratic probing to result in a sequence that consists of all permutations of mod
13, there must not be a slot that is visited twice during probing the difference between
2 probes are not a multiple of 13.
Consider 2 arbitrary probes: (k mod m + i + i
2
) and (k mod m + j + j
2
)
(k mod m + i + i
2
) - (k mod m + j + j
2
) 13n (n Z)
(k mod m + i + i
2
) - (k mod m + j + j
2
)
= i
2
j
2
+ i j
= (i j)(i + j + 1)
i + j + 1 can be a multiple of 13, for example i = 9, j = 3. Thus there are slots that are
visited at least twice during probing. Hence quadratic probing with c
1
= c
2
= 1 does not
result in a probe sequence that is a permutation of 0, 1, , 12.

Exercise 3:
a) Proof: By contradiction.
Assume there is no subset of U of size n with the property that they all hash to the same
position.
Then the maximum number of elements in a position is n 1. The maximum number of
elements that can exist in the universe is then:
|U| m(n 1) nm (contradict with the assumption that |U| > nm)
Thus there is a subset of U of size n with the property that they all hash to the same
position.
b) When resolve collisions with open addressing for case m = n, all m positions are filled.
Thus every unsuccessful search will have to go through all the positions of the table, thus
take (n) time.
When resolve collisions with chaining for case m = n, a search then would take (1) time
on average.
Thus resolving with chaining in this case is a better choice.

Exercise 4:
a) i
0
= h(k) mod m
i
1
= (h(k) + 1) mod m
i
2
= ((h(k) + 1) mod m + 2) mod m = (h(k) + 1 + 2) mod m
i
3
= ((h(k) + 1 + 2) mod m + 3) mod m = (h(k) + 1 + 2 + 3) mod m

i
l
= (h(k) + 1 + 2+ + l) mod m
= (h(k) +

) mod m
= (h(k) +

) mod m
Thus this is an instance of quadratic probing with c
1
= c
2
= 1/2.
Proof: By induction.
Induction Hypothesis: i
n
= (h(k) +

) mod m
Base: i
0
= h(k) mod m = (h(k) +

) mod m
Step: Assume the IH holds for n. We will prove that it holds for n + 1:
i
n+1
= (i
n
+ j
n+1
) mod m = (i
n
+ n + 1) mod m
= ((h(k) +

) mod m + n + 1) mod m (Induction Hypothesis)


= (h(k) +

+ n + 1) mod m
= (h(k) +

) mod m
b) m = 2
n
To prove that all table positions are examined in the worst case, it is sufficient to prove
that none of the position is visited twice during the probing sequence. This mean for
every arbitrary u, v < m and u v:
i
u
i
v

(h(k) +

) mod m (h(k) +

) mod m
(h(k) +

) - (h(k) +

) bm (b Z)

bm
Proof: By contradiction
When (u v) is even, (u + v + 1) is odd. Thus for

to be a multiple of m = 2
n
,

must be a multiple of m

m or u = v. This contradicts with the assumption


that u, v < m and u v.
When (u v) is odd, (u + v + 1) is odd. Thus for

to be a multiple of m = 2
n
,

must be a multiple of m

m. This contradicts with the assumption that u,


v < m.
Thus

cannot be a multiple of m for any arbitrary u and v. Hence all probe


values are difference.
It follows that all table positions are examined in the worst case.

Potrebbero piacerti anche