ps7sol

ps7sol - Introduction to Algorithms Massachusetts Institute...

This preview shows pages 1–3. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Algorithms November 13, 2006 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik Demaine and Madhu Sudan Problem Set 7 Solutions Problem Set 7 Solutions Problem 7-1. Typesetting In this problem you will write a program (real code that runs!) to solve the following typesetting scenario. Because of the trouble you may encounter while programming, we advise you to START THIS PROBLEM AS SOON AS POSSIBLE. You are given an input text consisting of a sequence of n words of lengths ‘ 1 , ‘ 2 , . . . , ‘ n , where the length of a word is the number of characters it contains. Your printer can only print with its built-in Courier 10-point fixed-width font set that allows a maximum of M characters per line. (Assume that ‘ i ≤ M for all i = 1 , 2 , . . . , n .) When printing words i and i + 1 on the same line, exactly one space character (blank) must be printed between the two words. In addition, any remaining space at the end of the line is padded with blanks. Thus, if words i through j are printed on a line, the number of extra space characters at the end of the line (after word j ) is M- j + i- j X k = i ‘ k . There are many ways to divide a paragraph into multiple lines. To produce nice-looking output, we want a division that fills each line as much as possible. A heuristic that has empirically shown itself to be effective is to charge a cost of the cube of the number of extra space characters at the end of each line. To avoid the unnecessary penalty for extra spaces on the last line, however, the cost of the last line is . In other words, the cost LINECOST ( i, j ) for printing words i through j on a line is given by LINECOST ( i, j ) = ∞ if words i through j do not fit on a line, if j = n (i.e., last line), M- j + i- ∑ j k = i ‘ k 3 otherwise . The total cost for typesetting a paragraph is the sum of the costs of all lines in the paragraph. An optimal solution is a division of the n words into lines in such a way that the total cost is minimized. (a) Argue that this problem exhibits optimal substructure. Solution: Note that LINECOST ( i, j ) is defined to be ∞ if the words i through j do not fit on a line to guarantee that no lines in the optimal solution overflow. (This relies on the assumption that the length of each word is not more than M .) Second, notice that LINECOST ( i, j ) is defined to be 0 when j = n , where n is the total number of words; only the actual last line has zero cost, not the recursive last lines of subproblems, 2 Problem Set 7 Solutions which, since they are not the last line overall, have the same cost formula as any other line. Now, consider an optimal solution of printing words 1 through n . Let i be the index of the first word printed on the last line of this solution. Then typesetting of words 1 , . . . , i- 1 must be optimal. Otherwise, we could paste in an optimal typesetting of these words and improve the total cost of solution, a contradiction. Also note thatof these words and improve the total cost of solution, a contradiction....
View Full Document

{[ snackBarMessage ]}

Page1 / 9

ps7sol - Introduction to Algorithms Massachusetts Institute...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online