National University of Singapore
School of Computing
CS3230 - Design and Analysis of Algorithms
Final Assessment
(Semester 2 AY2023/24)
Total Marks: 80 Time Allowed: 120 minutes
INSTRUCTIONS TO CANDIDATES:
1. Do NOT open this assessment paper until you are told to do so.
2. This assessment paper contains TWO (2) sections. It comprises THIRTEEN (13) printed pages.
3. This is an Open Book Assessment.
4. For Section A, use the bubbles on page 2 (use 2B pencil).
5. For Section B, answer ALL questions within the boxed space.
If you leave the box blank, you will get 0.5 mark (ONLY for essay questions worth ≥ 2).
However, if you write at least a single character and it is totally wrong, you will get a 0 mark.
You can use either a pen or a pencil. Just make sure that you write legibly!
6. Important tips: Pace yourself! Do not spend too much time on one (hard) question.
Read all the questions first! Some questions might be easier than they appear.
7. You can assume that all logarithms are in base 2.
Write your Student Number in the box below using (2B) pencil:
1
CS3230
A Multiple Choice Questions (16 × 2.5 = 40 marks)
Select the best unique answer for each question. Each correct answer is worth 2.5 marks.
Write your MCQ answers in the special MCQ answer box below for automatic grading.
We do not manually check your answer.
Write your MCQ answers in the answer box below using (2B) pencil:
No 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
A
B
C
D
E
2
CS3230
1. Which of the following statements is TRUE?
a). 100log log 𝑛 = Ω(𝑛)
b). 100log 𝑛 = 𝑂(𝑛)
2
c). 100log 𝑛 = 𝑂(𝑛2 )
d). 100log log 𝑛 = Θ(log 𝑛)
e). 100𝑛 = 𝜔(2𝑛 ) Answer.
2. 𝑛! is in
a). 𝑂(𝑛5 )
b). Θ(𝑛5 )
c). 𝜔(𝑛5 ) Answer.
d). 𝑜(𝑛5 )
e). None of the above
3. Which of the following statements is TRUE?
a). For any algorithm, let 𝐴(𝑛) and 𝑇 (𝑛) denote the average-case and worst-case time respec-
tively. Then it is always the case that 𝐴(𝑛) < 𝑇 (𝑛)
b). Any comparison-based sorting to sort 𝑛/ log 𝑛 integers must take Ω(𝑛 log 𝑛) time
c). There is no sorting algorithm that is both in-place and stable
d). All comparison-based sorting algorithms are stable
e). None of the above Answer.
4. Suppose 𝑓 (𝑛) ∈ Θ(𝑛3 log 𝑛) and 𝑔(𝑛) ∈ Θ(𝑛(log 𝑛)3 ). Then 𝑓 (𝑛) + 𝑔(𝑛) is in
a) Θ(𝑛3 (log 𝑛)3 )
b) Θ(𝑛3 (log 𝑛)) Answer.
c) Θ(𝑛(log 𝑛)3 )
d) Θ(𝑛(log 𝑛))
e) None of the above
3
CS3230
5. Consider the alphabet set {𝑎, 𝑏, 𝑐, 𝑑, 𝑒}. The frequencies of 𝑎, 𝑏, 𝑐, 𝑑, 𝑒 are 0.16, 0.08, 0.35, 0.07, 0.34
respectively. What is the ABL (Average Bit Length) value of an optimal prefix-free code? (Hint:
Try executing Huffman coding algorithm.)
a) 2.11 Answer. Consider the following encoding: 𝑎 : 110, 𝑏 : 1110, 𝑐 : 0, 𝑑 : 1111, 𝑒 : 10
b) 2.49
c) 3.01
d) 2.31
e) 1.93
6. Suppose we have the recurrence relation: 𝑇 (𝑛) = 𝑇 (𝑛/2) + 𝑇 (𝑛/3) + 𝑛. Then,
a) 𝑇 (𝑛) = Θ(𝑛) Answer.
b) 𝑇 (𝑛) = Θ(𝑛2 )
c) 𝑇 (𝑛) = Ω(𝑛 log 𝑛)
d) 𝑇 (𝑛) = 𝜔(𝑛)
e) None of the above
7. Which one of the following problems is not known to be NP-hard?
a). Independent set for trees Answer.
b). 3-dimensional matching
c). Vertex cover
d). Subset-sum
e). Hitting Set
8. (𝑛 + 2)! is in
a) Θ((2𝑛)!)
b) 𝜔(𝑛!) Answer.
c) Θ(𝑛!)
d) 𝑜(𝑛!)
e) Θ((𝑛 + 1)!)
9. You have three (decision) problems 𝐴, 𝐵 and 𝐶. You know that there is a polynomial time algo-
rithm for the problem 𝐴, problem 𝐵 is NP-hard, and problem 𝐶 cannot be solved in polynomial
time. Which of the following statements is TRUE?
a). There can be a polynomial time reduction from 𝐵 to 𝐴,
and another polynomial time reduction from 𝐶 to 𝐵
b). If 𝐵 is polynomial time reducible to 𝐴 and 𝐴 is polynomial time reducible to 𝐶, then P ̸= NP
c). 𝐴 is polynomial time reducible to 𝐶 Answer.
d). 𝐶 must be polynomial time reducible to 𝐵
e). None of the above
4
CS3230
10. While computing the time complexity of a recursive algorithm, you get the following recurrence
relation: 𝑇 (𝑛) = 125𝑇 (𝑛/5) + 𝑛3 / log3 𝑛. Then which one of the following is TRUE?
a). 𝑇 (𝑛) = Θ(𝑛3 ) Answer. By telescoping method, 𝑇 (𝑛) = Θ(𝑛3 ). Or by using the extension of
Master Theorem case 2c that we discussed in PA1-A, we have 𝑎 = 125, 𝑏 = 5, 𝑙𝑜𝑔5 125 = 3,
so case 2, but 𝑘 = −3, so we drop the log factor and just output 𝑇 (𝑛) = Θ(𝑛3 ).
b). 𝑇 (𝑛) = Θ(𝑛3 log 𝑛)
c). 𝑇 (𝑛) = Θ(𝑛3 / log2 𝑛)
d). 𝑇 (𝑛) = Θ(𝑛3 log2 𝑛)
e). 𝑇 (𝑛) = Θ(𝑛3 log log 𝑛/ log3 𝑛)
11. Which of the following statements is TRUE?
a). If an operation takes 𝑂(𝑓 (𝑛)) amortized time, then it also takes 𝑂(𝑓 (𝑛)) worst-case time
(per operation)
b). An adversary can construct an input of length 𝑛 to force the Randomized-Quicksort to run
in Ω(𝑛2 ) expected time
c). For any two positive-valued functions 𝑓, 𝑔, if 𝑓 ̸∈ 𝑂(𝑔), then 𝑓 ∈ Ω(𝑔)
d). Consider three positive-valued functions 𝑓, 𝑔, ℎ where ℎ = 𝑓 · 𝑔 (ℎ is equal to 𝑓 multiplied
with 𝑔), if 𝑓 ∈ Θ(𝑛) and 𝑔 ∈ 𝑜(1), then it may not hold that ℎ ∈ Ω(1) Answer.
e). If the average-case time complexity of an arbitrary sorting algorithm is 𝑂(𝑛 log 𝑛) (for 𝑛
numbers), then in the worst case it must take at most 𝑂(𝑛2 ) time
𝑛
12. In a comparison-based model, any deterministic algorithm for finding the 4 -th largest number
in an array of 𝑛 unsorted distinct integers must use
a). Ω(𝑛 log 𝑛) comparisons
b). 𝜔(𝑛 log 𝑛) comparisons
c). Θ(𝑛2 ) comparisons
d). Ω(𝑛) comparisons Answer.
e). None of the above
13. Suppose a Dynamic Programming algorithm for solving a problem uses a two-dimensional (2D)
array of size 𝑛 × 𝑛 to store the values computed during the algorithm’s execution. Then,
a). irrespective of which problem it is solving, the 2D array will be filled up column by column
b). irrespective of which problem it is solving, the 2D array will be filled up row by row
c). irrespective of which problem it is solving, the 2D array will be filled either row by row or
column by column
d). irrespective of which problem it is solving, and whether it is a top-down or bottom-up
method, the algorithm will take at most 𝑂(𝑛2 ) time
5
CS3230
e). we cannot claim any of the above to be true for every Dynamic Programming algorithm
Answer
14. Suppose 𝑇 (0) = 1, 𝑇 (1) = 1, 𝑇 (2) = 1, and for 𝑛 ≥ 3, 𝑇 (𝑛) = 𝑇 (𝑛 − 3) + 𝑛. Then, 𝑇 (𝑛) is in
a). Ω(𝑛3 )
b). Θ(𝑛2 ) Answer
c). 𝑂(𝑛)
d). 𝑂(𝑛 log 𝑛)
e). None of the above
15. Recall the definitions of the order of growth as sets of functions.
Which of the following statements is TRUE?
a). 𝑂(𝑛2 ) ⊆ Ω(𝑛2 )
b). Ω(𝑛2 ) ⊆ 𝑂(𝑛2 )
c). 𝑂(𝑛2 ) ∩ Ω(𝑛2 ) = ∅
d). 𝑂(𝑛2 ) ∩ 𝜔(𝑛2 ) = ∅ Answer
e). 𝑂(𝑛2 ) ∩ 𝑜(𝑛2 ) = ∅
16. Suppose we are given a problem 𝑃 from {0, 1}* to {yes,no} (i.e., 𝑃 is a decision problem).
Suppose for 𝑃 , you are given a randomized algorithm 𝐴, which runs in time 𝑂(𝑛2 ) and gives
the correct answer with a probability of at least 0.6. Suppose we have also proven that any
deterministic algorithm for 𝑃 must take time at least Ω(𝑛3 ). Then,
a). we can design another (randomized) algorithm for 𝑃 , which runs in time 𝑂(𝑛2 ), and gives
the correct answer with probability at least 3/4 Answer
b). we can prove that no (possibly randomized) algorithm that runs in time 𝑂(𝑛2 ) can solve
the problem 𝑃 with a probability of giving the correct answer being at least 3/4
c). we can give no guarantees about whether there exists a deterministic algorithm which solves
the problem 𝑃 in time 𝑂(𝑛2 )
d). there exists a randomized algorithm for the problem 𝑃 , which runs in time 𝑂(𝑛2 ), and gives
the correct answer with probability 1
e). None of the above
6
CS3230
B Essay Questions (40 marks)
B.1 Average-case Comparisons (2 + 3 + 5 = 10 marks)
Consider the following algorithm to find the two largest numbers from an 𝑛-length array 𝐴[1..𝑛] of 𝑛
distinct numbers.
Input: Given 𝑛 distinct numbers, 𝐴[1..𝑛] contains a random permutation of them.
Begin
1. If 𝐴[1] > 𝐴[2],
Then let 𝑎 = 𝐴[1] and 𝑏 = 𝐴[2],
Else let 𝑎 = 𝐴[2] and 𝑏 = 𝐴[1]
Endif
2. For 𝑖 = 3 to 𝑛 do:
2.1. If 𝑏 < 𝐴[𝑖] Then
2.2. If 𝑎 < 𝐴[𝑖],
Then let 𝑏 = 𝑎 and 𝑎 = 𝐴[𝑖]
Else let 𝑏 = 𝐴[𝑖]
Endif
Endif
//Comment: 𝑎 and 𝑏 store the two largest elements of the subarray 𝐴[1..𝑖].
End For
3. Output 𝑎, 𝑏.
End
a). (2 marks) For a fixed 𝑖, in the For loop iteration with index 𝑖, at step 2.1: what is the probability
that 𝐴[𝑖] > 𝑏?
(Your answer can be in terms of 𝑖. Hint: Observe the comment in the pseudocode carefully.).
2/𝑖
b). (3 marks) What is the expected number of comparisons made by the above algorithm? (Here,
we count only the comparisons made between 𝑎, 𝑏 and the array elements in steps numbered 1,
2.1, and 2.2 and not any comparisons done in the control statement of the For statement).
Give the bound in the form 𝑛 + 𝑂(𝑓 (𝑛)), where 𝑓 (𝑛) is as tight as possible.
𝑛 + 𝑂(log 𝑛)
c). (5 marks) Provide justification for your answers to parts (a) and (b).
(a) The probability that 𝐴[𝑖] > 𝑏 is the same as the probability that it is among the two largest
elements in 𝐴[1 : 𝑖]. This probability is 2/𝑖.
(b) The algorithm makes one comparison in step 1, and one comparison between 𝐴[𝑖] and 𝑏 in
the If statement of step 2.1 of each iteration of the for loop. This gives 𝑛 − 1 comparisons.
7
CS3230
In each iteration of the For loop, the 2nd If statement (step 2.2) makes one comparison between
𝑎 and 𝐴[𝑖] only if 𝑏 < 𝐴[𝑖] is true in the first If statement. This happens with probability 2/𝑖
in the iteration of the For loop with index 𝑖. Thus, the expected number of comparisons in step
2.2 is:
∑︀𝑛
𝑖=3 2/𝑖 = 2𝐻𝑛 − 3 = Θ(log 𝑛).
Hence, the total number of comparisons is 𝑛 + Θ(log 𝑛).
B.2 Maximizing Profit (1 + 3 + 4 + 2 = 10 marks)
You are a fisherman. Very early this morning, you caught 𝑛 fishes. These 𝑛 fishes weigh 𝑤1 , 𝑤2 , ..., 𝑤𝑛
kilograms respectively (you can assume that all 𝑛 fish weights are Integers between 1 to 1024 kilograms,
not necessarily distinct).
In the fish market, there are 𝑚 fish sellers described as a sequence of 𝑚 pairs (𝑥1 , 𝑝1 ), (𝑥2 , 𝑝2 ), ..., (𝑥𝑚 , 𝑝𝑚 ).
A fish seller 𝑗 with (𝑥𝑗 , 𝑝𝑗 ) pair means that this fish seller 𝑗 is willing to buy 𝑥𝑗 number of fish (𝑥𝑗 is
also an Integer, 1 ≤ 𝑥𝑗 ≤ 𝑛 (notice 𝑥𝑗 could be as large as 𝑛), and not necessarily distinct) at 𝑝𝑗 SGD
per kilogram (𝑝𝑗 is also an Integer, 1 ≤ 𝑝𝑗 ≤ 1024, and not necessarily distinct).
Design a greedy algorithm to compute the maximum profit (in SGD) that you can get by selling
(some, if not all) your 𝑛 fishes to (some of) these 𝑚 fish sellers optimally. Note that there are partial
marks if your greedy algorithm is correct only when 𝑥𝑗 = 1 for all 𝑚 fish sellers (e.g., see part a).1)).
For example, if you caught 𝑛 = 3 fishes with weights 4, 7, 5 and there are 𝑚 = 2 fish sellers
(1, 10), (10, 9), then the optimal strategy is to sell your second fish with weight 7 kilogram to the first
fish seller who only wants to buy 1 fish that day at price 10 SGD per kilogram (you get 7 × 10 = 70
SGD) and then sell your two other fishes to the second fish seller who can buy up to 10 fishes but you
only have two fishes left (you get 4 × 9 + 5 × 9 = 81 SGD). So your total profit is 70 + 81 = 151 SGD.
a). (2 × 0.5 = 1 mark) Judge your understanding:
Just write two output Integers, one for each test case below.
1). 𝑛 = 3, weights 9, 4, 5, 𝑚 = 4, fish sellers (1, 2), (1, 1), (1, 6), (1, 3)
2). 𝑛 = 10, weights 10, 8, 4, 28, 19, 2, 7, 5, 9, 1, 𝑚 = 3, fish sellers (2, 4), (1, 5), (4, 3)
a). 9 × 6 + 5 × 3 + 4 × 2 = 54 + 15 + 8 = 77 SGD.
b). 28 × 5 + (19 + 10) × 4 + (9 + 8 + 7 + 5) × 3 = 140 + 116 + 87 = 343 SGD.
b). (3 marks) Describe the optimal sub-structure of this problem and prove its correctness.
Suppose we sell 1 of our fish (let’s say fish 𝑖) to fish seller 𝑗. The sub-problem is how to
optimally sell our 𝑛 − 1 other fish (fish 𝑖 is no longer considered), and the fish seller 𝑗 state
becomes (𝑥𝑗 − 1, 𝑝𝑗 ) (if 𝑥𝑗 drops to 0, we remove fisherman 𝑗 from consideration).
We can use a cut-and-paste argument. Consider any optimal solution. If fish 𝑖 with weight 𝑤𝑖 is
sold to fish seller 𝑗 with state (𝑥𝑗 , 𝑝𝑗 ), then we obtain 𝑤𝑖 × 𝑝𝑗 SGD, fish 𝑖 is no longer considered,
and fish seller 𝑗 state becomes (𝑥𝑗 − 1, 𝑝𝑗 ) — and if 𝑥𝑗 − 1 = 0, we also remove fisherman 𝑗 from
consideration. Now, we claim that the remaining sub-problem must be optimal, too. Because if
it does not and it gives a higher profit, then we can add 𝑤𝑖 × 𝑝𝑗 to that profit (as we can sell
8
CS3230
fish 𝑖 to fish seller 𝑗 who is willing to buy 1 more fish at a price 𝑝𝑗 ) to get an even higher profit.
Contradiction! So, this establishes the optimal sub-structure of this problem.
c). (4 marks) Describe the greedy choice that works for this problem and prove its correctness.
PS: If your greedy choice is correct but your proof is not, you will still get partial marks.
We sort the 𝑛 fish in non-increasing weight. We sort the 𝑚 fish seller in non-increasing price per
kilogram. We greedily sell our next heaviest fish to the still available fish seller (that fish seller
still wants to buy 1 or more fish) that is willing to pay the highest price per kilogram (the most
generous).
Let fish 𝑖 be such that it’s weight 𝑤(𝑖) is the maximum and fish seller 𝑗 be such that 𝑝(𝑗) is the
maximum. We claim that there exists an optimal solution that sells fish 𝑖 to fish seller 𝑗.
There are a few cases to consider. Currently, the marking scheme is to give marks only to cases
1 to 4. All related to exchange arguments.
• Case 0 (likely not given any mark): If fish 𝑖 is sold to fish seller 𝑗, we are done.
• Case 1: We sell at least one fish 𝑖′ ̸= 𝑗 to fish seller 𝑗, and fish 𝑖 is sold to some other fish seller
𝑗 ′ . By assumption, 𝑤(𝑖′ ) ≤ 𝑤(𝑖) and 𝑝(𝑗 ′ ) ≤ 𝑝(𝑗). Therefore, 𝑤(𝑖′ ) × 𝑝(𝑗) + 𝑤(𝑖) × 𝑝(𝑗 ′ ) ≤
𝑤(𝑖) × 𝑝(𝑗) + 𝑤(𝑖′ ) × 𝑝(𝑗 ′ ) and we are not worse off if we sell 𝑖′ to 𝑗 ′ and 𝑖 to 𝑗, so there
exists an optimal solution which sells fish 𝑖 to seller 𝑗.
• Case 2: We do not sell any fish to fish seller 𝑗, and fish 𝑖 is sold to some other fish seller
𝑗 ′ . In this case, we can sell fish 𝑖 to fish seller 𝑗 instead of fish seller 𝑗 ′ . By assumption,
𝑝(𝑗 ′ ) ≤ 𝑝(𝑗). Therefore, 𝑤(𝑖) × 𝑝(𝑗 ′ ) ≤ 𝑤(𝑖) × 𝑝(𝑗), so there exists an optimal solution which
sells fish 𝑖 to fish seller 𝑗.
• Case 3: We sell at least one fish 𝑖′ ̸= 𝑗 to fish seller 𝑗, but fish 𝑖 sold is not sold. By
assumption, 𝑤(𝑖′ ) ≤ 𝑤(𝑖). We can instead sell fish 𝑖 to fish seller 𝑗 and choose not to sell
fish 𝑖′ to fish seller 𝑗.
• Impossible case 4: We do not sell any fish to fish seller 𝑗 (with 𝑥𝑗 ≥ 1), and fish 𝑖 is not
sold. This is not possible as by selling fish 𝑖 to fish seller 𝑗, get can easily get an extra
𝑤(𝑖) × 𝑝(𝑗) more profit.
In all cases, we make at least as much by selling fish 𝑖 to fish seller 𝑗. Establishing our greedy
choice correctness.
d). (2 marks) Combine the optimal sub-structure (in B.2.b) and the greedy choice (in B.2.c)
to design an algorithm that always outputs an optimal solution. Analyze the time complexity of
your greedy solution in terms of 𝑛 and 𝑚. Is it polynomial, pseudo-polynomial, or exponential
(choose the best option)?
The input size is 𝑛 log 1024 bits for the 𝑛 fish information, plus 𝑚 log(10247 + 1024) bits for fish
seller information.
First, sort the 𝑛 fish (by non-increasing weights) and 𝑚 fish sellers (by non-increasing price per
kilogram). These two two sorting routines cost us 𝑂(𝑛 log 𝑛 + 𝑚 log 𝑚)
9
CS3230
Then, we do one 𝑂(𝑛) greedy bipartite matching step that always matches the current heaviest
fish (we remove this fish afterward) with the most generous fish seller (we reduce his/her quota
by one, and if this drops to 0, we remove this seller). We do this only 𝑂(𝑛) time even if some fish
sellers can buy a gigantic amount of up to 10247 fish, we are upper bounded by 𝑛, the number
of fish that we have. Note that blowing up fish seller 𝑗 by 𝑥𝑗 times (alternative implementation)
may be very costly as 𝑥𝑗 is up to 10247 .
So, this algorithm is 𝑂(𝑛 log 𝑛 + 𝑚 log 𝑚) overall. This is a polynomial time algorithm, and
despite 𝑚 × MAX 𝑥𝑗 values can be very big, we will stop after processing all 𝑛 fishes.
10
CS3230
B.3 Priority Queue (10 marks)
Recall that the priority queue data structure supports the following operations:
• add(x): inserts 𝑥 into the queue; 𝑂(log 𝑛) (worst-case) time complexity
(𝑛 refers to the number of elements in the queue)
• top(): returns the largest element in the queue; 𝑂(1) (worst-case) time complexity
• pop(): removes the largest element from the queue; 𝑂(log 𝑛) (worst-case) time complexity
(𝑛 refers to the number of elements in the queue)
We now wish to support another operation with the following specifications:
• remove larger than(x): removes all items larger than 𝑥 from the queue
remove larger than(x) works by repeatedly checking whether top() is larger than 𝑥, and if so, calls
pop() until top() is ≤ 𝑥. More precisely, you may assume the following implementation:
remove_larger_than(x):
while (priority queue is not empty and top() > x):
pop()
Given an initially empty priority queue, prove that any sequence of 𝑛 (of the above four types of)
operations takes at most 𝑂(𝑛 log 𝑛) time. You can use any of the three amortized analysis techniques.
This question is similar to Tutorial 7 on multi-enqueue and multi-dequeue on a normal queue.
We can use the accounting method:
• for add(x), we charge log(𝑛) + (extra log(𝑛) + extra cost 𝑥) (save these in the bank), overall
2 log(𝑛) + 𝑥 or 𝒪(log 𝑛)
• for top(), it is 𝑂(1), free of charge (foc), nothing deducted from the bank
• for pop(), foc, deduct amount = top() from the bank
• for remove larger than(x), foc, we deduct top() from the bank 0, 1, ..., as many times until
top() is ≤ 𝑥, we should have enough in the bank, balance will not go negative
Therefore, any sequence of 𝑛 operations takes at most 𝒪(𝑛 log 𝑛).
We can also use the potential method with 𝜑 = size of queue × log(𝑛).
Aggregate work is harder to use for this question.
B.4 Respectful Coloring is NP-complete! (1 + 3 + 6 = 10 marks)
Suppose there are 𝑘 persons 𝑃1 , 𝑃2 , · · · , 𝑃𝑘 and 𝑛 balls 𝐵1 , 𝐵2 , · · · , 𝐵𝑛 . Each person provides his/her
preferred coloring of 𝑛 balls from the set of five colors {𝑟𝑒𝑑, 𝑏𝑙𝑢𝑒, 𝑔𝑟𝑒𝑒𝑛, 𝑦𝑒𝑙𝑙𝑜𝑤, 𝑝𝑖𝑛𝑘}. So essentially,
each person 𝑃𝑖 provides a sequence of colors 𝑐𝑖1 , 𝑐𝑖2 , · · · , 𝑐𝑖𝑛 , where 𝑐𝑖𝑗 ∈ {𝑟𝑒𝑑, 𝑏𝑙𝑢𝑒, 𝑔𝑟𝑒𝑒𝑛, 𝑦𝑒𝑙𝑙𝑜𝑤, 𝑝𝑖𝑛𝑘}
denotes 𝑃𝑖 ’s preferred color for the ball 𝐵𝑗 .
11
CS3230
Now, there is a painter whose job is to finally color all these 𝑛 balls. Unfortunately, in his/her
color palette, only two colors, 𝑟𝑒𝑑 and 𝑏𝑙𝑢𝑒, are available. The painter would like to color balls using
colors available in his/her palette while respecting the color preference of every person for at least one
ball. More specifically, we call a 𝑟𝑒𝑑 − 𝑏𝑙𝑢𝑒 coloring of balls a respectful coloring if for each person
𝑃𝑖 there exists a ball 𝐵𝑗 such that the coloring of 𝐵𝑗 is the same as 𝑃𝑖 ’s preferred color for 𝐵𝑗 (i.e.,
𝑐𝑖𝑗 ). If such a respectful final coloring exists, the painter would like to use that (if multiple red-blue
respectful colorings exist, choose one arbitrarily) to color the balls.
Red-Blue Respectful Coloring problem: Given color-preferences of 𝑘 persons on 𝑛 balls (as
described in the first paragraph), the problem is to decide whether there exists a 𝑟𝑒𝑑 − 𝑏𝑙𝑢𝑒 respectful
coloring or not.
For example, suppose there are 3 persons and 4 balls. The color preferences of three persons are
as follows: 𝑃1 : 𝑔𝑟𝑒𝑒𝑛, 𝑏𝑙𝑢𝑒, 𝑏𝑙𝑢𝑒, 𝑦𝑒𝑙𝑙𝑜𝑤, 𝑃2 : 𝑟𝑒𝑑, 𝑟𝑒𝑑, 𝑝𝑖𝑛𝑘, 𝑔𝑟𝑒𝑒𝑛, 𝑃3 : 𝑝𝑖𝑛𝑘, 𝑦𝑒𝑙𝑙𝑜𝑤, 𝑏𝑙𝑢𝑒, 𝑏𝑙𝑢𝑒. Then
the answer should be YES since 𝑟𝑒𝑑, 𝑏𝑙𝑢𝑒, 𝑏𝑙𝑢𝑒, 𝑏𝑙𝑢𝑒 is a valid 𝑟𝑒𝑑 − 𝑏𝑙𝑢𝑒 respectful coloring.
a). (1 mark) Judge your understanding: There are 4 persons and 3 balls. The color preferences of
four persons are as follows: 𝑃1 : 𝑟𝑒𝑑, 𝑏𝑙𝑢𝑒, 𝑦𝑒𝑙𝑙𝑜𝑤, 𝑃2 : 𝑝𝑖𝑛𝑘, 𝑟𝑒𝑑, 𝑔𝑟𝑒𝑒𝑛, 𝑃3 : 𝑦𝑒𝑙𝑙𝑜𝑤, 𝑝𝑖𝑛𝑘, 𝑏𝑙𝑢𝑒, 𝑃4 :
𝑏𝑙𝑢𝑒, 𝑝𝑖𝑛𝑘, 𝑟𝑒𝑑. Does there exist a 𝑟𝑒𝑑 − 𝑏𝑙𝑢𝑒 respectful coloring? (Tick one of the following op-
tions; if your answer is YES, then also provide a 𝑟𝑒𝑑 − 𝑏𝑙𝑢𝑒 respectful coloring as a sequence of
three colors.)
i). YES. Your 𝑟𝑒𝑑 − 𝑏𝑙𝑢𝑒 respectful coloring:
ii). NO. Answer.
b). (3 marks) Show that the Red-Blue Respectful Coloring problem is in NP.
The certificate would be an 𝑛-length sequence 𝑓1 , 𝑓2 , · · · , 𝑓𝑛 where each 𝑓𝑗 ∈ {𝑟𝑒𝑑, 𝑏𝑙𝑢𝑒}. A
certifier algorithm should verify whether the above sequence is a valid 𝑟𝑒𝑑 − 𝑏𝑙𝑢𝑒 respectful
coloring or not, which can easily be done by making a pass over color-preferences of each person.
Clearly, the certificate is polynomial in size, and the verification can be done in polynomial time.
c). (6 marks) Show that the Red-Blue Respectful Coloring problem is NP-hard.
(You may show a reduction from any of the NP-complete problems introduced in the lectures/
tutorials/ assignments/ practice set.)
Hint: Try a reduction from CNF-SAT or 3-SAT.
Consider a 3-SAT instance with 𝑛 variables 𝑥1 , · · · , 𝑥𝑛 and 𝑘 clauses 𝐶1 , · · · , 𝐶𝑘 . Now, create an
instance of the Red-Blue Respectful Coloring problem as follows: For each variable 𝑥𝑗 , consider
a ball 𝐵𝑗 , and for each clause 𝐶𝑖 , consider a person 𝑃𝑖 . The create color preference list for a
person 𝑃𝑖 as: If 𝑥𝑗 appears in 𝐶𝑖 as a positive literal, then set 𝑐𝑖𝑗 = 𝑏𝑙𝑢𝑒, else if 𝑥𝑗 appears
in 𝐶𝑖 as a negative literal then set 𝑐𝑖𝑗 = 𝑟𝑒𝑑, otherwise, set 𝑐𝑖𝑗 to be an arbitrary color from
{𝑔𝑟𝑒𝑒𝑛, 𝑦𝑒𝑙𝑙𝑜𝑤, 𝑝𝑖𝑛𝑘}.
Clearly, the above reduction takes polynomial time. Next, we argue that the 3-SAT instance is
satisfiable if and only if the reduced Red-Blue Respectful Coloring problem instance is a YES
instance.
12
CS3230
To show 3-SAT instance is satisfiable implies the reduced Red-Blue Respectful Col-
oring problem instance is a YES instance, consider a satisfying assignment 𝜎 and build a
red-blue coloring by setting 𝑓𝑗 = 𝑏𝑙𝑢𝑒 if 𝑥𝑗 = 1 in 𝜎; otherwise, 𝑓𝑗 = 𝑟𝑒𝑑. Since 𝜎 satisfies each
clause 𝐶𝑖 , by the construction, for each 𝑃𝑖 there exists a ball 𝐵𝑗 such that 𝑓𝑗 = 𝑐𝑖𝑗 .
For the converse, consider a valid 𝑟𝑒𝑑 − 𝑏𝑙𝑢𝑒 respectful coloring 𝑓1 , · · · , 𝑓𝑛 . Then create an
assignment 𝛼 by setting 𝛼(𝑥𝑗 ) = 1 if 𝑓𝑗 = 𝑏𝑙𝑢𝑒; 𝛼(𝑥𝑗 ) = 0 otherwise. Again, by an argument
similar to that in the last paragraph, 𝛼 is a satisfying assignment.
– END OF PAPER; All the Best –
13