The.Algorithm.Design.Manual.Springer-Verlag.1998
The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998
The Partition Problem Next: Approximate String Matching Up: Dynamic Programming Previous: Fibonacci numbers The Partition Problem Suppose that three workers are given the task of scanning through a shelf of books in search of a given piece of information. To get the job done fairly and efficiently, the books are to be partitioned among the three workers. To avoid the need to rearrange the books or separate them into piles, it would be simplest to divide the shelf into three regions and assign each region to one worker. But what is the fairest way to divide the shelf up? If each book is the same length, say 100 pages, the job is pretty easy. Just partition the books into equal-sized regions, so that everyone has 300 pages to deal with. But what if the books are not the same length? Suppose we used the same partition when the book sizes looked like this: I, for one, would volunteer to take the first section, with only 600 pages to scan, instead of the last one, with 2,400 pages. The fairest possible partition for this shelf would be where the largest job is only 1,700 pages and the smallest job 1,300. In general, we have the following problem: Input: A given arrangement S of non-negative numbers and an integer k. Output: Partition S into k ranges, so as to minimize the maximum sum over all the ranges. This so-called linear partition problem arises often in parallel processing, since we seek to balance the work done across processors so as to minimize the total elapsed run time. Indeed, the war story of Section revolves around a botched solution to this problem. Stop for a few minutes and try to find an algorithm to solve the linear partition problem. The beginning algorist might suggest a heuristic as the most natural approach to solve the partition problem. Perhaps they would compute the average size of a partition, , and then try to insert the dividers so as to come close to this average. However, such heuristic methods are doomed to fail on certain inputs, because they do not systematically evaluate all possibilities. Instead, consider a recursive, exhaustive search approach to solving this problem. Notice that the kth partition starts right after we placed the (k-1)st divider. Where can we place this last divider? Between the ith and (i+1)st elements for some i, where file:///E|/BOOK/BOOK2/NODE45.HTM (1 of 5) [19/1/2003 1:28:44]
The Partition Problem . What is the cost of this? The total cost will be the larger of two quantities, (1) the cost of the last partition and (2) the cost of the largest partition cost formed to the left of i. What is the size of this left partition? To minimize our total, we would want to use the k-2 remaining dividers to partition the elements as equally as possible. This is a smaller instance of the same problem, and hence can be solved recursively! Therefore, let us define M[n,k] to be the minimum possible cost over all partitionings of into k ranges, where the cost of a partition is the largest sum of elements in one of its parts. Thus defined, this function can be evaluated: with the natural basis cases of By definition, this recurrence must return the size of the optimal partition. But how long does it take? If we evaluate the recurrence without storing partial results, we will be doomed to spend exponential time, repeatedly recalculating the same quantities. However, by storing the computed values and looking them up as needed, we can save an enormous amount of time. How long does it take to compute this when we store the partial results? Well, how many results are computed? A total of cells exist in the table. How much time does it take to compute the result M[n',k']? Well, calculating this quantity involves finding the minimum of n' quantities, each of which is the maximum of the table lookup and a sum of at most n' elements. If filling each of k n boxes takes at most time per box, the total recurrence can be computed in time. To complete the implementation, we must specify the boundary conditions of the recurrence relation and an order to evaluate it in. These boundary conditions always settle the smallest possible values for each of the arguments of the recurrence. For this problem, the smallest reasonable value of the first argument is n=1, meaning that the first partition consists of a single element. We can't create a first partition smaller than regardless of how many dividers are used. The smallest reasonable value of the second argument is k=1, implying that we do not partition S at all. The evaluation order computes the smaller values before the bigger values, so that each evaluation has what it needs waiting for it. Full details are provided in the pseudocode below: Partition[S,k] (* compute prefix sums: *) p[0] = 0 for i=1 to n do (* initialize boundary conditions *) file:///E|/BOOK/BOOK2/NODE45.HTM (2 of 5) [19/1/2003 1:28:44]
- Page 131 and 132: War Story: Psychic Modeling Next: E
- Page 133 and 134: Exercises (b) If I prove that an al
- Page 135 and 136: Fundamental Data Types Next: Contai
- Page 137 and 138: Containers Next: Dictionaries Up: F
- Page 139 and 140: Dictionaries Next: Binary Search Tr
- Page 141 and 142: Binary Search Trees BinaryTreeQuery
- Page 143 and 144: Priority Queues Next: Specialized D
- Page 145 and 146: Specialized Data Structures Next: S
- Page 147 and 148: Sorting Next: Applications of Sorti
- Page 149 and 150: Applications of Sorting Figure: Con
- Page 151 and 152: Data Structures Next: Incremental I
- Page 153 and 154: Incremental Insertion Next: Divide
- Page 155 and 156: Randomization Next: Bucketing Techn
- Page 157 and 158: Randomization Next: Bucketing Techn
- Page 159 and 160: Bucketing Techniques Algorithms Mon
- Page 161 and 162: War Story: Stripping Triangulations
- Page 163 and 164: War Story: Stripping Triangulations
- Page 165 and 166: War Story: Mystery of the Pyramids
- Page 167 and 168: War Story: Mystery of the Pyramids
- Page 169 and 170: War Story: String 'em Up We were co
- Page 171 and 172: War Story: String 'em Up Figure: Su
- Page 173 and 174: Exercises Next: Implementation Chal
- Page 175 and 176: Exercises used to select the pivot.
- Page 177 and 178: Dynamic Programming Next: Fibonacci
- Page 179 and 180: Fibonacci numbers Next: The Partiti
- Page 181: Fibonacci numbers Next: The Partiti
- Page 185 and 186: The Partition Problem Figure: Dynam
- Page 187 and 188: Approximate String Matching Next: L
- Page 189 and 190: Approximate String Matching The val
- Page 191 and 192: Longest Increasing Sequence Will th
- Page 193 and 194: Minimum Weight Triangulation Next:
- Page 195 and 196: Limitations of Dynamic Programming
- Page 197 and 198: War Story: Evolution of the Lobster
- Page 199 and 200: War Story: Evolution of the Lobster
- Page 201 and 202: War Story: What's Past is Prolog Ne
- Page 203 and 204: War Story: What's Past is Prolog th
- Page 205 and 206: War Story: Text Compression for Bar
- Page 207 and 208: War Story: Text Compression for Bar
- Page 209 and 210: Divide and Conquer Next: Fast Expon
- Page 211 and 212: Fast Exponentiation Next: Binary Se
- Page 213 and 214: Binary Search Next: Square and Othe
- Page 215 and 216: Exercises Next: Implementation Chal
- Page 217 and 218: Exercises whose denominations are ,
- Page 219 and 220: The Friendship Graph Next: Data Str
- Page 221 and 222: The Friendship Graph friendship gra
- Page 223 and 224: Data Structures for Graphs linked t
- Page 225 and 226: War Story: Getting the Graph ``Well
- Page 227 and 228: Traversing a Graph Next: Breadth-Fi
- Page 229 and 230: Breadth-First Search Next: Depth-Fi
- Page 231 and 232: Depth-First Search Next: Applicatio
<strong>The</strong> Partition Problem<br />
Next: Approximate String Matching Up: Dynamic Programming Previous: Fibonacci numbers<br />
<strong>The</strong> Partition Problem<br />
Suppose that three workers are given the task of scanning through a shelf of books in search of a given piece of information. To<br />
get the job done fairly and efficiently, the books are to be partitioned among the three workers. To avoid the need to rearrange<br />
the books or separate them into piles, it would be simplest to divide the shelf into three regions and assign each region to one<br />
worker.<br />
But what is the fairest way to divide the shelf up? If each book is the same length, say 100 pages, the job is pretty easy. Just<br />
partition the books into equal-sized regions,<br />
so that everyone has 300 pages to deal with.<br />
But what if the books are not the same length? Suppose we used the same partition when the book sizes looked like this:<br />
I, for one, would volunteer to take the first section, with only 600 pages to scan, instead of the last one, with 2,400 pages. <strong>The</strong><br />
fairest possible partition for this shelf would be<br />
where the largest job is only 1,700 pages and the smallest job 1,300.<br />
In general, we have the following problem:<br />
Input: A given arrangement S of non-negative numbers and an integer k.<br />
Output: Partition S into k ranges, so as to minimize the maximum sum over all the ranges. This so-called linear partition<br />
problem arises often in parallel processing, since we seek to balance the work done across processors so as to minimize the<br />
total elapsed run time. Indeed, the war story of Section revolves around a botched solution to this problem.<br />
Stop for a few minutes and try to find an algorithm to solve the linear partition problem.<br />
<strong>The</strong> beginning algorist might suggest a heuristic as the most natural approach to solve the partition problem. Perhaps they<br />
would compute the average size of a partition, , and then try to insert the dividers so as to come close to this average.<br />
However, such heuristic methods are doomed to fail on certain inputs, because they do not systematically evaluate all<br />
possibilities.<br />
Instead, consider a recursive, exhaustive search approach to solving this problem. Notice that the kth partition starts right after<br />
we placed the (k-1)st divider. Where can we place this last divider? Between the ith and (i+1)st elements for some i, where<br />
file:///E|/BOOK/BOOK2/NODE45.HTM (1 of 5) [19/1/2003 1:28:44]