Tut 7 Solution
Tut 7 Solution
General Instructions: Argue logically. Write it in a manner that explains your logic very
clearly. Do not miss steps in between.
Q1[Midsem 2022-23] Given n > 1 intervals, the starting point of the intervals has been given in the array
S[n] and the ending points of the intervals has been given in the array E[n]. We want a set X which
consists of the points, of minimum size such that for all n intervals there is a point x ∈ X. Design an
algorithm to find such a set and prove the optimality of your algorithm.
Solution Sort the array E and maintain the array S such that (S[i], E[i]) is a given interval. Add E[1]
in X and set i = 1 and j = 1.
While ( i != n )
If X[j] does not belong to (S[i], E[i]) then
X = X U E[i]
j = j+1
Otherwise
i = i+1
Assume that O is an optimal set. Then pick the least index i such that O[i] ̸= X[i].
Note that upto O[i − 1] = X[i − 1] = E[j] we have covered at least j intervals. Now O[i] can never be
greater than X[i] because in this case we are not covering some intervals. Therefore, O[i] < X[i] = E[k]
where j < k. If inclusion of O[i] is covering more intervals than the inclusion of X[i] = E[k] then definitely
we have missed some of the intervals in our algorithm. But this is not the case in the proposed algorithm.
Hence the number of intervals which we are covering by including O[i] must be less than or equal to the
number of intervals which we are covering by including X[i] = E[k]. Then instead of O[i] we can include
X[i] = E[k]. It contradicts the minimality of i.
Q2[Compre 2022-23] Suppose you have a collection of n items i1 , i2 , . . . , in with weights w1 , w2 , . . . , wn
and a bag with capacity W . Describe a simple, efficient algorithm to select as many items as possible to
fit inside the bag e.g., the maximum cardinality set of items that have weights that sum to at most W .
Prove that your algorithm gives the optimal output.
Solution Same as that of Container Loading Problem. Try to recreate that proof by yourself.