The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998

18.04.2013 Views

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]

<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]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!