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

2022 Spring CS300 Final

Uploaded by

yunajessi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

2022 Spring CS300 Final

Uploaded by

yunajessi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

2022 Spring CS300 Final Name : , Student ID :

• 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

2. How about Floyd-Warshall? In Floyd-Warshall algorithm, we used dist(i, j, k) = the length


of the shortest path from i to j in which only nodes {1, 2, . . . , k} can be used as intermediate
nodes.

(a) Fill the DP equations in the blanks. (4 pts)

l(u, v) = edge (u, v)’s weight if exists


dist(i, j, 0) = l(u, v) for (i, j) ∈ E
dist(i, j, 0) = for (i, j) ∈
/E
dist(i, j, k) = min( , + ) for 1 ≤ k ≤ n

(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. Encode “helloworld” with your coding tree. (5 pts)

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.

1. Say that you can only do at most one job at a time.


(a) Define a function using dynamic programming that will find the maximum profit you
earn the next day. You may define additional helper functions and manipulate the input
jobs if needed (Hint: manipulate the inputs so that the jobs are “sorted” in some way).
(b) Explain why your function is correct.
2. Now, say that you can do two jobs at once.
(a) Define a new function using dynamic programming that will find the maximum profit
you earn the next day.
(b) Explain why your function 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.

• The sum of all elements of the sequence is n.


• For the length l of sequence a, ai = al−i+1 for all integers 1 ≤ i ≤ l. For example, {1, 123, 1}
and {1, 2, 2, 1} satisfies the condition, but {34, 43} does not.
• There exists some integer 1 ≤ k ≤ l that ai ≤ ai+1 for i < k and ai ≥ ai+1 for k ≤ i < l. For
example, {1, 2, 3, 2, 1} and {1, 2, 3, 1} satisfies the condition, but {1, 3, 2, 4} or {3, 2, 1, 2, 3}
does not.
For example, Count(6) = 7, as there are 7 cases that satisfy the condition: {6}, {3, 3}, {1, 4, 1},
{2, 2, 2}, {1, 2, 2, 1}, {1, 1, 2, 1, 1} and {1, 1, 1, 1, 1, 1}.
You will get credit only if you describe the algorithm which solves the problem.
You will get full credit only if your algorithm has time complexity not greater than
O(n2 ).

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.

1. (3 pts) Hamiltonian path problem is NP.


2. (3 pts) Independent set problem and Vertex cover problem are equally hard.

3. (3 pts) If a problem A is polynomial time reducible to a problem B and A is solved in


polynomial time, then B has a polynomial time algorithm.
4. (3 pts) We can decide any problem in NP in exponential time.
5. (4 pts) P ⊆ co-NP is an open question.

6. (4 pts) If NP ̸= co-NP, then P ̸= NP.

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

Recall the approximation ratio defined in the lecture.


Definition 1 (Approximation Ratio). An algorithm has an approximation ratio ρ(n) if for every
input of size n, the cost C of the produced solution satisfies
C C∗
 
max , ≤ ρ(n)
C∗ C
where C ∗ is the cost of the optimal solution.
To solve this problem, we will use simple approximation algorithm. The ‘cost’ C of this algorithm
is the length of output list L.

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 :

You can use this page to answer Problem 8

11

You might also like