10.1 First, we show that if NO evenly divides PO, then each of PjO(iO−1)PO+1 through PjiPO must be placed as the iOthO job on some processor. Suppose otherwise. Then in the supposed optimal ordering, we must be able to find some jobsPjxO and PjyO such that PjxO is the tOthOjob on some processor and PjyO is the tO+1thO job on some processor but txO > tyO. Let PjzO be the job immediately followingPjxO. If we swap PjyO and PjzO, it is easy to check that the mean pro-cessing time is unchanged and thus still optimal. But nowPjyO follows PjxO, which is impos-sible because we know that the jobs on any processor must be in sorted order from the results of the one processor case.
LetPjeO1, PjeO2, ...,PjeMO be the extra jobs if NO does not evenly divide PO. It is easy to see that the processing time for these jobs depends only on how quickly they can be scheduled, and that they must be the last scheduled job on some processor. It is easy to see that the first MO processors must have jobs PjO(iO−1)PO+1 through PjiPO+MO; we leave the details to the reader.
10.3
,
4 7 8 9
0
1 5
sp nl
3 :
6 2
10.4 One method is to generate code that can be evaluated by a stack machine. The two opera-tions are PushO (the one node tree corresponding to) a symbol onto a stack and Combine,O which pops two trees off the stack, merges them, and pushes the result back on. For the example in the text, the stack instructions are Push(s), Push(nl), Combine, Push(t), Com-bine, Push(a), ComCom-bine, Push(e), ComCom-bine, Push(i), Push (sp), ComCom-bine, Combine.
By encoding a CombineO with a 0 and a PushO with a 1 followed by the symbol, the total extra space is 2NO − 1 bits if all the symbols are of equal length. Generating the stack machine code can be done with a simple recursive procedure and is left to the reader.
10.6 Maintain two queues, QO1and QO2. QO1will store single-node trees in sorted order, and QO2 will store multinode trees in sorted order. Place the initial single-node trees on QO1, enqueueing the smallest weight tree first. Initially, QO2 is empty. Examine the first two entries of each of QO1 and QO2, and dequeue the two smallest. (This requires an easily implemented extension to the ADT.) Merge the tree and place the result at the end of QO2. Continue this step until QO1is empty and only one tree is left in QO2.
10.9 To implement first fit, we keep track of bins biO, which have more room than any of the lower numbered bins. A theoretically easy way to do this is to maintain a splay tree ordered by empty space. To insert wO, we find the smallest of these bins, which has at least wO empty space; after wO is added to the bin, if the resulting amount of empty space is less than the inorder predecessor in the tree, the entry can be removed; otherwise, a DecreaseKeyO is performed.
To implement best fit, we need to keep track of the amount of empty space in each bin.
As before, a splay tree can keep track of this. To insert an item of size wO, perform an insert of wO. If there is a bin that can fit the item exactly, the insert will detect it and splay it to the root; the item can be added and the root deleted. Otherwise, the insert has placed wO at the root (which eventually needs to be removed). We find the minimum element MO in the right subtree, which brings MO to the right subtree’s root, attach the left subtree to MO, and delete wO. We then perform an easily implemented DecreaseKeyO on MO to reflect the fact that the bin is less empty.
10.10 Next fit: 12 bins (.42, .25, .27), (.07, .72), (.86, .09), (.44, .50), (.68), (.73), (.31), (.78, .17), (.79), (.37), (.73, .23), (.30).
First fit: 10 bins (.42, .25, .27), (.07, .72, .09), (.86), (.44, .50), (.68, .31), (.73, .17), (.78), (.79), (.37, .23, .30), (.73).
Best fit: 10 bins (.42, .25, .27), (.07, .72, .09), (.86), (.44, .50), (.68, .31), (.73, .23), (.78, .17), (.79), (.37, .30 ), (.73).
First fit decreasing: 10 bins (.86, .09), (.79, .17), (.78, .07), (.73, .27), (.73, .25), (.72, .23), (.68, .31), (.50, .44), (.42, .37), (.30).
Best fit decreasing: 10 bins (.86, .09), (.79, .17), (.78), (.73, .27), (.73, .25), (.72, .23), (.68, .31), (.50, .44), (.42, .37, .07), (.30).
Note that use of 10 bins is optimal.
10.12 We prove the second case, leaving the first and third (which give the same results as Theorem 10.6) to the reader. Observe that
log pONO = log pO(bOmO) = mOpOlog pObO Working this through, Equation (10.9) becomes
TO(NO) = TO(bOmO) = aOmO
iO=0
Σ
mOBCD a___ ObOkO EFGiO
iOpOlog pObO If aO = bOkO, then
TO(NO) = aOmOlog pObO
i
Σ
O=0miOpO= OO(aOmOmOpO+1log pObO)
Since mO = log NO/log bO, and aOmO = NOkO, and bO is a constant, we obtain TO(NO) = OO(NOkOlog pO+1NO)
10.13 The easiest way to prove this is by an induction argument.
10.14 Divide the unit square into NO−1 square grids each with side 1/ √MMMMMNO−1. Since there are NO points, some grid must contain two points. Thus the shortest distance is conservatively given by at most√MMMMMMMM2/ (NO−1).
10.15 The results of the previous exercise imply that the width of the strip is OO(1/ √MMNOO).
Because the width of the strip is OO(1/ √MMNOO), and thus covers only OO(1/ √MMNOO) of the area of the square, we expect a similar fraction of the points to fall in the strip. Thus only OO(NO/ √MMNOO) points are expected in the strip; this is OO(√MMNOO).
10.17 The recurrence works out to
TO(NO) = TO(2NO/ 3) + TO(NO/ 3) + OO(NO)
This is not linear, because the sum is not less than one. The running time is OO(NOlog NO).
10.18 The recurrence for median-of-median-of-seven partitioning is TO(NO) = TO(5NO/ 7) + TO(NO/ 7) + OO(NO)
If all we are concerned about is the linearity, then median-of-median-of-seven can be used.
10.20 When computing the median-of-median-of-five, 30% of the elements are known to be smaller than the pivot, and 30% are known to be larger. Thus these elements do not need to be involved in the partitioning phase. (Extra work would need to be done to imple-ment this, but since the whole algorithm isn’t practical anyway, we can ignore any extra work that doesn’t involve element comparisons.) The original paper [9] describes the exact constants in the worst-case bound, with and without this extra effort.
10.21 We derive the values of sO andδ, following the style in the original paper [17]. Let RtO,XO be the rank of element tO in some sample XO. If a sample S'O of elements is chosen ran-domly from SO, and O|OS'O|O = sO, O|OSO|O = NO, then we’ve already seen that
EO(RtO,SO) = sO+1 NO+1 _____RtO,S'O
where EO means expected value. For instance, if tO is the third largest in a sample of 5 ele-ments, then in a group of 19 elements it is expected to be the tenth largest. We can also calculate the variance:
VO(RtO,SO) =
√ MMMMMMMMMMM
_________________________(RtO,SO)(sO−R(sO+1)tO,SO+1)(N2(sO+2)O+1)(NO−sO) = OO(NO/ √MMsOO) We choose vO1and vO2so thatEO(RvO1,SO) + 2dVO(RvO1,SO) ∼∼ kO ∼∼ EO(RvO2,SO) − 2dVO(RvO2,SO)
where dO indicates how many variances we allow. (The larger dO is, the less likely the ele-ment we are looking for will not be in S'O.)
The probability that kO is not between vO1and vO2is 2
d
∞
∫
erfOO(xO) dxO = OO(eO−dO2/dO)
If dO = log1/ 2NO, then this probability isο(1/ NO), specifically OO(1/ (NOlog NO)). This means that the expected work in this case is OO(log−1NO) because OO(NO) work is performed with
These mean and variance equations imply RvO1,S'O ≥ k
(NO+1) (sO+1)
______ − dO√MMsOO
and
RvO2,S'O ≤ k (NO+1)
(sO+1)
______ + dO√MMsOO This gives equation (A):
δ = dO√MMsOO =√MMsOOlog 1/ 2NO (A) If we first pivot around vO2, the cost is NO comparisons. If we now partition elements in SO that are less than vO2 around vO1, the cost is RvO2,SO, which has expected value kO + δ sO+1
NO+1 _____. Thus the total cost of partitioning is NO + kO + δ
sO+1 _____NO+1
. The cost of the selections to find vO1 and vO2in the sample S'O is OO(sO). Thus the total expected number of comparisons is
NO + kO + OO(sO) + OO(NOδ/ sO) The low order term is minimized when
sO = NOδ/ sO (B)
Combining Equations (A) and (B), we see that
sO2 = NOδ = √MMsONOlog 1/ 2NO (C)
sO3/ 2 = NOlog 1/ 2NO (D)
sO = NO2/ 3log 1/ 3NO (E)
δ = NO1/ 3log 2/ 3NO (F)
10.22 First, we calculate 12*43. In this case, XLO = 1, XRO = 2, YLO = 4, YRO = 3, DO1 = −1, DO2 = −1, XLOYLO = 4, XROYRO = 6, DO1DO2 = 1, DO3 = 11, and the result is 516.
Next, we calculate 34*21. In this case, XLO = 3, XRO = 4, YLO = 2, YRO = 1, DO1 = −1, DO2 = −1, XLOYLO = 6, XROYRO = 4, DO1DO2 = 1, DO3 = 11, and the result is 714.
Third, we calculate 22*22. Here, XLO = 2, XRO = 2, YLO = 2, YRO = 2, DO1 = 0, DO2 = 0, XLOYLO = 4, XROYRO = 4, DO1DO2 = 0, DO3 = 8, and the result is 484.
Finally, we calculate 1234*4321. XLO = 12, XRO = 34, YLO = 43, YRO = 21, DO1 = −22, DO2 = −2. By previous calculations, XLOYLO = 516, XROYRO = 714, and DO1DO2 = 484. Thus DO3 = 1714, and the result is 714 + 171400 + 5160000 = 5332114.
10.23 The multiplication evaluates to (acO − bdO) + (bcO + adO)iO. Compute acO, bdO, and (aO − bO)(dO − cO) + acO + bdO.
10.24 The algebra is easy to verify. The problem with this method is that if XO and YO are posi-tive NO bit numbers, their sum might be an NO+1 bit number. This causes complications.
10.26 Matrix multiplication is not commutative, so the algorithm couldn’t be used recursively on matrices if commutativity was used.
10.27 If the algorithm doesn’t use commutativity (which turns out to be true), then a divide and conquer algorithm gives a running time of OO(NOlog70143640) = OO(NO2.795).
10.28 1150 scalar multiplications are used if the order of evaluation is ( ( AO1AO2 ) ( ( ( AO3AO4 ) AO5 ) AO6 ) )
10.29 (a) Let the chain be a 1x1 matrix, a 1xA matrix, and an AxB matrix. Multiplication by using the first two matrices first makes the cost of the chain AO + ABO. The alternative method gives a cost of ABO + BO, so if AO > BO, then the algorithm fails. Thus, a counterex-ample is multiplying a 1x1 matrix by a 1x3 matrix by a 3x2 matrix.
(b, c) A counterexample is multiplying a 1x1 matrix by a 1x2 matrix by a 2x3 matrix.
10.31 The optimal binary search tree is the same one that would be obtained by a greedy stra-tegy: IO is at the root and has children andO and itO; aO and orO are leaves; the total cost is 2.14.
10.33 This theorem is from F. Yao’s paper, reference [58].
10.34 A recursive procedure is clearly called for; if there is an intermediate vertex, StopOverO on the path from sO to tO, then we want to print out the path from sO to StopOverO and then StopOverO to tO. We don’t want to print out StopOverO twice, however, so the procedure does not print out the first or last vertex on the path and reserves that for the driver.
_______________________________________________________________________________
_______________________________________________________________________________
/* Print the path between S and T, except do not print */
/* the first or last vertex. Print a trailing " to " only. */
void
PrintPath1( TwoDArray Path, int S, int T ) {
int StopOver = Path[ S ][ T ];
if( S != T && StopOver != 0 ) {
PrintPath1( Path, S, StopOver );
printf( "%d to ", StopOver );
PrintPath1( Path, StopOver, T );
} }
/* Assume the existence of a Path of length at least 1 */
void
PrintPath( TwoDArray Path, int S, int T ) {
printf( "%d to ", S );
PrintPath1( Path, S, T );
printf( "%d", T ); NewLine( );
}
_______________________________________________________________________________
_______________________________________________________________________________
10.35 Many random number generators are poor. The default UNIX random number generator randO uses a modulus of the form 2bO, as does the VAX/VMS random number generator.
UNIX does, however, provide better random number generators in the form of random.O The Turbo random number generators, likewise, are also deficient. The paper by Park and Miller [44] discusses the random number generators on many machines.
10.38 If the modulus is a power of two, then the least significant bit of the "random" number oscillates. Thus FlipO will always return heads and tails alternately, and the level chosen for a skip list insertion will always be one or two. Consequently, the performance of the skip list will beΘ(NO) per operation.
10.39 (a) 25 ≡ 32 modO 341, 210 ≡ 1 modO341. Since 322 ≡ 1 modO 341, this proves that 341 is not prime. We can also immediately conclude that 2340 ≡ 1 modO341 by raising the last equation to the 34thO power. The exponentiation would continue as follows:
220 ≡ 1 modO341, 221 ≡ 2 modO341, 242 ≡ 4 modO341, 284 ≡ 16 modO341, 285 ≡ 32 modO341, 2170 ≡ 1 modO341, and 2340 ≡ 1 modO341.
(b) If AO = 2, then although 2560 ≡ 1 modO 561, 2280 ≡ 1 modO561 proves that 561 is not prime. If AO = 3, then 3561 ≡ 375 modO 561, which proves that 561 is not prime. AO = 4 obviously doesn’t fool the algorithm, since 4140 ≡ 1 modO 561. AO = 5 fools the algorithm:
51 ≡ 5 modO 561, 52 ≡ 25 modO 561, 54 ≡ 64 modO561, 58 ≡ 169 modO561, 516 ≡ 511 modO561, 517 = 311 modO561, 534 ≡ 229 modO561, 535 ≡ 23 modO561, 570 ≡ 529 modO561, 5140 ≡ 463 modO561, 5280 ≡ 67 modO561, 5560 ≡ 1 modO561.
10.41 The two point sets are {0, 4, 6, 9, 16, 17} and {0, 5, 7, 13, 16, 17}.
10.42 To find all point sets, we backtrack even if Found == true, and print out information when line 2 is executed. In the case where there are some duplicate distances, it is possi-ble that several symmetries will be printed.
10.43
10.44 Actually, it implements both; AlphaO is lowered for subsequent recursive calls on line 9 when ValueO is changed at line 12. BetaO is used to terminate the whileO loop when a line of play leads to a position so good that it is obvious the human player (who has already seen a more favorable line for himself or herself before making this call) won’t play into it. To implement the complementary routine, switch the roles of the human and com-puter. Lines that change are 3 and 4 (obvious changes), 5 (replace AlphaO with BetaO), 6 (replace *ValueO<BetaO with *ValueO>AlphaO), 8 (obvious), 9 (replace HumanO . . . *ValueO,BetaO with CompO . . . AlphaO,*ValueO), and 11 (obvious).
10.46 We place circles in order. Suppose we are trying to place circlePjO, of radius rPjO. If some circle iO of radius riO is centered at xiO, then PjO is tangent to iO if it is placed at xiO + 2
√MMMM
riOrsPjO.To see this, notice that the line connecting the centers has length riO + rPjO, and the difference in yO-coordinates of the centers is O|OrPjO − riOO|O. The difference in xO-coordinates follows from the Pythagorean theorem.
To place circlePjO, we compute where it would be placed if it were tangent to each of the firstPjO−1 circles, selecting the maximum value. If this value is less than rPjO, then we place circlePjO at xPjO. The running time is OO(NO2).
10.47 Construct a minimum spanning tree TO of GO, pick any vertex in the graph, and then find a path in TO that goes through every edge exactly once in each direction. (This is done by a depth-first search; see Exercise 9.31.) This path has twice the cost of the minimum span-ning tree, but it is not a simple cycle.
Make it a simple cycle, without increasing the total cost, by bypassing a vertex when it is seen a second time (except that if the start vertex is seen, close the cycle) and going to the next unseen vertex on the path, possibly bypassing several vertices in the process. The cost of this direct route cannot be larger than original because of the triangle inequality.
If there were a tour of cost KO, then by removing one edge on the tour, we would have a minimum spanning tree of cost less than KO (assuming that edge weights are positive).
Thus the minimum spanning tree is a lower bound on the optimal traveling salesman tour.
This implies that the algorithm is within a factor of 2 of optimal.
10.48 If there are two players, then the problem is easy, so assume kO>1. If the players are num-bered 1 through NO, then divide them into two groups: 1 through NO / 2 and NO / 2+1 though NO. On the iOthOday, for 1 ≤ iO ≤ NO / 2, player pO in the second group plays players ((pO + iO) modO NO / 2) + 1 in the first group. Thus after NO / 2 days, everyone in group 1 has played everyone in group 2. In the last NO / 2−1 days, recursively conduct round-robin tournaments for the two groups of players.
10.49 Divide the players into two groups of sizeOINO / 2OK and OHNO / 2OJ, respectively, and recur-sively arrange the players in any order. Then merge the two lists (declare that pxO > pyO if xO has defeated yO, and pyO > pxO if yO has defeated xO−exactly one is possible) in linear time as is done in mergesort.
10.50
10.52 (a) Use dynamic programming. Let SkO=the best setting of words wkO, wkO+1, ... wNO, UkO= the ugliness of this setting, and lkO=for this setting, (a pointer to) the word that starts the second line.
To compute SkO−1, try putting wkO−1, wkO, ..., wMO all on the first line for kO < MO and
iO=k
Σ
MO−1wiO< LO. Compute the ugliness of each of these possibilities by, for each MO, comput-ing the ugliness of settcomput-ing the first line and addcomput-ing UmO+1. Let M'O be the value of MO that yields the minimum ugliness. Then UkO−1 = this value, and lkO−1 = M'O + 1. Compute values of UO and lO starting with the last word and working back to the first. The minimum ugliness of the paragraph is UO1; the actual setting can be found by starting at lO1 and following the pointers in lO since this will yield the first word on each line.(b) The running time is quadratic in the case where the number of words that can fit on a line is consistently Θ(NO). The space is linear to keep the arrays UO and lO. If the line length is restricted to some constant, then the running time is linear because only OO(1) words can go on a line.
(c) Put as many words on a line as can fit. This clearly minimizes the number of lines, and hence the ugliness, as can be shown by a simple calculation.
10.53 An obvious OO(NO2) solution to construct a graph with vertices 1, 2, ..., NO and place an edge (vO,wO) in GO iff avO < awO. This graph must be acyclic, thus its longest path can be found in time linear in the number of edges; the whole computation thus takes OO(NO2) time.
Let BESTO(kO) be the increasing subsequence of exactly kO elements that has the minimum last element. Let tO be the length of the maximum increasing subsequence. We show how to update BESTO(kO) as we scan the input array. Let LASTO(kO) be the last element in BESTO(kO). It is easy to show that if iO<PjO, LASTO(iO) < LASTO(PjO). When scanning aMO, find the largest kO such that LASTO(kO) < aMO. This scan takes OO(log tO) time because it can be performed by a binary search. If kO = tO, then xMO extends the longest subsequence, so increase tO, and set BESTO(tO) and LASTO(tO) (the new value of tO is the argument). If kO is 0 (that is, there is no kO), then xMO is the smallest element in the list so far, so set BESTO(1) and LASTO(1), appropriately. Otherwise, we extend BESTO(kO) with xMO, forming a new and improved sequence of length kO+1. Thus we adjust BESTO(kO+1) and LASTO(kO+1).
Processing each element takes logarithmic time, so the total is OO(NOlog NO).
10.54 Let LCSO(AO, MO, BO, NO) be the longest common subsequence of AO1, AO2, ..., AMO and BO1, BO2, ..., BNO. If either MO or NO is zero, then the longest common subsequence is the empty string. If xMO = yNO, then LCSO(AO, MO, BO, NO) = LCSO(AO, MO−1, BO, NO−1),AMO. Otherwise, LCSO(AO, MO, BO, NO) is either LCSO(AO, MO, BO, NO−1) or LCSO(AO, MO−1, BO, NO), whichever is longer. This yields a standard dynamic programming solution.
10.56 (a) A dynamic programming solution resolves part (a). Let FITSO(iO, sO) be 1 if a subset of the first iO items sums to exactly sO; FITSO(iO, 0) is always 1. Then FITSO(xO, tO) is 1 if either FITSO(xO − 1, tO − axO) or FITSO(xO − 1, tO) is 1, and 0 otherwise.
(b) This doesn’t show that PO = NPO because the sizeO of the problem is a function of NO and log KO. Only log KO bits are needed to represent KO; thus an OO(NKO) solution is exponential in the input size.
10.57 (a) Let the minimum number of coins required to give xO cents in change be COINO(xO);
COINO(0) = 0. Then COINO(xO) is one more than the minimum value of COINO(xO − ciO), giving a dynamic programming solution.
(b) Let WAYSO(xO, iO) be the number of ways to make xO cents in change without using the first iO coin types. If there are NO types of coins, then WAYSO(xO, NO) = 0 if xO ≠ 0, and WAYSO(0, iO) = 1. Then WAYSO(xO, iO − 1) is equal to the sum of WAYSO(xO − pciO, iO), for integer values of pO no larger than xO/ciO (but including 0).
10.58 (a) Place eight queens randomly on the board, making sure that no two are on the same row or column. This is done by generating a random permutation of 1..8. There are only 5040 such permutations, and 92 of these give a solution.
10.59 (a) Since the knight leaves every square once, it makes BO2 moves. If the squares are alternately colored black and white like a checkerboard, then a knight always moves to a different colored square. If BO is odd, then so is BO2, which means that at the end of the tour the knight will be on a different colored square than at the start of the tour. Thus the knight cannot be at the original square.
10.60 (a) If the graph has a cycle, then the recursion does not always make progress toward a base case, and thus an infinite loop will result.
(b) If the graph is acyclic, the recursive call makes progress, and the algorithm ter-minates. This could be proved formally by induction.
(c) This algorithm is exponential.