Using the master theorem, we have a=b=2 and f(n) = lg(n). Since lg(n) = O(n1-epsilon) for all epsilon between 0 and 1, we have T(n) = O(n).
I didn't mean to give you this one. If we try to use the master theorem, we have a=16, b=2, f(n) = (n lg(n))4. It's easy to check (try it!) that none of the three cases apply. You should try to show by the substitution method that T(n) = O((n lg(n))5).
Note: while it wouldn't be fair to ask you for the prelim to guess the right answer for T(n), it would be OK to ask you to verify the answer using the substitution method.
Apply the master theorem. We have a=9, b=3, and f(n) = n3 lg(n). It's easy to see that we're in case 3 (with epsilon between 0 and 1) and 9f(n/3) < n3lg(n)/3 = (1/3)f(n). Thus, T(n) = Omega(n3 lg(n)).
| Data Structure | insert | search | getMin | successor |
|---|---|---|---|---|
| sorted array | O(n) | O(log n) | O(1)* | O(log n) |
| unsorted array | O(1) | O(n) | O(n) | O(n) |
| balanced tree (red-black tree) | O(log n) | O(log n) | O(log n) | O(log n) |
| hashing | O(1)** | O(1)** | O(n) | O(n) |
| sorted linked list | O(n) | O(n) | O(1) | O(n) |
| unsorted linked list | O(1) | O(n) | O(n) | O(n) |
* In a sorted array, getMin() leaves a gap, but the data doesn't
have to be moved to fill in the gap until the next insert.
** For hashing, double hashing is assumed, so standard hashtable operations run in
constant time.
7
/ \
0 8
\
5
/ \
2 6
/ \
1 4
/
3
|
Preorder: 705214368 Inorder: 012345678 Postorder: 134265087 |
| word | ape | bat | bird | carp | dog | hare | ibex | mud | koala | stork |
|---|---|---|---|---|---|---|---|---|---|---|
| h1 | 6 | 0 | 6 | 7 | 0 | 2 | 0 | 6 | 1 | 8 |
| h2 | 6 | 1 | 5 | 7 | 8 | 6 | 10 | 5 | 2 | 2 |
Draw a picture of the resulting hashtable after inserting, in order, the following words: ibex, hare, ape, bat, koala, mud, dog, carp, stork. Also highlight cells that are looked at when trying to find bird. Do this for each of the following techniques.
| chaining | linear probing | double hashing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
|
The cells that are visited when searching for bird are:
Why not use h2(x) = (value of the last letter) mod m? (i.e., why did we include -1 and
+1?)
h2 cannot be allowed to return a value of zero. This would make the increment zero,
thus successive probes would all land in the same place.
Why not use the same function for both hash functions?
This would lead to clustering, since any two words that had the same index on their
first probe would have the same index on all subsequent probes. Also, this strategy could
allow an increment of zero.
| Space Available (in words) | 10000 |
| Words per Item | 7 |
| Words per Pointer | 1 |
| Number of Items | 1000 |
| Proportion Successful Searches | 1/3 |
Which hashing method, chaining or double hashing, looks best (i.e., the one that we expect to use the least amount of time)? You should use the following assumptions:
| BST | ||
|---|---|---|
D
/ \
A T
\ / \
C S U
/
R
/
E
|
( D S )
/ | \
(A C) (E R) (T U)
|
Assume now that we are using a hashtable of size 17 with the hash function h(x) = x mod 17 for the letter at position x in the alphabet (e.g., A=1, B=2, etc.).
3. Show the hashtable that results when we use chaining.
4. Show the hashtable that results when we use linear probing.
5. Show the hashtable that results when we use double hashing. Use g(x) = 1 + (x mod 13)
for the second hash function.
| Chaining | Linear Probing | Double Hashing |
|---|---|---|
0: 1: A R 2: S 3: T C 4: D U 5: E 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: |
0: 1: A 2: S 3: T 4: D 5: R 6: U 7: C 8: E 9: 10: 11: 12: 13: 14: 15: 16: |
0: 1: A 2: S 3: T 4: D 5: E 6: 7: R 8: 9: 10: 11: C 12: 13: U 14: 15: 16: |