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 rearranged.
Therefore, we
can speak of a book , where
, that has a
thickness
and height
.
The length of each bookshelf at this library is L.
Suppose all the books have the same height h (i.e. 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
does not fit, and then repeat
with subsequent shelves.
Show that the greedy algorithm always finds the optimal shelf
placement, and analyze its time complexity.
Unfortunately, the city has bad neighborhoods, which are defined as
intersections we do not want to walk in.
We are given an matrix BAD, where BAD[i,j] = ``yes''
if and only if the intersection between streets i and j is somewhere
we want to avoid.
(a) Give an example of the contents of BAD such that there is no path across the grid avoiding bad neighborhoods.
(b) Give an O( X Y ) algorithm to find a path across the grid that avoids bad neighborhoods.
(c) Give an O( X Y ) algorithm to find the shortest path across
the grid that avoids bad neighborhoods. You may assume that all blocks
are of equal length.
For partial credit, give an algorithm.
If there were no bad neighborhoods to contend with, the shortest path across the grid would have length (X-1) + (Y-1) blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves.
Give an algorithm that takes the array BAD and returns the number of safe paths of length X+Y-2. For full credit, your algorithm must run in O( X Y ).
the maximum is achieved by summing the third through seventh elements, where 59+26+(-53)+58+97 = 187. When all numbers are positive, the entire array is the answer, while when all numbers are negative, the empty array maximizes the total at 0.
In the United States, coins are minted with denominations of
1, 5, 10, 25, and 50 cents.
Now consider a country whose coins are minted with
denominations of units.
They seek an algorithm that will enable them to make change of n units
using the minimum number of coins.
(a) The greedy algorithm for making change repeatedly uses the biggest coin
smaller than the amount to be changed until it is zero.
Show that the greedy algorithm does not always give the minimum number of
coins in a country whose denominations are .
(b) Give an efficient algorithm that correctly determines the minimum number
of coins needed to make change of n units using
denominations .
Analyze its running time.
| a | b | c | |
| a | a | c | c |
| b | a | a | b |
| c | c | c | c |
For example, consider the following multiplication table and the string bbbba. Parenthesizing it (b(bb))(ba) gives a, but ((((bb)b)b)a) gives c.
Give an algorithm, with time polynomial in n and k, to decide whether such a parenthesization exists for a given string, multiplication table, and goal element.
A company database consists of 10,000 sorted names, 40% of whom are known as good customers and who together account for 60% of the accesses to the data base. There are two data structure options to consider for representing the database:
Demonstrate which option gives better expected performance. Does this change if linear search on an unsorted array is used instead of binary search for both options?
Suppose you are given an array A of n sorted numbers that has been
circularly shifted k positions to the right.
For example, is a sorted array that has been
circularly shifted k=2 positions, while
has been shifted k=4 positions.