Professional Documents
Culture Documents
Final Summer A 17
Final Summer A 17
Summer A 2017
Final Exam
Q(1) = 1
Q(2) = 3
Q(3) = 7
(d) Based on the results of the above questions and other input number n, describe what this
algorithm computes.
2. (2 points) Based on the definitions of O, , and , determine whether the following assertions
are true or false.
(a) 3* n * n * (n + 1) (n2) (true/false)
(b) n * n * (n + 1) + 7 * n * n O(n ) (true/false)
3
3. (1 point) The following algorithm is designed to calculate the number of leaves in a binary
search tree. Is this algorithm correct? (Yes / No)
Algorithm LeafCounter(T)
//Input: A binary search tree T
//Output: The number of leaves in T
if T = return 0
else return LeafCounter(TL)+ LeafCounter(TR)
M(n) = 2 * M(n 1) + 2
= 2 * [2 * M(n 2) + 2] + 2
= 4 * M(n-2) + 4
= 4 * [2 * M(n 3) + 2] + 4
= 8 * M(n 3) + 6
...
= 2i * M(n-i) + 2i
...
= 2n * M(0) + 2n
= (n)
5. (2 points) Consider the brute-force string-matching algorithm covered in the class. Present an
example of a text of length 7 and a pattern of length 4 that constitutes the worst-case input for
the brute-force string-matching algorithm. And also, present an example of best-case input for
the problem.
Worst-case:
Text = AAAAAAB
Pattern = AAAB
Best-case:
Text = AAABAAA
Pattern = AAAB
To bring the maximum value, which item(s) will you bring to the knapsack? Do you have only
one optimal solution?
Two solutions:
(A, E)
(B, C, E)
// Assume that n is a positive integer (= i.e. n > 1), and A[1..n] is a global array.
// Note that the index of array A starts from one, not zero.
// And also, dont forget the array A in the algorithm is global.
(a) Present execution result of the algorithm where an array A has 5and n is 1.
DoSomething(1) = [5]
(b) Present execution result of the algorithm where an array A has 5, 7and n is 2.
DoSomething(2) = [5, 7]
[7, 5]
a b h
d
e f c g
Starting at vertex a, traverse the graph using the depth-first search algorithm. After that, you
should present the final results of the mark array and DFS tree with only tree edges as we
discussed in the class. For the algorithm, you have to use our convention of the class (= ascending
order for the alphabetical characters).
A. 1
B. 2
C. 5
D. 6
E. 4
F. 3
G. 7
H. 8
10. (3 points) Assume that you have 8 identical-looking coins and a two-pan balance scale with
no weights. One of the coins is fake, but it is not known whether it is lighter or heavier than the
real 7 coins. Describe your idea to determine in the minimum number of weighings whether the
fake coin is lighter or heavier than the others. Present the minimum number of weighings and
your answer clearly. In the problem, remember that I do not ask you to find the fake coin
among 8 coins using the scale. You dont need to find out which coin is fake. The question is that
you should be able to identify whether the fake coin is heavier or lighter than the real coins.
11. (2 points) Assume that you are looking for a median value of a list that is composed of n
numbers. For example, if you have a list that is composed of 4, 1, 10, 7, 9, the median is 7. In this
problem, you can assume that n is always an odd value. Present an idea to find a median value in
a list. Present your idea in English.
I would sort the list in ascending order and then return the
middle element as the median value. Since the list size is always
odd, element n/2 will always be the median.