Logo Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Implementation

Program gif defines the method typeset which takes three arguments. The first, l, is an array of n integers that gives the lengths of the words in the sequence to be typeset. The second, D, specifies the desired paragraph width and the third, s, specifies the normal inter-word space.

   program33533
Program: Dynamic programming example--typesetting a paragraph.

The method first computes the lengths, tex2html_wrap_inline67907, of all possible subsequences (lines 6-12). This is done by using the dynamic programming paradigm to evaluate the recursive definition of tex2html_wrap_inline67907 given in Equation gif. The running time for this computation is clearly tex2html_wrap_inline58202.

The next step computes the one-line penalties tex2html_wrap_inline67933 as given by Equation gif (lines 13-21). This calculation is a straightforward one and its running time is also tex2html_wrap_inline58202.

Finally, the minimum total costs, tex2html_wrap_inline67725, of typesetting each subsequence are determined for all possible subsequences (lines 22-37). Again we make use of the dynamic programming paradigm to evaluate the recursive definition of tex2html_wrap_inline67725 given in Equation gif. The running time for this computation is tex2html_wrap_inline58514. As a result, the overall running time required to determine the best way to typeset a paragraph of n words is tex2html_wrap_inline58514.


next up previous contents index

Bruno Copyright © 1998 by Bruno R. Preiss, P.Eng. All rights reserved.