Dynamic Programming Problem Set Partial Solution CMPSC 465



Similar documents
Section IV.1: Recursive Algorithms and Recursion Trees

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Binary Search Trees CMPSC 122

Dynamic Programming. Lecture Overview Introduction

CS 2302 Data Structures Spring 2015

Kenken For Teachers. Tom Davis June 27, Abstract

Optimal Binary Search Trees Meet Object Oriented Programming

Order of Operations More Essential Practice

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

McKinsey Problem Solving Test Top Tips

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Price Theory Lecture 4: Production & Cost

Lecture 2 Mathcad Basics

CSIS 202: Introduction to Computer Science Spring term Midterm Exam

Mathematics Task Arcs

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Lecture 13: The Knapsack Problem

MAKE BIG MONEY QUICKLY! Low Start Up Cost! Easy To Operate Business! UNLIMITED INCOME POTENTIAL!

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

3. Mathematical Induction

START HERE THE BASICS TIPS + TRICKS ADDITIONAL HELP. quick start THREE SIMPLE STEPS TO SET UP IN UNDER 5 MINUTES

A guide to Sage One Accounts from your accountant

Make and register your lasting power of attorney a guide

Grade 7/8 Math Circles Sequences and Series

Helping you find and get on in work

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

Pre-Algebra Lecture 6

Pigeonhole Principle Solutions

HOW TO GET THE MOST OUT OF YOUR ACCOUNTANT

Equity Release your essential guide

Handouts for teachers

Sequential Data Structures

c. Given your answer in part (b), what do you anticipate will happen in this market in the long-run?

NPI Number Everything You Need to Know About NPI Numbers

ARKANSAS WORKERS COMPENSATION COMMISSION 324 Spring Street P.O. Box 950 Little Rock, AR

Basic Components of an LP:

Data Structures and Algorithms Written Examination

Investigating Investment Formulas Using Recursion Grade 11

Near Optimal Solutions

Ummmm! Definitely interested. She took the pen and pad out of my hand and constructed a third one for herself:

Financial Mathematics

Session 6 Number Theory

CPS122 - OBJECT-ORIENTED SOFTWARE DEVELOPMENT. Team Project

KEY FEATURES OF THE PERSONAL PENSION (TOP UP PLAN) Important information you need to read

PT AVENUE GUIDE OVERVIEW

Quick Start Guide Getting Started with Stocks

What you should know about: Windows 7. What s changed? Why does it matter to me? Do I have to upgrade? Tim Wakeling

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

The Mathematics 11 Competency Test Percent Increase or Decrease

Exercise 8: SRS - Student Registration System

The Cost of Production

MOST FREQUENTLY ASKED INTERVIEW QUESTIONS. 1. Why don t you tell me about yourself? 2. Why should I hire you?

This booklet is for candidates who are applying for entry level jobs with New York State and with local governments in the state.

Chapter 11 Number Theory

South Dakota. Opportunity Scholarship. Frequently Asked Questions

Lecture 12 Doubly Linked Lists (with Recursion)

Session 7 Fractions and Decimals

Discrete Mathematics and Probability Theory Fall 2009 Satish Rao, David Tse Note 10

To be used in conjunction with the Invitation to Tender for Consultancy template.

Better Together. Best regards, Team Gynzy

6th Grade Lesson Plan: Probably Probability

CASH ISA SAVINGS CONDITIONS. For use from 2nd September 2016.

Demand, Supply, and Market Equilibrium

PROBLEM SOLVING. 1. I m thinking of buying to let - where do I start?

Models for Incorporating Block Scheduling in Blood Drive Staffing Problems

Understanding Options: Calls and Puts

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Unit 5 Length. Year 4. Five daily lessons. Autumn term Unit Objectives. Link Objectives

NAME OF ASSESSMENT: Reading Informational Texts and Opinion Writing Performance Assessment

Reading 13 : Finite State Automata and Regular Expressions

What qualities are employers looking for in teen workers? How can you prove your own skills?

Research Tools & Techniques

2 Mathematics Curriculum

14.1 Rent-or-buy problem

Roman Numerals Case Study 1996 M. J. Clancy and M. C. Linn

Scheduling. Getting Started. Scheduling 79

Data Structures and Algorithms

BT Cloud Phone. A simpler way to manage your business calls.

Abstract The purpose of this paper is to present the results of my action research which was conducted in several 7 th /8 th grade language arts

How to Make the Most of Excel Spreadsheets

Key features of the Home Retail Group Personal Pension Plan

Using trusts can help to make sure your financial plans take care of the future

Online Fixed Rate Cash ISA Range

s = s = s =

Problem Set 7 Solutions

First programming project: Key-Word indexer

1.2 Solving a System of Linear Equations

A Guide to Cover Letter Writing

A Quick Algebra Review

Lecture 11: Tail Recursion; Continuations

Sample Online Syllabus

BUSINESS OCR LEVEL 2 CAMBRIDGE TECHNICAL. Cambridge TECHNICALS FINANCIAL FORECASTING FOR BUSINESS CERTIFICATE/DIPLOMA IN K/502/5252 LEVEL 2 UNIT 3

Social Return on Investment

Scheduling Best Practices

Solutions to Math 51 First Exam January 29, 2015

12 Tips for Negotiating with Suppliers

Criminal Justice I. Mr. Concannon Smith Website:

How to Use Solo Ads to. Grow Your Business

Transcription:

Dynamic Programming Problem Set Partial Solution CMPSC 465 I ve annotated this document with partial solutions to problems written more like a test solution. (I remind you again, though, that a formal problem write-up is not a test. You must introduce everything you re doing. You must write so that your writing tells everything that is going on. That requires good transitions and writing that flows, like you find in books and like you should learn in writing classes.) I draw attention here both to things that are very relevant for the final and areas where problems happened. Guidelines: You may opt to work on this assignment alone, with one partner, or in a group of three students. Turn in one submission per group, as always. There is to be absolutely no collaboration outside of these teams; you may not discuss the work with anyone other than your teammate(s) and the course staff. You may use any materials handed out in class and CLRS, but are strictly forbidden from using any other resources. This is a formal assignment. All of your solutions must stand alone, that is, one should not need to have this document in hand to make sense of your solutions. Present everything clearly, following standard conventions of written English and mathematical writing. Each problem should be presented starting on a new sheet of paper. We should be able to collect each problem separately without any difficulty. This assignment is due at the start of your lecture section on the last day of class, Friday, April 26, 2013. No late work is accepted; there is no time left in the semester to accept it late. Problem 1 Suppose you re running a consulting business that can move easily. It s just you and a few colleagues and a small amount of equipment. You have clients in both the northeast and northwest and are looking to optimize your business. Each month, you can either run your business out from an office in Boston (B) or in Seattle (S). The demands of your clients yield operating costs that vary based on where your current home base is. Thus, in month i, your operating cost is B i if you run the business out of Boston your operating cost is S i if you run the business out of Seattle Also, if you run the business out of one city in one month and switch to the other in the next month, you ll incur a moving cost to switch base offices. That moving cost is M. Given a sequence of n months, a plan is a sequence of n locations, each either S or B, such that the ith value represents the city where the business is housed during month i. The cost for a plan is the sum of the operating costs plus any necessary moving costs. You can begin a plan in either city. Your task is to find a optimal plan, where optimal means minimum cost in this case, given inputs M; B 1, B 2,, B n ; and S 1, S 2,, S n. For example, suppose that n = 4, M = 10, and operating costs are city month 1 month 2 month 3 month 4 Boston 1 3 20 30 Seattle 50 20 2 4 Then the plan of minimum cost would be {B, B, S, S} with a total cost of 1 + 3 + 2 + 4 + 10 = 20. Your tasks: a. Show that the following strategy does not correctly solve the problem: In each month, select the city whose operating cost is smaller. b. Give and explain an example of an instance in which every optimal plan must move at least three times. Page 1 of 5 PSU CMPSC 465 Spring 2013

c. Derive, using the principles of dynamic programming, a function that takes the inputs given above and returns the cost for an optimal plan. As with class examples, we start with the end and consider what could happen in month n. There are two cases: We could run our consulting business in Boston in month n. We could run our consulting business in Seattle in month n. An optimal plan over n months considers information about all n months. It is impossible to make any decisions with knowing about all n months. Thus, we ultimately will need to compare the optimal value of a plan that ends in Boston with the optimal value of a plan that ends in Seattle. We will need to compute both plans using two different recursive functions. Define the following: Let OPT B (i) be the optimal cost of a plan over months 1 to i where the business runs in Boston in month i Let OPT S (i) be the optimal cost of a plan over months 1 to i where the business runs in Seattle in month i Now consider what happens if we run the business out of Boston in month i. Then it is possible that we were in Boston in the previous month or we were in Seattle in the previous month and moved. Consider each case: Case That We Were in Boston: In this case, we must pay to run in the business in Boston in month i, which incurs a cost of B i. In addition, we need to pay the cost of running the business in the first i 1 months. We want an optimal plan, and we know we were in Boston in month i 1, so the optimal cost over months 1 to i 1 is just OPT B (i 1). Case That We Were in Seattle: In this case, we must pay to run in the business in Boston in month i, which incurs a cost of B i. We also need to pay a moving cost of M to relocate the business from Seattle to Boston. Finally, we need to pay the cost of running the business in the first i 1 months. We want an optimal plan, and we know we were in Seattle in month i 1, so the optimal cost over months 1 to i 1 comes from the other optimization function: OPT S (i 1). We want the optimal cost for a plan where month i is in Boston, and optimal cost means paying less money, so we take the minimum of the costs incurred in the two cases. As B i is part of the cost in either case, though, we add it outside of the minimum calculation for simplicity. The resulting optimal cost function, defined for i 2, is: OPT B (i) = B i + min(opt B (i 1), M + OPT S (i 1)) with a base case OPT B (1) = B 1 as this base case represents running the business for one month in Boston. We must carefully define the function for running the business out of Seattle in month i. By similar reasoning, the optimal cost function, defined for i 2, is: OPT S (i) = S i + min(opt S (i 1), M + OPT B (i 1)) with a base case OPT S (1) = S 1. To find the cost of an optimal plan over all n months, then, we want the following: min(opt B (n), OPT S (n)) Some notes: 1. Some of you confused n and the index of the current month. The variable n is fixed and defined in the problem statement. 2. Some of you defined a function with two inputs and took care of the issue of where month i runs that way. Either solution is fine. 3. You must compute what happens both under the assumption we end in Boston (in month n) and under the assumption we end in Seattle (in month n). Then, you need to pick whichever of those is better, and the corresponding recurrence leads to an optimal solution (and the other becomes irrelevant). 4. Defining a month 0 with optimal costs of 0 is fine too. Page 2 of 5 PSU CMPSC 465 Spring 2013

d. Provide pseudocode for an efficient algorithm to compute the cost of an optimal plan. Recall from class (p. 7 of the first set of D.P. notes) that while dynamic programming solves naturally recursive problems, the subproblems would overlap were we to compute them in a top-down fashion recursively, so instead we compute them bottom-up iteratively and can get a nice Θ(n) algorithm: Let BOS_COST[1..n] be a new empty array to hold optimal costs assuming month n is in Boston Let SEA_COST[1..n] be a new empty array to hold optimal costs assuming month n is in Seattle BOS_COST[1] = B 1 SEA_COST[1] = S 1 for i = 2 to n BOS_COST[i] = B i + min(bos_cost[i 1], M + SEA_COST[i 1]) SEA_COST[i] = S i + min(sea_cost[i 1], M + BOS_COST[i 1]) return min(bos_cost[n], SEA_COST[n]) An alterative that is asymptotically as good is to compute a memoized version of the recursive algorithm. But, the simple iterative algorithm given above is definitely the cleanest solution to this problem. Page 3 of 5 PSU CMPSC 465 Spring 2013

Problem 2 Suppose you re managing a business that manufactures classroom materials (like chalk and whiteboard markers and paper and such) and ships them to universities all around the country. For each of the next n weeks, there is a projected supply s i of equipment (measured in pounds), which has to be shipped. Each week s supply can be carried by one of two freight companies: Company A charges a fixed rate r per pound (so it costs r s i to ship a week s supply s i ) Company B makes contracts for a fixed amount c per week, independent of how much weight is shipped. Company B only will allow contracts in blocks of three consecutive weeks at a time. Define a schedule as a sequence of freight companies over each of the n weeks and define the cost of a schedule as the total amount paid to both shipping companies over the duration of the schedule. Your tasks, on an input of a sequence of supply values s 1, s 2,, s n : a. Derive, using the principles of dynamic programming, a function that takes the inputs given above and returns the cost for an optimal schedule. Let OPT(i) be the cost of an optimal schedule over weeks 1 to i. Consider what could happen in the ith week: We could hire Company A in Week i. This means we would have to pay r s i to Company A for week i. We also need to pay for all of the prior weeks. We want to make the best schedule possible, so we ll select an optimal schedule over Weeks 1 to i 1. This incurs a cost of OPT(i 1). Thus, the grand total cost in this case is r s i + OPT(i 1). We could hire Company B in Week i. Since we must do this in three-week blocks, this means we would have had to have employed Company B in Weeks i 1 and i 2 as well. So, we d have to pay Company B for 3 weeks at a rate of c per week, or 3c total. Then we want an optimal schedule over all previous weeks, which in this case is Weeks 1 to i 3. That costs OPT(i 3). Thus, the grand total cost in this case is 3c + OPT(i 3). The best cost is certainly the cheaper, so we get, for i 3, OPT(i) = min( r s i + OPT(i 1), 3c + OPT(i 3)) Handing the base of this recurrence is much trickier. The easy case is OPT(0) = 0. But what about Weeks 1 and 2? Analysis via a possibility tree [not shown here] yields that there are three cases for Weeks 1 and 2: Pick A in both weeks. Pick A in Week 1 and B in Week 2. Pick B in both weeks. So, computing the minimum cost requires computing our recurrence using each of the three possibilities as base cases and selecting whichever gives the minimum result OPT(n). Some notes: 1. What makes this problem distinct is that it involves subproblems that go back to 1 smaller and 3 smaller. That s important. But it s not all that different from Weighted Interval Scheduling, except that now you know exactly how many weeks to look back. 2. Certainly, the trickiest part of this problem was dealing with the base cases. I won t make you do something so complicated in an exam scenario. But, you needed to be very careful here. You need to make sure you handle Weeks 1 and 2 carefully and don t define a recurrence that needs to know about Week -2 during Week 1. a. Looking at all three cases for Weeks 1 and 2 and computing the recurrence using all three possibilities is the best solution I ve seen and I ve not been convinced by anything else I saw (but there may be other tricks. ) b. You might opt to define three recurrences, one with each different set of base cases. Or, you might take the base situation as a second argument to the recurrence. 3. Related to #2, watch out for being to quick to claim that if i is 1, the optimal cost comes from picking A. It may be that this is the start of a three-week contact with B. We need to start at the end and work backwards. Page 4 of 5 PSU CMPSC 465 Spring 2013

b. Give and explain psuedocode for a polynomial-time algorithm that returns a schedule of minimum cost. In brief, the best solution is to define three arrays, e.g. AA_COSTS[0..n], AB_COSTS[0..n], and BB_COSTS[0..n], initialize element 0 of each to 0, and initialize elements 1 and 2 according to whichever case is selected. Then, iteratively compute elements 3 to n of each using the OPT(i) recurrence. At the end, return the min of AA_COSTS[n], AB_COSTS[n], and BB_COSTS[n]. This algorithm runs in Θ(n) time. Extra Credit Problem Implement the algorithm from Problem 1d in Java. Your program must use language-independent data structures. Also, it must follow the usual programming conventions and produce text-based output. No user interaction is required, but a test driver and text-based sample runs are. Present your work using the same template used for programming assignments. Note that you must have a correct algorithm in Problem 1d and must earn at least a B on this entire assignment to be eligible for any extra credit. (This requirement is to remind you to prioritize the important algorithm design and writing aspects of this assignment before you put any time into programming.) Disclaimer: Copying this solution and sharing it in any means outside of CMPSC 465 Spring 2013 whether with one person or posting it anywhere else is expressly forbidden. Any student who does so is in violation of the academic integrity policies of this course (and, as it is not explicitly documented, a 465 Spring 2013 student found to have shared this solution is to be penalized by a final grade drop of at least 2 letter grades) and anyone who uses this solution outside of CMPSC 465 Spring 2013 is in violation of academic integrity for wherever it is used. The bottom line: This is here to help you; please be honest and use it as it was intended only. Page 5 of 5 PSU CMPSC 465 Spring 2013