This preview shows pages 1–10. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Solution to Problem 151 Taking the book's hint, we sort the points by .r—coordinate, left to right, in Oh: lg n)
time. Let the sorted points be, left to right. (pl. pg. p3. . . . . p”). Therefore, in is
the leftmost point, and p” is the rightmost. We deﬁne as our subproblems paths of the following form, which we call bitonic
paths. A bitonic pom PM, where 1" E j. includes all points 321. pg. . . . . p4,; it
starts at some point pf, goes strictly left to point pl, and then goes strictly right to
point p} . By “going strictly left,“ we mean that each point in the path has a lower .1;
coordinate than the previous point. Looked at another way, the indices of the sorted
points form a strictly decreasing sequence. Likewise, “going strictly right" means
that the indices of the sorted points form a strictly increasing sequence. Moreover,
PM, contains all the points p1. pg. 1);. . . . . p}. Note that p} is the rightmost point
in Pi”, and is on the rightgoing subpath. The leftgoing subpath may be degenerate,
consisting of just p1. Let us denote the euclidean distance between any two points Jo; and Jo} by mp} .
And let us denote by Hi. j], for l 5 1' 5 j E n, the length of the shortest bitonic
path PM. Since the leftgoing subpath may be degenerate. we can easily compute
all values b[l. j]. The only value of b[i. i] that we will need is Mn. n], which is the length of the shortest bitonic tour. we have the following formulation of b[i. j]
forlgi Ejftt: b[12] = IP1P1  b[i. j] b[t'. j — l] +p_I._1py fort' <: j — l. g,” _ Ly] “ﬂame j — 1] + Irapa}  Why are these formulas correct"? Any bitonic path ending at p; has 332 as its right—
most point. so it consists only of p1 and p3. Its length. therefore, is  p1 pgl. Now consider a shortest bitonic path P”. The point lug1 is somewhere on this path. If it is on the rightgoing subpath. then it immediately preceeds g. on this
subpath. Otherwise. it is on the leftgoing subpath. and it must be the rightmost point on this subpath. so i = j — 1. In the ﬁrst case. the subpath from p to p j_1
must be a shortest bitonic path PL j_1._ for otherwise we could use a cut—andpaste
argument to come up with a shorter bitonic path than Buy. (This is part of our opti—
nral substructure.) The length of PL}. therefore, is given by b[i. j — l] + p_.._1pj.
In the second case. p} has an immediate predecessor 11%. where k c: j — l. on
the rightgoing subpath. Optimal substructure again applies: the subpath from p
to pry1 must be a shortest bitonic path Raf.j_1._ for otherwise we could use cut—and
paste to come up with a shorter bitonic path than B. j. (‘We have implicitly relied
on paths having the same length regardless of which direction we traverse them.)
The length of RU. therefore. is given by minliri; j_1 {b[k. j — l] + IllaPf”. We need to compute Mn. 31]. In an optimal bitonic tour. one of the points adjacent
to p” must be 10,24. and so we have b[n.n] = b[n — 1.13] +pﬂ_1p,rl . To reconstruct the points on the shortest bitonic tour, we deﬁne t‘[;r'. j] to be the
immediate predecessor of p} on the shortest bitonic path Rwy. The pseudocode
below shows how we compute b[;r'. j] and i'[t'. j]: EL'CLIDEANTsplp)
sort the points so that (pl. pg. 13;. . . . . p”) are in order of increasing .t—coordinate
M1. 2] <— IP1P2
for} <— 3 to H
dofort' «c— ltoj—Z
I10 b[1".j]<— b[?' j  1] + P_.r—1P;
Iii. j] <— J' — 1
b[j — l. j] <— oo
fork<—ltoj—2
do a *— blﬁc. j — 1] + Iraml
ifq < w — 1. n
then DU — l. j] €— :11
Jo —1.n FA
N”. H] <— b[n — 1. H] + IPn—lpni
return I) andr We print out the tour we found by starting at p“. then a leftgoing subpath that
includes pp]. from right to left. until we hit p1. Then we print right—to—left the remaining subpath, which does not include pkl. For the example in Figure 15.9(b) on page 565? we wish to print the sequence 3);. p5. pi. p3. p1. pg. 55. Our code is
recursive. The right—toleft subpath is printed as we go deeper into the recursion.
and the left—to—right subpath is printed as we back out. PRINT—TOURH'. n) print p” print pk] It 4— r'[n — 1.3:]
PRIXT—PATHIEI'. l". n — 1) print p5.
PRINTPATHtrJ. j)
ift' {j
then it {— t'[i'. j]
print P;
ifk :> 1 then PRIXT—PATHG'. i. it)
else A" {— t'[j. f]
ifk :> 1
then PRINT—PATHG'. k. j)
print pg. The relative values of the parameters 1" and j in each call of PRINT—PATH indicate
which subpath we're workmg on. If :r' <: j. we're on the righttoleft subpath. and
if t' :} j. we're on the left—to—right subpath. The time to run EUCLIDEANTSP is OWE) since the outer loop on f iterates it — 2
times and the inner loops on 1" and it each run at most It — 2 times. The sorting step at the beginning takes Of It lg n} time. which the loop times dominate. The time to
run PRINTTOUR is 0U: ), since each point is printed just once. Solution to Problem 152 Note: we will assume that no word is longer than will ﬁt into a line. i.e.. i; E M
for all 1". First, we'll make some deﬁnitions so that we can state the problem more uniformly.
Special cases about the last line and worries about whether a sequence of words ﬁts
in a line will be handled in these deﬁnitions. so that we can forget about them when framing our overall strategy. ' Deﬁne erhusﬁ. j] = M — j +1' —Z;:=f lg to be the number of extra spaces at the
end of a line containing words 1" through j. Note that emu: may be negative.  Now deﬁne the cost of including a line containing words 1" through j in the sum
we want to minimize: 00 if atrium. j] <: O (i.e.. words i. . . . .j don't ﬁt) .
lcﬁ. j] = 0 ifj = n and arousﬁ. j] a 0 (last line costs 0) .
let'n'osﬁ'. ﬂ)3 othenvise . By making the line cost inﬁnite when the words don't ﬁt on it. we prevent such
an arrangement from being part of a minimal sum. and by making the cost 0 for the last line (if the words ﬁt), we prevent the arrangement of the last line from
inﬂuencing the sum being minimized. We want to minimize the sum of EC over all lines of the paragraph. Our subproblerns are how to optimally arrange words 1. j. where j =
1. . . . . u. Consider an optimal arrangement of words 1. . . . . j. Suppose we know that the
last line. which ends in word j, begins with word 1'. The preceding lines. therefore.
contain words 1. . . . . i — 1. In fact. they must contain an optimal arrangement of
words 1. . . . . 1' — 1. (Insert your favorite cutandpaste argument here.) Let c[j] be the cost of an optimal arrangement of words 1. . . . . j. If we know that
the last line contains words i. . . . . j, then c[j] : c[i — l]+ ic[i. j]. As a base case. when we're computing c[l]. we need c[O]. If we set :10] = 0. then c[1] = Ic[l. l],
which is what we want. But of course we have to ﬁgure out which word begins the last line for the sub—
problem of words 1. . . . . j. So we try all possibilities for word 1". and we pick the
one that gives the lowest cost. Here. i ranges from 1 to j. Thus. we can deﬁne c[j]
recursively by _ c ifj = 0.
CU]: lininch — l]+tc[i.j]l n; :> o.
iiiJ Note that the way we deﬁned 3c ensures that ' all choices made will ﬁt on the line (since an arrangement with L: 2 cc cannot
be chosen as the minimum), and  the cost of putting words i. . . . . j on the last line will not be 0 unless this really
is the last line of the paragraph (j = n) or words i . . . j ﬁll the entire line. We can compute a table of c values from left to right. since each value depends
only on earlier values. To keep track of what words go on what lines. we can keep a parallel p table that
points to where each a value came from. When c[j] is computed. if c[j] is based
on the value of cUc — 1]. set p[j] = it. Then after c[n] is computed. we can trace
the pointers to see where to break the lines. The last line starts at word phi] and
goes through word It. The previous line starts at word Hp [13]] and goes through
wordpﬁr] — l_. etc. In pseudocode. here’s how we construct the tables: PRINTNEATLYU. 11. Ml
Z> Compute nto'osh'. j] for l E 1' E j E 11.
for1' {— l to 11
(lo n.1't1'r15[1', 1'] «c— M —1‘,
forj <—1'+lto11
do monsﬁ. j] 4— n.1‘11us[1'.j — l] —1"J, — l
D Compute IC[1'. j] for l E 1' E j 5 11.
for1' {— l to 11
{lo forj <— 1' to 11
do if atrium. j] «z 0
then ic[1'. j] <— oo
elseif j = 11 and e.t1‘1‘ns[1'. j] 3 D
then ic[1'. j] <— 0
else in[1'.j] {— tom'nsﬁ. j])3
t> Compute CU] and pH] for l E j 5 11.
c[0] <— O
for} <— l to 11
(lo CU] 4— oo
for1' 4— 1 toj
do ifc[1' — l] + ic[1'. j] <: c[j]
then CU] <— c[1' — l] + ic[1'. j]
Mi] <— 1'
return 6 and p Quite clearly, both the time and space are (431(19}. In fact, we can do a bit better: we can get both the time and space dOwn to 9111111.
The key observation is that at most [ELI/2] words can ﬁt on a line. (Each word is
at least one character long, and there‘s a space between words.) Since a line with
words 1'... ., j contains j — 1' + 1 words, if j — 1' + l 3* {ELI/2] then we know
that ic[1'. j] = 130. We need only compute and store mtmsﬁ. j] and ic[1'. j] for
j — 1' + l E [NIKE]. And the inner for loop header in the computation of c[j]
and pH] can run from maxtl. j — MHZ] + 1) to j. W'e can reduce the space even further to G) (11). W'e do so by not storing the In
and atom tables, and instead computing the value of ic[1'. j] as needed in the last
loop. The idea is that we could compute tc[1'. j] in Oil) time if we knew the
value of e.t11‘ns[1'. j]. And if we scan for the minimum value in descending order
of 1', we can compute that as nttmsﬁ'. j] : ethnsﬁ' + l. j] — f, — 1. (Initially,
e.tt1'r15[j. j] = M — if.) This improvement reduces the space to 8101}, since now
the only tables we store are c and p. Here's how we print which words are on which line. The printed output of
GIVE—LINEStp. j) is a sequence of triples (it. 1'. j). indicating that words 1', . . . . j
are printed on line A". The return value is the line number k. G1\'ELINES(p.j)
1' *— PU]
ifr' : 1
thank «— 1
else k 4— GIVELINESip. i — 1) +1
print (Fc. 3'. 3')
return Fc The initial call is GIVELINESIp. 1:). Since the value of j decreases in each recur
sive call. GIVE—LINES takes a total of 0(33) time. Solution to Problem 161 Before we go into the various parts of this problem. let us ﬁrst prove once and for
all that the coinchanging problem has optimal substructure. Suppose we have an optimal solution for a problem of making change for 12 cents.
and we know that this optimal solution uses a coin whose value is c cents; let this
optimal solution use it coins. We claim that this optimal solution for the problem
of it cents must contain within it an optimal solution for the problem of H — c cents. We use the usual cut—and—paste argument. Clearly. there are it — 1 coins in the
solution to the it — c cents problem used within our optimal solution to the Fi' cents
problem. If we had a solution to the it — (7 cents problem that used fewer than i'; — 1
coins. then we could use this solution to produce a solution to the n cents problem
that uses fewer than 5’: coins. which contradicts the optimality of our solution. a. A greedy algorithm to make change using quarters. dimes. nickels. and pennies
works as follows: ' Give :3 : Lil/25] quarters. That leaves Na : i: mod 25 cents to make
change. A  Then give (i = Lug/1m dimes. That leaves Md 2 Mg mod 10 cents to make
change.  Then give k = Lind/SJ nickels. That leaves 13;.— = My mod 5 cents to make
change.  Finally. give p = 11.? pennies. An equivalent formulation is the following. The problem we wish to solve is
making change for n cents. If it = 0. the optimal solution is to give no coins.
If u 2» 0. determine the largest coin whose value is less than or equal to u.
Let this coin have value c. Give one such coin. and then recursively solve the
subproblem of making change for n — c cents. To prove that this algorithm yields an optimal solution. we ﬁrst need to show
that the greedy—choice property holds. that is. that some optimal solution to
making change for n cents includes one coin of value c. where c is the largest
coin value such that c : I}. Consider some optimal solution. If this optimal solution includes a coin of value c. then we are done. Otherwise. this optimal
solution does not include a coin of value c. We have four cases to consider:  If 1 5 n <: 5. then c = l. A solution may consist only of pennies. and so it
must contain the greedy choice. ' If 5 5 it «a: 10. then c = 5. By supposition. this optimal solution does not
contain a nickel. and so it consists of only pennies. Replace ﬁve pennies by
one nickel to give a solution with four fewer coins.  If 10 5 u «a: 25, then c = 10. By supposition, this optimal solution does not
contain a dime. and so it contains only nickels and pennies. Some subset of the nickels and pennies in this solution adds up to 10 cents. and so we can replace these nickels and pennies by a dime to give a solution with (between
1 and 9) fewer coins.  If 25 5 n, then c = 25. By supposition. this optimal solution does not
contain a quarter, and so it contains only dimes. nickels. and pennies. If
it contains three dimes, we can replace these three dimes by a quarter and
a nickel, giving a solution with one fewer coin. If it contains at most two
dimes. then some subset of the dimes. nickels. and pennies adds up to 25 cents. and so we can replace these coins by one quarter to give a solution
with fewer coins. Thus, we have shown that there is always an optimal solution that includes the
greedy choice, and that we can combine the greedy choice with an optimal solu
tion to the remaining subproblem to produce an optimal solution to our original
problem. Therefore. the greedy algorithm produces an optimal solution. For the algorithm that chooses one coin at a time and then recurses on sub—
problems. the running time is (dirk). where it is the number of coins used in an
optimal solution. Since it 5 n, the running time is 0m). For our ﬁrst descrip—
tion of the algorithm. we perform a constant number of calculations (since there
are only 4 coin types), and the running time is 0(1). When the coin denominations are CO. cl. . . . . (‘3‘, the greedy algorithm to make
change for u cents works by ﬁnding the denomination of such that j =
max {0 5 t' E k : Ci 5 it}. giving one coin of denomination of. and recurs
ing on the subproblem of making change for n — 0" cents. (An equivalent.
but more efﬁcient. algorithm is to give LII/cf] coins of denomination 6* and
Lin 1110(16?+1‘)/fCrJ coins of denomination 0" fort : O. l. . it — 1.) To show that the greedy algorithm produces an optimal solution. we start by
proving the following lennna: Lemma For i = O. l. . . . . 51'. let a; be the number of coins of denomination 6 used in
an optimal solution to the problem of making change for :1 cents. Then for
1': O. 1.....3.‘ — l._ wehave (L {z c. Proof If (11 3 c for some 0 5 t' {: it. then we can improve the solution by using
one more coin of denomination 6+1 and c fewer coins of denomination d . The
amount for which we make change remains the same. but we use :9 — l :> 0
fewer coins. I (lennna) To show that the greedy solution is optimal. we show that any non—greedy so—
lution is not optimal. As above. let j = marsh] E t' 5 it : cl 5 It}, so that the
greedy solution uses at least one coin of denomination o’ . Consider a non—
greedy solution. which must use no coins of denomination of or higher. Let the
non—greedy solution use a; coins of denomination c". for 1" = 0. l. . . . . j — l:_ . . 31 5' _  j _. .. j—l
thus we have 21:0 arc _ u. Slnce u 3 c . we hare that EEO r (rsCi 3 c' . C. Now suppose that the non—greedy solution is optimal. By the above lennna, o, Ec—lfori=0.1.....j—1.Thus,
j—l _.r—1 Eracl 5 Zlc— 1h“ i=0 i: ll
,..,‘
l"'3 
_
air
q“. cal—l
— lc—l)c_1
= cal—l
ea: cf. . . . . .F—l   ,
which contradicts our earlier assertion that 2;:0 nic’ 3 c3. ‘We conclude that
the non—greedy solution is not optimal. Since any algorithm that does not produce the greedy solution fails to be opti—
mal, only the greedy algorithm produces the optimal solution. The problem did not ask for the running time, but for the more efﬁcient greedy—
algorithm formulation, it is easy to see that the running time is ON"). since we
have to perform at most it each of the division, ﬂoor, and mod operations. With actual US. coins, we can use coins of denomination l, 10. and 25. 1When
H = 30 cents, the greedy solution gives one quarter and ﬁve pennies, for a total
of six coins. The non—greedy solution of three dimes is better. The smallest integer numbers we can use are l, 35. and 4. When it = 6 cents, the
greedy solution gives one 4—cent coin and two l—cent coins. for a total of three
coins. The non—greedy solution of two 3—cent coins is better. Since we have optimal substructure. dynamic programming might apply. And
indeed it does. Let us deﬁne c[j] to be the minimum number of coins we need to make change
for j cents. Let the coin denominations be d1. d3. . (is. Since one of the
coins is a penny. there is a way to make change for any amount j 3 1. Because of the optimal substructure, if we knew that an optimal solution for
the problem of making change for j cents used a coin of denomination d", we
would have c[j] : l + c[j — df]. As base cases, we have that c[j] : O for all
£50 To develop a recursive formulation, we have to check all denonmlations, giving a ﬁng. l+1mink{c[j—d,]} ifj :»l. CU] = We can compute the c[j] values in order of increasing j by using a table. The
following procedure does so, producing a table c[l . . Fl]. It avoids even exam
ining c[j] for j E O by ensuring that j 3 d, before looking up c[j — 02]. The
procedure also produces a table deucmﬂ . . n], where denomﬁ] is the denomi—
nation of a coin used in an optimal solution to the problem of making change
for j cents. COMPUTECHANGEUI. d. If) for} <— l to n
do GU] {— oo
fort' 4— 1toi:
do if} 3 0', and 1 +c[j —d;] <: c[j]
then c[j] {— 1+ c[j — all]
denomU] <— (1';
return c and demon: This procedure obviously runs in Oink) time. 1We use the following procedure to output the coins used in the optimal solution
computed by C OMPUTE— CHA NGE: GIVE—CHANGE}. dcnoml
if j r) 0
then give one coin of denomination dermmﬁ]
GIVE—CHANGEU — douch]. dcuom) The initial call is GIVE—CHANGE :1. 076110111). Since the value of the ﬁrst pa—
rameter decreases in each recursive call, this procedure runs in 001) time. ...
View
Full
Document
This note was uploaded on 07/02/2008 for the course COMPUTER S 583 taught by Professor Baek during the Spring '08 term at Korea University.
 Spring '08
 BAEK

Click to edit the document details