Anda di halaman 1dari 7

CST 370 Design and Analysis of Algorithms

Summer A 2018
Final Exam

Name: Josh Jones

Four-digits ID: 4444

 Test time is 2 hours and 30 minutes.


 Note that there are 13 problems in the midterm.
 This is a closed book exam. You can’t use a calculator during the
exam. However, as a reference during the exam, you can prepare “three
pages (= total of 6 sides)” cheat sheet. The cheat sheet can be typed or
hand-written.
 If possible, enter your answers directly into the Word file to increase the
readability of your answers. However, if it is too difficult or time
consuming to type in a Word file, please write down your answer on
paper. Then, take a picture and insert the picture into the Word file.
 During the exam, you must sign into the midterm Zoom session and turn
on the video. We will record the video. However, turn off the audio on
your computer.
 If you have a question during the exam, please use "Chat" in Zoom. I will
answer.
 When you finish the exam, submit your Word file (and PDF file) on the
iLearn. But keep the Word file well in case we need it.
 Use your time wisely—make sure to answer the questions you know first.
 Read the questions carefully.

CST370 Page 1 of 7 Final Exam


1. (1 point) Assume that Dr. Byun assigned a programming project which requires the time
complexity of Θ(n2). If your program’s basic operation runs (5*n*logn + 25) times, can you say
that your program meets the project requirement? (Yes/No).

No

2. (1 point) Order the following functions according to their order of growth (from the fastest to
the slowest):

(n*(n+1))/2, 2n, 0.1*n3 + 3*n + 1, 2*n*logn, 7*n + 5

7*n + 5  2*n*logn  (n*(n+1))/2  0.1*n3 + 3*n + 1  2n

3. (1 point) Consider the following master theorem:


T(n) = aT(n/b) + f (n) where f(n)  (nd), d  0

Master Theorem: If a < bd, T(n)  (nd)


If a = bd, T(n)  (nd log n)
If a > bd, T(n)  (nlogba )

Based on the above theorem, determine the time efficiency of the following formula T(n).

a = 2, b = 4, c = 1. So, this is the case 1  (n1) = (n)

4. (4 points) Solve the following recurrence relation using the backward substitution. What is
the time complexity of the recurrence relation?

M(n) = M(n – 1) + n, for n > 1 // recurrence relation


M(1) = 2 // initial condition

M(n) = M(n-1) + n // replace M(n-1) with “M(n-2) + (n-1)”


= [M(n-2) + (n-1)] + n
= M(n-2) + (n-1) + n // replace M(n-2) with “M(n-3) + (n-2)”
= [M(n-3) + (n-2)] + (n-1) + n
= M(n-3) + (n-2) + (n-1) + n
...
= M(n-i) + (n-(i-1)) + (n-(i-2)) + ... + (n-2) + (n-1) +n
...
= M(1) + 2 + 3 + ... + (n-2) + (n-1) +n
= 2 + 2 + 3 + ... + (n-2) + (n-1) +n
= (n*(n+1))/2+1
 (n2)

5. (3 points) Suppose you have three jars, A, B, and C, in a room. Jar A has 5 large black balls, 3
large red balls, and 2 large green balls. Jar B has 4 small black balls, 3 small red balls, and 2
small green balls. Jar C is empty. Thus, there are total 19 balls. Now, you will pick a few balls
from the jar A in the dark and place them in the jar C. After that, you will pick a few balls from
the jar B in the dark and place them in the jar C. Note that the color of the selected balls at the jars
A and B can not be confirmed because the surroundings are dark. Also, the numbers of balls

CST370 Page 2 of 7 Final Exam


selected from the jars A and B need not always be the same. Once you're done, you can turn on
the lights in the room and see the balls in the jar C.

(a) Assuming the worst case occurs, what is the minimum number of balls you have to choose to
get a matching pair? Here, a matching pair means that there must be one large ball and one
small ball of the same color in the jar C. But the color itself of the pair is not important. Present
just the number of balls. You don’t need to explain your answer.

9 balls: You should select 8 balls from the jar B and one from the jar A. By selecting
8 balls from the jar B, you know that it guarantees one ball from each color.

(b) Assuming the best case occurs, what is the minimum number of balls you have to choose to
get three matching pairs of each color (= black, red, green)? In other words, you should have
one pair of large and small black balls, one pair of large and small red balls, and one pair of large
and small green balls. Present just the number of balls. You don’t need to explain your answer.

6 balls: Since this is the best case, you can select just 3 balls from the jar A and 3
balls from the jar B.

(c) Assuming the worst case occurs, what is the minimum number of balls you have to choose to
get three matching pairs of each color (= black, red, green)? In other words, you should have
one pair of large and small black balls, one pair of large and small red balls, and one pair of large
and small green balls. Present just the number of balls. You don’t need to explain your answer.

17 balls: Since this is the worst case, you should select 9 balls from the jar A and 8
balls from the jar B to guarantee each color from each jar.

6. (1 point) Is the following graph a DAG? (Yes/No) There’s a cycle in the graph.

7. (2 points) Draw a binary tree with ten nodes labeled 0, 1, 2, ..., 9 in such a way that the inorder
and preorder traversals of the tree yield the following lists:
9, 3, 1, 4, 0, 2, 8, 6, 5, 7 (inorder)
2, 1, 9, 3, 0, 4, 8, 5, 6, 7 (preorder).

Note that the problem is asking to draw only one binary tree. If you can’t completely draw a
binary tree with the given information, explain why.

CST370 Page 3 of 7 Final Exam


8. (2 points) Consider a 2-3 tree as below. Add the following numbers to the tree one by one.
You should present each step clearly.

10, 11, 8, 9

7
2

5 5 5,10 5,10
/ \ / \ / | / | \
2 7, 10 2 7, 10, 11 2 7,11 2 7,8 11

5,10 5,8,10 5,8, 10


/ | \ / | \ / / | \
2 7,8,9 11 2 7,9 11 2 7 9 11

8
\ /
5
10
/ \ / \
2 7 9 11
9. (3 points) Apply the Warshall’s algorithm to get the transitive closure of the digraph defined by
the following graph. Present R(0), R(1), R(2), R(3), and R(4) as we discussed in the class.

1 2

3 4

R(0)
1 2 3 4

1 0 1 0 1

2 0 0 0 0

3 1 1 0 0

4 0 0 1 0

CST370 Page 4 of 7 Final Exam


R(1)
1 2 3 4

1 0 1 0 1

2 0 0 0 0

3 1 1 0 1

4 0 0 1 0

R(2)
1 2 3 4

1 0 1 0 1

2 0 0 0 0

3 1 1 0 1

4 0 0 1 0

R(3)
1 2 3 4

1 0 1 0 1

2 0 0 0 0

3 1 1 0 1

4 1 1 1 1

R(4)
1 2 3 4

1 1 1 1 1

2 0 0 0 0

3 1 1 1 1

4 1 1 1 1

CST370 Page 5 of 7 Final Exam


10. (3 points) Assume that you are going to solve the MST (Minimum Spanning Tree) problem
using the Prim’s algorithm for the following graph. Draw the final MST. For the problem, you
have to start from the vertex a. You must also provide the sequence of vertices to be added to the
"visited" set as we covered in the class.

Visited = {a, b, f, e, d, c}

11. (3 points) Assume that you are going to solve the single-source shortest-paths problem using
the Dijkstra’s algorithm for the following graph. For the problem, you should start from the
vertex a. Fill out the table as you learned in the class.

a 4 c

2 1 4
1

b 3 d 6 e

V a b c d e
a 0a 2a 4a 1a 
d 2a 2d 1a 
b 2a 2d 

CST370 Page 6 of 7 Final Exam


c 2d 6c
e 6c

12. (2 points) [Puzzle] 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 don’t 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.

We can determine in two weighings.

Step 1. Divide the coins into two groups (= 4 coins per group) and weigh them.
Step 2. Take the four coins from heavier group.
Step 3. Split this heavier group into two group of two coins and weigh them.

At the step 3, if the scale balances, we know that all the coins in the heavier group are real
and the fake coin is lighter than the rest.
At the step 3, if the scale is out of balance, then we know that the fake coin is in the heavy
pile and therefore the fake coin is heavier than the others.

Of course, you can pick the four coins from lighter group at the step two. Then, you can
determine similar to the step 3 with the lighter group.

13. (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.

Sort First and then I would find the median position by using this idea:
Mid = ((high limit– low limit)/2) +1
Low limit would equal to 0 for this list and high limit would be 4
When we plug in the numbers, we get (4-0)/2 + 1 = 3, the third index is 7, which happens to
be the median.

CST370 Page 7 of 7 Final Exam

Anda mungkin juga menyukai