Introduction to Algorithms
October 24, 2004
Massachusetts Institute of Technology
6.046J/18.410J
Professors Piotr Indyk and Charles E. Leiserson
Handout 18
Problem Set 4 Solutions
Reading:
Chapters 17, 21.1–21.3
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 in the exercises.
Mark the top of each sheet with your name, the course number, the problem number, your
recitation section, the date and the names of any students with whom you collaborated.
Threehole punch your paper on submissions.
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 the 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. Full credit will be given only to correct algorithms
which are
which are described clearly
. Convoluted and obtuse descriptions will receive low marks.
Exercise 41.
The Ski Rental Problem
A father decides to start taking his young daughter to go skiing once a week. The daughter may
lose interest in the enterprise of skiing at any moment, so the
k
th week of skiing may be the last,
for
any
k
. Note that
k
is
unknown
.
The father now has to decide how to procure skis for his daughter for every weekly session (until
she quits). One can
buy
skis at a onetime cost of
B
dollars, or
rent
skis at a weekly cost of
R
dollars. (Note that one can buy skis at any time—e.g., rent for two weeks, then buy.)
Give a 2competitive algorithm for this problem—that is, give an online algorithm that incurs a
total cost of at most twice the ofﬂine optimal (i.e., the optimal scheme if
k
is known).
Problem 41.
Queues as Stacks
Suppose we had code lying around that implemented a stack, and we now wanted to implement a
queue. One way to do this is to use two stacks
S
1
and
S
2
. To insert into our queue, we push into
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
2
Handout 18: Problem Set 4 Solutions
stack
S
1
. To remove from our queue we first check if
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 ErikDemaine
 Algorithms, Analysis of algorithms, Skiing, Online algorithm, nearest donut shop

Click to edit the document details