📄 page461.html
字号:
<HTML><HEAD><TITLE>Application: Typesetting Problem</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html6477" HREF="page462.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6475" HREF="page458.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6471" HREF="page460.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6479" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0014430000000000000000">Application: Typesetting Problem</A></H2><A NAME="secalgstypeset"> </A><P>Consider the problem of typesetting a paragraph of justified text.A paragraph can be viewed as a sequence of <I>n</I><I>></I>0 words, <IMG WIDTH=114 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline67693" SRC="img1714.gif" >.The objective is to determine how to break the sequenceinto individual lines of text of the appropriate size.Each word is separated from the next by some amount of space.By stretching or compressing the space between the words,the left and right ends of consecutive lines of text are made to line up.A paragraph looks best when the amount ofstretching or compressing is minimized.<P>We can formulate the problem as follows:Assume that we are given the lengths of the words, <IMG WIDTH=94 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline68291" SRC="img1856.gif" >,and that the desired length of a line is <I>D</I>.Let <IMG WIDTH=28 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline68295" SRC="img1857.gif" > represent the sequence of wordsfrom <IMG WIDTH=15 HEIGHT=14 ALIGN=MIDDLE ALT="tex2html_wrap_inline67677" SRC="img1712.gif" > to <IMG WIDTH=16 HEIGHT=15 ALIGN=MIDDLE ALT="tex2html_wrap_inline68299" SRC="img1858.gif" > (inclusive).That is,<P> <IMG WIDTH=340 HEIGHT=17 ALIGN=BOTTOM ALT="displaymath68283" SRC="img1859.gif" ><P>for <IMG WIDTH=93 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline68301" SRC="img1860.gif" >.<P>Let <IMG WIDTH=25 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline68303" SRC="img1861.gif" > be the sum of the lengths of the words in the sequence <IMG WIDTH=28 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline68295" SRC="img1857.gif" >.That is,<P> <IMG WIDTH=294 HEIGHT=48 ALIGN=BOTTOM ALT="displaymath68284" SRC="img1862.gif" ><P>The <em>natural length</em>,for the sequence <IMG WIDTH=28 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline68295" SRC="img1857.gif" > is the sum of the lengths of the words, <IMG WIDTH=25 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline68303" SRC="img1861.gif" >plus the normal amount of space between those words.Let <I>s</I> be the normal size of the space between two words.Then the natural length of <IMG WIDTH=28 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline68295" SRC="img1857.gif" > is <IMG WIDTH=98 HEIGHT=25 ALIGN=MIDDLE ALT="tex2html_wrap_inline68315" SRC="img1863.gif" >.Note, we can also define <IMG WIDTH=25 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline68303" SRC="img1861.gif" > <em>recursively</em> as follows:<P><A NAME="eqnalgslength"> </A> <IMG WIDTH=500 HEIGHT=48 ALIGN=BOTTOM ALT="equation33265" SRC="img1864.gif" ><P><P>In general,when we typeset the sequence <IMG WIDTH=28 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline68295" SRC="img1857.gif" > all on a single line,we need to stretch or compress the spaces between the words so that the length of the line is the desired length <I>D</I>.Therefore, the amount of stretching or compressing is given bythe difference <IMG WIDTH=142 HEIGHT=25 ALIGN=MIDDLE ALT="tex2html_wrap_inline68323" SRC="img1865.gif" >.However, if the sum of the lengths of the words, <IMG WIDTH=25 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline68303" SRC="img1861.gif" >,is longer than the desired line length <I>D</I>,it is not possible to typeset the sequence on a single line.<P>Let <IMG WIDTH=23 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline68329" SRC="img1866.gif" > be the <em>penalty</em> associated with typesettingthe sequence <IMG WIDTH=25 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline68303" SRC="img1861.gif" > on a single line.Then,<P><A NAME="eqnalgspenalty"> </A> <IMG WIDTH=500 HEIGHT=48 ALIGN=BOTTOM ALT="equation33278" SRC="img1867.gif" ><P>This definition is of penalty is consistent with the stated objectives:The penalty increases as the difference betweenthe natural length of the sequence and the desired length increases andthe infinite penalty disallows lines that are too long.<P>Finally, we define the quantity <IMG WIDTH=24 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline68121" SRC="img1817.gif" > for <IMG WIDTH=93 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline68301" SRC="img1860.gif" >as the minimum total penalty required to typeset the sequence <IMG WIDTH=28 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline68295" SRC="img1857.gif" >.In this case, the text may be all on one lineor it may be split over more than one line.The quantity <IMG WIDTH=24 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline68121" SRC="img1817.gif" > is given by<P><A NAME="eqnalgscost"> </A> <IMG WIDTH=500 HEIGHT=57 ALIGN=BOTTOM ALT="equation33290" SRC="img1868.gif" ><P><P>We obtain Equation <A HREF="page461.html#eqnalgscost"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> as follows:When <I>i</I>=<I>j</I> there is only one word in the paragraph.The minimum total penalty associated with typesetting the paragraphin this case is just the penalty which results from puttingthe one word on a single line.<P>In the general case, there is more than one word in the sequence <IMG WIDTH=28 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline68295" SRC="img1857.gif" >.In order to determine the optimal way in which to typeset the paragraphwe consider the cost of putting the first <I>k</I> words of the sequenceon the first line of the paragraph, <IMG WIDTH=25 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline68347" SRC="img1869.gif" >,plus the minimum total cost associated with typesettingthe rest of the paragraph <IMG WIDTH=42 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline68349" SRC="img1870.gif" >.The value of <I>k</I> which minimizes the total cost alsospecifies where the line break should occur.<P><BR> <HR><UL> <LI> <A NAME="tex2html6480" HREF="page462.html#SECTION0014431000000000000000">Example</A><LI> <A NAME="tex2html6481" HREF="page463.html#SECTION0014432000000000000000">Implementation</A></UL><HR><A NAME="tex2html6477" HREF="page462.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6475" HREF="page458.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6471" HREF="page460.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6479" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -