Hash Practice

advertisement
Hashing Practice
1.
Given input {4371, 1323, 6173, 4199, 4344, 9679, 1989}
and a hash function h(x) = x (mod 10), show the resulting :
a. open addressing hash table using linear probing
0
1
2
3
4
5
6
7
8
9
Rehash Function : ______________________________
b. open addressing hash table using quadratic probing
0
1
2
3
4
5
6
7
8
9
c. separate chaining hash table
0
1
2
3
4
5
6
7
8
9
Rehash Function : ______________________________
2. A spelling checker reads an input file and prints out all words not in some
on-line dictionary. Suppose the dictionary contains 30,000 words and the
file is large, so that the algorithm can make only one pass through the input
file.
a)
How large should the hash table be to minimize collisions?
b)
Assuming an average word is seven characters, and assuming a
quadratic probing hash table, what is the average amount of memory
being utilized by the hash table?
c)
Suppose RAM is limited, and you decide not to store the entire
dictionary within a hash table. So, you come up with another
algorithm. You decide that you will declare an array table of bool
(initialized to false) indexed from 0 through TableSize-1. As
we read in a word , we set table[hash(word)] = true.
True or False?
i)
If a word hashes to a location with a value false, the word is
not in the dictionary.
ii)
If a word hashes to a location with a value true, then the word
is in the dictionary.
{4371, 1323, 6173, 4199, 4344, 9679, 1989}
h(x) = x (mod 10)
a. open addressing hash table using linear probing
0
1
2
3
4
5
6
7
8
9
RehashFunction
Function:
______________________________
Rehash
: ________________________________
b. open addressing hash table using quadratic probing
0
1
2
3
4
5
6
7
8
9
Rehash Function:______________________________
c. separate chaining hash table
0
1
2
3
4
5
6
7
8
9
A spelling checker reads an input file and prints out all words not
in some on-line dictionary. Suppose the dictionary contains
30,000 words and the file is large, so that the algorithm can make
only one pass through the input file.
a)
How large should the hash table be to minimize collisions?
b)
Assuming an average word is seven characters, and assuming
a quadratic probing hash table, what is the average amount of
memory being utilized by the hash table?
c)
Suppose RAM is limited, and you decide not to store the
entire dictionary within a hash table. So, you come up with
another algorithm. You decide that you will declare an array
table of bool (initialized to false) indexed from 0
through TableSize-1. As we read in a word , we set
table[hash(word)] = true.
True or False?
i)
If a word hashes to a location with a value false, the
word is not in the dictionary.
ii)
If a word hashes to a location with a value true, then
the word is in the dictionary.
Download