2022 Spring CS300 Final
2022 Spring CS300 Final
• All problems are worth 20 points. Unless stated otherwise, subproblems are weighted equally.
Problem 1.
1. Dijkstra does not work with negative edges. Make a counterexample graph that Dijkstra
fails to find a shortest path. Pseudocode of the algorithm is given in the next page.
(a) Draw a counterexample here. Graph must have exactly 5 vertices. Graph must have
negative-weighted edge but no negative cycles. Start vertex is 1. (7 pts)
(b) Write the output distances of Dijkstra algorithm using your counterexample. Also, write
the deleted order when you get the output. (deleted order by the priority queue) (3 pts)
vertex 1 2 3 4 5
output distance 0
deleted order : 1 → → → →
(c) Use Bellman-Ford algorithm to find correct shortest path distance of your counterex-
ample. (3 pts)
vertex 1 2 3 4 5
output distance 0
(b) Will the Floyd-Warshall algorithm work at the graph with negative edges but no nega-
tive cycles? True or False. You don’t need to prove. (3 pts)
1
2022 Spring CS300 Final Name : , Student ID :
Algorithm 1 Dijkstra
1: for all v ∈ V do
2: dist(u) ← ∞
3: end for
4: dist(s) ← 0
5: H ← makequeue (V ) ▷ using dist-values as keys
6: while H is not empty do
7: u ←deletemin(H)
8: for all edges (u, v) ∈ E do
9: if dist(v) > dist(u) + l(u, v) then
10: dist(v) ← dist(u) + l(u, v)
11: decreasekey(H, v)
12: end if
13: end for
14: end while
Algorithm 2 Bellman-Ford
1: for all v ∈ V do
2: dist(v) ← ∞
3: end for
4: dist(s) ← 0
5: for i ∈ {1, . . . , |V | − 1} do
6: for all edges (u, v) ∈ E do
7: dist(v) ← min{dist(v), dist(u) + l(u, v)}
8: end for
9: end for
2
2022 Spring CS300 Final Name : , Student ID :
Problem 2.
Consider this Frequency table.
Symbol Frequency
d 3.38
e 11.16
h 3.00
l 5.49
o 7.16
r 7.58
w 1.29
1. Given Symbol and Frequency, Find optimal Huffman coding tree. What is the cost of your
coding tree? (10 pts)
2. Fill the codebook. (5 pts)
Symbol codeword
d
e
h
l
o
r
w
3
2022 Spring CS300 Final Name : , Student ID :
Problem 3.
Given the <Graph> whose edges have different weights,
<Graph>
1. (5 points) Draw the minimum spanning tree of <Graph>, and calculate the sum of weights.
4
2022 Spring CS300 Final Name : , Student ID :
2. (10 points) Draw the second minimum spanning tree of <Graph>, and calculate the
sum of weights. The second MST means that it has the smallest sum of weights except MST.
3. (5 points) Draw the third minimum spanning tree of <Graph>, and calculate the sum
of weights. The third MST means that it has the smallest sum of weights except MST,
second MST.
5
2022 Spring CS300 Final Name : , Student ID :
Problem 4.
Every night, you receive a list of n ≥ 0 jobs, where the i-th job has start time si , end time ei , and
its profit pi . Each can be freely accepted or rejected. You wish to maximize the total profit at
the end of the next day by scheduling what jobs to do and what to reject. For simplicity, assume
that all start and end times are unique. For example, if a job has start time 10:00AM end time
11:00AM, no other job’s end time nor start time can be 10:00AM or 11:00AM. Everyone needs a
short break! For each problem, if the function given in (a) does not use dynamic programming as
its main method, you cannot get points in (b) even if the algorithm itself is correct.
6
2022 Spring CS300 Final Name : , Student ID :
Problem 5.
Write a pseudocode of a function Count which gets a positive integer n as an input and returns
the number of finite sequence of positive integers which satisfies the following conditions.
7
2022 Spring CS300 Final Name : , Student ID :
Problem 6.
Give True or False for each of the following statements. Justify your answers. Incorrect justification
will earn 0 pts even with correct answer.
8
2022 Spring CS300 Final Name : , Student ID :
Problem 7.
You are a president of a club with N members in KAIST. Each member has a relationship with
each of the other (N − 1) members as either friendly, stranger, or hate, where all friendly, stranger,
and hate are bidirectional. You, as a president, are fully aware of all the relationships between the
members and concerned about the stranger and hate relationships. So you decided to plan for a
summer club MT in which all the N members must participate. There are a few things you need
to decide, and you want to show the hardness of those problems. These are the following problems.
1. Given that p, and each member’s hate members as input. You want to know whether you
can choose at least p members to go to the mart and buy stuffs needed for MT. To buy
without any trouble, chosen members should not hate each other. Show that this problem is
NP-complete using that 3-SAT is NP-hard and reduction.
2. Given that q, and all members’ friendly members as input. You want to know whether you
can choose at most q MCs. To make sure every member enjoy MT, all the students must be
friendly with at least one of the MCs. Show that this problem is NP-complete by making
a reduction from Problem 7-1.
9
2022 Spring CS300 Final Name : , Student ID :
Problem 8.
You are given n items with size 0 < ai ≤ 1. You need to pack these items with the minimal number
of size-one bins. Formally, minimize the number of bins k, which satisfy following two conditions.
• We use set Sj to denote j-th bin. Sj ’s elements are items in that bin.
• Every item is contained in a bin.
k
[
Sj = {1, 2, . . . , n}
j=1
• Each bin allows size at most one for the sum of sizes of the items.
X
ai ≤ 1 j = 1, . . . , k
i∈Sj
1: function FirstFit(a1 , . . . , an )
2: L ← [{}] ▷ L is a list of set.
3: for 1 ≤ i ≤ n do
4: for 1 ≤ j ≤ len(L) do
5: S ← L[j]
P ▷ Get j-th set in L.
6: if ai + k∈S ak ≤ 1 then ▷ If new item fits in this bin
7: S ←S∪i ▷ Store it.
8: Break ▷ End the inner loop.
9: end if
10: end for
11: if i was not inserted to any bin then
12: L = L + {i} ▷ If it fits nowhere, create new bin.
13: end if
14: end for
15: Return L
16: end function
1. Using the FirstFit algorithm on the following values of ai , show the result of L. (3pts)
a1 = 0.4 a2 = 0.8 a3 = 0.5 a4 = 0.1 a5 = 0.7
a6 = 0.6 a7 = 0.1 a8 = 0.4 a9 = 0.2 a10 = 0.4
2. Solve the worst time complexity T (n) of FirstFit with justification. (5pts)
3. Prove that the FirstFit’s approximation ratio satisfy ρ(n) ≤ 2. (6pts)
4. Prove that the FirstFit’s approximation ratio ρ(n) < 3/2 is impossible if P ̸= N P . (Hint
: Use an reduction
P from Pthe set partition problem, which finds S ⊂ [N ] such that for given
weights wi , w
i∈S i = / wi . You can use the fact that the set partition problem is
i∈S
NP-Hard.) (6pts)
10
2022 Spring CS300 Final Name : , Student ID :
11