Introduction to Algorithms
Day 26
Massachusetts Institute of Technology
6.046J/18.410J
SingaporeMIT Alliance
SMA5503
Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson
Handout 27
Problem Set 8
MIT students:
This problem set is due in lecture on
Day 29
.
Reading:
Chapters 15, 16, 23
Both exercises and problems should be solved, but
only the problems
should be turned in.
Exercises are intended to help you master the course material. Even though you should not turn in
the exercise solutions, you are responsible for material covered by the exercises.
Mark the top of each sheet with your name, the course number, the problem number, your
recitation instructor and time, the date, and the names of any students with whom you collaborated.
MIT students:
Each problem should be done on a separate sheet (or sheets) of threehole punched
paper.
You will often be called upon to “give an algorithm” to solve a certain problem. Your writeup
should take the form of a short essay. A topic paragraph should summarize the problem you are
solving and what your results are. The body of your essay should provide the following:
1. A description of the algorithm in English and, if helpful, pseudocode.
2. At least one worked example or diagram to show more precisely how your algorithm works.
3. A proof (or indication) of the correctness of the algorithm.
4. An analysis of the running time of the algorithm.
Remember, your goal is to communicate. Graders will be instructed to take off points for convo
luted and obtuse descriptions.
Exercise 81.
Do exercise 15.44 on page 356 of CLRS
Exercise 82.
Do exercise 16.22 on page 384 of CLRS.
Exercise 83.
Do exercise 16.23 on page 384 of CLRS.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document2
Handout27:ProblemSet8
Exercise 84.
Do exercise 23.18 on page 567 of CLRS.
Exercise 85.
Do exercise 23.22 on page 573 of CLRS.
Problem 81. 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 have an input text consisting of a sequence of
words of lengths
, where the
length of a word is the number of characters it contains. Your printer can only print with its builtin
Courier 10point ﬁxedwidth font set that allows a maximum of
characters per line. (Assume
that
for all
.) When printing words
and
on the same line, 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
through
are printed on a line, the number
of extra space characters at the end of the line (after word
) is
There are many ways to divide a paragraph into multiple lines. To produce nicelooking output,
we want a division that ﬁlls 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
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '04
 PiotrIndykandCharlesE.Leiserson
 Algorithms, Typesetting, CLRS, Linotype machine, average completion time

Click to edit the document details