ps4sol

ps4sol - Introduction to Algorithms Massachusetts Institute...

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

View Full Document Right Arrow Icon
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. Three-hole punch your paper on submissions. You will often be called upon to “give an algorithm” to solve a certain problem. Your write-up 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, pseudo-code. 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 4-1. 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 one-time 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 2-competitive algorithm for this problem—that is, give an online algorithm that incurs a total cost of at most twice the offline optimal (i.e., the optimal scheme if k is known). Problem 4-1. 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Handout 18: Problem Set 4 Solutions stack S 1 . To remove from our queue we first check if S 2
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/20/2012 for the course CS 6.006 taught by Professor Erikdemaine during the Fall '08 term at MIT.

Page1 / 5

ps4sol - Introduction to Algorithms Massachusetts Institute...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online