0% found this document useful (0 votes)
12 views

2022 Spring CS300 Midterm Solution Grading Criteria

Uploaded by

yunajessi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

2022 Spring CS300 Midterm Solution Grading Criteria

Uploaded by

yunajessi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

2022 Spring CS300 Midterm Solution Name : , Student ID :

• All problems are worth 20 points. Unless stated otherwise, subproblems are weighted equally.

Problem 1. Asymptotic Analysis


All functions in this problem are positive. For each of the following statements, answer True or
False and BRIEFLY JUSTIFY you answer.

(a) If f (n) = O(g(n)) then 2f (n) = O(2g(n) ).


(b) If f (n) = ω(g(n)) and g(n) = ω(h(n)) then f (n) = ω(h(n)).
(c) f (n) = o(g(n)) if and only if g(n) = ω(f (n)).

(d) If f (n) = Ω(g(n)) then f (f (n)) = Ω(g(g(n)).


(e) f (n) = Θ(g(n)) if and only if f (n) = o(g(n)) and f (n) = ω(g(n)).

Answer: F,T,T,F,F

(a) FALSE, when f (n) = 2n, g(n) = n.


(b) TRUE, For any c > 0, there is some n0 making f (n) > cg(n) and g(n) > h(n) with the if
conditions. Therefore, for any c > 0 exist n0 such that g(n) > ch(n) for all n ≥ n0 ” holds.
(c) TRUE,
f (n) = o(g(n))
⇔ for any c > 0, there exists n0 such that f (n) < cg(n), for all n ≥ n0
⇔ for any c > 0, there exists n0 such that 1/c · f (n) < g(n), for all n ≥ n0
⇔ g(n) = ω(f (n))
n
(d) FALSE, when f (n) = 2n , and g(n) = 2n+1 . g(g(n)) = 2 · (22 )2 .
(e) FALSE, when f (n) = g(n) = n.

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.

wrong justification examples


• given counterexample is not POSITIVE function. ex) f (n) = −n.

• 1-(d) is FALSE, it is correct only when f and g are increasing function.


• 1-(e) is FALSE, correct statement is f (n) = Θ(g(n)) if and only if f (n) = O(g(n)) and
f (n) = Ω(g(n)).
• 1-(e) is FALSE, definition c1 f (n) < g(n) < c2 f (n) and c3 f (n) ≤ g(n) ≤ c4 f (n) are different.
later have equality.

1
2022 Spring CS300 Midterm Solution Name : , Student ID :

Problem 2. Solving Recurrences


Here is an algorithm for division between non-negative integers.

Algorithm 1 Division algorithm


Require: b ̸= 0
procedure Division(a, b)
if a < b then return (0, a)
end if
if a < 2b then return (1, a − b)
end if
(q, r) = Division(⌊ a2 ⌋, b)
q ← 2q, r ← 2r
if a is odd then r ← r + 1
end if
(q ′ , r′ ) = Division(r, b)
return (q + q ′ , r′ )
end procedure

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 :

Problem 3. Divide and Conquer


When implementing methods for a class, it is common to write tests to verify them. An important
test is one that calls multiple methods at once, to check compatibility between the methods. In
this problem, we consider such function test_all, which calls all methods for a given class at
once. An example Python class and test_all function is given below.
1 # cs300 . py
2 class CS300 () :
3 # I mp le m en ta t io ns omitted . May fail under some condition .
4 def __init__ ( self ) :
5 def method_1 ( self ) :
6 def method_2 ( self ) :
7 def method_3 ( self ) :
8
9 # Some other file
10 def test_all () :
11 cs = cs300 . CS300 ()
12 cs . method_1 ()
13 cs . method_2 ()
14 cs . method_3 ()

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.

(1) (5 points) Explain why the above algorithm is incorrect.


(2) (5 points) Extend the above algorithm to make it correct. Hint: use your solution to
(a).

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 :

Problem 4. Quick Sort


A student tries to write pseudo code for Quick Sort with the pivot element to be the last element
of the subarray. Below is her pseudo code. Answer the following questions with short descriptions
to justify your answer.

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 :

Description (from Solution 1)


In line 4, for loop should start from the first element of the array and end before the last
element, to compare with every elements except pivot. In line 10, the pivot element should
be inserted in place. In line 17, sorting subarray should not include previous pivot element.

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 :

Problem 5. Lower Bound


In modern sorting algorithms, we do not use a single algorithm. Rather, we first skim the array
(which takes Θ(n) time), and choose suitable algorithm for sorting. For example, we know some
algorithms achieve best time complexity O(n) in special cases, and if we find that the given array
is such special case, we can use the algorithm, resulting O(n) running time.

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

• comparison takes Θ(1).


√ n
• n! ∼ 2πn ne
• l is an array with unique values with some length. l[1], . . . , l[length].

• l[i : j] is subarray: [l[i], l[i + 1], . . . , l[j]].


• t(i) as the index at sorted array of i-th element.
For example, if we have l = [3, 7, 2, 9], we have t(1) = 2, t(2) = 3, t(3) = 1, and t(4) = 4.
Write the lower bound as T (n) = Ω(f (n)) or T (n, k) = Ω(f (n, k)), with enough justifi-
cation. Try to give the tightest lower bound, which means that you can not say something like
“everything is Ω(1)”.
(a) The length of array is n×k. Each k-sized block of array l[1 : k], l[k +1 : 2k], . . . , l[(n−1)k +1 :
nk] is partially sorted. Formally,

l[i1 + jk] < l[i2 + jk] for all 1 ≤ i1 < i2 ≤ k, 0 ≤ j ≤ n − 1.

For example, [1, 3, 4, 8, 10, 2, 5, 6, 7, 9] with (k = 5, n = 2) satisfy this condition.


(b) The length of array is 2n − 1, and it follows ‘heap’ structure. Formally,

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

l[f (x)] ≤ l[f (y)] for all x, y ∈ {0, 1}n that xi ≤ yi , ∀1 ≤ i ≤ n

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,

|i − t(i)| ≤ k for all 1 ≤ i ≤ n.

For example, [2, 1, 5, 3, 4, 6, 8, 9, 7, 10] with k = 2 satisfy this condition.

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

log2 L(n) = Ω(n2n )

(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

log2 L(n, k) ≥ log2 (k!)n/k = Ω(n log2 k)

(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

Figure 1: Median of medians diagram

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.

Figure 2: Recursion Tree

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

n log3 n ≤ T (n) ≤ n log 23 n

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 :

• Skipping explanation about size of subproblem makes contradiction (-3pts)

• Wrong reasoning about contradiction (-3pts)


• Explain that during the proof, explain that the sum of constant = 1 therefore makes contra-
diction (-0pt)
• Assuming that T (n) = O(n log n) and perform substitution (-0pt).

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)

• Not specifying the each level of tree takes O(n) (-3pt)


• Not specifying the bound (and say the height is about log n or log 3n etc..) (-2pt)
• The reasoning is correct, but the ≤ sign is flipped / bound flipped (-1pt)

• 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 )

Graph1 is a bipartite graph, but Graph2 is not.

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

Draw a result of bipartite matching on Figure <Fig 1>.

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)

(0pt) All other things

(c) (6pt)

(0pt) All other things

16
2022 Spring CS300 Midterm Solution Name : , Student ID :

Problem 8. SCC
Consider the directed graph G in the below figure.

(a) Write all the strongly connected components.


(b) Find the source and sink vertices in the meta-graph of G.

Answer:

(a) {a,b,c}, {d}, {e}, {f,g,h,i,k}, {j}


(b) source : {a,b,c}
sink : {e}, {j}
Grading Criteria:

(a) • 2 points for each option


• 1 point if an option is almost complete but does not contain 1-2 nodes.
(b) • 10 points if source and sink vertices are correct.
• -2 points if source and sink are switched.
• -2 points if the class notation has not been properly used. (e.g., a, b, c instead of {a, b,
c})
• -2 points if a node class has not been mentioned.

17

You might also like