You are on page 1of 7

CST 370 Design and Analysis of Algorithms

Summer A 2017
Final Exam

Name: ________________Huy Nguyen____________________

CST370 Page 1 of 7 Final


1. (4 points) Consider the following recursive algorithm.
Algorithm Q(n)
if n = 1
return 1
else
return Q(n - 1) + 2 * n 1

(a) Present the return value for Q(1).

Q(1) = 1

(b) Present the return value for Q(2).

Q(2) = 3

(c) Present the return value for Q(3).

Q(3) = 7

(d) Based on the results of the above questions and other input number n, describe what this
algorithm computes.

Each iteration increases increments n by 2n-1.

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

(c) 2 * n * (n 1) O(n*log n) (true/false)


(d) 4 * (n * n ) + 3 * n (n) (true/false)

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)

CST370 Page 2 of 7 Final


4. (5 points) Solve the following recurrence relation using the backward substitution as we
discussed in the class. In the problem, you should present the intermediate steps and final time
complexity of the recurrence relation.

M(n) = 2 * M(n 1) + 2 // recurrence relation

M(1) = 5 // initial condition

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

CST370 Page 3 of 7 Final


6. (2 points) Assume that you want to solve a knapsack problem. Your knapsack capacity is 6
and there are five items as below

item weight value


A 3 $15
B 2 $10
C 1 $5
D 4 $30
E 3 $40

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)

7. (3 points) Consider the following algorithm.

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

Algorithm DoSomething (n)


1. if (n = 1)
2. print the current content of the whole array A on the screen;
3. else
4. for i 1 to n do
5. DoSomething(n 1); // Recursive call.
6. if n is odd
7. swap A[1] and A[n];
8. else
9. swap A[i] and A[n];
10. return;

(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]

CST370 Page 4 of 7 Final


(c) (Bonus Problem) Present execution result of the algorithm where an array A has 5, 7, 9and
n is 3. Note that this is a bonus problem. So, if you solve the problem, you will get an extra-
credit.
DoSomething(3) = [ 5, 7, 9 ]
[ 7, 5, 9 ]
[ 9, 5, 7 ]
[ 5, 9, 7 ]
[ 7, 9, 5 ]
[ 9, 7, 5 ]

CST370 Page 5 of 7 Final


8. (4 points) Consider the following graph.

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

CST370 Page 6 of 7 Final


9. (2 points) We know that the angle of the two clock hands is 90 degrees at 3:00(PM). Then what
is the angle of the two clock hands when the time is 3:30 (PM)? Explain your answer.

The small hand will be half way between the 3 and 4.


So,
180 - 105 = 75

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.

Minimum 3 weighings to find out if fake coin is heavier or


lighter:

First weigh: coins 1,2,3 vs coins 4,5,6


If balance:
Either 7 or 8 is fake.
Individually compare 7 and 8 with one of the real
coins in the previous weighing. This will answer if the
coin is lighter or heavier.

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.

CST370 Page 7 of 7 Final

You might also like