Problem Set 8. MIT students: Each problem should be done on a separate sheet (or sheets) of three-hole punched paper.

Similar documents
Post-Routing Layer Assignment for Double Patterning

Submitting, checking and correcting your manuscript

Manuscript Preparation - Introduction. by Vonda N. McIntyre

ENGR 40M Project 3b: Programming the LED cube

Lesson 25: Solving Problems in Two Ways Rates and Algebra

PSC300 Operation Manual

Adaptive Key Frame Selection for Efficient Video Coding

CS 5014: Research Methods in Computer Science

Guidelines for Manuscript Preparation for Advanced Biomedical Engineering

Formatting Your Thesis or Dissertation

Technology Overview LTCC

SIMULATION MODELING FOR QUALITY AND PRODUCTIVITY IN STEEL CORD MANUFACTURING

Janice Lee. Recitation 2. TA: Milo Phillips-Brown

Presentation: Part 1 A little care goes a long, long way

Springer Guidelines For The Full Paper Production

Corporate Identity and Visual Identity Guidelines June 2011

Guide for Authors. Agricultural Economics, 8 (1992) Elsevier Science Publishers B.V., Amsterdam. Manuscript

The perforator machine below shows in the front, the three keys. The left is for dots, the centre is for space and the right is for dashes.

Guidelines for the posters in M. Huber's courses. 1. Introduction

mcs 2015/5/18 1:43 page 15 #23

Mission. Lab Project B

A Step-by-Step Guide to Writing a Good History Day Paper

J.J. Thomson, Cathode Rays and the Electron

Sample Paper NAT IE. Instructions

Otter Bay Books A GUIDE FOR AUTHORS OTTER BAY S PROMISES. A Quality Book COMMENTS FROM OUR AUTHORS

3.22 Finalize exact specifications of 3D printed parts.

Instructions for producing camera-ready manuscript using MS-Word for publication in conference proceedings *

Signalling Cable Equivalent Sizes

AskDrCallahan Calculus 1 Teacher s Guide

Classroom Setup... 2 PC... 2 Document Camera... 3 DVD... 4 Auxiliary... 5

CPS311 Lecture: Sequential Circuits

Santa Clara University Department of Electrical Engineering

You will be first asked to demonstrate regular operation with default values. You will be asked to reprogram your time values and continue operation

AWT Guidelines for Speakers

Computer Aided Book Binding Design

Chapter 08 - Mechanization of the Printing Press

Introduction to Probability Exercises

Charles University in Prague

A MACHINE MADE THIS BOOK

Lecturer, JIMS, Vasantkunj

NOTEBOOKS. C. General Guidelines for Maintaining the Lab Notebook

1/ 19 2/17 3/23 4/23 5/18 Total/100. Please do not write in the spaces above.

Journal of Undergraduate Research Submission Acknowledgment Form

Offset Printing Workbook

Standard L A T E X Report

VGA Extender LR EXT-VGA-141LR. User s Manual

Table of Contents. What s Saddle-Stitching

ILLUSTRATING Children s. Everything you need to know about book finishes. Grab some books off your bookshelf (any will do), and tell me:

WELDING CONTROL UNIT: TE 450 USER MANUAL

Note: Please use the actual date you accessed this material in your citation.

The Computer Revolution in Education:

Brand Identity Standards

Research Article. ISSN (Print) *Corresponding author Shireen Fathima

Chapter 5: Synchronous Sequential Logic

EECS 270 Group Homework 4 Due Friday. June half credit if turned in by June

1.0 SDSU Research Foundation. The New SDSU Research Foundation Graphic Identity System

DTS400B - DZS400BP 3/9/07 10:14 AM Page 1

Part 1: Introduction to Computer Graphics

Why t? TEACHER NOTES MATH NSPIRED. Math Objectives. Vocabulary. About the Lesson

This Errata Sheet contains corrections or changes made after the publication of this manual.

Leveraging 300 mm Technology Solutions to Enable New MEMS Process Capabilities

Sequential Storyboards introduces the storyboard as visual narrative that captures key ideas as a sequence of frames unfolding over time

Informatics Enlightened Station 1 Sunflower

Walk-around Graphing Activity: Cubed Roots Level 1

Chapter 2: Lines And Points

SCENEMASTER 3F QUICK OPERATION

2 nd Int. Conf. CiiT, Molika, Dec CHAITIN ARTICLES

Formed Channel Letters Durable and Flexible

MATH BOOKMAKING IDEAS TO FLIP, FLAP, AND FOLD

Display Dilemma. Display Dilemma. 1 of 12. Copyright 2008, Exemplars, Inc. All rights reserved.

TITLE OF CHAPTER FOR PD FCCS MONOGRAPHY: EXAMPLE WITH INSTRUCTIONS

Copyright 2008 Society of Manufacturing Engineers. FUNDAMENTALS OF TOOL DESIGN Progressive Die Design

Title: Camera-Ready Guidelines for Thermal Conductivity 30/Thermal Expansion 18

BOOK ARTS & LETTERPRESS: COURSE SYLLABUS

Computer Systems Architecture

A BEM STUDY ON THE EFFECT OF SOURCE-RECEIVER PATH ROUTE AND LENGTH ON ATTENUATION OF DIRECT SOUND AND FLOOR REFLECTION WITHIN A CHAMBER ORCHESTRA

Chapter 10: Books and the Power of Print

Chapter 5 Synchronous Sequential Logic

Digital Integrated Circuits EECS 312

Identification - electrical services

Digital Logic. ECE 206, Fall 2001: Lab 1. Learning Objectives. The Logic Simulator

Proceedings of the Third International DERIVE/TI-92 Conference

NCPC 2007 Problem A: Phone List 3. Problem A. Phone List

The new four subterranean halls in the Musikverein Building in Vienna

PRESS FOR SUCCESS. Meeting the Document Make-Ready Challenge

Discoloration and ratty dust jacket. Pen underlining. Moderate wear.

Dissertation/Thesis Preparation Manual College of Graduate Studies Austin Peay State University

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR NPTEL ONLINE CERTIFICATION COURSE. On Industrial Automation and Control

CPA Standard 014. Clearing Replacement Document Design Standard

NSI45020T1G. Constant Current Regulator & LED Driver. 45 V, 20 ma 15%

Blasting to Open Ramelli Pit

Kaytee s Contest. Problem of the Week Teacher Packet. Answer Check

Graphics Standards Manual

Department of American Studies M.A. thesis requirements

COLOUR CHANGING USB LAMP KIT

Laboratory 1 - Introduction to Digital Electronics and Lab Equipment (Logic Analyzers, Digital Oscilloscope, and FPGA-based Labkit)

Build your own: Track Display

Cambridge International Examinations Cambridge International General Certificate of Secondary Education

Memorandum. December 1, The Doctoral Candidate. Office of the Registrar. Instructions for Preparing the Doctoral Dissertation

Amendments to the Harmonized System Explanatory Notes

Transcription:

Introduction to Algorithms Day 26 Massachusetts Institute of Technology 6.046J/18.410J Singapore-MIT 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 three-hole punched paper. 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 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 convoluted and obtuse descriptions. Exercise 8-1. Do exercise 15.4-4 on page 356 of CLRS Exercise 8-2. Do exercise 16.2-2 on page 384 of CLRS. Exercise 8-3. Do exercise 16.2-3 on page 384 of CLRS.

, -, " 2 Handout 27: Problem Set 8 Exercise 8-4. Do exercise 23.1-8 on page 567 of CLRS. Exercise 8-5. Do exercise 23.2-2 on page 573 of CLRS. Problem 8-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 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 built-in Courier 10-point fixed-width 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 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&' )( for printing words through on a line is given by +, / if words through do not fit on a line, linecost&' )(*., % if 0 (i.e., last line), 1 "4# "576 23 otherwise The total cost for typesetting a paragraph is the sum of the costs of all lines in the paragraph. An optimal solution is an division of the words into lines in such a way that the total cost is minimized. (a) Argue that this problem exhibits optimal substructure. (b) Recursively define the value of an optimal solution. (c) Give an efficient algorithm to compute the cost of an optimal solution. Analyze the running time and storage space of your algorithm. (d) Write code (in any programming language you wish 1 ) to print an optimal division of the words into lines. For simplicity, assume that a word is any sequence of characters that are not blanks. Thus, a word is a substring strictly between two space characters or bounded by the beginning or end of the input. 1 The solution will be written in C.

-! D Handout 27: Problem Set 8 3 For part (d), you should turn in a printout of the code you have written and the output of your program on the input samples which are provided at the end of the problem set. Use two val ues of (the maximum number of characters per line), namely 8:9 and ;7%, on each input sample. You can download these input samples from the 6.046 webpage. If, for some reason, you need to type the samples in yourself, please be careful about reproducing text accurately. For example, substitut ing double quotes in place of two single quotes will result in a different layout. Remember that collaboration, as usual, is allowed to solve problems, but you must write your program by yourself. (e) Suppose instead that the cost of a line is defined as just the number of extra spaces. That is, when words through are put on a line, the cost of that line is +, / if words through do not fit on a line, linecost <=&= >(*,. % if 0 (i.e., last line),?@ 3 "$# " otherwisea and that the total cost is still the sum of the costs of all lines in the paragraph. Give an efficient algorithm that finds an optimal solution in this case. Problem 8-2. Scheduling to minimize average completion time Suppose you are given a set B of tasks, where task requires C units of processing time to complete, once it has started. You have one computer on which to run these tasks, and the computer can run only one task at a time. A schedule assigns which tasks run during what times on the computer. For any schedule, let D denote the completion time of task, that is, the time at which task completes processing. Your goal is to find a schedule that minimizes the average completion time, that is, minimizing E # (a) Suppose there are two tasks with C GF and C IH. Consider (1) the schedule in which task 1 runs first, followed by task 2 and (2) the schedule in which task 2 runs first, followed by task 1. In each case, state the values of D and D and compute the average completion time. (b) Give an algorithm that schedules the tasks so as to minimize the average completion time. Each task must run nonpreemptively, that is, once task is started, it must run continuously for C units of time. Prove that your algorithm minimizes the average completion time, and state the running time of your algorithm. Suppose now that the tasks are not all available at once. That is, each task has a release time J before which it is not available to be processed. Suppose also that we allow preemption, so that a task can be suspended and restarted at a later time. For example, a task with processing time

C 4 Handout 27: Problem Set 8 LK may start running at time 1 and be preempted at time 4. It can then resume at time 10 but be preempted at time 11 and finally resume at time 13 and complete at time 15. Task has run for a total of 6 time units, but its running time has been divided into three pieces. (c) Give an algorithm that schedules the tasks so as to minimize the average completion time in this new scenario. Prove that your algorithm minimizes the average completion time, and state the running time of your algorithm. Input sample 1. (From A Capsule History of Typesetting, by Brown, R.J.) The first practical mechanized type casting machine was invented in 1884 by Ottmar Mergenthaler. His invention was called the Linotype. It produced solid lines of text cast from rows of matrices. Each matrice was a block of metal usually brass into which an impression of a letter had been engraved or stamped. The line-composing operation was done by means of a keyboard similar to a typewriter. A later development in line composition was the Teletypewriter. It was invented in 1913. This machine could be attached directly to a Linotype or similar machines to control composition by means of a perforated tape. The tape was punched on a separate keyboard unit. A tape-reader translated the punched code into electrical signals that could be sent by wire to tape-punching units in many cities simultaneously. The first major news event to make use of the Teletypewriter was World War I. Input Sample 2. (From an 1882 issue of the London Graphic.) The process of electrotyping may be briefly described as follows: The wood block or color plate is placed in a bed of wax, which has been melted, and allowed to cool until it has arrived at the proper consistency. It is then submitted to a great pressure in a press of hydraulic or other construction. In this way a facsimile of the original is produced but with every detail reversed. This was impression is then covered with a thin coating of black lead, such being a good conductor of electricity, and is hung by means of a brass rod in a large bath filled with a solution of sulfate of copper and sulfuric acid. Side by side with this bath is a powerful battery of Smee s construction, that is to say zinc, and platinized silver in dilute sulfuric acid. The current generated by this battery is put into connection with the wax mold hung in the bath, and also with a sheet of copper also hung side by side with the mold. The effect of the electricity is in the first place to decompose the copper and in the second place to attract the particles of copper to the mold. In a short time a thin coating of copper has formed along the mold, of which it is again the reverse, and consequently the exact facsimile of the original block. The shell, as it is called, is then filled up at the back with metal in order to make the surface perfectly hard and suitable for printing. After being made smooth and uniform in thickness by means of lathes and planing machines, it is mounted upon wood and is ready for the machine. Input Sample 3. (Of unknown origin.) The International Typographical Union, was described as, the oldest union in America, and organized to prevent the use of labor saving improvements. The union fought hard for its members and when times were hard would send money and train fare to unemployed Typesetters, and direct

Handout 27: Problem Set 8 5 them to places where prospects were better. When preset advertising copy began to be provided by advertisers, in the late nineteenth century, the union required that this type could be used as received only if a union Typesetter was employed to reset, print, proof, and throw away the same copy. The union leader who negotiated this requirement is reported to have been a Mr. Bogus, and this redundant make-work typesetting was called bogus type and added a word to the language. (There are other explanations for the word, but none contradicts this one). Even as late as the 1980s, most type was set on lead casting machines, and the production manager at the San Jose News complained that his reporters stories were being retyped by 400-dollar a month secretaries who type 80 words a minute and don t make mistakes, and then retyped at 40 words a minute on Linotype machines by 800-dollar a month Typesetters who do make mistakes. Input sample 4. (From Out of Their Minds, by Shasha, Lazere. Springer-Verlag, New York, 1995, page 99.) Throughout his life, Knuth had been intrigued by the mechanics of printing and graphics. As a boy at Wisconsin summer camp in the 1940s, he wrote a guide to plants and illustrated the flowers with a stylus on the blue ditto paper that was commonly used in printing at that time. In college, he recalls admiring the typeface used in his math texbooks. But he was content to leave the mechanics of designing and setting type to the experts. I never thought I would have any control over printing. Printing was done by typographers, hot lead, scary stuff. Then in 1977, I learned about new printing machines that print characters made out of zeros and ones, just bits, no lead. Suddenly, printing was a computer science problem. I couldn t resist the challenge of developing computer tools using the new technology with which to write my next books. Knuth designed and implemented TeX, a computer language for digital typography. He explored the field of typography with characteristic thoroughness. For example, he wrote a paper called The letter S in which he dissects the mathematical shape of that letter through the ages, and explains his several day effort to find the equation that yields the most pleasing outline. Here is what Sample 1 should look like when typeset with HM%. Feel free to use this output to debug your code. Total cost=5304 [43] The first practical mechanized type casting [38] machine was invented in 1884 by Ottmar [42] Mergenthaler. His invention was called the [43] "Linotype". It produced solid lines of text [46] cast from rows of matrices. Each matrice was a [45] block of metal -- usually brass -- into which [46] an impression of a letter had been engraved or

6 Handout 27: Problem Set 8 [46] stamped. The line-composing operation was done [47] by means of a keyboard similar to a typewriter. [43] A later development in line composition was [46] the "Teletypewriter". It was invented in 1913. [42] This machine could be attached directly to [41] a Linotype or similar machines to control [46] composition by means of a perforated tape. The [45] tape was punched on a separate keyboard unit. [46] A tape-reader translated the punched code into [48] electrical signals that could be sent by wire to [50] tape-punching units in many cities simultaneously. [45] The first major news event to make use of the [31] Teletypewriter was World War I.