Midterm Grading Criteria
Midterm Grading Criteria
• All problems are worth 15 points. Unless states otherwise, subproblems are weighted equally.
Problem 1
Determine whether each statement is True or False and briefly justify your answer for each state-
ment. Assume that every given function here is positive. If the statement is false, providing a
counter-example is enough.
Grading Criteria:
(+1 points) If you got the correct answer (True or False).
(+2 points) If you got the correct justification.
Grading Criteria:
(+1 points) If you got the correct answer (True or False).
(+2 points) If you got the correct justification.
Grading Criteria:
(+1 points) If you got the correct answer (True or False).
(+2 points) If you got the correct justification.
True. Let g be any function such that g(n) = o(f (n)). Since g is asymptotically positive,
let n0 be such that n ≥ n0 implies g(n) ≥ 0. Then f (n) + g(n) ≥ f (n) so f (n) + o(f (n)) =
Ω(f (n)). Next, choose n1 such that n ≥ n1 implies g(n) ≤ f (n). Then f (n) + g(n) ≤
f (n) + f (n) = 2f (n), so f (n) + o(f (n)) = O(f (n)). This implies f (n) + o(f (n)) = Θ(f (n)).
Grading Criteria:
(+1 points) If you got the correct answer (True or False).
(+2 points) If you got the correct justification.
(e) f (n) = Θ(g(n)) if and only if f (n) = o(g(n)) and f (n) = ω(g(n)).
Grading Criteria:
(+1 points) If you got the correct answer (True or False).
1
2024 Spring CS300 Midterm Solution Name : , Student ID :
2
2024 Spring CS300 Midterm Solution Name : , Student ID :
Problem 2
Using the indicated method, find a tight asymptotic bound on the recursion formula. The answer
should contain the process of finding the tight asymptotic bound.
(a) (5 points) Using the recursion tree method, find a tight asymptotic bound on the recursion
formula.
n 3n
T (n) = 2T ( ) + T ( ) + n2
2 8
Solution:
n2
( n2 )2 ( n2 )2 ( 3n
8 )
2
( n4 )2 ( n4 )2 ( 3n
16 )
2
( n4 )2 ( n4 )2 ( 3n
16 )
2
( 3n
16 )
2
( 3n
16 )
2
( 9n
64 )
2
..
.
θ(1)
depth is log2 n. Then the lower bound is the cost of full tree with longest depth, and upper
bound is the cost of full tree with shortest depth.
log 8 n log 8 3
The leaves number of full tree with shortest depth is 3 3 = n 3 .
41 41 log 83 3−1 log 8 3
n2 (1 + 64 + ( 41 2
64 ) + · · · + ( 64 ) ) + θ(1) × n 3 ≤ T (n)
2
T (n) = Ω(n )
Geometry series is constant. So, upper bound of T (n) = Ω(n2 ).
The leaves number of full tree with longest depth is 3log2 n = nlog2 3 .
41 41 2
T (n) ≤ n2 (1 + 64 + ( 64 ) + · · · + ( 41
64 )
log2 3−1
) + θ(1) × nlog2 3
2
T (n) = O(n )
The lower bound of T (n) = O(n2 ). So the tight asymptotic bound is Θ(n2 ).
Grading Criteria:
(+2 points) If your solution is Θ(n2 ).
(+1 point) If your draw write recursion tree.
(+2 points) If you write the correct lower, upper bound.
(−1 points) If you did not separate the leaves cost.
(−1 points) If you write wrong leaves number.
(−2 points) If you did not use recursion method. (If you write correct answer but not using
recursion method, then you will get +3 − 2 = +1 point.)
(Note) If you use wrong values in process, each mistake deducts 1 point.
3
2024 Spring CS300 Midterm Solution Name : , Student ID :
(b) Using the substitution method, find a tight asymptotic bound on the recursion formula.
n √
T (n) = 4T ( ) + n
2
Solution:
Guess that T (n) = Ω(n2 ), and prove that there exists c and n0 such that,
√ n2 √
r
n n
T (n) = 4T ( ) + n ≤ 4(c1 − c2 )+ n
2 4 2
√ √ √ √ √ √ √ √
≤ c1 n2 − c2 n − (2 2c2 n − c2 n − n) ≤ c1 n2 − c2 n − n{(2 2 − 1)c2 − 1}
√
≤ c1 n2 − c2 n
Therefore, for bit enough c1 and c2 > √1 , T (n) = O(n2 ).
2 2−1
Grading Criteria:
(+3 points) If your solution is Θ(n2 ).
(+2 points) If you write the correct lower, upper bound.
(−1 points) If you did not mention proper c range.
(−2 points) If you did use substitution method. (If you write correct answer but not using
substitution method, then you will get +3 − 2 = +1 point.)
(Note) If you use wrong values in process, each mistake deducts 1 point.
(c) Using the master theorem, find a tight asymptotic bound on the recursion formula.
n
T (n) = 2T ( ) + n lg n
2
Solution:
T (n) = aT ( nb ) + f (n)
In this recursion equation a = 2, b = 2, f (n) = n lg n
f (n) = n lg n = Θ(nlogb a lgk n) for constant k = 1. So, T (n) = Θ(nlogb a lgk+1 n) = Θ(n lg2 n)
Grading Criteria:
4
2024 Spring CS300 Midterm Solution Name : , Student ID :
5
2024 Spring CS300 Midterm Solution Name : , Student ID :
Problem 3
(a) (7 points) Suppose we multiply two n-digit octet numbers X and Y using divide-and-conquer
algorithms. We divide X into A and B, and Y into C and D, where A, B, C and D are
n/2-digit numbers.
X = g n/2 A + B
Y = g n/2 C + B
Consider the first implementation of such an algorithm whose recurrence relation of the time
complexity is T (n) = aT (n/b) + Θ(nc ):
XY = g n AC + g n/2 BC + g n/2 AD + BD
Next, we have the second implementation of an algorithm whose recurrence relation of the
time complexity is T (n) = dT (n/e) + Θ(nf ):
Solution:
The first implementation involves 4 multiplication of digits of size n/2. Conquering process
contains 3 additions of digits which contributes as Θ(n).
The second implementation involves 3 multiplication of digits of size n/2. Conquering pro-
cess contains 6 additions of digits which contributes as Θ(n).
g contributes as shifts for octet numbers. Therefore, the final answer is:
[4, 2, 1, 3, 2, 1, 8]
Grading Criteria:
(+1 points) For each correct number.
(b) (6 points) Toom-Cook algorithm is a generalized version of the Karatsuba algorithm. Similar
to Karatsuba algorithm, it divides the problem into k different subproblems. Toom-Cook
3-way multiplication is a widely known case of Toom-Cook multiplication where k = 3. The
recurrence relation can be expressed as follows:
Write down the running time of the algorithm in (a) and the Toom-Cook algorithm in Θ
notation and conclude which one is asymptotically faster. Write down the whole process to
get the full score (hint: 21.58 ≈ 3, 31.46 ≈ 5).
Solution:
Using the master theorem, we can easily find the running times. For the first and second
implementation, we have:
6
2024 Spring CS300 Midterm Solution Name : , Student ID :
Since log3 5 < log2 3 < 2, Toom-Cook 3-way multiplication is asymptotically fastest algo-
rithm.
Grading Criteria:
(+2 points) For getting correct running time for each algorithm.
(+2 points) Compare each running time and conclude that Toom-Cook 3-way multiplication
is asymptotically faster.
(−1.5 points) For showing no process.
7
2024 Spring CS300 Midterm Solution Name : , Student ID :
Problem 4
(a) (5 points) Given an array [9, 7, 5, 11, 12, 2, 14, 3, 10, 6], use the QuickSort Adaptive to sort the
array. Instead of tracing each recursive call, describe the state of the array after the first
two levels of recursion (i.e., after the first partitioning and then after the first partitioning of
each of those partitions). This approach will show how the algorithm progresses in broader
strokes, which is a different perspective from the detailed step-by-step trace.
Grading Criteria:
After the first partitioning with pivot 11 (the middle element): [9, 7, 5, 3, 6, 2, 10, 11, 12, 14]
Pivot 11 is now at its correct position, with elements less than 11 to its left and elements
greater than 11 to its right.
After partitioning the left part [9, 7, 5, 3, 6, 2, 10] with pivot 5: [2, 3, 5, 6, 7, 9, 10]
After partitioning the right part [12, 14] with pivot 12: [12, 14]
(b) (5 points) Suppose QuickSort Origin is used to sort an array where all elements are identical,
e.g., [4, 4, 4, 4, 4]. How many recursive calls to quicksort will be made before the algorithm
terminates? Just write down the number of calls.
Grading Criteria:
8 calls or 2(n − 1) calls (5 points)
9 calls or 2n − 1 calls (including initial call) (5 points)
4 calls or (n − 1) calls (didn’t consider that there are 2 QuickSort Origin calls within Quick-
Sort Origin - 4 points)
5 calls or n calls (didn’t consider that there are 2 QuickSort Origin calls within QuickSort
Origin but including initial call - 4 points)
(c) (5 points) If QuickSort Origin is used to sort an array in reverse order (e.g., [5, 4, 3, 2, 1]),
what will be the total number of recursive quicksort function calls made to sort the entire
array?
Grading Criteria:
8 calls or 2(n − 1) calls (5 points)
9 calls or 2n − 1 calls (including initial call) (5 points)
4 calls or (n − 1) calls (didn’t consider that there are 2 QuickSort Origin calls within Quick-
Sort Origin - 4 points)
5 calls or n calls (didn’t consider that there are 2 QuickSort Origin calls within QuickSort
Origin but including initial call - 4 points)
8
2024 Spring CS300 Midterm Solution Name : , Student ID :
Problem 5
(a) (5 points) Describe the worst-case scenario for the randomized quicksort algorithm. Explain
the condition that leads to the worst-case scenario, and find the worst-case time complexity
of quicksort in terms of n, the number of elements in the array.
Grading Criteria:
(+2 points) The worst-case is that only the maximum (or minimum) value is selected as a
pivot.
(+3 points) Time complexity = O(n2 )
(b) (5 points) Describe the best-case scenario for the randomized quicksort algorithm. Explain
the condition that leads to the best-case scenario, and find the best-case time complexity of
quicksort in terms of n, the number of elements in the array.
Grading Criteria:
(+2 points) The base-case is that the pivot divides the array into two equal-sized sub-arrays
(+3 points) Time complexity = O(n log n)
(c) (5 points) Find the recurrence relation T (n) of the worst-case linear-time selection algorithm,
when the input array is divided into groups of 7 instead of 5.
n 5n
T (n) ≤ T ( ) + T ( ) + O(n)
7 7
Grading Criteria:
n
(+2 points)
7
5n
(+2 points)
7
(+1 points) n
9
2024 Spring CS300 Midterm Solution Name : , Student ID :
Problem 6
(a) When drawing the DFS tree, write the number of vertices for each depth. The depth of the
root node is 0. (e.g. If there are 1 vertex for depth 0 and 2 vertices for depth 1, write in the
form of {0 : 1, 1 : 2})
Solution:
{0 : 1, 1 : 2, 2 : 1, 3 : 2, 4 : 2}
Grading Criteria:
(+5 points) Solution is correct.
(+3 points) Solution is wrong, but the correct DFS tree is drawn.
(b) Fill in the table below showing the number of outgoing edge by edge type of each vertex.
Solution:
Grading Criteria:
(+5 points) Solution is correct.
(−1 points) per incorrect one vertex(one row). But the minimum score is 0.
If you write connecting vertex instead of the number, 50% of the total score is deducted.
10
2024 Spring CS300 Midterm Solution Name : , Student ID :
(c) Fill in the table below showing the discoveryTime and finishTime for each vertex when you
finish the above algorithm.
Solution:
discoveryTime finishTime
A 1 28
B 7 10
C 3 23
D 14 16
E 18 20
F 12 21
G 5 22
H 25 27
Grading Criteria:
(+5 points) Solution is correct.
(−1 points) per incorrect one vertex(one row). But the minimum score is 0.
11
2024 Spring CS300 Midterm Solution Name : , Student ID :
Problem 7
(a) (6 points) Find all the strongly connected components(SCCs) of G (if the node u and node
v are a SCC, write in form of {u, v}).
Solution:
{a, b, d, e}, {c, f }, {g, j, k}, {h, i, m}, {l}, {n, o}
Grading Criteria:
(+1 points) Each correct answer.
(−1 points) If 6 correct answers + additional wrong answers.
(b) (2 points) Find the source vertices and sink vertices (The vertices should be in the form of
SCC).
Solution:
Grading Criteria:
(+0.5 points) Each correct answer.
(−0.5 points) Each additional wrong answer.
(−0.5 points) Source is not SCC form.
(−0.5 points) Sink is not SCC form.
(c) (5 points) How many possible topological orderings does the meta-graph of G have?
Solution:
For convenience, rename each node of the meta-graph of G as 1 through 6 as shown above.
Then, let’s focus on node 3. If arranged in order, nodes 1 and 5 should be on the left side
of node 3, nodes 4 and 6 should be on the right side of node 3, and the order of the same
side doesn’t matter. Then we only need to count the positions where node 2 can lie between
nodes 1 and 4.
1, 5, 3, 4, 6 → 3
1, 5, 3, 6, 4 → 4
5, 1, 3, 4, 6 → 2
5, 1, 3, 6, 4 → 3
12
2024 Spring CS300 Midterm Solution Name : , Student ID :
Grading Criteria:
(+5 points) Correct answer.
(d) (2 points) What is the minimum number of edges you must add to G to make it strongly
connected?
Solution:
Consider edges that make the meta-graph strongly connected. No matter which edge is
selected between the vertices in the SCC connected by those edges, it makes G strong con-
nected.
Grading Criteria:
(+2 points) Correct answer.
13
2024 Spring CS300 Midterm Solution Name : , Student ID :
Problem 8
(a) (3 points) Decide whether Dijkstra’s algorithm can get the shortest distance between vertices
on the following graphs (circle yes or no). If the algorithm cannot be applied, provide a
reason. Notice that there may exist a case where same vertex is visited more than twice.
Grading Criteria:
(+0.25 points) If your choice is correct.
(+1.0 points) If the provided reason is correct.
14
2024 Spring CS300 Midterm Solution Name : , Student ID :
(b) (7 points) Using the propety (i) and (ii), below proves the following theorem – Dijkstra’s
algorithm terminates with dist(v) = distance to v for all v ∈ V – by contradiction.
When x was added to R, update the distance between (x, y) - dist(x) + l(x, y) = dist(y). It
implies that dist(y) = distance to y ≤ distance to u < dist(u).
It contradicts the initial assumption, dist(u) ≤ dist(y) (because u is added prior to y).
Grading Criteria:
(+2 points) get relationship between x and y (or y and u)
(+2 points) get the inequality of dist(y) ≤ distance to u ≤ dist(u)
(+3 points) show contradiction - u is in R, dist(u) < dist(y). Contradiction.
(c) when comparing x to u with l(x, y) + l(y, u), you cannot use triangle inequality because
both value are literally same.
15
2024 Spring CS300 Midterm Solution Name : , Student ID :
(c) (5 points) Run Dijkstra’s algorithm, and get the shortest distance from S to D (dist(D)).
In the table, write down the dist(v) value (distance from S to particular vertex). First line
is written down for you, and you must fill in all the blanks.
S A B C D
0 1 3 4 ∞
0 1 2 3 ∞
0 1 2 3 4
0 1 2 3 4
dist(D) : 4
Grading Criteria:
(+0.3 points) Each blank.
(+0.5 points) Correct dist(D).
(Note) Writing dist(D) without filling the table is 0 points.
16