CSCI 4020 Computer Algorithms PDF
CSCI 4020 Computer Algorithms PDF
Problem Set 3
Linyun Fu
March 16, 2014
Chapter 6, Problem 11
Suppose youre consulting for a company that manufactures PC equipment
and ships it to distributors all over the country. For each of the next n weeks,
they have a projected supply si of equipment (measured in pounds), which
has to be shipped by an air freight carrier.
Each weeks supply can be carried by one of two air freight companies, A
or B.
1
Then the optimal schedule would be to choose company A for the first three
weeks, then company B for a block of four consecutive weeks, and then
company A for the final three weeks.
Answer
We define MINCOST (i) to be the lowest cost that could be achieved to
ship the equipment for the first i weeks, and OP T (i, j) to be the company
chosen for the j-th week to achieve MINCOST (i). Since the best schedule
for the first i weeks is either achieved through choosing company A for the
i-th week, or choosing company B for the i 3, i 2, i 1 and i-th week, we
have
MINCOST[0] = 0
for i from 1 to min{n, 3} do
MINCOST[i] = MINCOST[i-1] + r si
for j from 1 to i-1 do
OPT[i][j] = OPT[i-1][j]
end
OPT[i][i] = A
end
for i from 4 to n do
MINCOST[i] = min{MINCOST[i-1]+r si , MINCOST[i-4]+4c}
if MINCOST[i-1]+r si < MINCOST[i-4]+4c then
for j from 1 to i-1 do
OPT[i][j] = OPT[i-1][j]
2
end
OPT[i][i] = A
else
for j from 1 to i-4 do
OPT[i][j] = OPT[i-4][j]
end
OPT[i][i-3] = OPT[i][i-2] = OPT[i][i-1] = OPT[i][i] = B
end
end
Chapter 6, Problem 15
On most clear days, a group of your friends in the Astronomy Department
gets together to plan out the astronomical events theyre going to try ob-
serving that night. Well make the following assumptions about the events.
There are n events, which for simplicity well assume occur in sequence
separated by exactly one minute each. Thus event j occurs at minute
j; if they dont observe this event at exactly minute j, then they miss
out on it.
The sky is mapped according to a one-dimensional coordinate system
(measured in degrees from some central baseline); event j will be taking
place at coordinate dj , for some integer value dj . The telescope starts
at coordinate 0 at minute 0.
The last event, n, is much more important than the others; so it is
required that they observe event n.
The Astronomy Department operates a large telescope that can be used
for viewing these events. Because it is such a complex instrument, it can
only move at a rate of one degree per minute. Thus they do not expect to be
able to observe all n events; they just want to observe as many as possible,
3
limited by the operation of the telescope and the requirement that event n
must be observed.
We say that a subset S of the events is viewable if it is possible to observe
each event j S at its appointed time j, and the telescope has adequate
time (moving at its maximum of one degree per minute) to move between
consecutive events in S.
The problem. Given the coordinates of each of the n events, find a
viewable subset of maximum size, subject to the requirement that it should
contain event n. Such a solution will be called optimal.
Example. Suppose the one-dimensional coordinates of the events are as
shown here.
Event 1 2 3 4 5 6 7 8 9
Coordinate 1 -4 -1 4 5 -4 6 7 -2
(a) Show that the following algorithm does not correctly solve this problem,
by giving an instance on which it does not return the correct answer.
In your example, say what the correct answer is and also what the
algorithm above finds.
4
(b) Give an efficient algorithm that takes values for the coordinates d1 , d2 , ..., dn
of the events and returns the size of an optimal solution.
Answer
Subproblem (a)
Consider the following set of events:
Event 1 2 3 4 5
Coordinate 0 -1 2 3 2
The algorithm will mark all the five events as legal and add event 1, 2,
and 5 in order to the set S, yielding the answer {1, 2, 5}, but the correct
answer should be {1, 3, 4, 5}.
Subproblem (b)
We use OP T (i) to denote the size of the maximum viewable subset of the
first i events that contains event i. It can be achieved through observing some
previous event such that the telescope can catch event i in time thereafter:
1 else if |di | i;
0 otherwise (4)
for i from 1 to n do
if |di | > i then
OPT[i] = 0
else
OPT[i] = 1
for j from 1 to i-1 do
if OPT[j]6=0 and |di dj | ij and OPT[j]+1>OPT[i] then
5
OPT[i] = OPT[j]+1
end
end
end
end