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 M[i,j] = D[i,j] = x for i=1 to n do M[i,1] = p[i] for i=1 to k do (* evaluate main recurrence *) for i=2 to n do for j = 2 to k do Figure: Dynamic programming matrices M and D in partitioning file:///E|/BOOK/BOOK2/NODE45.HTM (3 of 5) [19/1/2003 1:28:44] for x = 1 to i-1 do if (M[i,j] > ) then

The Partition Problem Figure: Dynamic programming matrices M and D in partitioning The implementation above in fact runs faster than advertised. Our original analysis assumed that it took time to update each cell of the matrix. This is because we selected the best of up to n possible points to place the divider, each of which requires the sum of up to n possible terms. In fact, it is easy to avoid the need to compute these sums by storing the set of n prefix sums , since . This enables us to evaluate the recurrence in linear time per cell, yielding an algorithm. By studying the recurrence relation and the dynamic programming matrices of Figures and , you should be able to convince yourself that the final value of M(n,k) will be the cost of the largest range in the optimal partition. However, what good is that? For most applications, what we need is the actual partition that does the job. Without it, all we are left with is a coupon for a great price on an out-of-stock item. The second matrix, D, is used to reconstruct the optimal partition. Whenever we update the value of M[i,j], we record which divider position was required to achieve that value. To reconstruct the path used to get to the optimal solution, we work backward from D[n,k] and add a divider at each specified position. This backwards walking is best achieved by a recursive subroutine: ReconstructPartition(S,D,n,k) If (k = 1) then print the first partition else ReconstructPartition(S,D,D[n,k],k-1) Print the kth partition { } It is important to catch the distinction between storing the value of a cell and what decision/move it took to get there. The latter is not used in the computation but is presumably the real thing that you are interested in. For most of the examples in this chapter, we will not worry about reconstructing the answer. However, study this example closely to ensure that you know how to obtain the winning configuration when you need it. file:///E|/BOOK/BOOK2/NODE45.HTM (4 of 5) [19/1/2003 1:28:44]

<strong>The</strong> Partition Problem<br />

M[i,j] =<br />

D[i,j] = x<br />

for i=1 to n do M[i,1] = p[i]<br />

for i=1 to k do<br />

(* evaluate main recurrence *)<br />

for i=2 to n do<br />

for j = 2 to k do<br />

Figure: Dynamic programming matrices M and D in partitioning<br />

file:///E|/BOOK/BOOK2/NODE45.HTM (3 of 5) [19/1/2003 1:28:44]<br />

for x = 1 to i-1 do<br />

if (M[i,j] > ) then

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

Saved successfully!

Ooh no, something went wrong!