CS 1501 Summer 2006:  Quiz 1 Solution
(thanks to Jiang Zheng)

1)      Fill in the Blanks and True/False (24 points -- 2 points each).
Complete the statements below with the MOST APPROPRIATE words/phrases.

a)         An algorithm has a run-time of Theta(n2) and, for n = 4 runs in 4 seconds on a computer.  How long will a problem of double the size (n = 8) take to run on the same computer? ______16__________

b)         Order the following growth rates from smallest (best) to greatest (worst):  n2  nlgn  lgn  n!  2n  n3 _____lgn < nlgn < n2 < n3  < 2n  < n! ______________

c)         Assume a graph has N vertices.  An upper bound on the worst case run-time of a Hamiltonian Cycle algorithm on the graph is Theta ____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.
F. 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.
F. 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.
F. In linear time, , where n is the size of the two strings. 

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.
T.

k)         The Knuth Morris Pratt string matching algorithm improves over the brute force algorithm in the worst case, but not in the normal case.
T.

l)           In the Rabin-Karp string matching algorithm, if the hash value of the pattern matches that of a substring of the text, the pattern has been found.
F.  Different strings might have the same hash value – a hash collision.


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 sunny

a)         Draw the de la Briandais tree that results after inserting the strings shown in the order shown above.

       

b)         In Assignment 1 you utilized a DLB to efficiently do 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 search its child’s link list. If a string terminator is found, the search string is contained in the DLB as a word. 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.   

 


3)      (10 points)  Consider the chessboard below with the queens in the marked locations.  Assume that we are using the recursive algorithm discussed in class, and that the current recursive call is attempting to place a queen in column 6.  Explain in detail the sequence of steps that occur until the first instance in which a queen is actually placed onto the board, or until a queen is moved from a previous position to a new valid location (in other words, continue until a queen is placed somewhere new, but not necessarily in column 6).  In any case, indicate where the queen is finally placed and why.  Assume that I know nothing about the algorithm or the theory behind it, and that you are teaching it to me with your explanation, so be VERY detailed.

 

0

1

2

3

4

5

6

7

0

Q

 

 

 

 

 

 

 

1

 

 

 

Q

 

 

 

 

2

 

Q

 

 

 

 

 

 

3

 

 

 

 

 

 

 

 

4

 

 

 

 

 

Q

 

 

5

 

 

Q

 

 

 

 

 

6

 

 

 

Q

 

 

 

7

 

 

 

Q

 

 

 

 

 

            This algorithm will test the possible spaces in column 6 first. Only one queen can be placed in each row, so row 0, 1, 2, 4, 5, 6 are all failed. Also no two queens can be on the same diagonal. Row 3 fails because it is on the same diagonal with the queen on column 5. Row 7 fails because it is on the same diagonal with queen on column 2. 

             This algorithm will then backtrack the queen in column 5 for a new position. Row 5 and 6 fails because of the queens on row 5 and row 6. Row 7 fails because of the queen on column 4 is on the diagonal position.

            This algorithm will then backtrack the queen in column 4 for a new position. Row 7 fails because the queen on column 2 is on the same diagonal.

            This algorithm will then backtrack the queen in column 3 for a new position. Row 2 and 5 fail because the queen on column 1 and column 2. Row 3 fails because of the queen on column 0 is on the same diagonal. Row 4 and 6 fail because they are on the same diagonal with the queen on column 2. But row 7 has no collision. So queen on column 3 is moved to row 7.