Ada 6
Ada 6
Problem 1 Consider the problem of making change for n rupees using the fewest number
of coins. Suppose that the available coins are in denominations that are powers of c, i.e., the
denominations are c0 , c1 , · · · , ck , for some integer c > 1 and k ≥ 1. Show that the following
greedy algorithm always yields an optimal solution – pick as many coins of denomination ck
as possible, then pick as many coins of denomination ck−1 and so on.
Solution. We will prove this using exchange argument. Let σ denote an ordering of the coins
as picked by the Greedy solution - that is the denominations of the coins are non-increasing.
Assume for contradiction that there exists a different optimal solution which uses stricly lesser
number of coins. Let σ ? be the ordering of the coins in non-decreasing order of denomination
in this optimal solution.
Let i be the first index such that coin i in σ has a larger denomination - say cj compared
to coin i in σ ? . Here is the crucial claim. There exists an i0 > i such that the sum of the
denominations of coins i to i0 in σ ? is exactly equal to cj . Before seeing the proof of this fact,
let us see why this helps. We exchange all the coins i to i0 in σ ? with the single coin coin i
from σ. It is easy to see that then we have a solution which has a stricly lesser number of
coins compared to σ ? contradicting the fact that it is optimal.
Now on to the proof of the fact. We are going to prove the following by induction. For
any j 0 ≥ 1, consider a sequence of numbers, each of the form c` , ` ≤ j 0 such that the numbers
0
are in non-increasing order and they sum up to at least cj . Then there exists a prefix of these
0
numbers that sum up to exactly cj . The base case is j 0 = 1 which is easy to prove - either we
have c many 1s or just one c. Now suppose this is true for all j 0 = 1, 2, · · · j − 1 and consider
cj . Consider cj as a sequence of c many copies of cj−1 - crucially note that c is an integer here.
Now apply induction hypothesis on each of cj−1 .
Problem 2 [KT-Chapter 4] Let us consider a long, quiet country road with houses scattered
very sparsely along it. (We can picture the road as a long line segment, with an eastern
endpoint and a western endpoint.) Further, let’s suppose the residents of all these houses are
avid cell phone users. You want to place cell phone base stations at certain points along the
road, so that every house is within 4 kilometers of one of the base stations. Give an efficient
algorithm that achieves this goal, using as few base stations as possible. Prove the correctness
of your algorithm.
Solution: There is a simple greedy algorithm here. Let h denote the left-most house. Then
we place a base station 4 km to the right of h. Now remove all houses which are covered by
this base station, and repeat.
The proof follows the ‘greedy stays ahead’ strategy. It is easy to show (by induction) that
if the algorithm locates base stations at b1 , · · · , bk and some other algorithm (which could be
the optimal algorithm) places base stations at b01 · · · b0k (from left to right), then b1 ≥ b01 , b2 ≥ b02
and so on. Therefore k ≤ k 0 .
Problem 3
You are given two sets X and Y of n positive integers each (assume all distinct for simplicity
in each individual list). You are asked to arrange the elements in each of the sets X and Y in
1
some order. Let xi be the i thQelement of X in this order, and define yi similarly. Your goal
is to arrange them such that ni=1 xyi i = xy11 × xy22 × · · · xynn is maximized.
Solution. The algorithm is simple and intuitive. Just sort X and Y in non-increasing order.
Now for the proof, first of all assume optimal sorts X in exactly this order. Why is this
fine ? Well, if opt does not do that, we can simply change the ordering of X and generate
a new-ordering of Y such that xi is still matched to the same yj as in the original optimal
ordering.
Now we need to prove that there exists an optimal solution which orders Y in non-increasing
order as well. This can be easily done by an exchange argument. Assume that this is not
true. Then there exists an inversion pair in Y. Perform an exchange argument and show that
you can strictly increase the objective function value in this process.
Problem 4 (Vertex Cover.) Although we have not started with graphs yet, I guess all of you
know what graphs are. So suppose you have an undirected connected graph G(V, E). Your
task is the following - pick a subset of vertices such that every edge in E is ‘covered’ - this
means that you must pick at least one end-point of every edge in your set. This set is then
known as a vertex cover. The problem of course has trivial solution - just pick all vertices !
As usual, the catch is - we want to pick the vertex cover of minimum possible size.
Consider now the following greedy algorithm - pick the vertex with maximum degree in
the remaining graph. Remove that vertex and all the edges incident upon it. Continue the
process until all edges have been covered. Is this algorithm optimal ? Prove that, otherwise
find an example where greedy is not optimal.
Solution. Consider just a path of 5-vertices : v0 , v1 , v2 , v3 , v4 . The greedy algorithm might
choose v1 - this will cover edges (v0 , v1 ), (v1 , v2 ). After those are removed, greedy might choose
v2 and then it still needs to choose either v3 or v4 . The optimal solution here is clearly v1 , v3 .