2022 Spring CS300 Midterm Solution Grading Criteria
2022 Spring CS300 Midterm Solution Grading Criteria
• All problems are worth 20 points. Unless stated otherwise, subproblems are weighted equally.
Answer: F,T,T,F,F
Grading Criteria
(+4) correct answer + proper justification
(+3) For problem 1-(b) and (c) proper justification but you used exist c/for some c.
(+3) For problem 1-(d) Give some proper condition and “In this condition, (d) may not be satis-
fied”, without giving counterexample
(+0) wrong answer / no justification / wrong justification.
1
2022 Spring CS300 Midterm Solution Name : , Student ID :
In this algorithm, the returned tuple (q, r) represents the quotient and remainder of a ÷ b, respec-
tively, so that they satisfy a = bq + r, with 0 ≤ r < b.
Explain and express the time complexity of the algorithm in terms of a and b, using big Θ no-
tation. Assume that all basic operations cost time O(1), including multiplication and division by 2.
Solution:
It is sufficient to only consider the two recursive calls of the function; all other run in con-
stant time. Then, assuming a ≥ 2b, the running time of division(a, b) is then T (a, b) =
T a2 , b + T (2r, b) + Θ(1), where r is the remainder of the division a ÷ b. As 0 ≤ r ≤ b − 1,
0 ≤ 2r < 2r + 1 < 2b holds, thus T (2r, b) = Θ(1) and T (2r + 1, b) = Θ(1). So we can write a
simpler representation for the running time: T (a, b) = T a2 , b + Θ(1). Also, the recursion ends
if and only if ab < 2. With the Master’s theorem, we can conclude that the running time of the
algorithm is T (a, b) = Θ log ab .
Grading Criteria:
Explanation of time complexity on the second recursive call: 8pts
Correct equation for the time complexity: 8pts
Correct time complexity: 4pts
2
2022 Spring CS300 Midterm Solution Name : , Student ID :
If test_all fails, we would like to reduce test_all by calling less methods to find the buggy
method efficiently. In this problem, the execution of test_all and finding out the kind of error
takes constant time. But, we do not know where test_all failed, just that it has.
(a) (10 points) Say that there is a single problematic method. Describe an O(log n) algorithm
that finds the problematic method.
(b) Now, consider the case where two methods are needed to both exist for a program to fail.
Such cases are much harder to reduce because they require both methods to be alive for
failure to be detected. That is, if test_all has only one of the methods, it will succeed. To
solve this issue, we have the following divide and conquer algorithm.
• Base: If test_all with two methods fail, return the two as problematic.
• Ind 1: Delete the second half of test_all, and see if it fails. If it does, recursively call
the algorithm with the new test_all.
• Ind 2: Delete the first half of test_all, and see if it fails. If it does, recursively call
the algorithm with the new test_all.
Solution:
(a) Use a similar style to binary search. Delete the last ⌊n/2⌋ of test_all and execute it. If
it fails, repeat on the new test_all until we have one method left. If not, repeat on the
deleted methods of test_all until we have one method left, and return it as problematic.
As there is only one failing method, a method fails in the subproblem if and only if it fails in
the original, so this algorithm will correctly find the failing method in the original test_all.
Since we have one subproblem and constant time extra operations, the recursive formula is
T (n) = T (n/2) + Θ(1). Hence by case 2 of the master method, time complexity is O(log n).
(b) (1) If there is only problematic method in the first half and one in the second half, then
neither Ind 1 nor Ind2 nor its recursive subproblems will fail, hence we cannot find
the two problematic methods.
(2) We know that one problematic method lies in the first half, and the other in the second.
Thus, we find them as follows.
• Ind 3: Apply the algorithm in (a) to the first half of test_all only. That is, when
deleting methods from test_all, we only delete from the first half of it, always
having the second half.
• Ind 4: Similarly, find the apply the algorithm in (a) to the second half.
3
2022 Spring CS300 Midterm Solution Name : , Student ID :
If we always keep first/second half, then test_all will fail if and only if the problematic
method is alive in the second/first half. Thus, this problem can be reduced to two
problems of problem (a), so the above extension using the solution to (a) is correct.
Grading Criteria:
(a) • 4 points for algorithm description. 2 points if only binary search is mentioned.
• 4 points for algorithm correctness explanation.
• 2 point for time analysis.
(b) (1) 5 points for correct explanation.
(2) • 3 points for algorithm description.
• 2 points for correctness explanation.
• I originally intended for one to use their solution to (a), but since I did not mention
time complexity in the problem, any solution is fine.
4
2022 Spring CS300 Midterm Solution Name : , Student ID :
1: function Partition(A, p, q)
2: x ← A[q]
3: i←p
4: for j ← p + 1 to q do
5: if A[j] ≤ x then
6: exchange A[i] with A[j]
7: i←i+1
8: end if
9: end for
10: return i
11: end function
12:
13: function Sort(A, p, r)
14: if p < r then
15: q ← Partition(A, p, r)
16: Sort(A, p, q − 1)
17: Sort(A, q, r)
18: end if
19: end function
20:
21: Sort(A, 1, A.length)
(a) Find all the errors she made and fix the algorithm.
(b) Give a tight worst-case input list A for the fixed algorithm, where the length of the list
|A| > 5 and all elements in A are distinct. Note that if there is an example with the same
length which yields more iterations, your example is not a tight worst-case example.
5
2022 Spring CS300 Midterm Solution Name : , Student ID :
(a) Solution 1
1: function Partition(A, p, q)
2: x ← A[q]
3: i←p
4: for j ← p to q − 1 do
5: if A[j] ≤ x then
6: exchange A[i] with A[j]
7: i←i+1
8: end if
9: end for
10: exchange A[i] with A[q]
11: return i
12: end function
13: function Sort(A, p, r)
14: if p < r then
15: q ← Partition(A, p, r)
16: Sort(A, p, q − 1)
17: Sort(A, q + 1, r)
18: end if
19: end function
20: Sort(A, 1, A.length)
Solution 2
1: function Partition(A, p, q)
2: x ← A[q]
3: i←p−1
4: for j ← p to q − 1 do
5: if A[j] ≤ x then
6: i←i+1 ▷ (swap line 6 and line 7)
7: exchange A[i] with A[j]
8: end if
9: end for
10: exchange A[i + 1] with A[q]
11: return i + 1
12: end function
13: function Sort(A, p, r)
14: if p < r then
15: q ← Partition(A, p, r)
16: Sort(A, p, q − 1)
17: Sort(A, q + 1, r)
18: end if
19: end function
20: Sort(A, 1, A.length)
6
2022 Spring CS300 Midterm Solution Name : , Student ID :
Grading Criteria
(+8) Correct algorithm
(+2) Reasonable descriptions to justify answer. You will get 0 credit if your algorithm is
wrong.
7
2022 Spring CS300 Midterm Solution Name : , Student ID :
(b) Solution
Any examples that satisfies [a1 , a2 , ..., an ] where n > 5 and ai < aj , i < j. For example, [1,
2, 3, 4, 5, 6]
Description
At every partition, the algorithm will choose pivots that have lowest values with the given
list. It will generate (n-1):0 splits for every n iterations.
Grading Criteria
(+8) Correct example.
(+3) Reversed example. For example, [6, 5, 4, 3, 2, 1]. This is not a worst-case example
on the pseudo code. However, if we try to sort with descending order, this yields a tight
worst-case.
(+2) Reasonable descriptions to justify answer. You will get 0 credit if your example is
neither correct nor reversed.
(-1) If |A| ≤ 5 or |A| not known.
8
2022 Spring CS300 Midterm Solution Name : , Student ID :
With this motivation, in this problem, you will see constrained arrays, and you should derive the
lower bounds of comparison based sort for such arrays.
Use below assumptions
l[i] < l[2i] and l[i] < l[2i + 1] for all 1 ≤ i ≤ 2n−1 − 1.
For example, [1, 4, 2, 11, 9, 5, 3, 13, 15, 10, 14, 12, 6, 8, 7] satisfy this condition.
(c) The length of array is 2n , and it follows Pn‘Boolean Lattice’ structure. Formally, consider the
binary representation f (t1 , . . . , tn ) = i=1 ti 2i−1 + 1, then
For example, [1(000), 3(001), 2(010), 4(011), 5(100), 7(101), 6(110), 8(111)] satisfy this condi-
tion where number inside parenthesis is binary representation of index.
(d) The length of array is n, and the array is ‘k-sorted’. Formally,
9
2022 Spring CS300 Midterm Solution Name : , Student ID :
Overall, this problem is computing the lower bound of comparison based sort. As taught in lecture,
it is enough to compute number of possible total ordering, given the partial order (constraint). If
we denote L(P ) as the number of total order given partial order P , then since each comparison
will at most divide it by 2, so the lower bound is log2 (L(P )). For the computation, we will use
following approximation of binomial coefficient, derived from approximation of n!.
22n
2n
∼√
n nπ
If you are interested, you can search with keyword ‘Linear Extension’, which is one of the major
topic in algebraic combinatorics. Also, there is a paper stating that this lower bound is achievable,
using the notion of graph entropy. https://dl.acm.org/doi/10.1145/129712.129731
(a) Ignoring the constraint we have (nk)! rearrangements, then since each blocks are sorted, we
have (nk)!
(k!)n . Taking logarithm gives
log2 L(n) = log2 (nk)! − n log2 (k!) ∼ nk log nk − nk log k = Ω(nk log n)
(b) Denote L(n) the number of possible total ordering of ‘heap’ structure of size 2n − 1. Then it
is recursively defined as
L(1) = 1
2n+1 − 2
L(n + 1) = L(n)2 ×
2n − 1
which gives
log2 L(n + 1) = 2 log2 L(n) + Ω(2n )
Solving recurrence relation gives
(c) Instead of exact result, we can derive approximate bound for this problem and (d). If we
only consider the rearrangement in each ‘level’ (indices having same number of 1), we can
derive
n
Y n
L(n) ≥ !
i=0
i
which then gives
n
X n n n n
log2 L(n) ≥ log2 ≥ log2 = Ω(n2n )
i=0
i i ⌈n/2⌉ ⌈n/2⌉
n
2n
(This is optimal result, and you can prove it by upper bound L(n) ≤ ⌈n/2⌉ .)
(d) First divide the array to size k blocks, giving ⌈n/k⌉ blocks. If we rearrange only in the blocks,
we obtain k-sorted list. Using similar approach to (a), we have
(This is optimal result, and you can prove it by upper bound L(n, k) ≤ (2k + 1)n .)
Grading criteria is given as following (for non-optimal but nontrivial ones, only 1pt).
(a) Derive the number of possible rearragement (3pt), give the lower bound (2pt).
(b) Derive the recursive formula for T (3pt), give the lower bound (2pt).
(c) Derive (reasonable) possible rearrangements (3pt), give the lower bound (2pt).
(d) Derive (reasonable) possible rearrangements (3pt), give the lower bound (2pt).
10
2022 Spring CS300 Midterm Solution Name : , Student ID :
Problem 6. Selection
Short Answers
n
a)
6
n
b) for both blanks
3
n 2n
c) T (n) ≤ T ( ) + T ( ) + O(n)
3 3
Explanations
n
a) As in the figure, the medians are of whole array, and median of medians x is located at the
3
n 1 n
center, making ∗ = of elements smaller than x.
3 2 6
2
b) The of each 3-element group are less then or equal to each group medians. Since half of
3
2 1 n
group medians are less than or equal to x, total ∗ ∗ n = of elements are smaller than x. In a
3 2 3
n
similar way, of elements are larger than x.
3
n
c) T ( ) comes from recursively selecting the median of medians
3
2n
T ( ) comes from the maximum size of the subarray when SELECT function is recursively called.
3
The O(n) comes from the PARTITION function, and selecting the medians for each 3-element
group.
Grading Criteria
• Writing x instead n and the fraction is correct (other variable than x is fine) (-1pt)
• Wrong denominator (wrong fraction) is considered as wrong answer.
• For each wrong blanks, points are given as below.
• For a), wrong answer (0pt)
• For b), one wrong answer (-1pt) / both are wrong (0pt)
• For c), 1 points for each blank
11
2022 Spring CS300 Midterm Solution Name : , Student ID :
Long Answers
Assume that T (k) ≤ c × k for all k < n.
k 2k
Then, T (k) ≤ T ( ) + T ( ) + O(k)
3 3
k 2k
=c× +c× + O(k)
3 3
= c × k + O(k) ≥ c × k, c is positive constant (contradiction)
n 2n
To satisfy the relation, + should be smaller than 1. But the size of the subproblem is equal
3 3
to the original problem, thus does not satisfy T (n) ≤ c × n. Therefore, T (n) is not linear.
[Recursion Tree]
No, it does not satisfy T(n) = O(n). Using a recursive tree method, the tree can be drawn like
below.
The leftmost height of tree at level k would be k = log3 n and the rightmost height of the tree at
level k would be k = log 23 n. Getting the bound for the recurrence relation, it becomes
Within the same height, the time complexity is n(cn), and the lower bound becomes n log n.
Therefore, the recurrence relation is not linear.
Grading Criteria
• Wrong or no short answer (-1pts)
2n
• State that subproblem c( ) alone makes T (n) not linear (-7pts)
3
• Only stating certain method is used and skipping the explanation / undone answer (1pt)
• Nothing is written or only yes or no is written (0pt)
Substitution Method
• Convert O(n) into c′ × n and stating c × k + c′ × n ≤ c × k (-5pts)
• Error on explanation: Stating that sum of constant on subproblem ¿ n satisify O(n) or sum
of constant on subproblem ¡ n does not satisfy the linearity (-5pts)
12
2022 Spring CS300 Midterm Solution Name : , Student ID :
Master Theorem
2n
• Not getting the complexity of the tight bound (e.g. convert into T (n) ≤ 2 × T ( ) and solve
3
about RHS) (-5pts)
• No explanation which method is used and some intermediate process is missing (-3pts)
Recurrence Tree
• Only drawing the recurrence tree and not giving the explanation (-5pt)
• No point deduction for only getting one tight bound for the recursion tree (-0pt)
The recurrence relation equation is wrong and influenced the explanation I thought giving zero for
all is to cruel, so I gave some base points and take points off.
• In case subproblem makes up to n, subtracted 1pt / graded following above guideline ((−1 +
α)pts)
Usually -4pts: no explanation why cn does not work (-3pts) and extra (-1pt).
• The subproblem make up to n, but O(1) term makes the recurrence relation linear subtract
1pts / graded following above guideline ((−1 + α)pts)
Usually -5 6pts: added up constant time together with the subproblem and say satisfy O(n)
(-5pts) and extra (-1pt).
Correct subproblem explanation but omitting O(1) makes lienar (-3pts), wrong ans (-1pt),
and extra (-1pt).
• The recurrence relation is when the array is divided as 5: 4pts for just memorizing the class
slide / graded following above guideline ((−7 + α)pts)
Usually -7pts because memorized answer has no error itself.
• The sum of the recurrence relation is more or less than n and proof is done: 6pts not knowing
if the student will correctly answer when recurrence relation sums up to n / graded following
above guideline ((−5 + α)pts)
Usually -5pts if substitution / recurrence tree proof works.
More deduction if the proof is wrong.
13
2022 Spring CS300 Midterm Solution Name : , Student ID :
Problem 7. DFS
A Bipartite graph is a graph whose vertices can be divided into two disjoint and independent sets
U and V . That is, every edge should connect a vertex in U to one in V , not in the same set.
Here are some examples. (A, B ∈ U and C, D ∈ V )
<Fig 1>
1. (8 points) You can easily make a bipartite graph using DFS. Write the way in a few sentences.
(hint: using 2 colors for grouping)
2. (6 points) Draw a bipartite graph for <Fig 1>. DFS should be executed in alphabetical
order. (Left is a different group than the right)
14
2022 Spring CS300 Midterm Solution Name : , Student ID :
3. (6 points) Given U , V sets for a bipartite graph, if a vertex in U only matches to one vertex
in V , 1:1 matching, it is called a bipartite matching. Here is an algorithm for maximum
bipartite matching using DFS.
Here is a result of bipartite matching on Graph1. In this case, A is matched with D and B
is matched with C.
15
2022 Spring CS300 Midterm Solution Name : , Student ID :
solution
(a) (8pt) Whenever any node is visited, check the node with different color which is different
than previous node.
Or any other sentences to make the graph into 2 disjoint groups are okay.
(0pt) All other things
(b) (6pt)
(c) (6pt)
16
2022 Spring CS300 Midterm Solution Name : , Student ID :
Problem 8. SCC
Consider the directed graph G in the below figure.
Answer:
17