Algorithm Design
Algorithm Design
Algorithm Design
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
322<br />
Chapter 6 Dynamic Programming<br />
/3 cannot appear in consecutive minutes. For example, if your job is on<br />
machine A in minute i, and you want to switch to mach~e B, then your<br />
choice for minute i + 1 must be move, and then your choice for minute i + 2<br />
canbe B. The value of a plan is the total number of steps that you manage<br />
to execute over the n minutes: so it’s the sum of ai over all minutes in<br />
which the job is on A, plus the sum of bi over all minutes in which the job<br />
is on B.<br />
bn, find a plan of<br />
maximum value. (Such a strategy will be called optgmal.) Note that your<br />
plan can start with either of the machines A or B in minute 1.<br />
Example. Suppose n = 4, and the values of a~ and bi are given by the<br />
following table.<br />
Minute 1 Minute 2 Minute 3 Minute 4<br />
A 10 1 1 10<br />
B 5 1 20 20<br />
Then the plan of maximum value would be to choose A for:minute 1,<br />
then move for minute 2, and then B for minutes 3 and 4. The value of this<br />
plan would be 10 + 0 + 2O + 20 = 5O.<br />
(a) Show that the following algorithm does not correctly solve this<br />
problem, by giving an instance on which it does not return the correct<br />
In minute I, choose the machine achieving the larger of a I, bl<br />
Set<br />
~hile i < n<br />
What was the choice in minute i--I?<br />
If A:<br />
If hi+ l >ai+ai+l then<br />
Choose moue in minute i and B in minute i+ 1<br />
Proceed to iteration i+ 2<br />
Else<br />
Choose A in minute<br />
Proceed to iteration i+<br />
Endif<br />
If B: behave as above with roles of A and B reversed<br />
EndWhile<br />
11.<br />
12.<br />
Exercises<br />
In your example, say what the correct answer is and also what the<br />
algorithm above finds.<br />
b n and returns the value of an optimal plan.<br />
an and<br />
Suppose you’re consulting for a company that manufactures PC equipment<br />
and ships it to distributors all over the country. For each of the<br />
next n weeks, they have a projected supply s~ of equipment (measured in<br />
pounds), whi4h has to be shipped by an air freight carrier.<br />
Each week’s supply can be carried by one of two air freight companies,<br />
AorB.<br />
Company A charges a fixed rate r per pound (so it costs r- s~ to ship<br />
a week’s supply si).<br />
Company B makes contracts for a fixed amount c per week, independent<br />
of the weight. However, contracts with company B must be made<br />
In blocks of four consecutive weeks at a time.<br />
A schedule, for the PC company, is a choice of air freight Company<br />
(A or B) for each of the n weeks, with the restriction that company B,<br />
whenever it is chosen, must be chosen for blocks of four contiguous<br />
weeks at a 0me. The cost of the schedule is the total amount paid to<br />
company A and B, according to the description above.<br />
Give a polynomial-time algorithm that takes a sequence of supply<br />
Sn and returns a schedule of minimum cost.<br />
Example. Suppose r = 1, c = 10, and the sequence of values is<br />
11, 9, 9, 12, 12, 12, 12, 9, 9, 11.<br />
Then the optimal schedule would be to choose company A for the first<br />
three weeks, then company B for a block of four consecutive weeks, and<br />
then company A for the fInal three weeks.<br />
Suppose we want to replicate a file over a collection of n servers, labeled<br />
Sn. TO place a copy of the file at server Si results in a placement<br />
cost of q, for an integer q > 0.<br />
Now, if a user requests the file from server Si, and no copy of the file is<br />
present at S,, then the servers S~+l, S~+2, S,+3... are searched In order until<br />
a copy of the file is fInally found, say at server S i, where j > i. This results<br />
In an access cost ofj - i. (Note that the lower-indexed servers S~_> S~_2 ....<br />
are not consulted In this search.) The access cost is 0 if S i holds a copy of<br />
the file. We will require that a copy of the file be placed at server Sn, so<br />
that all such searches ~ terminate, at the latest, at<br />
323