Summer A 2018
Final Exam
No
2. (1 point) Order the following functions according to their order of growth (from the fastest to
the slowest):
Based on the above theorem, determine the time efficiency of the following formula T(n).
4. (4 points) Solve the following recurrence relation using the backward substitution. What is
the time complexity of the recurrence relation?
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
(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.
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
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
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
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
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.
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.