Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the problem of storing n books on shelves in a library. The order of th

ID: 3835129 • Letter: C

Question

Consider the problem of storing n books on shelves in a library. The order of the books is fixed by the cataloging system and so cannot be rearraged. Therefore, we can speak of a book bi , where 1 i n, that has a thickness ti and height hi . The length of each bookshelf at this library is L. (1) Suppose all the books have the same height h (i.e. h = hi = hj for all i, j) and the shelves are all separated by a distance of greater than h, so any book fits on any shelf. The greedy algorithm would fill the first shelf with as many books as we can until we get the smallest i such that bi does not fit, and then repeat with subsequent shelves. Show that the greedy algorithm always finds the optimal shelf placement, and analyze the time complexity. (2) This is a generalization of the previous problem. Now consider the case where the height of the books is not constant, but we have the freedom to adjust the height of each shelf to that of the tallest book on the shelf. Thus the cost of a particular layout is the sum of the heights of the largest book on each shelf. (a) Give an example to show that the greedy algorithm of stuffing each shelf as full as possible does not always give the minimum overall height. (b) What technique should we use to solve this problem? (c) What are the subproblems? (d) How many subproblems are there? (e) Give an algorithm for this problem, and analyze its time complexity

Explanation / Answer

a) Consider a set of three books, each of thickness 1, and with the first book having height 1, and the second and
third books having height 2. Suppose that L = 2. Clearly, the b1 must be placed on the first shelf. The greedy
algorithm then places b2 on the first shelf. This now fills the first shelf, and so we then must place b3 on the
second shelf.
Since the largest book on each shelf is of height 2, the cost of this layout is 4. However, we can do better. Instead
of this layout, we can place the first book on the first shelf, and the last two books on the second shelf. In this
case, the first shelf has only one book of height 1, and the second shelf has books only of height 2, and so the
cost of this layout is 3, which is better than what the greedy algorithm produced. Notice that this example is
simple, which is why it is the best example. The simplest example is the best example!
You should write a proof of the optimality of the greedy algorithm for the case where all the book heights are
constant (problem 3-1), and determine what in that proof goes wrong in the case of non-equal heights.

e)prodecure PLACEBOOKS (H; T )
var
2
COST : array(1::n) of real;
currentshelf : real;
i; j; start : integer;
begin
COST[n] := H[n];
for i := n
1 downto 1 loop
currentshelf := T [i];
Append start to left end list.
for j := i + 1 to n loop
currentshelf := currentshelf + T [j];
if currentshelf L and
maxfH[i];:::;H[j]g + COST [j + 1] COST [i] then
COST [i] = maxfH[i];:::;H[j]g + COST [j + 1]
N extShelf [i] = j + 1
end if;
end for loop;
end for loop;
end PLACEBOOKS

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote