24fall CS300 Assignment5 Solution
24fall CS300 Assignment5 Solution
12 23:59:00
2024 Fall CS300 TA: Jisung Hwang (4011hjs@kaist.ac.kr)
Solution. Even if (pi , pi+1 ) has the smallest difference pi+1 − pi value, it does not mean that
(pi , pi+1 ) must be used to calculate the minimum value. (counter example: N = 3, M = 2, p1 =
0, p2 = 2, p3 = 3, p4 = 5, p5 = 200, p6 = 300. Although p3 − p2 is the smallest, the sum of p2 − p1
and p4 − p3 gives the minimum value.)
However, if it is not used, its two adjacent pairs must be used together. Because, if only one
of its adjacent pair is used to calculate the minimum value, replacing the pair into (pi , pi+1 ) can
make the sum smaller, so the sum cannot be the minimum. Else if both of the pairs are not used,
still (pi , pi+1 ) can replace a pair, making the sum smaller. (Criteria 1)
Since the pair with the smallest difference is used or its two adjacent pairs are used together,
we can design the following algorithm by repeating this process.
Grading Criteria:
(+10 points) Correct algorithm with optimal time complexity.
(+5 points) Correctly working with suboptimal time complexity.
(+5 points) Identifying the Criteria 1.
(+5 points) Used greedy algorithm.
1
Assignment 5 Due: 2024.11.12 23:59:00
2024 Fall CS300 TA: Jisung Hwang (4011hjs@kaist.ac.kr)
(a) (5 points) Complete the frequency table of the characters (including the space character).
Char. ‘’ A C D E F H I K L M N O P R S T W Y Z
Freq. 12 11 3 4 7 2 2 9 1 1 9 8 11 4 9 6 10 1 1 1
.
| \
. .
| \ | \
. . . .
|\ |\ | \ | \
A O . s . . . .
|\ |\ |\ |\ |\
. S E . . N I M R T
|\ |\ |\
. C . D . P
|\ |\ |\
W Y Z F H .
|\
K L
Char. ‘’ A C D E F H I K L
Code. 011 000 01001 10011 1000 100101 101000 1100 1010010 1010011
Char. M N O P R S T W Y Z
Code. 1101 1011 001 10101 1110 0101 1111 010000 010001 100100
(d) (10 points) Calculate the compression ratio ( # original# bits - # encoded bits
original bits ) compared to the
original sentence. (Note that each character takes 8 bits.)
896−445
Bits for characters: 8 × 112 = 896, bits for encoded one: 445. → 896 = 50.33%.
Grading Criteria:
(Full credit) Correct answer. The answer may be different from the solution here in (b) and (c).
(+5 points) Correct answer based from the previous results.
2
Assignment 5 Due: 2024.11.12 23:59:00
2024 Fall CS300 TA: Jisung Hwang (4011hjs@kaist.ac.kr)
Solution. A binary tree with N nodes and height that H is uniquely determined by the
left and right subtrees of the root node, and the larger height of the left and right subtrees
is H − 1. However, it’s not guaranteed that both subtrees have height H − 1, meaning the
subproblems differ from the original problem definition. To apply dynamic programming, we
can slightly modify the problem to instead find the number of binary trees with N nodes and
a height at most H. (Criteria 1) Then, the left and right subtrees has at most H − 1.
Since the total nodes of the left and right subtrees are N − 1, it suffices to sum all possible
cases making the sum of nodes be N − 1.
Grading Criteria:
(Full credit) The algorithm correctly yields the values.
(+10 points) Identifying the num[n, h] formula.
(+10 points) Identifying the Criteria 1.
(−10 points) Incorrect time complexity.
3
Assignment 5 Due: 2024.11.12 23:59:00
2024 Fall CS300 TA: Jisung Hwang (4011hjs@kaist.ac.kr)
(b) (25 points) Given M (∈ N), you are tasked with calculating the number
P of non-increasing
sequences p1 ≥ p2 ≥ p3 ≥ . . . of integers such that i pi = M and pi = j 1{pj ≥ i} where
P
i is a positive integer, and 1 is an indicator function.
Design an algorithm that computes the number of sequences with a time complexity of
O(M 3/2 ).
Since pk+1 < k +1, we know pj ≤ pk+1 ≤ k for j > k. Thus the above one can be summarized
as follows:
1{pj ≥ m} = k2 + 1{pj ≥ m} = k2 +
X X X X X X
pm = k 2 + pj .
1≤m≤k j>k 1≤m≤k 1≤m≤k j>k j>k
P
Then we can utilize i pi = M as follows:
X X X X X
M= pi = pi + pi = k 2 + 2 pj ⇒ 2 pj = M − k 2 .
i 1≤i≤k i>k i>k i>k
So, it P
suffices to find the number of possible non-increasing pi ’s (i > k) such that pk+1 <= k
and 2 i>k pj = M − k 2 .
Simply speaking, we need to know P the number of non-increasing non-negative integer se-
quence qi such that q1 ≤ K and i qi = N for specific √ K ≥ 1 and N ≥ 0. (Criteria 1)
Since k 2 ≤ M , the problem suffices to be solved for K ≤ M .
P given N and K, if q1 is set to be K, then the next values have to satisfy q2 ≤ K and
For
i≥2 qi = N − K, and this is identical to the problem with N − K and K. Else if q1 is
smaller than K, then it becomes the problem with N and K − 1. (Criteria 2)
4
Assignment 5 Due: 2024.11.12 23:59:00
2024 Fall CS300 TA: Jisung Hwang (4011hjs@kaist.ac.kr)
Grading Criteria:
(Full credit) The algorithm correctly yields the values.
(+10 points) Identifying the Criteria 2.
(+10 points) Identifying the Criteria 1.
(−10 points) Incorrect time complexity.