Data Structures and Algorithms with Object-Oriented Design Patterns in Python
next up previous 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.

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

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

The next step computes the one-line penalties tex2html_wrap_inline68329 as given by Equation gif (lines 8-14). This calculation is a straightforward one and its running time is also tex2html_wrap_inline58629.

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


next up previous index

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