CS 1501 Summer 2009:
Quiz 1 Solution
1) Fill in the Blanks and True/False (22
points -- 2 points each).
Complete the statements below with the MOST APPROPRIATE words/phrases.
a)
(and b) Two algorithms, A and B, have the following
run-time information:
i)
Algorithm A has a run-time of Theta(2n) and, for
n = 4 runs in 4 seconds on a computer.
ii)
Algorithm B has a run-time of Theta(n2) and, for
n = 4, also runs in 4 seconds on the same computer.
How long
will each algorithm take to run (in seconds) a problem of size n = 8 using the same
computer? Algorithm A: _______64_______________
Algorithm
B:_________16_____________
c)
Order the following growth rates from smallest (best)
to greatest (worst): n2 nlgn lgn
n! 2n n3 _____lgn < nlgn < n2 < n3 < 2n < n! ______________
d)
Given a multiway radix search trie in which 64-bit
keys are compared 8 bits at a time, the maximum height of the
tree is _____8_____ and interior
nodes will each have up to __28=256____
children.
e)
Consider an empty separate
chaining hash table of size 100. If we hash 200 keys into this table, the average chain length will be _____2____ and the worst case chain length
will be ___200_____.
f)
An example text
string and pattern string that
will produce the worst case for the brute force string matching algorithm
are:
A (text) = ___AAAAAAAAAAAAB__________________
P (pattern) = ___AAAB__________
Indicate whether each of the following is TRUE or FALSE, explaining why in an informative way for
false answers.
g)
Pruning is a technique that improves the asymptotic
run-times of exhaustive search algorithms.
False. Pruning can improve the exhaustive search in practice, but can’t
change the asymptotic runtime.
h)
We used DLBs in Assignment 1
rather than regular multiway tries
because DLBs allow for faster searches.
False. DLBs are slower than multiway tries, but they can save memory
consumption.
i)
The Java String operator "+" appends one String to another in constant time.
False. It requires linear time (in the
length of the strings) since a new object must be created
j)
Due to the Pigeonhole Principle,
I cannot avoid collisions in hashing if the size of my key space is larger than the size of my hash table.
True
k)
Hashing a string by adding the
ASCII values of its characters results in a good hash function, since it utilizes the entire key.
False. It does not take position of the characters
into account (among other things).
2)
(16 points – 8 + 8) Consider an emtpy de la Briandais Tree, which
uses the lower case letters (plus a string termination character) as its
alphabet, using the implementation that we discussed in lecture. Also consider the following strings: run
sunday runny sun sunnny
a)
Draw the de la Briandais tree
that results after inserting the strings shown in the order shown above.

a)
In Assignment 1 the searchPrefix method for
your DLB did two things:
i)
Test to see if a string is contained in the
DLB
ii)
Test to see if a string is a prefix of some
string contained in the DLB
Explain the DLB search
algorithm in detail and clearly show how each of the two above
determinations can be made.
Start from the first character of the given string.
Sequentially search the linked list of sibling nodes pointed by root. If the
character is found, follow the child link and repeat the same procedure with the
next character in the string and the child’s linked list. If the search fails
before reaching the end of the string, the string is neither a word nor a
prefix. If the last character in the string is reached, continue to search its
child’s linked list. If a string terminator is found, the search string is a
word in the DLB. If some other characters are also found, it is contained in
the DLB and also it is a prefix of some strings. If only other characters are
found and the string terminator is not found, it is a prefix of some strings
contained in the DLB but not a word.
1) (12 points – 6 + 6) Consider the two open addressing hash tables, with h(x) = x mod 13, shown
below. Also consider the following keys
(in order): 23, 17, 36, 24, 26, 37, 49
|
h2(36) = 4 h2(37) = 5 h2(49) = 6 |
|
Double
Hashing |
||||
|
Index |
key |
|
Index |
key |
||
|
0 |
26 |
|
0 |
26 |
||
|
1 |
37 |
|
1 |
36 |
||
|
2 |
49 |
|
2 |
|
||
|
3 |
|
|
3 |
37 |
||
|
4 |
17 |
|
4 |
17 |
||
|
5 |
|
|
5 |
|
||
|
6 |
|
|
6 |
|
||
|
7 |
|
|
7 |
|
||
|
8 |
|
|
8 |
|
||
|
9 |
|
|
9 |
49 |
||
|
10 |
23 |
|
10 |
23 |
||
|
11 |
36 |
|
11 |
24 |
||
|
12 |
24 |
|
12 |
|
||
a)
Assume that linear probing
is being used for collision resolution.
Show the table after the keys shown above are inserted in the order
shown above.
b)
Assume now that double hashing
is being used for collision resolution, with h2(x) = (x mod 11) +
1. Show the table after the keys shown
above are inserted in the order shown above.