0% found this document useful (0 votes)
5 views5 pages

24fall CS300 Assignment5 Solution

The document outlines Assignment 5 for CS300, which includes three problems related to algorithms: a greedy algorithm for minimizing differences in ordered reals, Huffman encoding for data compression, and dynamic programming for counting binary trees and non-increasing sequences. Each problem has specific grading criteria and requires the design of algorithms with defined time complexities. The assignment is due on November 12, 2024, with penalties for late submissions.

Uploaded by

장여경
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)
5 views5 pages

24fall CS300 Assignment5 Solution

The document outlines Assignment 5 for CS300, which includes three problems related to algorithms: a greedy algorithm for minimizing differences in ordered reals, Huffman encoding for data compression, and dynamic programming for counting binary trees and non-increasing sequences. Each problem has specific grading criteria and requires the design of algorithms with defined time complexities. The assignment is due on November 12, 2024, with penalties for late submissions.

Uploaded by

장여경
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/ 5

Assignment 5 Due: 2024.11.

12 23:59:00
2024 Fall CS300 TA: Jisung Hwang (4011hjs@kaist.ac.kr)

• Please submit before the submission deadline.


• Assignment submitted one (1) day after the assignment deadline will be accepted with 20%
deduction on corresponding assignment grade.
• Assignment submitted more than one (1) day late will not be accepted.

Problem 1: Greedy algorithm (25 points)


Let N be a positive integer.PGiven 2N ordered reals p1 < p2 < · · · < p2N and M (≤ N ), you are
tasked with getting min{ij } 1≤j≤M (pij +1 − pij ), where 1 ≤ ij < 2N and |ij − ik | ≥ 2 for every
j, k ∈ {1, . . . , M }, j ̸= k.
Design an algorithm that calculates the minimum value with a time complexity of O(N log N ).

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.

Algorithm 1 Solution for Problem 1


Input: N, M, p1 , .P
. . , p2N
Output: min{ij } 1≤j≤M (pij +1 − pij )

left[i] ← i − 1 for all i ∈ {1, . . . , 2N } ▷ O(N )


right[i] ← i + 1 for all i ∈ {1, . . . , 2N } ▷ O(N )
diff[i] ← pi+1 − pi for all i ∈ {1, . . . , 2N − 1} ▷ O(N )
m←0 ▷ Counts how many pairs are added
ret ← 0 ▷ Initialize the return value as 0
H ← makequeue(key : pi+1 − pi , value : (pi+1 − pi , i, i + 1) for all i ∈ {1, . . . , 2N − 1})
▷ O(N log N )
while m < M do ▷ Iterate O(N ) times
(v, l, r) ← deletemin(H) ▷ O(log N )
if l = right[l] and r = left[r] then ▷ If reflects the latest status
ret ← ret + v
m←m+1
l′ ← left[l]
r′ ← right[r]
diff[l′ ] ← diff[l′ ] + diff[r] − v
left[r′ ] ← l′
right[l′ ] ← r′
if 1 ≤ l′ and r′ ≤ 2N then
add(H, key : diff[l′ ], value : (diff[l′ ], l′ , r′ )) ▷ O(log N )
return ret

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)

Problem 2: Huffman Encoding (25 points)


Apply Huffman encoding to the sentence provided below.

HARDWARE LIMITATIONS AND MEMORY CONSTRAINTS MAKE DATA


COMPRESSION METHODS IMPORTANT FOR PERFORMANCE OPTIMIZATION

(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

(b) (5 points) Construct a Huffman tree.

.
| \
. .
| \ | \
. . . .
|\ |\ | \ | \
A O . s . . . .
|\ |\ |\ |\ |\
. S E . . N I M R T
|\ |\ |\
. C . D . P
|\ |\ |\
W Y Z F H .
|\
K L

(s denotes the space character.)


(c) (5 points) Generate the binary codes for each character.

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)

Problem 3: Dynamic Programming (50 points)


(a) (25 points) You are tasked with calculating the number of binary trees with N (≥ 1) nodes
and a height H(≥ 1).
A binary tree consisting of only the root node is considered to have a height of 1. Also, the
left and right subtrees of each node are ordered and distinct. For instance, with N = 2 and
H = 2, the number of binary trees is 2, not 1.
Design an algorithm with a memory usage of O(N H) and a time complexity of O(N 2 H).

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.

Algorithm 2 Solution for Problem 3-a


Input: N, H
Output: # binary trees with N nodes and a height H
num[n, h] ← 0 for all n ≤ N, h ≤ H ▷ O(N H) memory
for all h = 1, . . . , H do ▷ O(H) time
for all n = 0, . . . , min(N, 2h − 1) do ▷ O(N ) time
if n = 0 or n = 2h − 1 then
num[n, h] = 1
else
for all i = 0, . . . , n − 1 do
num[n, h] = num[n, h] + num[i, h − 1] × num[n − 1 − i, h − 1] ▷ O(N ) time
return num[N, H] − num[N, H − 1]

The time complexity of this algorithm is O(N 2 H).

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

Solution. Let k be the maximal i such that pi ≥ i. (M ≥ 1 ⇒ k ≥ 1) Then, for m ≤ k,

1{pj ≥ m} = 1{pj ≥ m} + 1{pj ≥ m}.


X X X
pm =
j j≤k j>k

If j ≥ k, then pj ≥ pk ≥ k ≥ m. The above one becomes pm = k + j>k 1{pj ≥ m}. Which


P
means, pm (m ≥ k) is uniquely determined by pj (j > k).
P P
In addition, to utilize the fact i pi = M , let us calculate 1≤m≤k pm .

1{pj ≥ m}) = k2 + 1{pj ≥ m}.


X X X X X
pm = (k +
1≤m≤k 1≤i≤k j>k 1≤m≤k j>k

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)

Algorithm 3 Solution for Problem 3-b


Input: M
Output: # {pi } √
num[n, k] ← 0 for all n ≤ M, k ≤ ⌊ M ⌋ ▷ O(M 3/2 ) memory
for all n = 0, . . . , M do√ ▷ O(M ) time
for all k = 1, . . . , ⌊ M ⌋ do ▷ O(M 1/2 ) time
if n ≤ 1 or k = 1 then
num[n, k] = 1
else if k ≤ n then
num[n, k] = num[n, k − 1] + num[n − k, k]
else
num[n, k] = num[n, k − 1]
ret ← 0 √ ▷ Initialize the return value as 0
for all i = 1, . . . , ⌊ M ⌋ do ▷ O(M 1/2 ) time
if M − i2 ≡ 0 mod 2 then
2
ret ← ret + num[ M −i 2 , i]
return ret

The time complexity of this algorithm is O(M 3/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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy