Horowitz and Sahani, Fundamentals of Computer Algorithms, 2ND Edition PDF
Horowitz and Sahani, Fundamentals of Computer Algorithms, 2ND Edition PDF
ol1 nl a for example. n is the smallest value in »—1]. The amount of stack space needed can be reduced to O(logn) which is less than 2 log n. As remarked in Section 3.4, InsertionSort is exceedingly fast for n less than about 16. Hence InsertionSort can be used to speed up QuickSort2 whenever g—p < 16. The exercises explore various possibilities for selection of the partition element. 3.5.1 Performance Measurement QuickSort and MergeSort were evaluated on a SUN workstation 10/30. In both cases the recursive versions were used. For QuickSort the Partition fu ry out the median of three rule (i.e. the partitioning clement was the median of alm], all (m+p—1)/2|] and alp—1]). Each data ed of random integers in the range (0, 1000). Tables 3.5 and 3.6 record the actual computing times in milliseconds. Table 3.5 displays the average computing times. For each n, 50 random data sets were used. Table 3.6 shows the worst-case computing times for the 50 data Scanning the tables, we immediately see that QuickSort is faster than MergeSort for all values. Even though both algorithms require O(n logn) time on the average, QuickSort usually performs well in practice. The exer- cises «liscuss other tests that would make useful comparisons. 3.5.2 Randomized Sorting Algorithms Though algorithm QuickSort has an average time of O(n log n) on n elements, its worst-case time is O(n”). On the other hand it does not make use of any160 CHAPTER 3. DIVIDE-AND-CONQUER 1 Algorithm QuickSort2(p, q) 2 // Sorts the elements in alp : g] 3 4 // stack is a stack of size 2log(n) 5 repeat 6 in while (p 5. But 5 is not a magic number; in the machine employed, this seems to give the best results. In general this number should be determined empirically. 1 Algorithm RQuickSort(p, q) 2 // Sorts the elements alp],... ,a{g] which reside in the global 3 // array a[1: n] into ascending order. a[n +1] is considered to 4 // be defined and must be > all the elements in a{1 : 5 6 if (p5) then 9 Interchange(a,Random() mod (q— p+ 1) +p,p)3 10 n(a,p.q + 1); 1 is the position of the partitioning element 12 RQuickSort(p, j — 1)3 13 RQuickSort(j + 1,q)5 14 15 } Algorithm 3.15 Randomized quick sort algorithm The proof of the fact that RQuickSort has an expected O(n logn) time is the same as the proof of the average time of QuickSort. Let A(n) be the average time of RQuickSort on any input of n elements, Then the number of elements in the second part will be 0,1,2,....m—2, or n—1, all with an equal probability of 1 (in the probability space of outcomes for the randomizer) Thus the recurrence relation for A(n) will be A(n) = 2 SD (Ae-1) + Anh) #41 1Skhen This is the same as Equation 3.4, and hence its solution is O(n logn). RQuickSort and QuickSort (without employing the median of three ele- ments rule) were evaluated on a SUN 10/30 workstation. Table 3.7 displays3.5. QUICKSORT 163 the times for the two algorithms in milliseconds averaged over 100 runs. For each 7, the input considered was the sequence of numbers 1,2,...,n._ As we can sce from the table, RQuickSort performs much better than QuickSort. Note that the times shown in this table for QuickSort are much more than the corresponding entries in Tables 3.5 and 3.6, ‘The reason is that Quick- Sort makes @(n2) comparisons on inputs that are already in sorted order. However. on random inputs its average performance is very good. n 1000 | 2000 | 3000 | 4000 | 5000 | QuickSort [195.5 | 759.2] 1728 [3165 | 4829 [RQuickSort [9.4 [21.0 [305 | 416 | 528 Table 3.7 Comparison of QuickSort and RQuickSort ou the input afi] = i, 1 é,}. After having partitioned X into s+1 parts, we sort each part recursively. For a proper choice of s, the number of comparisons made in this algorithm is only nlogn + 0(nlogn) Note the constant 1 before nlogn. We see in Chapter 10 that this number is very close to the information theoretic lower bound for sorting. Choose s = j35- The and comparisons if we u: sample clements in an array, say 6[ ], for each « € X, we can determine which part X; it belongs to in < logn comparisons using binary search on 6{ ]. Thus the partitioning process takes n log n + O(n) comparisons. In the exercises you are asked to show that with high probability the cardinality164 CHAPTER 3. DIVIDE-AND-CONQUER Algorithm RSort(a,7) // Sort the elements a{1 : nj. Randomly sample s elements from al ]s Sort this sample; Partition the input using the sorted sample as partition keys; Sort each part separately; wryoakenre Algorithm 3.16 A randomized algorithm for sorting of each X; is no more than O("logn) = O(log?n). Using HeapSort or MergeSort to sort each of the X;’s (without employing recursion on any of them), the total cost of sorting the X;’s is a4 oH YE OUK Hog |Xsl) = max {log Xi} 1° OUD ist j, then the kth-smallest element is the (k — j)th-smallest element in alj +1: n]. The resulting algorithm ig function Select1 (Algorithm 3.17). This function places the kth-smallest element into position a[k] and partitions the remaining elements so that afi] < alk], 1alk], k alk] fork 0, such that TKn) < ens +l YS Thlm-)+ Y Tie@-v), n>2 Mk Reign So, R(n) < ont A max {¥} km-)+ Y RE-Y} 1stek kien Fi nt not Rin) < ent amex { SY R)+ HRW}, n>2 (3.8) nok F We assume that ¢ is chosen such that R(1) < ¢ and show, by induction on n, that R(n) < 4en. Induction Base: For n = 2, (3.8) gives Rin) < 20+ } max {R(1), R()} < 2.5¢ < 4en Induction Hypothesis: Assume R(n) < den for all n,2 2, the elements of C’ can be computed using matrix multiplication and addition operations applied to matrices of size n/2 x n/2. Since n is a power of 2, these matrix products can be recur- wvely computed by the same algorithm we are using for the n x n case. This algorithm will continue applying itself to smaller-sized submatrices until n becomes suitably small (n = 2) so that the product is computed directly. To compute AB using (3.12), we need to perform eight multiplications of n/2 x n/2 matrices and four additions of n/2 x n/2 matrices. Since two n/2xn/2 matrices can be added in time cn? for some constant c, the overall computing time T(n) of the resulting divide-and-conquer algorithm is given by the recurrence elements are typically T(n) = b n<2 ™=\ 8T(n/2)+en? n>2 where b and c are constants. This recurrence can be solved in the same way as earlier recurrences to obtain T(n) = O(n*). Hence no improvement over the conventional method has been made. Since matrix multiplications are more expensive than matrix additions (O(n3) versus O(n2)), we can attempt to reformulate the equations for Ciz so as to have fewer multiplications and possibly more additions. Volker Strassen has discovered a way to compute the Cjj’s of (3.12) using only 7 multiplications and 18 additions or subtractions. His method involves first computing the seven n/2 x n/2 matrices P, Q, R, S, T, U, and Vas in (3.13). Then the Cij’s are computed using the formulas in (3.14). As can be seen, P, Q, R, S, T, U, and V can be computed using 7 matrix multiplications and 10 matrix additions or subtractions. The Cjj’s require an additional 8 additions or subtractions:3.7. STRASSEN’S MATRIX MULTIPLICATION P = (Au + Ax)(Bir + Boo) Q = (Aa + Av) Bi Ro = Ai(By2— Bn) 8 Az(Bo — Bu) T (An + Aiz) Boo U (Ag, — Ari)(Bui + Bia) Vo = (Az = A22)(Bai + Boe) P+S-T+V R+T Cy = Q+8 Cy = P+R-Q+U The resulting recurrence relation for T(n) is 4 b n<2 Tn) = { IT(n/2)+ar2 n>2 where a and 6 are constants. Working with this formula, we get Tn) = an?[l+7/4+ (7/4)? +--+ (7/4) 1] + FTL) en? (7/4)!82" + 7°82", © a constant logs 4+loge 7log24 4 ql027 ) = O(n?*!) IA en O(n: EXERCISES Verify by hand that Equations 4 for Cri, C12, Cai, and C22. 3 and 181 (3.13) (3.14) (3.15) 3.14 yield the correct values 2. Write an algorithm that multiplies two n x n matrices using O(n*) op- erations. Determine the precise number of multiplic and array element accesses. 3. IE is a nonnegative constant, then prove that the recurrence tn) =f n=1 Y= 1 37(n/2)+kn n>1 has the following solution (for na power of 2): T(n) = 3kn!°%* — 2kn ions, additions, (3.16) (3.17)184 CHAPTER 3. DIVIDE-AND-CONQUER Figure 3.6 Convex hull: an example (1) obtain the vertices of the convex hull (these vertices are also called ex- treme points), and (2) obtain the vertices of the convex hull in some order (clockwise, for example). Here is a simple algorithm for obtaining the extreme points of a given set 5 of points in the plane. To check whether a particular point p € S is extreme, look at each possible triplet of points and see whether p lies in the triangle formed by these three points. If p lies in any such triangle, it is not extreme; otherwise it is. Testing whether p lies in a given triangle can be done in O(1) time (using the methods described in Section 3.8.1). Since there are @(n*) possible triangles, it takes @(n°) time to determine whether a given point is an extreme point or not. Since there are n points, this algorithm runs in a total of ©(n*) time. ing divide-and-conquer, we can solve both versions of the convex hull problem in @(7 log n) time. We develop three algorithms for the convex hull in this section, The first has a worst-case time of @(n?) whereas its aver- age time is @(nlogn). This algorithm has a divide-and-conquer structure similar to that of QuickSort. The second has a worst-case time complexity of O(nlogn) and is not based on divide-and-conquer. The third algorithm is based on divide-and-conquer and has a time complexity of (nlogn) in the worst case. Before giving further details, we digress to discuss some primitive geometric methods that are used in the convex hull algorithms. 3.8.1 Some Geometric Primitives Let A be an n x n matrix whose elements are {aj}, 1 1 Consider the directed line segment (p,,p2) from some point p1 = (21.41) to some other point pz = (1r2, y2). If q = (0r3,ys) is another point, we say q is to the left (right) of (p1,p2) if the angle ppoq is a left (right) turn. (An angle is said to be a left (right) turn if it is less than or equal to (greater than or equal to) 180°.) We can check whether q is to the left (right) of (p1,p2) by evaluating the determinant of the following matrix: vy} ty £3 oe cesses If this determinant is positive (negative), then q is to the left (right) of (p1,p2). If this determinant is zero, the three points are colinear. This test can be used, for example, to check whether a given point p is within a triangle formed by three points, say p;,p2. and py (in clockwise order). The point p is within the triangle iff p is to the right of the line segments (p;,p2), (p2,ps), aud (p3,p1)- Also, for any three points (1,41), (2+y2). and (x. formed by the corresponding triangle is given by one- determinant. Let. py, p2,..+.Pn be the verti order. Let p be any other point. It ). the signed area If of the above of the convex polygon Q in clockwi desired to check whether p li in the interior of Q or outside. Consider a horizontal line h that extends from —oe to oo and goes through p. There are two possibilities: (1) h does not intersect. any of the edges of Q, (2) h intersects some of the edges of Q. If case (I) is true, then, p is outside Q. In case (2), there can be at most two points of intersection. If h intersects Q at a single point, it is counted as two. Count the number of points of intersections that are to the left of p. If this number is even, then p is external to Q; otherwise it is internal to Q This method of checking whether p is interior to Q takes @(n) time. 3.8.2 The QuickHull Algorithm An algorithm that is similar to QuickSort can be devised to compute the convex hull of a set X of n points in the plane. This algorithm, called QuickHull, first identifies the two points (call them p; and pa) of X with the sinallest and largest -coordinate values. Assume now that there are no ties. Later we see how to handle ties, Both p; and p» are extreme points and part of the convex hull. The set X is divided into X; and Xz so that186 CHAPTER 3. DIVIDE-AND-CONQUER X; has all the points to the left of the li gment (p),p2) and X2 has all the points to the right of (p1,p2). Both X; and X include the two points p: and py. Then, the convex bulls of X; and X» (called the upper hull and lower hull, respectively) are computed using a divide-and-conquer algorithm called Hull. The union of these two convex hulls is the overall convex hull. If there is more than one point with the smallest «-coordinate, let p, and pi be the points from among these with the least and largest y-coordinates, respectively. Similarly define p', and pf for the points with the largest x- coordinate values. Now X1 will be all the points to the left of (p/p) (including p'’ and p'!) and X2 will be all the points to the right of (p},p)) (including p{, and p5). In the rest of the discussion we assume for simplicity that there are no ties for p; and pz. Appropriate modifications are needed in the event of ties. now describe how Hull computes the convex hull of X;. We determine a point of X, that belongs to the convex hull of X1 and use it to partition the problem into two independent subproblems. Such a point is obtained by computing the area formed by p,,p, and p» for each p in X, and picking the one with the largest (absolute) area. Ties are broken by picking the point p for which the angle ppip2 is maximum. Let ps be that point. Now X; is divided into two parts; the first part contains all the points of X, that are to the left of (p1,ps) (including p; and ps), and the second part contains all the points of X; that are to the left of (p3, p2) (including py and pz) (see Figure 3.7). There cannot be any point of X, that is to the left of both (p1,p3) and (p3,p2). Also, all the other points are interior points and can be dropped from future consideration. The convex hull of each part is computed recursively, and the two convex hulls are merged easily by placing one next to the other in the right order. If there are m points in X), we can identify the point of division ps in time O(m). Partitioning X; into two parts can also be done in O(m) time. Merging the two convex hulls can be done in time O(1). Let T(m) stand for the run time of Hull on a list of m points and let 1m and mg denote the sizes of the two resultant parts. Note that m; +m: , all of which are left turns. The final hull obtained is p, 1,3, 4,6,9, and 10, which are points on the hull in counterclockwise (cew) order. a is given in Algorithm 3.21. In this algorithm the set of s a doubly linked list ptslist. Function Scan runs in O(n) triplet examined, either the scan moves one node ahead or one point gets removed. In the latter case, the scan moves one node back. Also note that for each triplet, the test as to whether a left or right turn is formed can be done in O(1) time. Function Area computes the signed area formed by three points. The major work in the algorithm is in sorting the points. Since sorting takes O(n log n) time, the total time of Graham's scan algorithm is O(n log). 3.8.4 An O(nlogn) Divide-and-Conquer Algorithm In this section we present a simple divide-and-conquer algorithm, called DCHull, which also takes O(nlogn) time and computes the convex bull in clockwise order.3.8. CONVEX HULL 189 point. = record{ float .r; float y; point «prev; point *next; Algorithm Scan(list) // list is a pointer to the first node in the input list. ap = list; «pl := list; repeat { p2:= (pl — next); if (p24 nest) £0) then p3 else return; // End of the list temp := Area((pl + x). (pl + y). (p24 2), (p2 + y), (p3 + (p3 + y))s if (temp > 0.0) then pl := (pl + next); // If pl, p2,p3 form a left turn, move one point ahead: // Xf not, delete p2 and move back. else (p2 + next); (pl + newt) := p3; (p3 + prev) := pl; delete p2; pl:= (pl prev); } until (false); Algorithm ConvexHull(ptslist) { // ptslist is a pointer to the first item of the input list. Find 7/ the point p in ptslist of lowest y-coordinate. Sort the 7/ points according to the angle m Sort(pislist); Scan(ptslist); PrintList(ptslist)s Algorithm 3.21 Graham's scan algorithm190 CHAPTER 3. DIVIDE-AND-CONQUER Given a set X of n points, like that in the case of QuickHull, the problem is reduced to finding the upper hull and the lower hull separately and then putting them together. Since the computations of the upper and lower hulls very similar, we restrict our discussion to computing the upper hull. The divide-and-conquer algorithm for computing the upper hull partitions X into two nearly equal halves. Partitioning is done according to the x-coordinate values of points using the median ¢-coordinate as the splitter (see Section 3.6 for a discussion on median finding). Upper hulls are recursively computed for the two halves. These two hulls are then merged by finding the line of tangent ( a straight line cting a point each from the two halve such that all the points of X are on one side of the line) (see Figure 3.9). Figure 3.9 Divide and conquer to compute the convex hull To begin with, the points p; and py are identified [where p; (pz) is the point with the least (largest) «-coordinate value]. This can be done in O(n) time. Ties can be handled in exactly the same manner as in QuickHull. So, assume that there are no ties. All the points that are to the left of the line segment (p1,p2) are separated from those that are to the right. This separation also can be done in O(n) time. From here on, by input” and *X” we mean all the points that are to the left of the line segment (p1,p2). Also let |X| = N. Sort the input points according to their x-coordinate values. Sorting, can be done in O(N log N) time. This sorting is done only once in the computation of the upper hull. Let q1,q2,-.-.qiv be the sorted order of these3.8. CONVEX HULL 191 points. Now partition the input into two equal halves with q1,q2,...,@N/2 in the first half and qyjo41.dyyo42+++--dy in the second half. The upper hull of each half is computed recursively. Let H; and H be the upper hulls. Upper hulls are maintained as linked lists in clockwise order. We refer to the first element in the list as the leftmost point and the last element as the rightuiost point The line of tangent is then found in O(log? N) time. If (u,v) is the line of tangent, then all the points of H; that are to the right of u are dropped, Similarly, all the points that are to the left of v in Hz are dropped. The remaining part of Hy, the line of tangent, and the remaining part of Ho form the upper hull of the given input set. If 7(N) is the run time of the above recursive algorithm for the upper hull on an input of NV points, then we have T(N) = 2T(N/2) + O(log? N) which solves to P(N) = O(.V). Thus the run time is dominated by the initial sorting step. The only part of the algorithm that remains to be specified is how to find the line of tangent (u,v) in O(log? N) time, The way to find the tangent is to start from the middle point, call it p, of Hy. Here the middle point refers to the middle element of the corresponding list. Find the tangent of p with Hy, Let (p,q) be the tangent. Using (p,q), we can determine whether u is to the left of, equal to, or to the right of p in Hy, A binary search in this fashion on the points of H reveals u. Use a similar procedure to isolate v. Lemma 3.1 Let Hy and Hy be two upper hulls with at most m points each. If p is any point of Hy, its point q of tangency with Hy can be found in O(log m) time. Proof. If q' is any point in Hj, we can check whether q' is to the left of, equal to, or to the right of q in O(1) time (see Figure 3.10). In Figure 3.10, wx and y are the left and right neighbors of q' in Hy. respectively. If Zpq's is aright. turn and dpq'y is a left turn, then q is to the right of q' (see case 1 of Figure 3.10). If Zpq'e and /pq'y are both right turns, then q/ = q (see case 2 of Figure 3.10); otherwise q is to the left of q/ (see case 3 of Figure 3.10). ‘Thus we can perform a binary search on the points of Hy and identify q in O(log m) time. a Lemma 3.2 If H; and H2 are two upper hulls with at most m points each, their common tangent can be computed in O(log” m) time. Proof. Let u € Hy and v € Hp be such that (u,v) is the line of tangent. Also let p be an arbitrary point of Hy and let q € Hz be such that (p,q) is a192 CHAPTER 3, DIVIDE-AND-CONQUER. case | case 3 Figure 3.10 Proof of Lemma 3.1 tangent of Hz. Given p and q, we can check in O(1) time whether w is to the left of, equal to, or to the right of p (see Figure 3.11). Here x and y are left and right neighbors, respectively, of p in Hy. If (p,q) is also tangential to Ay, then p =u. If Zxpq is a left turn, then u is to the left of p; otherwise u is to the right of p. This suggests a binary search for u. For each point p of Hj chosen, we have to determine the tangent from p to Hy and then decide the relative positioning of p with respect to u. We can do this computation in O(logm x logm) = O(log? m) time. oO In summary, given two upper hulls with ¥ points each, the line of tangent can be computed in O(log? N) time. Theorem 3.4 A convex hull of n points in the plane can be computed in O(n log n) time. Oo3.9. REFERENCES AND READINGS 193 A, Figure 3.11 Proof of Lemma 3 EXERCISES 1. Write an algorithin in pseudocode that implements QuickHull and test t using suitable data. de the divide-and-conquer algorithm DCHull and test it using ap- propriate data. 3. Run the three algorithms for convex hull discussed in thi: ion on various random inputs and compare their performances. 4, Algorithm DCHull can be modified as follows: Instead of using the mnedian as the splitter, we could use a randomly chosen point as the splitter. Then X is partitioned into two around this point. The rest of the function DCHull is the same. Write code for this modified algorithm. and compare it with DCHull empirically. 5. Let $ be a set of n points in the plane. Tt is given that there is only a constant (say ¢) number of points on the hull of $. Can you devise a convex hull algorithm for $ that runs in time o(nlogn)? Conceive of special algorithms for ¢ = 3 and ¢ =4 first and then generalize. 3.9 REFERENCES AND READINGS Algorithm MaxMin (Algorithm 3.6) is due to I. Pohl and the quicksort algo- rithm (Algorithm 3.13) is due to C. A. R. Haore. The randomized sorting algorithin in Algorithm 3.16 is due to W. D. Frazer and A. C. McKeller and194 CHAPTER 3. DIVIDE-AND-CONQUER the selection algorithm of Algorithm 3.19 is due to M. Blum, R. Floyd, V. Pratt, R. Rivest and R. E. Tarjan. For more on randomized sorting and selection see: “Expected time bounds for selection,” by R. Floyd and R. Rivest, Commu nications of the ACM 18, no. 3 (1975): 165-172 “Samplesort: A Sampling Approach to Minimal Storage Tree Sorting,” by W. D. Frazer and A. C. McKellar, Journal of the ACM 17, no. 3 (1970): 496-507. “Derivation of Randomized Sorting and Selection Algorithms,” by S. Ra- jasekaran and J. H. Reif, in Parallel Algorithm Derivation and Program Transformation, edited by R. Paige, J. H. Reif, and R. Wachter, Kluwer Academic Publishers, 1993, pp. 187-205. ‘The matrix multiplication algorithm in Section 3.7 is due to V. Strassen. For more information on the matrix multiplication problem see “Matrix mul- tiplication via arithmetic progressions,” by D. Coppersmith and 8. Wino- grad, Journal of Symbolic Computation 9 (1990): 251-280. A complex O(n?) time algorithm for multiplying two n x n matrices is given in this paper. For more applications of divide-and-conquer see: Computational Geometry, by F. Preparata and M. I. Shamos, Springer Verlag, 1985. Computational Geometry: An Introduction Through Randomized Algorithms by K. Mulmuley, Prentice-Hall, 1994. Introduction to Algorithms: A Creative Approach, by U. Wesley, 1989. lanber, Addison- 3.10 ADDITIONAL EXERCISES 1. What happens to the worst-case rin time of quicksort if we use the median of the given keys as the splitter key? (Assume that the selection algorithm of Section 3.6 is employed to determine the median). 2. The sets A and B have n elements each given in the form of sorted arrays. Present an O(n) time algorithm to compute AUB and AN B. 3. The sets A and B have m and n elements (respectively) from a linear order. These sets are not necessarily sorted. Also assume that m > n). Pr worst case in time O (ni2£%) and checks whether all these n numbers each number is an integer in the ent an algorithm that runs in the are distinct. Your algorithm should use only O(n) space. Let S' be a sequence of n? integers in the range [1,n]. Let R(i) be the number of i's in the sequence (for i = 1,2,...,n). Given 8, we have to compute an approximate value of R(i) for each i. If N(j) is an approximation to R(i),i = 1,...,n, it should be the case that (with high probability) N(i) > R(i) for each i and DL, N(i) = O(n?). Of course we can do this computation in deterministic O(n”) time. Design a randomized algorithm for this problem that runs in time O(n log? n).Chapter 4 THE GREEDY METHOD 4.1 THE GENERAL METHOD ‘The greedy method is perhaps the most straightforward design technique we consider in this text, and what’s more it can be applied to a wide variety of problems. Most, though not all, of these problems have n inputs and require us to obtain a subset that satisfies some constraints. Any subset that satis- fies these constraints is called a feasible solution. We need to find a feasible solution that either maximizes or minimizes a given objective function. A feasible solution that does this is called an optimal solution. There is usu- ally an obvious way to determine a feasible solution but not necessarily an optimal solution. The greedy method suggests that one can devise an algorithm that works in stages, considering one input at a time. At each stage, a decision is made regarding whether a particular input is in an optimal solution. This is done by considering the inputs in an order determined by some selection proce- dure. If the inclusion of the next input into the partially constructed optimal solution will result in an infeasible solution, then this input is not added to the partial solution. Otherwise, it is added. The selection procedure itself is based on some optimization measure. This measure may be the objecti function. In fact, several different optimization measures may be plausible for a given problem. Most of these, however, will result in algorithms that generate suboptimal solutions. This version of the greedy technique is called the subset paradigm. We can describe the subset paradigm abstractly, but more pr above, by considering the control abstraction in Algorithm 4.1. ‘The function Select selects an input from a[] and removes it. The selected input’s value is assigned to x. Feasible is a Boolean-valued function that determines whether « can be included into the solution vector. The function Union combines x with the solution and updates the objective function. The isely than oe198 CHAPTER 4. THE GREEDY METHOD 1 Algorithm Greedy(a,n) 2 //a{l:n| contains the n inputs. 3 4 solution := 0; // Initialize the solution. 5 for i:=1 ton do 6 { 7 = Select (a); 8 if Feasible(solution, xr) then 9 solution := Union(solution, x); 10 } u return solution; 12} function Greedy d s a greedy algorithm will look, once a particular problem is chosen and the functions Select, Feasible, and Union are properly implemented. For problems that do not call for the s greedy method we make de dering the inputs in some order. Each decision is made using an optimization criterion that can be computed using decisions already made. Call this version of the greedy method the ordering paradigm. Sections 3, 4.4, and 4.5 consider problems that fit the subset paradigm, and Sec 4.7, and 4.8 consider problems that fit the ordering paradigm. tion of an optimal subset, in the EXERCISE 1. Write a control abstraction for the ordering paradigm. 4.2 KNAPSACK PROBLEM Let us try to apply the greedy method to solve the knapsack problem. We are given n objects and a knapsack or bag. Object é has a weight w; and the knapsack has a capacity m. If a fraction 27;, 0 <2; < 1, of object i is placed into the knapsack, then a profit of p;r; is earned. The objective is to obtain a filling of the knapsack that maximizes the total profit earned. Since the knapsack capacity is m, we require the total weight of all chosen objects to be at most m. Formally, the problem can be stated as4.2, KNAPSACK PROBLEM 199 maximize > pyri (4.1) Sten subject to S> wir; wyng < m T and 2; =Oorl, l 6, then the node u gets split and d(u) is set to zero. Computation proceeds from the leaves toward the root. In the tree of Figure 4.2, let 6 = 5. For each of the leaf nodes 7,8,5,9, and 10 the delay is zero. The delay for any node is computed only after the delays for its children have been determined. Let u be any node and C(u) be the set of all children of u. Then d(u) is given by d(u) = —- + w(u,v)} Using the above formula, for the tree of Figure 4.2, d(4) = 4. Since d(4) + w(2,4) = 6 > 6, node 4 gets split. We set d(4) = 0. Now d(2) can be4.3. TREE VERTEX SPLITTING 205, Figure 4.2 An example tree computed and is equal to 2. Since d(2) +1(1,2) exceeds 6, node 2 gets split and d(2) is set to zero. Then d(6) is equal to 3. Also, since d(6)+w(3,6) > 6, node 6 has to be split. Set d(6) to zero. Now d(3) is computed as 3. Finally, d(1) is computed as 5. Figure 4.3 shows the final tree that results after splitting the nodes 2, 4, and 6. This algorithm is described in Algorithm 4.3, which is invoked as TVS(root, 6), root being the root of the tree. The order in which TVS visits (i.e., computes the delay values of) the nodes of the tree is called the post order and is studied again in Chapter 6. Figure 4.3 The final tree after splitting the nodes 2, 4, and 6206 CHAPTER 4. THE GREEDY METHOD 1 Algorithm TVS(T, 6) 2 // Determine and output the nodes to be split. 3 // w() is the weighting function for the edges. 4 5 if (T #0) then 6 { 7 dt 8 for each child v of T do 9 10 TVS(u, 6)3 u d[T] = max{d{T], dlv] + w(T,»)}5 12 13 if ((F is not the root) and 4 (dT) + w(parent(T),T) > 5) then 15 16 write (T); d[T] = 0; 17 } 18 19 } Algorithm 4.3 The tree vertex splitting algorithm Algorithm TVS takes @(n) time, where n is the number of nodes in the tree. This can be seen as follows: When TVS is called on any node T, only a constant number of operations are performed (excluding the time taken for the recursive calls). Also, TVS is called only once on each node T in the tree. Algorithm 4.4 is a revised version of Algorithm 4.3 for the special case of directed binary trees. A sequential representation of the tree (see Section 2.2) has been employed. The tree is stored in the array tree[ ] with the root at tree[1]. Edge weights are stored in the array weight| ]. If tree[i] has a tree node, the weight of the incoming edge from its parent is stored in weight[i]. The delay of node i is stored in dji]. The array d[ | is initialized to zero at the beginning. Entries in the arrays tree| | and weight[ ] corresponding to nonexistent nodes will be zero. As an example, for the tree of Figure 4.2, tree{ ] will be set to {1,2,3,0,4,5,6,0,0,7,8,0,0,9, 10} starting at cell 1. Also, weight[ ] will be set to {0,4,2,0, 2, 1,3, 0,0, 1,4,0, 0,2,3} at the beginning, starting from cell 1. The algorithm is invoked as TVS(1, 6). Now. we show that TVS (Algorithm 4.3) will always split a minimal number of nodes.4.3. TREE VERTEX SPLITTING 207 Algorithm TVS(i, 6) // Determine and output a minimum cardinality split set. // The tree is realized using the sequential representation. // Root is at tree[1]. N is the largest number such that 1 2 3 4 5 Y/ ode. 6 7 8 bs if (tree[i] #0) then // If the tree is not empty if (21 > N) then d{i] := 0; // i is a leaf. else i 1ax(d[i], d[2i] + weight|2i]); a if (2i+1< N) then 15 TVS(2i +1, 8)3 16 d{i] := max(qfi), d[2i + 1] + weight[2i + 1])s 3 19 if ((tree[i] # 1) and (d{i] + weight[i] > 6)) then 21 write (¢ree{i]); di] : Algorithm 4.4 TVS for the special case of binary trees Theorem 4.2 Algorithm TVS outputs a minimum cardinality set U such that d(P/U) < 6 on any tree T, provided no edge of T has weight > 6. Proof: The proof is by induction on the number of nodes in the tree. If the has a single node, the theorem is true. Assume the theorem for all trees e |U|—1. In other words, |W’| > |U|. a EXERCISES 1. For the tree of Figure 4.2 solve the TVSP when (a) 6 = 4 and (b) 6=6. 2. Rewrite TVS (Algorithm 4.3) for general trees. Make use of pointers. 4.4 JOB SEQUENCING WITH DEADLINES We are given a set of n jobs. Associated with job ¢ is an integer deadline dj > 0 and a profit pj > 0. For any job i the profit p; is earned iff the job is completed by its deadline. To complete a job, one has to process the job on a machine for one unit of time. Only one machine is available for processing jobs. A feasible solution for this problem is a subset J of jobs such that each. job in this subset can be completed by its deadline. The value of a feasible solution J is the sum of the profits of the jobs in J, or Dicy pi. An optimal solution is a feasible solution with maximum value. Here problem involves the identification of a subset, it fits the subset paradigm. Example 4.2 Let n= 4, (p1,p2,ps; ps) = (100, 10, 15, 27) and (di, da, ds, da) (2,1,2,1). The feasible solutions and their values are: feasible processing solution sequence value 1 (1,2) 1 110 2 (14,3) 1, 30r3,1 115 3 (4) 4 127 ae (ea) oes 25 5. (3,4) 4,3 42 6 (ot 100 7 QQ) 2 10 8 (3) 8 15 9 (4) 4 QT Solution 3 is optimal. In this solution only jobs 1 and 4 are proc: the value is 127. These jobs must be processed in the order job 4 followed by job 1. Thus the processing of job 4 begins at time zero and that of job 1 is completed at time 2. a sed and4.4, JOB SEQUENCING WITH DEADLINES 209 ‘To formulate a greedy algorithm to obtain an optimal solution, we must formulate an optimization measure to determine how the next job is chosen. As a first attempt we can choose the objective function 37j.) pi as our op- timization measure. Using this measure, the next job to include is the oue that increases 7.) p; the most, subject to the constraint that the resulting J is a feasible sohition. This requires us to consider jobs in nonincreasing order of the p;’s. Let us apply this criterion to the data of Example 4.2. We begin with J =0 and Dy; p: =0. Job 1 is added to J as it has the largest profit and J = {1} is a feasible solution. Next, job 4 is considered. The solution J = {1,4} is also feasible. Next, job 3 is considered and discarded as J = {1,3,4} is not feasible. Finally, job 2 is considered for inclusion into J. It is discarded as J = {1,2,4} is not feasible. Hence, we are left with the solution J = {1,4} with value 127. This is the optimal solution for the given problem instance. Theorem 4.4 proves that the greedy algorithm just described always obtains an optimal solution to this sequencing problem. Before attempting the proof, let us see how we can determine whether a given JJ is a feasible solution. One obvious way is to try out all possible permutations of the jobs in J and check whether the jobs in J can be pro- cessed in any one of these permutations (sequences) without violating the deadlines. For a given pernmtation o = 71, %2,i3,.-., i, this is easy to do, since the earliest time job ig,1 < q < k, will be completed is q. If q > dj,. then using c, at least job i, will not be completed by its deadline, However, if |J| = i, this requires checking i! permutations. Actually, the feasibility of a set J can be determined by checking only one permutation of the jobs in J. This permutation is any one of the permutations in which jobs are ordered in nondecreasing order of deadlines. Theorem 4.3 Let J be a set of k jobs and o = it, i2,..., i, a permutation of jobs in J such that dj, < dj, <-+- < dj,. Then J is a feasible solution iff the jobs in J can be processed in the order o without violating any deadline. Proof: Clearly, if the jobs in J can be processed in the order o without violating any deadline, then J is a feasible solution. So, we have only to show that if J is feasible, then o represents a possible order in which the jobs can be processed. If J is feasible, then there exists o! = r1,72.....Tr such that d,, >, 1 a. In o! we can interchange rq and ry. Since d,, > dy,, the resulting permutation o” = 81,82,...)6 represents an order in which the jobs can be processed without violating a deadline. Continuing in this way, o! can be transformed into o without violating any deadline. Hence, the theorem is proved. oO ‘Theorem 4.3 is true even if the jobs have different processing times t; > 0 (see the exercises).210 CHAPTER 4. THE GREEDY METHOD Theorem 4.4 The greedy method described above always obtains an opti- mal solution to the job sequencing problem. Proof: Let (pi,d;),1 < ipy for all jobs b that are in J but not in J. To see this, note that if p, > pa, then the greedy method would consider job b before job a and include it into I. Now, consider feasible schedules $; and Sy for I and J respectively. Let. ibe a job such that i € I and i € J. Let i be scheduled from ¢ to +1 in S; and t' to t'+1lin Sj. Ift < t’, then we can interchange the job (if any) heduled in [¢’, #” + 1] in S; with i. If no job is scheduled in [¢’, t! + 1] in J, then i is moved to {#’, +1]. The resulting schedule is also feasible. If t’ < t, then a similar transformation can be made in $j. In this way, we can obtain ‘hedules S', and S', with the property that all jobs common to J and J are theduled at the same time. Consider the interval [tq,te + 1] in S} in which the job a (defined above) is scheduled. Let b be the job (if any) scheduled in S', in this interval. From the choice of a,pq > py. Scheduling a from ta to t; + 1 in $4 and discarding job b gives us a feasible schedule for job set J! = J~ {b}U {a}. Clearly, J’ has a profit value no less than that of J and differs from I in one less job than J does. By repeatedly using the transformation just described, J can be trans- formed into I with no decrease in profit value. So J must be optimal. 0 A high-level description of the greedy algorithm just discussed appears as Algorithm 4.5. This algorithm constructs an optimal set J of jobs that can be processed by their due times. The selected jobs can be processed in the order given by Theorem 4.3. Now, let us see how to represent the set J and how to carry out the test of lines 7 and 8 in Algorithm 4.5. Theorem 4.3 tells us how to determin whether all jobs in J U {i} can be completed by their deadlines. We avoid sorting the jobs in J each time by keeping the jobs in J ordered by deadlines. We can use an array d[1 : n] to store the deadlines of the jobs in the order of their p-values. The set J itself can be represented by a one- dimensional array J[1 : k] such that J[r], 1 py > ++: > pn. Further it assumes that'n > 1 and the deadline d{i] of job i is at least 1. Note that no job with d{i] <1 can ever be finished by its deadline. Theorem 4.5 proves that JS is a correct implementation of the greedy strategy. Theorem 4.5 Function JS is a correct implementation of the greedy-based method described above. Proof: Since d{i] > 1. the job with the largest p; will always be in the greedy solution. As the jobs are in nonincreasing order of the pj's, line 8 in Algorithm 4.6 includes the job with largest p;. ‘The for loop of line 10 considers the remaining jobs in the order required by the greedy method described earlier. At all times, the set of jobs already included in the solution is maintained in J. If J[i], 1 < i < k, is the set already included, then J is such that d[J[iJ] < d[J[i + 1], 1 dli], w w. This is verified in line 16 (note r = w on exit from the while loop if d[J[q]] 4a, w <41,1<1< nare the deadlin 3. // ave ordered such that pil] > pl2] > - 4 5 // is the ith job in the optimal solution, 1 <7 < k. 5 // Also, at termination d{J{i]] < d[J[i +1], 1 d{i]) and (d[J[r)] #r)) do r= 16 if ((d[J[r]] < d[i]) and (d{i] > r)) then V7 18 // Insert i into J[]. 19 ‘0 (r +1) step —1 do J[qg+1 20 i; kth 21 22 23 return k; 24 } Algorithm 4.6 Greedy algorithm for sequencing unit time jobs with dead- lines and profits For JS there are two possible parameters in terms of which its complexity can be measured. We can use n, the number of jobs, and s, the number of jobs included in the solution J. The while loop of line 15 in Algorithm 4.6 is iterated at most h times. Fach iteration takes @(1) time. If the c of line 16 is true, then lines 19 and 20 are executed. These ©(k — r) time to insert job i. Hence, the total time for each iteration of the for loop of line 10 is ©(k). This loop is iterated n — 1 times. If s is the final value of k, that is, s is the number of jobs in the final solution, then the total time needed by algorithm JS is @(sn). Since s pl2| > +++ > pln] and that 6 = min{n,max;(d{i))}. 4{ 5 // Initially there are b +1 single node trees. 6 0 to b do ffi]:= i; 7 0; // Initialize. 8 to n do 9 {// Use greedy rule, 10 q:= CollapsingFind(min(n, d{i])); ql if (f(q] #0) then 12 13 k= k+13 J[k]:= i; // Select job i. 14 m= CollapsingFind( f(g) 1) 15 WeightedUnion(rn, 4); 16 fla] := fim); // q may be new root. 7 } 18. 19 } Algorithm 4.7 Faster algorithm for job sequencing EXERCISES 1 You are given a set of n jobs. Associated with each job # i time #; and a deadline d; by which it must be complet schedule is a permutation of the jobs such that if the jobs are processed in that order, then each job finishes by its deadline, Define a greedy schedule to be one in which the jobs are processed in nondecreasing order of deadlines. Show that if there exists a feasible schedule, then all greedy schedules are feasible. [Optimal assignment] Assume there are n workers and n jobs. Let v¥jj be the value of assigning worker i to job j. An assignment of workers to jobs corresponds to the assignment of 0 or 1 to the variables aj, 1 < i, 3 0. 3. (a) What is the solution generated by the function JS when n = 7, (Piypa,.-+.P7) = (3,5,20, 18, 1,6,30), and (di.dg,....d7) = (1,3,4,3, 2,1, 2)? (b) Show that Theorem 4.3 is true even if jobs have different process- ing requirements. Associated with job i is a profit pj > 0, a time requirement t; > 0, and a deadline d; > ti. (c) Show that for the situation of part (a), the greedy method of this ss Id an optimal solution. 4. (a) For the job section, show that the subset J repre processed according to err job i in J hasn't been assigned a processing time, then assign it to the slot [a — 1, a], where a is the least integer r such that 1 cost{k, j])) 25 then near|k] := js 6 } 2 return mincost; 28 F Algorithm 4.8 Prim’s minimum-cost spanning tree algorithin222 CHAPTER 4. THE GREEDY METHOD 4 in with no edges ; he current graph with no edges selected. Edge (1,6) is the frst edge considered. It is inchuded in the spanning tree being built. This yields the graph of Figure 4.8(b). Next, the edge (3, 4) is selected and included in the tree (Figure 4.8(c)). The next edge to be considered is (2,7). Its inclusion in the tree being built does not create a cycle, so we get the graph of Figure 4.8(d). Edge (2,3) is considered next and included in the tree Figure 4.8(e). Of the edges not yet. considered, (7,4) has the least cost. It is considered next. Its inclusion in the tree results in a cycle, so this edge is discarded. Edge (5,4) is the next edge to be added to the tree being built. This results in the configuration of Figure 4.8(f). The next edge to be considered is the edge (7,5). It is discarded, as its inclusion creates a cycle. Finally, edge (6,5) is considered and inchided in the tree being built. This completes the spanning tree. The resulting tree (Figure 4.6(b)) has cost 99. a For clarity, Kruskal’s method is written out more formally in Algorithm 4.9. Initially E is the set of all edges in G. The only functions we wish to perform on th are (1) determine an edge with minimum cost (line 4) and (2) delete this edge (line 5). Both these functions can be performed efficiently if the edges in E are maintained as a sorted sequential list. It is not essential to sort all the edges so long as the next edge for line 4 can be determined easily. If the edges are maintained as a minheap, then the next edge to consider can be obtained in O(log |E|) time. The construction of the heap itself takes O(|E|) time. ‘To be able to perform step 6 efficiently, the vertices in G should be grouped together in such a way that one can easily determine whether the vertices » and w are already connected by the earlier selection of edges. If they are, then the edge (v,w) is to be discarded. If they are not, then (v, w) is to be added to #. One possible grouping is to place all vertices in the same connected component of t into a set (all connected components of t will also be trees). Then, two vertices 7 and w are connected in ¢ iff they are in the same set. For example, when the edge (2,6) is to be considered, the sets are {1,2}, {3,4,6}, and {5}. Vertices 2 and 6 are in different sets so these sets are combined to give {1,2,3,4,6} and {5}. The next edge to be considered is (1,4). Since vertices 1 and 4 are in the same set, the edge is rejected. The edge (3,5) connects vertices in different sets and results in the final span- ning tree. Using the set representation and the union and find algorithms of Section 2.5, we can obtain an efficient (almost linear) implementation of line 6. The computing time is, therefore, determined by the time for lines 4 and 5, which in the worst case is O(|E|log |E|). If the representations discussed above are used, then the pseudocode of Algorithm 4.10 results. In line 6 an initial heap of edges is constructed. In line 7 each vertex is assigned to a distinct set (and hence to a distinct tree). The set t is the set of edges to be included in the minimum-cost spanning4.5. MINIMUM-COST SPANNING TREES 223 (d) te) (f Figure 4.8 Stages in Kruskal’s algorithm tree and i is the number of edges in t. The set t can be represented as a sequential list using a two-dimensional array ¢[{1 : n—1, Edge (u,v) can be added to ¢ by the assignments ¢{i, 1] =u; and ¢{i, 2] = v3. In the while loop of line 10, edges are removed from the heap one by one in nondecreasing order of cost. Line 14 determines the sets containing u and v. If j # k, then su and v are in different sets (and so in different trees) and edge included into ¢, The sets containing u and v are combined (line 20) v, the edge (u,v) is discarded as its inclusion into £ would create a cycle. Line 23 determines whether a spanning tree was found. It follows that i # n — 1 iff the graph G is not connected. computing time is O(|E|log |E|), where E is the edge set of G. Theorem 4.6 Kruskal’s algorithm generates a minimum-cost spanning tree for every connected undirected graph G.224 CHAPTER 4, THE GRE) wer oakene t=O while ((¢ has less than n ~ 1 edges) and (E 0) do { Choose an edge (v, w) from E of lowest cost; Delete (v,w) from Es if (v,w) does not create a cycle in t then add (v,w) to 3 else discard (v,w) Algorithm 4.9 Early form of minimum-cost spanning tree algorithm due to Kruskal Algorithm Kruskal( £, cost, n,t) // Bis the set of edges in G. G has n vertices. cost[u, v] is the // cost of edge (u,v). t is the set of edges in the minimum-cost, // spanning tree. The final cost is returned. Construct @ heap out of the edge costs using Heapifys vertex isina different set. = 0.05 while ((i cost(q). Now, reconsider the graph with edge set E(t’) U {q}. Removal of any edge on the cycle q,¢1,€2,.« ill leave behind a tree ¢” (Exercise 5). In particular, if we delete the edge then the resulting tree ¢” will have a cost no more than the cost of f’ (as cost(e)) > cost(e)). Hence, t” is also a minimmum-cost tree. By repeatedly using the transformation described above, tree t! can be transformed into the spanning tree ¢ without any increase in cost. Henc is a minimum-cost spanning tree. 4.5.3 An Optimal Randomized Algorithm (+) Any algorithm for finding the minimum-cost spanning tree of a given graph G(V,E) will have to spend Q(|V| + |E|) time in the worst case, since it has to examine each node and each edge at least once before determining the correct answer. A randomized Las Vegas algorithm that runs in time O(\V| + |E\) can be devised as follows: (1) Randomly sample m edges from G (for some suitable m). (2) Let G’ be the induced subgraph; that is, G’ has V as its node set and the sampled edges in its edge set. ‘The subgraph G! need not be connected. Recursively find a minimum-cost spanning tree for each component of G'. Let F be the resultant minimum-cost spanning forest of G'. (3) Using F, eliminate certain edges (called the F-heavy ed of G that cannot possibly be in a minimum-cost spanning tree. Let G" be the graph that results from G after elimi: (4) Recursively find a minimum-cost spanning will also be a minimum-cost spanning tree for G. Steps 1 to 3 are useful in reducing the number of edges in G. The al- gorithm can be speeded up further if we can reduce the number of nod in the input graph as well. Such a node elimination can be effected using the Borivka steps. In a Borivka step, for each node, an incident edge with minimum weight is chosen. For example in Figure 4.9(a), the edge (1,3) is226, CHAPTER 4. THE GREEDY METHOD chosen for node 1, the edge (6,7) is chosen for node 7, and so on. All the chosen edges are shown with thick lines. The connected components of the induced graph are found. In the example of Figure 4.9(a), the nodes 1, 2, and 3 form one component, the nodes 4 and 5 form a second component, and the nodes 6 and 7 form another component. Replace each component with a single node, The component with nodes 1, 2, and 3 is replaced with the node a. The other two components are replaced with the nodes b and c. respectively. Edges within the individual components are thrown away. The resultant graph is shown in Figure 4.9(b). In this graph keep only an edge of minimum weight between any two nodes. Delete any isolated nodes. Since an edge is chosen for every node, the number of nodes after one Borvka step reduces by a factor of at least two. A minimum-cost span- ning tree for the reduced graph can be extended easily to get a minimum- cost spanning tree for the original graph. If £! is the set of edges in the minimum-cost spanning tree of the reduced graph, we simply include into E' the edges chosen in the Bortvka step to obtain the minimum-cost span- ning tr ges for the original graph. In the example of Figure 4.9, a minimum-cost spanning tree for (c) will consist of the edges (a,b) and (b, ¢) Thus a minimum-cost spanning tree for the graph of (a) will have the edge: (1,3), (3, 2), (4,5), (6,7), (3,4), and (2,6). More details of the algorithms are given below. Definition 4.2 Let F bea forest that forms a subgraph of a given weighted graph G(V, £). Ifu and v are any two nodes in F, let F(u,v) denote the path (if any) connecting u and v in F and let Feost(u,v) denote the maximum weight of any edge in the path F(u,v). If there is no path between u and v in F, Feost(u,v) is taken to be oo. Any edge (z,y) of G is said to be F-heavy if cost|,y] > Feost(x,y) and F-light otherwise, a Note that all the edges of F are F-light. Also, any F-heavy edge cannot belong to a minimum-cost spanning tree of G. ‘The proof of this is left as an exercise. The randomized algorithm applies two Borivka steps to reduce the number of nodes in the input graph. Next, it samples the edges of G and processes them to eliminate a constant fraction of them. A minimum-cost spanning tree for the resultant reduced graph is recursively computed. From this tree, a spanning tree for G is obtained. A detailed description of the algorithm appears as Algorithm 4.11. Lemma 4.3 states that Step 4 can be completed in time O(|V| + |E}) The proof of this can be found in the references supplied at the end of this chapter. Step 1 takes O(/V|+|E|) time and step 2 takes O(|B|) time. Step 6 takes O(|E|) time as well. The time taken in all the recursive calls in steps 3 and 5 can be shown to be O(|V|+|E]). For a proof, see the references at the end of the chapter. A crucial fact that is used in the proof is that both the number of nodes and the mumber of edges are reduced by a constant factor, with high probability, in each level of recursion,4.5. MINIMUM-COST SPANNING TREES 227 (b) (c) Figure 4.9 A Borivka step Lemma 4.3 Let G(V,£) be any weighted graph and let F be a subgraph of G that forms a forest. Then, all the F-heavy edges of G can be identified in time O(\V| + |Z). a Theorem 4.7 A minimum-weight spanning tree for any given weighted graph can be computed in time O(|V| + |E|) a EXERCISES 1. Compute a minimum cost spanning tree for the graph of Figure 4.10 using (a) Prim’s algorithm and (b) Kruskal’s algorithm. Prove that Prim’s method of this section generates minimum-cost spanning trees,228 CHAPTER 4. THE GREEDY METHOD Step 1. Apply two Boriivka steps. At the end, the number of nodes will have decreased by a factor at least 4. Let the resultant graph be G(V, ) Step 2. Form a subgraph G'(V', B") of G, where each edge of G is chosen randomly to be in E’ with probability . The expected number of edges in B’ is #1 Step 3. Recursively find a minimum-cost spanning forest F’ for a. Step 4. Eliminate all the F-heavy edges from G. With high probability, at least a constant fraction of the edges of G will be eliminated. Let G” be the resultant graph. Step 5. Compute a minimum-cost spanning tree (call it T”) for G" recursively. The tree T" will also be a minimum-cost spanning tree for G. Step 6. Return the edges of T” together with the edges chosen in the Boriivka steps of step 1. These are the edges of a minimum- cost spanning tree for G. Algorithm 4.11 An optimal randomized algorithm 3. (a) Rewrite Prim’s algorithm under the assumption that the graphs are represented by adjacency lists. (b) Program and run the above version of Prim algorithm against Algorithm 4.9. Compare the two on a representative set of graphs (c) Analyze precisely the computing time and space requirements of your new version of Prim’s algorithm using adjacency lists. 4. Program and run Kruskal’s algorithm, described in Algorithm 4.10. 5. (a) Show that if is a spanning tree for the undi You will have to modify functions Heapify and Adjust of Chapter 2. Use the same test data you devised to test Prim’s algorithm in Exercise 3. ected graph G, then the addition of an edge q, q ¢ E(t) and q € E(G), to t creates a unique cycle.4.6. OPTIMAL STORAGE ON TAPES 229 Figure 4.10 Graph for Exercise 1 (b) Show that if any of the edges on this unique cycle is deleted from E(t) U{q}, then the remaining edges form a spanning tree of G. 6. In Figure 4.9, find a minimum-cost spanning tree for the graph of part (c) and extend the tree to obtain a minimum cost spanning tree for the graph of part (a). Verify the correctness of your answer by applying cither Prim’s algorithm or Kruskal’s algorithm on the graph of part (a). 7. Let G(V, E) be any weighted connected graph. (a) If C is any eycle of G, then show that the heaviest edge of C cannot belong to a minimum-cost spanning tree of G. (b) Assume that F is a forest that is a subgraph of G. Show that any F-heavy edge of G cannot belong to a minimum-cost spanning tree of G. 8. By considering the complete graph with n vertices, show that the num- ber of spanning trees in an n vertex graph can be greater than 2"! ~2 4.6 OPTIMAL STORAGE ON TAPES ‘There are n programs that are to be stored on a computer tape of length 1. Associated with each program i is a length l;,1 < i Ij, then interchanging ig and iy results in a permutation J’ with dl!) = |Sn-k+ 0h, | + (0 ka ky +i, + (n—b+ hi, Subtracting d(J’) from d(Z), we obtain d(I) — d(I') (n-a+I1)(li, —i,) + (n— b+ 1)(li, — bi) (b—a)(li, — fi) > 0 Hence, no permutation that is uot in nondecreasing order of the 1,’s can have minimum d. It is easy to see that all permutations in nondecreasing order of the I;’s have the same d value. Hence, the ordering defined by i; = 4,1 1 tapes, Tp,..-.Tin1, then the programs are to be distributed over these tapes. For each tape a storage permutation is to be provided. If is the storage permutation for the subset of programs on tape j, then d(J,) s as defined earlier. The total retrieval time (TD) is Socjem 1 dj). The objective is to store the programs in such a way as to minimize T'D. The obvious generalization of the solution for the one-tape case is to consider the programs in nondecreasing order of l's. The program currently232 CHAPTER 4. THE GREEDY METHOD 1 Algorithm Store(n,m) 2 // nis the number of programs and m the number of tapes. 3 0; // Next tape to store on 5 = 1tondo 6 7 write ("append program", i, 8 “to permutation for tape", j)s 9 j= (J +1) mod m; 10 } u } Algorithm 4.12 Assigning programs to tapes being considered is placed on the tape that re: in the minimum increas in TD. This tape will be the one with the least amount of tape used so far. If there is more than one tape with this property, then the one with the smallest index can be used. If the jobs are initially ordered so that |) < lp < +++ 1. The problem is to select a maximum subset @ of the programs for storage on the tape. (A maximum subset is one with the maximum number of programs in it). A greedy algorithm for this problem would build the subset Q by including programs in nondecreasing order of aj (a) Assume the P; are ordered such that a1 < a2 < +++ < aq. Write a function for the above strategy. Your function should output an array s[1 : n] such that s[i] = 1 if P; is in Q and sfi] = 0 otherw a (b) Show that this strategy always finds a maximum subset Q such that Dpcqai Sl. (c) Let Q be the subset obtained using the above greedy strategy. How small can the tape utilization ratio (p,q a1)/l get? (d) Suppose the objective now is to determine a subset of programs that. maximizes the tape utilization ratio. A greedy approach234 CHAPTER 4. THE GREEDY METHOD would be to consider programs in nonincreasing order of aj. If there is enough space left on the tape for P;, then it is included in Q. Assume the programs ar red so that ay > az > +++ > dn. Write a function i time and space complexity? (c) Show that the strategy of part (d) doesn’t necessarily yield a subset that maximizes (S°p,cqai)/l. How small can this ratio get? Prove your bound. 4, Assume n programs of lengths l1,[9,... ln are to be stored on a tape. Program i is to be retrieved with frequency f;. If the programs are stored in the order #1,i2,...,%n, the expected retrieval time (ERT) is [Eu x 4] ISA (a) Show that storing the programs in nondecreasing order of I; does not necessarily minimize the ERT. (b) Show that storing the programs in nonincreasing order of f; does not necessarily minimize the ERT. Show that the ERT is minimized when the programs are stored in nonincreasing order of f;/li. 5. Consider the tape storage problem of this section. Assume that two tapes T1 and T2, are available and we wish to distribute n given programs of lengths ly, l2,...Jy onto these two tapes in such a manner that the maximum retrieval time is minimized. That is, if A and B are the sets of programs on the tapes T1 and 2 respectively, then we wish to choose A and B such that max { Sieqlis Dien hi } is minimized. A possible greedy approach to obtaining and B would be to start with A and B initially empty. Then consider the programs one at a time. The program currently being considered is assigned to set A if Sel = min { Dicali, Diew li }; otherwise it is assigned to B. Show that this does ‘not guarantee optimal solutions even if ly < [p< +++ < In Show that the same is true if we require f) > ly > +++ > ly. 4.7 OPTIMAL MERGE PATTERNS In Section 3.4 we saw that two sorted files containing n and m records respectively could be merged together to obtain one sorted file in time O(n+ m). When more than two sorted files are to be merged together, the merge can be accomplished by repeatedly merging sorted files in pairs. Thus, if4.7. OPTIMAL MERGE PATTERNS 235 files 21.72.73, and 21 are to be merged, we could first merge 21 and x2 to get a file yy. Then we could merge y; and <3 to get yo. Finally, we could merge yz and 2 to get the desired sorted file. Alternatively, we could first merge 2; and ry getting y1, then merge 3 and x4 and get yp, and finally merge y; and yp and get the desired sorted file. Given n sorted files, there are many ways in which to pairwise merge them into a single sorted file. Different pairings require differing amounts of computing time. The problem we address ourselves to now is that of determining an optimal way (one requiring the fowest comparisons) to pairwise merge n sorted files. Since this problem calls for an ordering among the pairs to be merged, it fits the ordering paradigm Example 4.9 The files 1,7, and :ry are three sorted files of length 30,20, and 10 records each. Merging 1 and :r2 requires 50 record moves. Merging the result with «3 requires another 60 moves. The total number of record moves required to merge the three files this way is 110. If, instead, we first merge tz and 3 (taking 30 moves) and then 21 (taking 60 moves), the total record moves made is only 90. Hence, the second merge pattern is faster than the first. a A greedy attempt to obtain an optimal merge pattern is easy to formulate. Since merging an n-record file and an m-record file requires possibly n+ m record moves, the obvious choice for a selection criterion is: at each step merge the two smallest size files together. Thus, if we have five files (x),...,25) with sizes (20, 30, 10,5,30), our greedy rule would generate the following merge pattern: merge x; and -r3 to get 2; (|zi| = 15), merge 2, and xy to get zz (|zo| = 35), merge zy and x5 to get 23 (|z3| = 60), and merge z, and 25 to get the answer 24. The total number of record moves is 205 One can verify that this is an optimal merge pattern for the given problem instance. The merge pattern such as the one just described will be referred to as a two-way merge pattern (each merge step involves the merging of two files). The two-way merge patterns can be represented by binary merge trees. Figure 4.11 shows a binary merge tree representing the optimal merge pattern obtained for the above five files. The leaf nodes are drawn as squares and represent the given five files. These nodes are called external nodes. The remaining nodes are drawn as circles and are called internal nodes. Each internal node has exactly two children, and it represents the file obtained by merging the files represented by its two children. ‘The number in each node is the length (i., the number of records) of the file represented by that node. external node 21 is at a distance of 3 from the root node 24 (a node at level i is at a distance of i— 1 from the root). Hence, the records of file 24 are moved three times, once to get 21, once again to get 2», and finally one more time to get z4. If d, is the distance from the root to the external236 CHAPTER 4. THE GREEDY METHOD Figure 4.11 Binary merge tree representing a merge pattern node for file x; and qi, the length of 2; is then the total number of record moves for this binary merge tree is Sava i This sum is called the weighted external path length of the tree. An optimal two-way merge pattern corresponds to a binary merge tree with minimum weighted external path length. The function Tree of Algo- rithm 4.13 uses the greedy rule stated earlier to obtain a two-way merge tree for n files. The algorithm has as input a list list of n trees. Bach node in a tree has three fields, Ichild, rchild, and weight. Initially, each tree in list has exactly one node. This node is an external node and has [child and rehild fields zero whereas weight is the length of one of the n files to be merged. During the course of the algorithm, for any tree in list with root node t, t - weight is the length of the merged file it represents (t + weight equals the sum of the lengths of the external nodes in tree t). Function Tree uses two functions, Least(list) and Insert(list,t). Least(list) finds a tree in list whose root has least weight and returns a pointer to this tree. This tree is removed from list. Insert(list,t) inserts the tree with root t into list. The- orem 4.10 shows that Tree (Algorithm 4.13) generates an optimal two-way merge tree.4,7. OPTIMAL MERGE PATTERNS 237 treenode = record { treenode * child; treenode + rchilds integer weight; k 1 Algorithm Tree(n) 2 // list is a global list of n single node 3. // binary trees as described above. at 5 for i:= 1ton—1do of 7 pt := new treenodes // Get a new tree node. 8 (pt + Ichild) := Least(List); // Merge two trees with 9 (pt + rehild) := Least(list); // smallest lengths. 10 (pt + weight) := ((pt + Ichild) > weight) u +((pt + rehild) + weight); 12 Insert (list, pt)s 13 14 return Least(list); // Tree left in list is the merge tree 15 } Algorithm 4.13 Algorithm to generate a two-way merge tree Example 4.10 Let us see how algorithm Tree works when list initially rep- resents six files with lengths (2,3,5,7,9, 13). Figure 4.12 shows list at the end of each iteration of the for loop. The binary merge tree that results at the end of the algorithm can be used to determine which files are merged. Merging is performed on those files which are lowest (have the greatest depth) in the tree. a The main for loop in Algorithm 4.13 is executed n — 1 times. If list is kept in nondecreasing order according to the weight value in the roots, then Least(list) requires only O(1) time and Insert(list,t) can be done in O(n) time. Hence the total time taken is O(n2). In case list is represented as a minheap in which the root value is less than or equal to the values of its children (Section 2.4), then Least(list) and Insert(List, t) can be done in O(log n) time. In this case the computing time for Tree is O(nlogn). Some speedup may be obtained by combining the Insert of line 12 with the Least of line 9.238, CHAPTER 4. THE GREEDY METHOD Theorem 4.10 If ist initially contains n > 1 single node trees with weight values (q1,92,.--,@n), then algorithm Tree generates an optimal two-way merge tree for n files with these lengths. Proof: The proof is by induction on n. For n = 1, a tree with no internal nodes is returned and this tree is clearly optimal. For the induction hypoth- esis, assume the algorithm generates an optimal two-way merge tree for all am), 1 L(T2). (b) Using the data of a, obtain T1 and T2 € D such that L(T1) = L(L2) but SL(T1) > SL(T2) (c) Show that if the subalgorithm Least used in algorithm Tree is such that in case of a tie it returns the tree with least depth, then Tree a tree with the properties of T* 4.8 SINGLE-SOURCE SHORTEST PATHS Graphs can be used to represent the highway structure of a state or country with vertices representing cities and edges representing sections of highway. The edges can then be assigned weights which may be either the distance between the two cities connected by the edge or the average time to drive along that, sect hway. A motorist wishing to drive from city A to B would be interested in answers to the following questions:242 CHAPTER 4. THE GREEDY METHOD Path Length 11,4 10 2) 1,4,5 25 314,52 45 4) 1,3 45 (a) Graph (b) Shortest paths from 1 Figure 4.15 Graph and shortest paths from vertex 1 to all destinations ¢ Is there a path from A to B? If there is more than one path from A to B, which is the shortest path? The problems defined by these questions are special cases of the path problem we study in this section. The length of a path is now defined to be the sum of the weights of the edges on that path. The starting vertex of the path is referred to as the sourc ind the last vertex the destination. The graphs are digraphs to allow for one-way streets. In the problem we consider, we are given a directed graph G = (V,B), a weighting function cost for the edges of G, and a source vertex vp. The problem is to determine the shortest paths from vp to all the remaining vertices of G. It is assumed that all the weights are positive. The shortest path between vg and some other node v is an ordering among a subset of the edges. Hence this problem fits the ordering paradigm. Example 4.11 Consider the directed graph of Figure 4.15(a). The numbers on the edges are the weights. If node 1 is the source vertex, then the shortest path from 1 to 2 is 1,4,5,2. The length of th 1,2 which is of length 50. There is no path from 1 to 6. Figure 4.15(b) lists the shortest paths from node 1 to nodes 4,5, 2, and 3, respectively. The paths have been listed in nondecreasing order of path length. a To formulate a greedy-based algorithm to generate the shortest paths. we must conceive of a multistage solution to the problem and also of an optimization measure. One possibility is to build the shortest paths one by4.8. SINGLE-SOURCE SHORTEST PATHS 243 one. As an optimization measure we can use the sum of the lengths of all paths so far generated. For this measure to be minimized, each individual path must be of minimum length. If we have already constructed i shortest paths, then using this optimization measure, the next path to be constructed should be the next shortest minimum length path. The greedy way (and also a systematic way) to generate the shortest paths from vp to the remaining vertices is to generate these paths in nondecreasing order of path length. First, a shortest path to the nearest vertex is generated, Then a shortest path to the second nearest vertex is gencrated, and so on, For the graph of Figure 4.15(a) the nearest vertex to v = 1 is 4 (cos#[1,4] = 10). The path 1,4 is the first path gener The second nearest vertex to node 1 is 5 and the distance betwoen | and 5 is 25. The path 1,4,5 is the next path generated. In order to generate the shortest paths in this order, we need to be able to determine (1) the next vertex to which a shortest path must be generated and (2) a shortest path to this vertex. Let S denote the set of vertices (including vo) to which the shortest paths have already been generated. For w not in S, let dist{w] be the length of the shortest path Starting from v9, going through only those vertices that are in S, and ending at, w. We observe that: 1. If the next. shortest path is to vertex u, then the path begins at 1, ends at u, and goes through only those vertices that are in §. To prove this, we must show that all the intermediate vertices on the shortest path to ware in S. Assume there is a vertex w on this path that is not in S. Then, the vp to u path also contains a path from vg to w that is of length less than the vg to u path. By assumption the shortest. paths are being generated in nondecreasing order of path length, and so the shorter path vp to w must already have been generated. Hence, there can be no intermediate vertex that is not in S. 2. The destination of the next path generated must be that of vertex u which has the minimum distance, dist{u]. among all vertices not in S’ This follows from the definition of dist and observation 1. In case there are several vertices not in S with the same dist, then any of these may be selected 3. Having selected a vertex u as in observation 2 and generated the short- est vo to u path, vertex u becomes a member of S. At this point the length of the shortest paths starting at vy, going though vertices only in S, and ending at a vertex w not in S may decrease: that is, the value of dist{w] may change. If it does change, then it must be due to a shorter path starting at vp and going to u and then to w. The intermediate vertices on the vp to u path and the u to w path must all be in S. Further, the vp to uw path must be the shortest such path: otherwise dist[w] is not defined properly, Also, the u to w path can be chosen so as not to contain any intermediate vertices. Therefore,244 CHAPTER 4. THE GREEDY METHOD we can conclude that if dist{w] is to change (i.e., decrease), then it is because of a path from vp to u to w, where the path from vp to u is the shortest such path and the path from u to w is the edge (u, w) The length of this path is dist[u] + cost{u, w]. The above observations lead to a simple Algorithm 4.14 for the single- source shortest path problem. This algorithm (known as Dijkstra’s algo- rithm) only determines the lengths of the shortest paths from vp to all other vertices in G. The generation of the paths requires a minor extension to this algorithm and is left as an exercise. In the function ShortestPaths (Algorithm 4.14) is assumed that the n vertices of G are numbered 1 through n. The maintained as a bit array with S[é] = 0 if vertex i is not in S and if it is. It is assumed that the graph itself is represented by its cost adjacency matrix with cost{i, j]’s being the weight of the edge (i,j). The weight cost(i, j] is set to some large number, oo, in case the edge (i, j) is not in E(G). For i= j, cost{i, j) can be set to any nonnegative number without affecting the outcome of the algorithm. From our earlier discussion, it is easy to see that the algorithm is correct. The time taken by the algorithm on a graph with n vertices is O(n?). To see this, note that. the for loop of line 7 in Algorithm 4.14 takes @(n) time. The for loop of line 12 is executed n — 2 times. Each execution of this loop requires O(n) time at lines 15 and 16 to select the next vertex and again at the for loop of line 18 to update dist. So the total time for this loop is O(n?). In case a list t of vertices currently not in s is maintained, then the number of nodes on this list would at any time be n — num. This would speed up lines 15 and 16 and the for loop of line 18, but the asymptotic time would remain O(n). This and other variations of the algorithm are explored in the exercises. Any shortest path algorithm must examine each edge in the graph at least once since any of the edges could be in a shortest path. Hence, the minimum possible time for such an algorithm would be Q(/B]). Since c adjacency matrices were used to represent the graph, it takes O(n?) time just to determine which edges are in G, and so any shortest path algorithm using this representation must take Q(n?) time. For this representation then, algorithm ShortestPaths is optimal to within a constant factor. If a change to adjacency lists is made, the overall frequency of the for loop of line 18 can be brought down to O(|E]) (since dist can change only for vertices adjacent from u). If V — S$ is maintained as a red-black tree (see Section 2.4.2), each execution of lines 15 and 16 takes O(logn) time. Note that a red-black tree supports the following operations in O(logn) time: insert, delete (an arbitrary clement), find-min, and search (for an arbitrary element). Each update in line 21 takes O(logn) time as well (since an update can be done using a delete and an insertion into the red-black tree). Thus the overall run time is O((n + |E|) log n).4.8. SINGLE-SOURCE SHORTEST PATHS 245 1 Algorithm ShortestPaths(v, cost, dist,n) 2 // dist{j), 1 dist{u] + cost[u,w])) then 21 dist[w] := dist{u] + cost{u, w]s 22 23 } Algorithm 4.14 Greedy algorithm to generate shortest paths Example 4.12 Consider the eight vertex digraph of Figure 4.16(2) with cost adjacency matrix as in Figure 4.16(b). The values of dist and the vertices selected at each iteration of the for loop of line 12 in Algorithm 4.14 for finding all the shortest paths from Boston are shown in Figure 4.17. To begin with, S contains only Boston. In the first iteration of the for loop (that is, for num = 2), the city u that is not in S and whose dist[u] is minimum is identified to be New York. New York e the set $. Also the dist{ | values of Chicago, Miami, and New Orleans get altered since there are shorter paths to these cities via New York. In the next iteration of the for loop, t! ‘ity that enters S is Miami since it has the smallest dis¢[ | value from among all the nodes not in S. None of the dist{ | values are altered. The algorithm continues in a similar fashion and terminates when only seven of the eight vertices are in S. By the definition of dist, the distance of the last vertex, in this case Los Angeles, is correct as the shortest path from Boston to Los Angeles can go through only the remaining six vertices. 0246 CHAPTER 4. THE GREEDY METHOD Boston _G) /250 4 San Francisco New York O— 300) 100 ae 100 Los Angeles New Orleans (a) Digraph 1 A 3 4 5 6 7 8 1 0 7 2 | 300 0 3. 100 800 0 4 1200 0 5 1500 0. .250) 6 1000 0 900 1400 | 7 0 1000 8 | 1700 o| (b) Length-adjacency matrix Figure 4.16 Figures for Example 4.12 One can easily verify that the edges on the shortest paths from a ver- tex v to all remaining vertices in a connected undirected graph @ form a spanning tree of G. This spanning tree is called a shortest-path spanning tree. Clearly, this spanning tree may be different for different root vertices v. Figure 4.18 shows a graph G, its minimum-cost spanning tree, and a shortest-path spanning tree from vertex 14.8. SINGLE-SOURCE SHORTEST PATHS 247 Iteration | S Vertex, LA SF DEN CHI BOST MIA NO| selected. | a QQ) 3) 4] (s] (6) a {8} Initial | - 0 +00 yoo 1500 oO 250 +0 +00 | 15.6. iL } Figure 4.17 Action of ShortestPaths EXERCISES 1. Use algorithm ShortestPaths to obtain in nondecreasing order the lengths of the shortest paths from vertex 1 to all remaining vertices in the di- graph of Figure 4.19. 2. Using the directed graph of Figure 4.20 explain why ShortestPaths will not work properly. What is the shortest path between vertices 1, and v7 7 3. Rewrite algorithm ShortestPaths under the following assumptions: (a) @ is represented by its adjacency lists, The head nodes are HEAD(1),..., HEAD(m) and each list node has three fields: VER- TEX, COST, and LINK. COST is the length of the corresponding edge and n the number of vertices in G. (b) Instead of representing S, the set of vertices to which the shortest paths have already been found, the set T = V(G) — $ is repre- sented using a linked list. What can you say about the computing time of your new algorithm relative to that of ShortestPaths? 4. Modify algorithm ShortestPaths so that it obtains the shortest. p: in addition to the lengths of these paths. What is the computing time of your algorithn248 CHAPTER 4. THE GREEDY METHOD (c) Shortest path spanning tree from vertex 1. Figure 4.18 Graphs and spanning trees Figure 4.19 Directed graph4.9. REFERENCES AND READINGS 249 Figure 4.20 Another directed graph 4.9 REFERENCES AND READINGS The linear time algorithm in Section 4.3 for the tree vertex splitting problem can be found in “Vertex upgrading problems for VLSI,” by D. Paik, Ph.D thesis, Department of Computer Science, University of Minnesota, October 1991. ‘The two greedy methods for obtaining minimum-cost spanning trees are due to R. C. Prim and J. B. Kruskal, respectively, An O(c log log») time spanning tree algorithm has been given by A. C. Yao. The optimal randomized algorithm for minimum-cost spanning trees pre- sented in this chapter appears in “A randomized linear-time algorithm for finding minimum spanning tr y P.N. Klein and R. E. Tarjan, in Pro- ceedings of the 26th Annual Symposium on Theory of Computing, 1994, pp. 9-15. See also “A randomized linear-time algorithm to find minimum span- ning trees,” by D. R. Karger, P. N. Klein, and R. E. Tarjan, Journal of the ACM 42, no. 2 (1995): 321-328. Proof of Lemma 4.3 can be found in “Verification and sensitivity analysis of minimum spanning trees in linear time.” by B. Dixon, M. Rauch, and R. E Tarjan, SIAM Journal on Computing 21 (1992): 1184-1192, and in “A simple minimum spanning tree verification algorithm,” by V. King, Proceedings of the Workshop on Algorithms and Data Structures, 1 5. ‘A very nearly linear time algorithm for minimum-cost spanning trees ap- pears in “Efficient algorithms for finding minimum spanning trees in undi- rected and directed graphs,” by H. N. Gabow, Z. Galil, T. Spencer, and R. E. Tarjan, Combinatorica 6 (1986): 109-122.250 CHAPTER 4. THE GREEDY METHOD A linear time algorithm for minimum-cost spanning trees on a stronger model where the edge weights can be manipulated in their binary form is given in “Trans-dichotomous algorithms for minimum spanning trees and shortest paths,” by M. Fredman and D. E. Willard, in Proceedings of the ist Annual Symposium on Foundations of Computer Science, 1990. pp. 719-725. The greedy method developed here to optimally store programs on tapes was first devised for a machine scheduling problem. In this problem n jobs have to be scheduled on m processors. Job i takes ¢; amount of time. ‘The time at which a job finishes is the sum of the job times for all jobs preced- ing and including job i. The average finish time corresponds to the mean access time for programs on tapes. The (m!)"/™ schedules referred to in Theorem 4.9 are known as SPT (shortest processing time) schedules. The rule to generate SPT schedules as well as the rule of Exercise 4 (Section 4.6) are due to W. E. Smith. The greedy algorithm for generating optimal merge trees is due to D. Huffman. For a given set {q1,.-.,@n} there are many sets of Huffman codes mini- mizing )> qidj. From amongst. these code sets there is one that has minimum Xd; and minimum max {d;}. An algorithm to obtain this code set was given by E. S. Schwartz. The shortest-path algorithm of the text is due to E. W. Dijkstra. For planar graphs, the shortest-path problem can be solved in linear time as has been shown in “Faster shortest-path algorithms for planar graphs,” by P. Klein, S. Rao, and M. Rauch, in Proceedings of the ACM Symposium on Theory of Computing, 1994 The relationship between greedy methods and matroids is discussed in Combinatorial Optimization, by E. Lawler, Holt, Rinehart and Winston, 1976 4.10 ADDITIONAL EXERCISES 1. (Coin changing] Let An = {a1,a2,...,@n} be a finite set of distinct oin types (for example, ay = 50, a2 = 25f, a3 = 10¢, and so on.) We an assume each a; is an integer and a, > a2 > --- > a_. Each type is available in unlimited quantity. The coin-changing problem is to make up an exact amount C using a minimum total number of coins. C is an integer > 0.4.10. ADDITIONAL EXERCISES 251 (a) Show that if, # 1, then there exists a finite set of coin types and a C for which there is no solution to the coin-changing problem. (b) Show that there is always a solution when ay, = 1 (c) When a, = 1, a greedy solution to the problem makes change by using the coin types in the order ay.a2,...,4,. When coin type a; is being considered, as many coins of this type as possible are given. Write an algorithm based on this strategy. Show that this algorithm doesn’t necessarily generate solutions that use the minimum total number of coins. (d) Show that if Ay = {h"-1,k"-2,....k°} for some k > 1, then the greedy inethod of part (c) always yields solutions with a minimun number of coins 2. [Set cover] You are given a family $ of m sets $,1 w;r; <_m and 0<2;<1) a problem (Section ions, We have to 1, then on Example 5.2 [Optimal merge patterns] This problem was discussed in Sec- tion 4.7, An optimal merge pattern tells us which pair of files should be merged at each step. As a decision sequence, the problem calls for us to de- cide which pair of files should be merged first, which pair second, which pair third, and so on. An optimal sequence of decisions is a least-cost sequence. o Example 5.3 [Shortest path] One way to find a shortest path from vertex ito vertex j in a directed graph G is to decide which vertex should be the second vertex, which the third, which the fourth, and so on, until vertex j is reached. An optimal sequence of decisions is one that results in a path of least length. ao254 CHAPTER 5. DYNAMIC PROGRAMMING For some of the problems that may be viewed in this way, an optimal sequence of decisions can be found by making the decisions one at a time and never making an erroneous decision. This is true for all problems solvable by the greedy method. For many other problems, it is not possible to make stepwise decisions (based only on local information) in such a manner that the sequence of decisions made is optimal. Example 5.4 [Shortest path] Suppose we wish to find a shortest path from vertex i to vertex j. Let A; be the vertices adjacent from vertex 7. Which of the vertices in A; should be the second vertex on the path? There is no way to make a decision at this time and guarantee that future decisions leading to an optimal sequence can be made. If on the other hand we wish to find a shortest path from vertex i to all other vertices in G, then at each step, a correct decision can be made (see Section 4.8). a One way to solve problems for which it is not possible to make a sequence of stepwise decisions leading to an optimal decision sequence is to try all pos- sible decision sequences. We could enumerate all decision sequences and then pick out the best. But the time and space requirements may be prohibitive Dynamic programming often drastically reduces the amount of enumeration by avoiding the enumeration of some decision be optimal. In dynamic programming an optimal sequence of de obtained by making explicit appeal to the principle of optimality. Definition 5.1 [Principle of optimality] The principle of optimality states that, an optimal sequence of decisions has the property that whatever the initial state and decision are, the remaining decisions must constitute an optimal decision sequence with regard to the state resulting from the first decision. a Thus, the essential difference between the greedy method and dynamic programming is that in the greedy method only one decision sequence is ever generated. In dynamic programming, many decision sequences may be generated. However, sequences containing suboptimal subsequences cannot be optimal (if the principle of optimality holds) and so will not (as far as possible) be generated. Example 5.5 [Shortest path] Consider the shortest-path problem of Exam- ple 5.3. Assume that i, i1, i2,..., ig, 7 is a shortest path from i to j. Starting with the initial vertex i, a decision has been made to go to vertex i. Fol- lowing this decision, the problem state is defined by vertex i) and we need to find a path from #; to j. It is clear that the sequence iy, i2,....ix,j must constitute a shortest i; to j path. If not, let i,71,72,--.,1%q)Jj be a shortest in to j path. Then é,é1,r1,++,7q.j is an i to j path that is shorter than the path 7, 1, 2,...,i.j. Therefore the principle of optimality applies for this problem. oO5.1. THE GENERAL METHOD 255 Example 5.6 [0/1 knapsack] The 0/1 knapsack problem is similar to the knapsack problem of Section 4.2 except that the ;’s are restricted to have a value of either 0 or 1. Using KNAP(I, j,y) to represent the problem maximize D)cic; piti subject to Dycjcj witi SY (5.1) aj =0orl, T 0 and gn(y) = ~c0 for y <0. From gn(y), one can obtain gn—1(y) using (5.3) with i=n-—1. Then, using gn_1(y), one can obtain g,-2(y). Repeating in this way, one can determine g;(y) and finally go(m) using (5.3) with i = 0. Example 5.11 fora knapsack] ce der the case in which n = 3, wr = 2,we = 3,03 =4, pr = 1pe = . and m = 6. We have to compute g0(6). The value of ‘0(6) = max ‘t(6). gi(4) + 1}: In turn, gi (6) = max {g2(6), 92(3)+2}. But go(6) = max {93(6), 93(2)4 5} = max {0,5} = 5. Also, go(3) = max {g3(3), g3(3 — 4) + 5} = max {0,—oo} = 0. Thus, 1(6) = max {5,2} Similarly, 91(4) = max {g2(4), go(4~ 3) +2}. But go(4) = max {99(4), go(4—4) +5} = max {0,5} = 5. The value of ga(1) = max {g9(1), 93(1 4) +5} = max {0,—co} = 0. Thus, gi(4) = max {5,0} = Therefore, go(6) = max {5,5 +1} =6. o Example 5.12 [Shortest path] Let P; be the set of vertices adjacent to ver- tex j (that is, k € P; iff (k, j) € E(G)). For each k € Pj, let Ty, be a shortest i to k path. The principle of optimality holds and a shortest i to j path is the shortest of the paths {I,. j|k € Pj} To obtain this formulation, we started at vertex j and looked at the last decision made. The last decision was to use one of the edges (k,j), k € Pj. In a sense, we are looking backward on the i to j path. a Example 5.13 [0/1 knapsack] Looking backward on the sequence of deci- sions 21,2... ,n, we see that Fy) = max {fj-1(y), f-1(y ~ wy) + ry} (5.4) where f;(y) is the value of an optimal solution to KNAP(I, j.y)-5.2. MULTISTAGE GRAPHS 257 The value of an optimal solution to KNAP(1,7, 7) is fy(m). Equation 5.4 can be solved by beginning with fo(y) = 0 for all y, y >'0. and fo(y) for all y, y <0. From this, fi, fo,-... Jn can be successively obtained. The solution method outlined in Examples 5.12 and 5.13 may indicate ible decision sequences to obtain an optimal not the case. Be- cause of the use of the principle of optimality, decision sequences containing subsequences that are suboptimal are not considered. Although the total number of different decisi s is exponential in the number of dec sions (if there are d ns to be made then t! are d” possible decision sequences), dynamic programming algorithms often have a polynomial complexity. Another important feature of the dynamic programming approach is that optimal solutions to subproblems are retained so as to avoid recomputing their values. The use of these tabulated values makes it natural to recast the recursive equations into an iterative algorithm. Most of the dynamic programming algorithms in this chapter are expressed in this way. ‘The remaining sections of this chapter apply dynamic programming to a. variety of problems. These examples should help you understand the method better and also realize the advantage of dynamic programming over explicitly enumerating all decision sequences. EXERCISES 1. The principle of optimality does not hold for every problem whose solution can be viewed as the result of a sequence of decisions. Find two problems for which the principle does not hold. Explain why the principle does not hold for these problems. 2. For the graph of Figure 5.1, find the shortest path between the nodes 1 and 2. Use the recurrence relations derived in Examples 5.10 and 5.13. 5.2. MULTISTAGE GRAPHS A multistage graph G = (V, E) is a directed graph in which the vertic partitioned into k > 2 disjoint sets V;, 1 0, for every edge (i,j), we only require that G have no cycles with negative length. Note that if we allow G to contain a cycle of negative length, then the shortest path between any two vertices on this cycle has length —oo. Let us examine a shortest i to j path in G, i # j. This path originates at vertex i and goes through some intermediate vertices (possibly none) and terminates at vertex j. We can assume that this path contains no cycles for if there is a cycle, then this can be deleted without increasing the path length (no cycle has negative length). If k is an intermediate vertex on this shortest path, then the subpaths from i to k and from k to j must be shortest paths from i to & and k to j, respectively. Otherwise, the i to j path is not of minimum length. So, the principle of optimality holds. This alerts us to the prospect of using dynamic programming. If k is the intermediate vertex with highest index, then the i to k path is a shortest i to k path in G going through no vertex with index greater than & — 1. Similarly the k to j path is a shortest k to j path in G going through no vertex of index greater than266 CHAPTER 5. DYNAMIC PROGRAMMING k—1. We can regard the construction of a shortest i to j path as first requiring a decision as to which is the highest indexed intermediate vertex k. Once this decision has been made, we need to find two shortest paths, one from i to k and the other from k to j. Neither of these may go through a vertex with index greater than k — 1. Using A*(i,j) to represent the length of a shortest path from i to j going through no vertex of index greater than k, we obtain A(i,j) = min { min {A*'(i,&) + A*1(k, j)}, cost(i, j)} (5.7) 1sk (n—1)M, then there is no directed path from i to j in G. Even for this choice of 00, care should be taken to avoid any floating point overflows. ‘The time needed by AllPaths (Algorithm 5.3) i lly easy to deter- mine because the looping is independent of the data in the matrix A. Line 11 is iterated n? times, and so the time for AllPaths is @(n*). An exercise examines the extensions needed to obtain the i to j paths with these lengths. Some speedup can be obtained by noticing that the innermost for loop need be executed only when A(i,k) and A(k,j) are not equal to oo.5,3. ALL-PAIRS SHORTEST PATHS 269 EXERCISES 1. (a) Does the recurrence (5.8) hold for the graph of Figure 5.7? Why? Figure 5.7 Graph for Exercise 1 (b) Why does Equation 5.8 not hold for graphs with cycles of negative length? 2. Modify the function AllPaths so that a shortest. path is output for each , j). What are the time and space complexities of the 3. Let A be the adjacency matrix of a directed graph G. Define the transitive closure A+ of A to be a matrix with the property A+ (i,j) = iff G has a directed path, containing at least one edge, from vertex i to vertex j. A*(i,j) = 0 otherwise. The reflexive transitive closure A* is a matrix with the property A*(i,j) = 1 iff G has a path, containing zero or more edges, from i to j. A*(i,j) = 0 otherwise. (a) Obtain A* and A’ for the directed graph of Figure 5.8. Figure 5.8 Graph for Exercise 3 (b) Let A*(i, 3) = 1 iff there is a path with zero or more edges from to j going through no vertex of index greater than k. Define A° in terms of the adjacency matrix A.270 CHAPTER 5. DYNAMIC PROGRAMMING (c) Obtain a recurrence between A* and A’ similar to (5.8). Use the logical operators or and and rather than min and +. (4) Write an algorithm, using the recurrence of part (c), to find A* Your algorithm can use only O(n2) space. What is its time com- plexity? (e) Show that A* = A x A‘, where matrix multiplication is defined as At(i,j) = VBL,(A(i,k) A A%(k,j)). The operation V is the logical or operation, and A the logical and operation. Hence A* may be computed from A*. 5.4 SINGLE-SOURCE SHORTEST PATHS: GENERAL WEIGHTS We now consider the single-source shortest path problem discussed in Section 4.8 when some or all of the edges of the directed graph G may have negative length. ShortestPaths (Algorithm 4.14) does not necessarily give the correct results on such graphs. To see this, consider the graph of Figure 5.9. Let v = 1 be the source vertex. Referring back to Algorithm 4.14, since n = 3, the loop of lines 12 to 22 is iterated just once. Also u = 3 in lines 15 and 16, and so no changes are made to dist[ |. The algorithm terminates with dist(2] = 7 and dist(3] = 5. The shortest path from 1 to 3 is 1,2,3. This path has length 2, which is less than the computed value of dis¢(3] Figure 5.9 Directed graph with a negative-length edge When negative edge lengths are permitted, we require that the graph have no cycles of negative length. This is necessary to ensure that shortest paths consist of a finite number of edges. For example, in the graph of Figure 5.5, the length of the shortest path from vertex 1 to vertex 3 is oo. The length of the path 1, 2,1,2,1,2,-++,1,2,3 can be made arbitrarily small as was shown in Example 5.14. When there are no cycles of negative length, there is a shortest path between any two vertices of an n-vertex graph that has at most n— 1 edges5.4. SINGLE-SOURCE SHORTEST PATHS: GENERAL WEIGHTS 271 on it, To see this, note that a path that has more than n — 1 edges must. repeat at least one vertex and hence must contain a cycle. Elimination of the cycles from the path results in another path with the same source and destination. This path is cycle-free and has a length that is no more than that of the original path, as the length of the eliminated cycles was at least zero, We can use this observation on the maximum number of edges on a cycle-free shortest path to obtain an algorithm to determine a shortest path from a source vertex to all remaining vertices in the graph. As in the case of ShortestPaths (Algorithm 4.14), we compute only the length, dist[u], of the shortest path from the source vertex v to u. An exercise examines the extension needed to construct the shortest paths. Let dist®{u] be the length of a shortest path from the source vertex v to vertex u under the constraint that the shortest path contains at most £ edges. Then, dist![u] = cost[v,u], 1 1, edges has no more than k — 1 edges, then dist*[u] = dist*![ul. 2. If the shortest path from v to u with at most k, k > 1, edges has exactly k edges, then it is made up of a shortest. path from v to some vertex j followed by the edge (j,u). The path from v to j has k= 1 edges, and its length is dist*—'(j]. All vertices i such that the edge (i,u) is in the graph are candidates for j. Since we are interested in a shortest path, the 7 that minimizes dist*~"[i] + cost{i, u] is the correct value for j. These observations result in the following recurrence for dist: dist*[u] = min {dist (ul, min {dist*—"[i] + cost[i,u]}} This recurrence can be used to compute dist® from dist*—", for k = 2,3,. n-1 Example 5.16 Figure 5.10 gives a seven-vertex graph, together with the arrays dist*, k = 1,...,6. These arrays were computed using the equation just given. For instance, dist*(1] = 0 for all k since 1 is the source node. Also, dist"[2] = 6, dist![3] = 5, and dist"[4] = 5, since there are edges from272 CHAPTER 5, DYNAMIC PROGRAMMING 1 to these nodes. The distance dist'|] is oo for the nodes 5,6, and 7 since there are no edges to these from 1. dist?[2}_ = min {dist'[2], min; dist! i) + cost{i, 2]} = min {6,0 + 6,5 — 2,5 +00, 00 + 00,00 + 00,00 + co} = Here the terms 0 + 6,5 — 2,5 + 00, 00 + 00,00 +00, and 00 + 00 correspond to a choice of i = 1,3,4,5,6, and 7, respectively. The rest of the entries are computed in an analogous manner. a | dist*(1..7) 234567 6 5 5 ame 335540 135247 135045 135043 135043 (a) A directed graph (b) dist* Figure 5.10 Shortest paths with negative edge lengths hows that if we use the same memory location dis¢[u] for dist*[u], k = 1,...,n—1, then the final value of dist[u] is still dist"—"[u). Using this fact and the recurrence for dist shown above, we arrive at the pseudocode of Algorithm 5.4 to compute the length of the shortest path from vertex v to each other vertex of the graph. ‘This algorithm is referred to as the Bellman and Ford algorithm. An exercise 7 to 12 takes O(n?) time if adja- are used and O(e) time if adjacency lis Here e is the number of edges in the graph. The overall complexity is O(n?) when adjacency matrices are used and O(ne) when adjacency lists are used. The observed complexity of the shortest-path algorithm can be reduced by not- ing that if none of the dist values change on one iteration of the for loop of lines 7 to 12, then none will change on successive iterations. So, this loop can be rewritten to termina ither after n — 1 iterations or after the5.4. SINGLE-SOURCE SHORTEST PATHS: GENERAL WEIGHTS 273 1 Algorithm BellmanFord(v, cost, dist,n) 2 // Single-source/all-destinations shortest 3/7 paths with negative edge costs 4 5 for i= 1 to n do // Initialize dist. 6 dist{i] := cost[u, i}; 7 for k:=2 to n—1do 8 for each u such that uv and u has 9 at least one incoming edge do 10 for each (i,u) in the graph do u if dist{u] > dist{i) + cost{i,u] then 12 dist\u] == dist{i] + cost[i,ul3 isy Algorithm 5.4 Bellman and Ford algorithm to compute shortest paths first iteration in which no dist values are changed, whichever occurs first. Another possibility is to maintain a queue of vertices i whose dist values changed on the previous iteration of the for loop. These are the only values for i that need to be considered in line 10 during the next iteration. When a queue of these values is maintained, we can rewrite the loop of lines 7 to 12 so that on each iteration, a vertex i is removed from the queue, and the dist values of all vertices adjacent from 7 are updated as in lines 11 and 12. Vertices whose dist values decrease as a result of this are added to the end of the queue unless they are already on it. The loop terminates when the queue becomes empty. These two strategies to improve the performance of BellmanFord are considered in the exercises. Other strategies for improving performance are discussed in References and Readings. a EXERCISES e shortest paths from node 1 to every other node in the graph 5.11 using the Bellman and Ford algorithm: 2. Prove the correctness of BellmanFord (Algorithm 5.4). Note that. this algorithm does not faithfully implement the computation of the recur- rence for dist, In fact, for k < n—1, the dist values following iteration k of the for loop of lines 7 to 12 may not be dist®. 3. Transform BellmanFord into a program. Assume that graphs are repre- sented using adjacency lists in which each node has an additional field