Tutorial 5
HKUST
Question 1
Your input is 𝑘 sorted lists that need to be merged into one sorted
list.
The “obvious” method is to modify the merging procedure from
mergesort; at every step, compare the smallest items from each
list and move the minimum one among them to the sorted list.
Finding the minimum value of 𝑘 items requires 𝑂(𝑘) time so,
if the lists contain 𝑛 items in total the full 𝑘-way merge would take
𝑂(𝑛𝑘) time.
This can be improved.
Show that this is the best possible. That is, any comparison-based
sorting algorithm to sort the 𝑛/𝑘 lists into one sorted list with 𝑛
elements will need to make at least Ω(𝑛 log 𝑘) comparisons.
Note that you can not derive this lower bound by simply combining
the lower bounds for the individual lists.
4
Solution 2
First calculate the number of different possible output (orderings).
𝑛
Let 𝐿𝑖 be list 𝑖, for 𝑖 = 1, ⋯ , :
𝑘
The final list is the concatenation 𝐿 = 𝐿1 𝐿2 ⋯ 𝐿𝑛/𝑘 .
Each list 𝐿𝑖 has 𝑘! possible orderings.
5
Solution 2
Recall that a binary tree of height ℎ has at most 2ℎ leaves.
Thus, in our problem, height h of the comparison tree must satisfy
𝑛Τ𝑘 𝑛Τ𝑘
2ℎ ≥ 𝑘! ֜ ℎ ≥ log( 𝑘! )
Note that
𝑛Τ𝑘
𝑛
log 𝑘! = ⋅ log 𝑘!
𝑘
𝑛
= Θ 𝑘 log 𝑘
𝑘
= Θ(𝑛 log 𝑘)
7
Solution 3
(a) Recall that a number 𝑚 ∈ [0, 𝑛2 − 1] can be uniquely
represented in base 𝑛 as a pair (𝑚1 , 𝑚0 ) where
𝑚 = 𝑚1 𝑛 + 𝑚0 and 0 ≤ 𝑚0 , 𝑚1 ≤ 𝑛 − 1
𝒅 𝒏 + 𝒌 = 𝑶(𝒏)
8
Solution 3
(b) If the numbers are in 0, 𝑛𝑡 − 1 , solution is almost the same.
𝑚 = 𝑚𝑖 𝑛𝑖 and ∀𝑖, 0 ≤ 𝑚𝑖 ≤ 𝑛 − 1
𝑖=0
The figure below shows part of the decision tree for mergesort
operating on a list of 4 numbers, 𝑎1 , 𝑎2 , 𝑎3 , 𝑎4 . Please expand
subtree 𝑇3 , i.e., show all the internal (comparison) nodes and
leaves in subtree 𝑇3 .
𝑎1 : 𝑎2
≤ >
𝑎3 : 𝑎4 𝑎3 : 𝑎4
≤ > ≤ >
𝑇1 𝑇2 𝑇3 𝑇4
10
Solution 4
Mergesort on 𝑎1 , 𝑎2 , 𝑎3 , 𝑎4 first starts by comparing 𝑎1 , 𝑎2 and then
comparing 𝑎3 , 𝑎4 to get two sorted lists of size 2. It then merges those lists.
If 𝑎2 ≤ 𝑎3 then 𝑎2 is smallest
𝑎2 : 𝑎3 item and removed from list
≤ and 𝑎1 is compared to 𝑎3 .
𝑎1 : 𝑎3 If 𝑎1 ≤ 𝑎3 then 𝑎1 is next
≤ > smallest item and removed.
Only 𝑎3 , 𝑎4 remain and they
𝑎2 , 𝑎1 , 𝑎3 , 𝑎4 𝑎1 : 𝑎4 are in same list.
If 𝑎1 > 𝑎3 then 𝑎3 is next
smallest item and removed.
Only 𝑎1 , 𝑎4 remain. 11
Solution 4
Mergesort on 𝑎1 , 𝑎2 , 𝑎3 , 𝑎4 first starts by comparing 𝑎1 , 𝑎2 and then
comparing 𝑎3 , 𝑎4 to get two sorted lists of size 2. It then merges those lists.
Continue this process to build Tree 𝑇3 , the decision tree that merges the
two sorted lists 𝑎2 , 𝑎1 and 𝑎3 , 𝑎4 .
𝑎2 : 𝑎3
≤ >
𝑎1 : 𝑎3 𝑎2 : 𝑎4
≤ > ≤
≤ >
𝑎2 , 𝑎1 , 𝑎3 , 𝑎4 𝑎1 : 𝑎4 𝑎1 : 𝑎4 𝑎3 , 𝑎4 , 𝑎2 , 𝑎1
≤ > ≤ >
𝑎2 , 𝑎3 , 𝑎1 , 𝑎4 𝑎2 , 𝑎3 , 𝑎4 , 𝑎1 𝑎3 , 𝑎2 , 𝑎1 , 𝑎4 𝑎3 , 𝑎2 , 𝑎4 , 𝑎1
12
Question 5
Note: Proving that something does not work usually means to find
a counter-example.
13
Solution 5
Sort intervals by starting time so that 𝒇𝟏 𝒇𝟐 … 𝒇𝒏.
𝑑 ← 0 // # classrooms used so far Algorithm
for 𝑗 ← 1 to 𝑛 was modified
if lecture 𝒋 is compatible with some classroom 𝒌 then
to use finish
schedule lecture 𝑗 in classroom 𝑘
else time rather
allocate a new classroom 𝑑 + 1 than starting
schedule lecture 𝑗 in classroom 𝑑 + 1 time
𝑑 ←𝑑+1
(a) Prove that the greedy algorithm for change making always uses
the smallest number of possible coins if the coin set consists of
quarters (25 cents) , dimes (10) , nickels (5), and pennies (1).
(b) Suppose that the available coins are in denominations that are
powers of 𝑐. i.e. the denominations are 𝑐 0 , 𝑐1 , ⋯ , 𝑐 𝑘 for some
integers 𝑐 > 1 and 𝑘 ≥ 1. Show that the greedy algorithm always
yields an optimal solution.
(c) Give a set of coin denominations for which the greedy algorithm
does not yield an optimal solution. As noted, your set should
include a penny so that there is a solution for every value of 𝑛.
16
Solution 6a
(a) Prove that the greedy algorithm for change making always uses
the smallest number of possible coins if the coin set consists of
quarters (25 cents) , dimes (10) , nickels (5), and pennies (1).
We will actually show the stronger fact that 𝑥𝑖 = 𝑥𝑖′ for all 𝑖.
17
Solution 6a (cont)
𝑥1 + 5𝑥5 + 10𝑥10 + 25𝑥25 = 𝑛 = 𝑥1′ + 5𝑥5′ + 10𝑥10
′ ′
+ 25𝑥25
𝑿 minimizes number of coins used.
The first thing to notice is that 𝑥1 = (𝑛 mod 5).
● 𝑥1 < 5 because if 𝑥1 > 5 then replacing 5 pennies with a nickel
uses a smaller number of coins, contradicting the optimality of 𝑋.
● 𝑛 − x1 = 5x5 + 10x10 + 25x25 . Thus n = x1 mod 5.
Since x1 < 5 this implies x1 = (n mod 5).
𝑥1′ = (𝑛 mod 5) as well since the greedy algorithm will use as
many nickels as possible before using pennies.
Thus 𝑥1 = 𝑥1′ = 𝑛 mod 5 .
● If 𝑥10 = 2 then 𝑥5 = 0.
Otherwise, replace two dimes and one nickel by one quarter by
setting 𝑥10 = 𝑥10 − 2, 𝑥5 = 𝑥5 − 1 and 𝑥25 = 𝑥25 + 1,
again contradicting minimality.
19
Solution 6a (cont)
5𝑥5 + 10𝑥10 + 25𝑥25 = 𝑛 − 𝑥1 = 5𝑥5′ + 10𝑥10
′ ′
+ 25𝑥25
where 𝑥1 = (𝑛 mod 5) and
𝑥5 ≤ 1, 𝑥10 ≤ 2, and If 𝑥10 = 2 then 𝑥5 = 0
Note this only allows five options for (𝑥5 , 𝑥10 , 𝑥25 ) and we can
check each of them to see that greedy gives the same solution.
● 𝑥5 , 𝑥10 , 𝑥25 = 0, 0, 𝑥25 ,
Greedy also sets 𝑥5′ , 𝑥10
′ ′
, 𝑥25 = 0, 0, 𝑥25 => Greedy is also optimal
● 𝑥5 , 𝑥10 , 𝑥25 = 1, 0, 𝑥25 ,
Greedy also sets 𝑥5′ , 𝑥10
′ ′
, 𝑥25 = 1, 0, 𝑥25 => Greedy is also optimal
● 𝑥5 , 𝑥10 , 𝑥25 = 0, 1, 𝑥25 ,
Greedy also sets 𝑥5′ , 𝑥10
′ ′
, 𝑥25 = 0, 1, 𝑥25 => Greedy is also optimal
● 𝑥5 , 𝑥10 , 𝑥25 = 0, 2, 𝑥25 ,
Greedy also sets 𝑥5′ , 𝑥10
′ ′
, 𝑥25 = 0, 2, 𝑥25 => Greedy is also optimal
● 𝑥5 , 𝑥10 , 𝑥25 = 1, 1, 𝑥25 ,
Greedy also sets 𝑥5′ , 𝑥10
′ ′
, 𝑥25 = 1, 1, 𝑥25 => Greedy is also optimal
𝑛′ = 𝑎𝑖 𝑐 𝑖 = 𝑜𝑖 𝑐 𝑖 .
𝑖≤𝑗 𝑖≤𝑗
where, (from prev page,) 𝑎𝑖 < 𝑐. Furthermore, 𝑎𝑗 ≠ 𝑜𝑗 .
If 𝑜𝑗 > 𝑎𝑗 then
𝑎𝑖 𝑐 𝑖 = 𝑛′ − 𝑎𝑗 𝑐 𝑗 = 𝑜𝑗 − 𝑎𝑗 𝑐 𝑗 + 𝑜𝑖 𝑐 𝑖 ≥ 𝑐 𝑗
𝑖<𝑗 𝑖<𝑗
This is impossible because Greedy would then have taken more than 𝑎𝑗
coins of size 𝑐 𝑗 . Thus 𝑜𝑗 < 𝑎𝑗 . But this implies
𝑜𝑖 𝑐 𝑖 = 𝑛′ − 𝑜𝑗 𝑐 𝑗 = 𝑎𝑗 − 𝑜𝑗 𝑐 𝑗 + 𝑎𝑖 𝑐 𝑖 ≥ 𝑐 𝑗 .
𝑖<𝑗 𝑖<𝑗
This immediately implies there exists 𝑖 < 𝑗 such that 𝑜𝑖 ≥ 𝑐.
֜ Take those 𝑜𝑖 coins of size 𝑐 𝑖 and replace them with 1 coin of size 𝑐 𝑖+1
and (𝑜𝑖 − 𝑐) coins of size 𝑐 𝑖 reducing the number of coins, contradicting
optimality of 𝑂!
֜ Greedy is optimal! 22
Solution 6c
(c) Give a set of coin denominations for which the greedy algorithm
does not yield an optimal solution. As noted, your set should include
a penny so that there is a solution for every value of 𝑛.
23
Solution 6
As an example the points above are given on a line and you are given the
length of a 1-unit interval.
Show how to place a minimum number of such intervals to cover the points. 25
Solution 7
Keep the points in an array. Walk through the array as follows.
1. Set 𝑥 = 𝑥1
2. Walk through the points in increasing order
until finding the first 𝑗 such that 𝑥𝑗 > 𝑥 + 1
3. Output
4. If there was no such 𝑗 in (2) then stop. Otherwise, set 𝑥 = 𝑥𝑗
5. Go to Step (2)
The diagram above is the solution for the points on the previous page
26
Solution 7
Keep the points in an array. Walk through the array as follows.
1. Set 𝑥 = 𝑥1
2. Walk through the points in increasing order
until finding the first 𝑗 such that 𝑥𝑗 > 𝑥 + 1
3. Output
4. If there was no such 𝑗 in (2) then stop. Otherwise, set 𝑥 = 𝑥𝑗
5. Go to Step (2)
1. Output 𝑥𝑖 , 𝑥𝑖 + 1
2. Find min 𝑗 such that 𝑥𝑗 > 𝑥𝑖 + 1
3. If such a 𝑗 does not exist, stop, else return Greedy(j,k)
(ii) 𝑋 = 𝑛 and we know that the algorithm is correct for all problems with 𝑋 < 𝑛.
28
Solution 7
1. Output 𝑥𝑖 , 𝑥𝑖 + 1
2. Find min 𝑗 such that 𝑥𝑗 > 𝑥𝑖 + 1
3. If such a 𝑗 does not exist, stop, else return Greedy(j,k)
Now, suppose that there is a solution to OPT different than the Greedy one.
≤ 1 + O(𝑘, 𝑛)
= O(1, 𝑛) Final Result!
31