CSE 5311 Homework 3 Solution

Similar documents
Solutions to Homework 6

Matrix-Chain Multiplication

Linear Algebra Notes for Marsden and Tromba Vector Calculus

Near Optimal Solutions

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

Dynamic Programming. Lecture Overview Introduction

What does the number m in y = mx + b measure? To find out, suppose (x 1, y 1 ) and (x 2, y 2 ) are two points on the graph of y = mx + b.

Lecture 2 Matrix Operations

14.1 Rent-or-buy problem

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Triangle deletion. Ernie Croot. February 3, 2010

Unit 4: Dynamic Programming

3. Mathematical Induction

9.2 Summation Notation

Math 55: Discrete Mathematics

Sample Induction Proofs

Approximation Algorithms

The Point-Slope Form

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Lecture 18: Applications of Dynamic Programming Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Basic Proof Techniques

Answer Key for California State Standards: Algebra I

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

The Assignment Problem and the Hungarian Method

Mathematical Induction. Lecture 10-11

SECTION 10-2 Mathematical Induction

Scheduling Shop Scheduling. Tim Nieberg

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

Warshall s Algorithm: Transitive Closure

Single machine parallel batch scheduling with unbounded capacity

1.7 Graphs of Functions

Lecture 1: Systems of Linear Equations

Math 115 Spring 2011 Written Homework 5 Solutions

136 CHAPTER 4. INDUCTION, GRAPHS AND TREES

The theory of the six stages of learning with integers (Published in Mathematics in Schools, Volume 29, Number 2, March 2000) Stage 1

Unit 1 Number Sense. In this unit, students will study repeating decimals, percents, fractions, decimals, and proportions.

The Taxman Game. Robert K. Moniot September 5, 2003

Mathematical Induction

Graph Theory Problems and Solutions

arxiv: v2 [math.ho] 4 Nov 2009

Solution to Homework 2

A linear combination is a sum of scalars times quantities. Such expressions arise quite frequently and have the form

Weight of Evidence Module

Dynamic Programming Problem Set Partial Solution CMPSC 465

Analysis of Algorithms I: Optimal Binary Search Trees

Sum of Degrees of Vertices Theorem

v w is orthogonal to both v and w. the three vectors v, w and v w form a right-handed set of vectors.

Graph Theory Lecture 3: Sum of Degrees Formulas, Planar Graphs, and Euler s Theorem Spring 2014 Morgan Schreffler Office: POT 902

NOTES ON LINEAR TRANSFORMATIONS

x x y y Then, my slope is =. Notice, if we use the slope formula, we ll get the same thing: m =

COMBINATORIAL PROPERTIES OF THE HIGMAN-SIMS GRAPH. 1. Introduction

Kenken For Teachers. Tom Davis June 27, Abstract

5 Homogeneous systems

Homework until Test #2

Chapter 3. Cartesian Products and Relations. 3.1 Cartesian Products

Session 7 Fractions and Decimals

Systems of Linear Equations

Arrangements And Duality

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS. + + x 2. x n. a 11 a 12 a 1n b 1 a 21 a 22 a 2n b 2 a 31 a 32 a 3n b 3. a m1 a m2 a mn b m

Vieta s Formulas and the Identity Theorem

Algorithm Design and Analysis

Lecture 3: Finding integer solutions to systems of linear equations

CS473 - Algorithms I

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

1 Review of Newton Polynomials

Matrix Differentiation

Notes on Determinant

Practice with Proofs

2.1. Inductive Reasoning EXAMPLE A

Data Structures and Algorithms Written Examination

Solution of Linear Systems

LECTURE 4. Last time: Lecture outline

SYSTEMS OF PYTHAGOREAN TRIPLES. Acknowledgements. I would like to thank Professor Laura Schueller for advising and guiding me

Closest Pair Problem

6.3 Conditional Probability and Independence

1. Prove that the empty set is a subset of every set.

To give it a definition, an implicit function of x and y is simply any relationship that takes the form:

SCORE SETS IN ORIENTED GRAPHS

Scalar Valued Functions of Several Variables; the Gradient Vector

Chapter 3. Distribution Problems. 3.1 The idea of a distribution The twenty-fold way

Algorithm Design and Analysis Homework #1 Due: 5pm, Friday, October 4, 2013 TA === Homework submission instructions ===

Part 1 Expressions, Equations, and Inequalities: Simplifying and Solving

Acquisition Lesson Planning Form Key Standards addressed in this Lesson: MM2A3d,e Time allotted for this Lesson: 4 Hours

Lecture 2 Mathcad Basics

Applied Algorithm Design Lecture 5

Midterm Practice Problems

NF5-12 Flexibility with Equivalent Fractions and Pages

MODERN APPLICATIONS OF PYTHAGORAS S THEOREM

Chapter 19 Operational Amplifiers

CSE 135: Introduction to Theory of Computation Decidability and Recognizability

Chapter 6: Graph Theory

Definition Given a graph G on n vertices, we define the following quantities:

Pricing I: Linear Demand

The GMAT Guru. Prime Factorization: Theory and Practice

Cpt S 223. School of EECS, WSU

Solutions to Homework 10

Systems of Linear Equations in Three Variables

Linear Programming. March 14, 2014

INCIDENCE-BETWEENNESS GEOMETRY

Introduction to Scheduling Theory

Transcription:

CSE 5311 Homework 3 Solution Problem 15.1-1 Show that equation (15.4) follows from equation (15.3) and the initial condition T (0) = 1. We can verify that T (n) = n is a solution to the given recurrence by the substitution method. We note that for n = 0, the formula is true since 0 = 1. For n > 0, substituting into the recurrence and using the formula for summing a geometric series yields Problem 15.1- n 1 T (n) = 1 + j=0 j = 1 + ( n 1) = n Show, by means of a counterexample, that the following greedy strategy does not always determine an optimal way to cut rods. Define the density of a rod of length i to be p i, that is, its value per inch. The greedy strategy for a rod of length n cuts off a first piece of length i, where 1 i n, having maximum density. It then continues by applying the greedy strategy to the remaining piece of length n i. Here is a counterexample for the greedy strategy: length i 1 3 4 price p i 1 0 33 36 p i /i 1 10 11 9 Let the given rod length be 4. According to a greedy strategy, we first cut out a rod of length 3 for a price of 33, which leaves us with a rod of length 1 of price 1. The total price for the rod is 34. The optimal way is to cut it into two rods of length each fetching us 40 dollars. 1

Problem 15.1-3 Consider a modification of the rod-cutting problem in which, in addition to a price p i for each rod, each cut incurs a fixed cost of c. The revenue associated with a solution is now the sum of the prices of the pieces minus the costs of making the cuts. Give a dynamic-programming algorithm to solve this modified problem. MODIFIED CUT ROD( p, n, c ) l e t r [ 0.. n ] be a new array r [ 0 ] = 0 f o r j = 1 to n q = p [ j ] f o r i= 1 to j 1 q = max( q, p [ i ]+ r [ j i ] c ) r [ j ] = q r eturn r [ n ] The major modification required is in the body of the inner for loop, which now reads q = max(q, p[i] + r[j i] c). This change reflects the fixed cost of making the cut, which is deducted from the revenue. We also have to handle the case in which we make no cuts (when i equals j); the total revenue in this case is simply p[j]. Thus, we modify the inner for loop to run from i to j 1 instead of to j. The assignment q = p[j] takes care of the case of no cuts. If we did not make these modifications, then even in the case of no cuts, we would be deducting c from the total revenue. Problem 15.-4 Describe the subproblem graph for matrix-chain multiplication with an input chain of length n. How many vertices does it have? How many edges does it have, and which edges are they? The vertices of the subproblem graph are the ordered pairs v ij, where i j. If i = j, then there are no edges out of v ij. If i < j, then for every k such that i k j, the subproblem graph contain edges (v ij, v ik ) and (v ij, v i+1,j ). These edges indicate that to solve the subproblem of optimal parenthesizing the product A i A j, we need to solve subproblems of optimally parenthesizing the products A i A k and A k+1 A j. The number of vertices is i=1 j=i 1 = n(n + 1),

and the number of edges is i=1 j=i (j i) = = n i t (substituting t = j i) i=1 t=0 i=1 (n i)(n i + 1). Substituting r = n i and reversing the order of summation, we obtain i=1 (n i)(n i + 1) = 1 n 1 (r + r) r=0 = 1 ( (n 1)n(n 1) + 6 (n 1)n(n + 1) = 6 ) (n 1)n (by equations (A.3) and (A.1)) Thus, the subproblem graph has Θ(n ) vertices and Θ(n 3 ) edges. Problem 15.-5 Let R(i, j) be the number of times that table entry m[i, j] is referenced while computing other table entries in a call of MATRIX-CHAIN-ORDER. Show that the total number of references for the entire table is i=1 j=1 R(i, j) = n3 n 3 (Hint: You may find equation (A.3) useful.) Each time the l-loop executes, the i-loop executes n l +1 times. Each time the i-loop executes, the k-loop executes j i = l 1 times, each time referencing m twice. Thus the total number of times that an entry of m is referenced while 3

computing other entries is n i= (n l + 1)(l 1)/. Thus, i=1 j=i Problem 16.1-1 R(i, j) = (n l + 1)(l 1) l= n 1 = (n l)l l=1 n 1 n 1 = nl = l=1 n(n 1)n l=1 l (n 1)n(n 1) 6 = n 3 n n3 3n + n 3 = n3 n 3 Give a dynamic-programming algorithm for the activity-selection problem, based on recurrence (16.). Have your algorithm compute the sizes c[i, j] as defined above and also produce the maximum-size subset of mutually compatible activities. Assume that the inputs have been sorted as in equation (16.1). Compare the running time of your solution to the running time of GREEDY-ACTIVITY- SELECTOR. The tricky part is determining which activities are in the set S ij. If activity k is in S ij, then we must have i < k < j, which means that j i, but we must also have that f i s k and f k s j. If we start k at j 1 and decrement k, we can stop once k reaches i, but we can also stop once we find that f k. We create two fictitious activities, a 0 with f 0 = 0 and a n+1 with s n+1 =. We are interested in a maximum-size set A 0,n+1 of mutually compatible activities in S 0.n+1. We ll use tables c[0..n + 1, 0..n + 1]as in recurrence (16.) (so that c[i, j] = A ij, and act[0..n + 1, 0..n + 1], where act[i, j] is the activity k that we choose to put into A ij. We fill the tables in according to increasing difference j i, which we denote by l in the pseudocode. Since S ij = if j i <, we initialize c[i, j] = 0 for all i and c[i, i + 1] = 0 for 0 i n. As in RECURSIVE-ACTIVITY -SELECTOR and GREEDY -ACTIVITY-SELECTOR, the start and finish times are given as arrays s and f, where we assume that the arrays already include the two fictitious activities and that the activities are sorted by monotonically increasing finish time. DYNAMIC ACTIVITY SELECTOR( s, f, n ) l e t c [ 0.. n+1, 0.. n+1] and act [ 0.. n+1, 0.. n+1] be new t a b l e s f o r i = 0 to n c [ i, i ] = 0 4

c [ i, i +1] = 0 c [ n+1, n+1] = 0 f o r l = to n+1 f o r i = 0 to n l +1 j = i+l c [ i, j ] = 0 k = j 1 while f [ i ] < f [ k ] i f f [ i ] <= s [ k ] and f [ k ] <= s [ j ] and c [ i, k ] + c [ k, j ] + 1 > c [ i, j ] do c [ i, j ] = c [ i, k ] + c [ k, j ] + 1 act [ i, j ] = k k = k 1 p r i n t A max s i z e s e t o f mutually compatible a c t i v i t i e s p r i n t c [ 0, n+1] p r i n t The s e t c o n t a i n s PRINT ACTIVITIES( c, act, 0, n+1) PRINT ACTIVITIES( c, act, i, j ) i f c [ i, j ] > 0 k = act [ i, j ] p r i n t k PRINT ACTIVITIES( c, act, i, k ) PRINT ACTIVITIES( c, act, k, j ) The PRINT-ACTIVITIES procedure recursively prints the set of activities placed into the optimal solution A ij. It first prints the activity k that achieved the maximum value of C[i, j], and then it recurses to print the activities in A ik and A kj. The recursion bottoms out when c[i, j] = 0, so that A ij =. Whereas GREEDY-ACTIVITY-SELECTOR runs in Θ(n) time, the DYNAMIC- ACTIVITY -S ELECTOR procedure runs in O(n 3 ) time. Problem 16.1- Suppose that instead of always selecting the first activity to finish, we instead select the last activity to start that is compatible with all previously selected activities. Describe how this approach is a greedy algorithm, and prove that it yields an optimal solution. The proposed approach selecting the last activity to start that is compatible with all previously selected activities is really the greedy algorithm but starting from the end rather than the beginning. Another way to look at it is as follows. We are given a set S = {a 1, a,, a n } of activities, where a i = [s i, f i ), and we propose to find an optimal solution by selecting the last activity to start that is compatible with all previously selected activities. Instead, let us create a set S = {a 1, a,, a n}, where a i = [f i, s i ). That is, a i is a i in reverse. Clearly, a subset of {a 1, a,, a n } S is mutually compatible if and only if the corresponding subset {a 1, a,, a n} S is 5

also mutually compatible. Thus, an optimal solution for S maps directly to an optimal solution for S and vice versa. The proposed approach of selecting the last activity to start that is compatible with all previously selected activities, when run on S, gives the same answer as the greedy algorithm from the text selecting the first activity to finish that is compatible with all previously selected activities when run on S. The solution that the proposed approach finds for S corresponds to the solution that the text s greedy algorithm finds for S, and so it is optimal. Problem 16.1-3 Not just any greedy approach to the activity-selection problem produces a maximum-size set of mutually compatible activities. Give an example to show that the approach of selecting the activity of least duration from among those that are compatible with previously selected activities does not work. Do the same for the approaches of always selecting the compatible activity that overlaps the fewest other remaining activities and always selecting the compatible remaining activity with the earliest start time. For the approach of selecting the activity of least duration from those that are compatible with previously selected activities: This approach selects i i 1 3 s i 0 3 f i 3 4 6 duration 3 3 just a, but the optimal solution selects a 1, a 3. For the approach of always selecting the compatible activity that overlaps the fewest other remaining activities: This approach first selects a 6, and i i 1 3 4 5 6 7 8 9 10 11 s i 0 1 1 1 3 4 5 5 5 6 f i 3 3 3 4 5 6 7 7 7 8 # of overlapping activities 3 4 4 4 4 4 4 4 4 3 after that choice it can select only two other activities (one of a 1, a, a 3, a 4 and one of a 8, a 9, a 1 0, a 1 1). An optimal solution is a 1, a 5, a 7, a 1 1. For the approach of always selecting the compatible remaining activity with the earliest start time, just add one more activity with the interval Œ0; 14/ to the example in Section 16.1. It will be the first activity selected, and no other activities are compatible with it. 6

Problem 16.-4 Professor Gekko has always dreamed of inline skating across North Dakota. He plans to cross the state on highway U.S., which runs from Grand Forks, on the eastern border with Minnesota, to Williston, near the western border with Montana. The professor can carry two liters of water, and he can skate m miles before running out of water. (Because North Dakota is relatively flat, the professor does not have to worry about drinking water at a greater rate on uphill sections than on flat or downhill sections.) The professor will start in Grand Forks with two full liters of water. His official North Dakota state map shows all the places along U.S. at which he can refill his water and the distances between these locations. The professor s goal is to minimize the number of water stops along his route across the state. Give an efficient method by which he can determine which water stops he should make. Prove that your strategy yields an optimal solution, and give its running time. The optimal strategy is the obvious greedy one. Starting with both bottles full, Professor Gekko should go to the westernmost place that he can refill his bottles within m miles of Grand Forks. Fill up there. Then go to the westernmost refilling location he can get to within m miles of where he filled up, fill up there, and so on. Looked at another way, at each refilling location, Professor Gekko should check whether he can make it to the next refilling location without stopping at this one. If he can, skip this one. If he cannot, then fill up. Professor Gekko doesn t need to know how much water he has or how far the next refilling location is to implement this approach, since at each fillup, he can determine which is the next location at which he ll need to stop. This problem has optimal substructure. Suppose there are n possible refilling locations. Consider an optimal solution with s refilling locations and whose first stop is at the kth location. Then the rest of the optimal solution must be an optimal solution to the subproblem of the remaining n k stations. Otherwise, if there were a better solution to the subproblem, i.e., one with fewer than s 1 stops, we could use it to come up with a solution with fewer than s stops for the full problem, contradicting our supposition of optimality. This problem also has the greedy-choice property. Suppose there are k refilling locations beyond the start that are within m miles of the start. The greedy solution chooses the kth location as its first stop. No station beyond the kth works as a first stop, since Professor Gekko would run out of water first. If a solution chooses a location j < k as its first stop, then Professor Gekko could choose the kth location instead, having at least as much water when he leaves the kth location as if he d chosen the jth location. Therefore, he would get at least as far without filling up again if he had chosen the kth location. Problem 16.3-1 Explain why, in the proof of Lemma 16., if x.freq = b.freq, then we must have a.freq = b.freq = x.freq = y.freq. 7

We are given that x.freq y.freq are the two lowest frequencies in order, and that a.freq b.freq. Now, b.freq = x.freq a.freq x.freq a.freq = x.freq (since x.freq is the lowest frequency), and since y.freq b.freq, b.freq = x.freq y.freq x.freq y.freq = x.freq (since x.freq is the lowest frequency), Thus, if we assume that x.freq = b.freq, then we have that each of a.freq, b.freq, and y.freq equals x.freq, and so a.freq = b.freq = x.freq = y.freq. 8