Framework for Design of Dynamic Programming Algorithms

Similar documents
Chapter 3 Dynamic programming

CS473-Algorithms I. Lecture 10. Dynamic Programming. Cevdet Aykanat - Bilkent University Computer Engineering Department

Lecture 13: Chain Matrix Multiplication

1 i n (p i + r n i ) (Note that by allowing i to be n, we handle the case where the rod is not cut at all.)

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

Greedy Algorithms 1 {K(S) K(S) C} For large values of d, brute force search is not feasible because there are 2 d {1,..., d}.

Write an algorithm to find the maximum value that can be obtained by an appropriate placement of parentheses in the expression

Chain Matrix Multiplication

Dynamic Programming. Design and Analysis of Algorithms. Entwurf und Analyse von Algorithmen. Irene Parada. Design and Analysis of Algorithms

We augment RBTs to support operations on dynamic sets of intervals A closed interval is an ordered pair of real

Efficient Sequential Algorithms, Comp309. Problems. Part 1: Algorithmic Paradigms

1 Non greedy algorithms (which we should have covered

Dynamic Programming II

15.4 Longest common subsequence

Algorithm Design Techniques part I

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

Theorem 2.9: nearest addition algorithm

CMPSCI 311: Introduction to Algorithms Practice Final Exam

Greedy algorithms is another useful way for solving optimization problems.

We ve done. Now. Next

Parameterized graph separation problems

The divide and conquer strategy has three basic parts. For a given problem of size n,

Greedy Algorithms 1. For large values of d, brute force search is not feasible because there are 2 d

Mathematical and Algorithmic Foundations Linear Programming and Matchings

Elements of Dynamic Programming. COSC 3101A - Design and Analysis of Algorithms 8. Discovering Optimal Substructure. Optimal Substructure - Examples

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017

/463 Algorithms - Fall 2013 Solution to Assignment 3

Faster parameterized algorithms for Minimum Fill-In

Solution for Homework set 3

We will give examples for each of the following commonly used algorithm design techniques:

Subset sum problem and dynamic programming

CS141: Intermediate Data Structures and Algorithms Dynamic Programming

1. Lecture notes on bipartite matching February 4th,

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Computer Science 236 Fall Nov. 11, 2010

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

Unit-5 Dynamic Programming 2016

Algorithms for Data Science

Greedy Algorithms CHAPTER 16

Problem Set 1. Solution. CS4234: Optimization Algorithms. Solution Sketches

1 Linear programming relaxation

3 No-Wait Job Shops with Variable Processing Times

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

COLUMN GENERATION IN LINEAR PROGRAMMING

MC 302 GRAPH THEORY 10/1/13 Solutions to HW #2 50 points + 6 XC points

Faster parameterized algorithms for Minimum Fill-In

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

CMSC351 - Fall 2014, Homework #4

AXIOMS FOR THE INTEGERS

On Covering a Graph Optimally with Induced Subgraphs

/ Approximation Algorithms Lecturer: Michael Dinitz Topic: Linear Programming Date: 2/24/15 Scribe: Runze Tang

Lecture 15 : Review DRAFT

6.856 Randomized Algorithms

MAT 243 Test 2 SOLUTIONS, FORM A

Greedy Algorithms and Matroids. Andreas Klappenecker

Midterm solutions. n f 3 (n) = 3

Handout 9: Imperative Programs and State

Rigidity, connectivity and graph decompositions

arxiv:cs/ v1 [cs.ds] 20 Feb 2003

1. Lecture notes on bipartite matching

Towards a Memory-Efficient Knapsack DP Algorithm

Advanced Algorithms Class Notes for Monday, November 10, 2014

Problem Set 1. Problem 1-1.

Lecture 4: Dynamic programming I

Lecture 2 - Graph Theory Fundamentals - Reachability and Exploration 1

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

Selection (deterministic & randomized): finding the median in linear time

Week 11: Minimum Spanning trees

6.001 Notes: Section 4.1

15.4 Longest common subsequence

Algorithms for Integer Programming

Algorithms - Ch2 Sorting

Greedy Algorithms and Matroids. Andreas Klappenecker

1 Matching in Non-Bipartite Graphs

1. Chapter 1, # 1: Prove that for all sets A, B, C, the formula

Design and Analysis of Algorithms

CS261: Problem Set #1

Dynamic Programming. Outline and Reading. Computing Fibonacci

Lecture 57 Dynamic Programming. (Refer Slide Time: 00:31)

Chordal deletion is fixed-parameter tractable

Density estimation. In density estimation problems, we are given a random from an unknown density. Our objective is to estimate

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Dynamic Programming I Date: 10/6/16

UML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM

Discrete Mathematics Introduction

Algorithms and Data Structures

Algorithmics. Some information. Programming details: Ruby desuka?

CLASS-ROOM NOTES: OPTIMIZATION PROBLEM SOLVING - I

Divide and Conquer Algorithms

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Advanced Combinatorial Optimization September 17, Lecture 3. Sketch some results regarding ear-decompositions and factor-critical graphs.

Greedy Algorithms CLRS Laura Toma, csci2200, Bowdoin College

Figure 4.1: The evolution of a rooted tree.

12.1 Formulation of General Perfect Matching

Lecture 2: Getting Started

Matching Algorithms. Proof. If a bipartite graph has a perfect matching, then it is easy to see that the right hand side is a necessary condition.

Lecture 4: September 11, 2003

Algorithms. Ch.15 Dynamic Programming

NP-Hardness. We start by defining types of problem, and then move on to defining the polynomial-time reductions.

Density estimation. In density estimation problems, we are given a random from an unknown density. Our objective is to estimate

Transcription:

CSE 441T/541T Advanced Algorithms September 22, 2010 Framework for Design of Dynamic Programming Algorithms Dynamic programming algorithms for combinatorial optimization generalize the strategy we studied for design and analysis of greedy algorithms. To build a proof of optimality, we first expand the proof structure for greedy algorithms to accommodate multiple first choices and multiple subproblems. We then reduce the high-level algorithm description to a standard recurrence form and find an efficient computation order for it. 1 A Running Example: Matrix Chain Multiplication Let A 1... A N be a collection of matrices, where A i has size p i q i. Provided that these matrices are conformable (q i = p i+1 for every i), they can be multiplied. Multiplying an n p and a p m matrix requires, to a first approximation, npm operations. Because matrix multiplication is associative, any parenthesization of the product computation yields the same result; however, not all parenthesizations have the same cost. Our problem is to find a parenthesization of the matrix chain that minimizes the total cost of all pairwise multiplications needed to compute its product. 2 A High-Level Algorithm and Its Correctness Optimal parenthesization of matrix chains does not appear to have a good greedy algorithm. That is, no single first choice is necessarily consistent with optimality. We will therefore consider algorithms that make a collection of possible first choices and keep the one that yields the best answer. As the first choice in our example, we will decide how to split the chain A 1... A N into two parts, A 1... A k and A k+1... A N. To compute the product of the chain, we first compute product Π 1 from A 1... A k and Π 2 from A k+1... A N, then compute the final product as Π 1 Π 2. There are N 1 locations at which we could split the chain A 1... A N into two nonempty parts. If we were to recursively compute the costs of the best solutions that make each of these N 1 initial splits, we could improve our result by comparing these solutions and returning the best one overall. Trying multiple first choices might be a good heuristic, but does it guarantee an optimal solution? To argue for optimality, we will extend the inductive proof template from our work with greedy algorithms to accommodate more than one first choice. Here s roughly how the proof will look: 1. To prove: the proposed algorithm yields an optimal solution for problem instances of all sizes. 2. We will proceed by induction on the size of the problem instance P. 3. Bas: For the smallest possible P, we show directly that we obtain an optimal solution. (The smallest P is typically empty or, if that is not allowed, contains only one element.) 1

4. Ind: Let P be given, and let c 1... c n be the list of initial choices investigated by the algorithm for P. 5. After making choice c k, 1 k n, show that we are left with one or more strictly smaller subproblems P k,1... P k,m of P. 6. By the IH, applying our algorithm to each subproblem yields optimal subsolutions Π k,1... Π k,m. 7. We then show that combining the subsolutions with choice c k yields a feasible solution Π k to P, and that this solution is optimal among all solutions making choice c k. 8. Finally, we argue that at least one choice in c 1... c n is consistent with some globally optimal solution to P, and so taking the best Π k among 1 k n yields a global optimum. QED Fleshing out this template requires that we provide a base case argument in point 3 (required, but usually easy), and, crucially, that we provide justifications for points 5 and 7-8. Here follows some advice on how to supply these essential parts of the proof. 8. Show that at least one choice c k, 1 k n, is compatible with optimality. In other words, show that there is always an optimal solution to P that makes one of the choices c 1... c n ; hence, finding the best solution given each of these choices is guaranteed to discover a global optimum. This claim is the complete choice property for the algorithm. Here s an example of this argument for our example problem. Suppose Π is an optimal solution to problem instance P. At its top level, Π splits the matrices A 1... A N into two groups A 1... A k and A k+1... A N that are multiplied together. Let c k be the choice of splitting P after A k ; then Π must make one of the choices c 1... c N 1. Hence, finding optimal solutions given each of these choices and keeping the best result overall is guaranteed to yield a global optimum. QED Note: much of the time, as in the above example, this part of the proof is trivial because we simply make every possible first choice; hence, it is not hard to see that one such choice appears in some optimal solution. However, it may sometimes be possible to show that a more restricted set of first choices is guaranteed to produce an optimum. In the extreme case of greedy algorithms, one choice is always sufficient. 5 and 7a. Show that for all first choices c k, making choice c k leaves one or more strictly smaller versions of the original problem, each with no external constraints. This is the inductive structure property, just as in the greedy case. Note that you need to prove the property for every initial choice c k and every subproblem induced by this choice; however, you may be able to write the proof just once if all the proofs would be similar. In our example, making first choice c k leaves two subproblems P k,l and P k,r, corresponding to the matrix chains A 1... A k and A k+1... A N, respectively. Each of these two subproblems is strictly smaller than P. Moreover, any solutions Π k,l and Π k,r to the subproblems may feasibly be combined by multiplying the resulting product matrices, so there are no external constraints imposed on the subproblems solutions. The above holds for all 1 k N 1. 7b. Show that for all first choices c k, combining choice c k with optimal solutions to its induced subproblems yields a solution Π k to P that is optimal among all 2

solutions making choice c k. This is the optimal substructure property, just as in the greedy case. Again, you need to prove the property for every first choice c k. Each of these proofs can be done using the standard form of contradiction argument we saw in the greedy case. In our example, suppose that making choice c k leaves subproblems P k,l and P k,r with optimal solutions Π k,l and Π k,r. Combining these subsolutions with choice c k yields a parenthesization Π k for P. Denote by V (Π) the objective value associated with a solution Π. By the problem definition, we have that V (Π k ) = V (Π k,l) + V (Π k,r) + p 1 q k q N. Now suppose that Π k is not optimal for P among all solutions making choice c k. Let Π k be such an optimal solution. Because Π k makes the same initial choice as Π k, we have that V (Π k) = V (Π k,l) + V (Π k,r) + p 1 q k q N for some subsolutions Π k,l + Π k,r. But if Π k is better than Π k, it must be that V (Π k,l) + V (Π k,r) < V (Π k,l) + V (Π k,r), which is not possible unless at least one of Π k,l or Π k,r is better than Π k,l or Π k,r, respectively. But this contradicts the optimality of the Π s. Conclude that Π k must in fact be optimal for P among all solutions making choice c k. QED Note: as in the greedy case, this argument works without modification for any objective V that can be decomposed into a sum or product of parts, one of which depends only on the initial choice c k and the others of which depend only on the induced subproblems. 3 Describing an Efficient Solution We now know that our proposed algorithm is optimal, but we don t yet have a fully specified implementation of it. We will now develop a recurrence, or formal recursive description of our algorithm, which will help us work out an efficient way to implement it. Along the way, we will borrow some notation from the theory of polyhedral analysis, which is used by compiler writers and hardware designers to efficiently implement nested loops and other recursive or iterative computations. 3.1 Building a Recurrence Our recursive algorithm solves a large number of subproblems of the full problem P. To formally describe the algorithm, we will develop a simple notation for subproblems and use our observations from the optimality proof to describe how a subproblem s solution is built up from solutions to even smaller subproblems. We will index the subproblems of a problem instance P by a vector z of one or more integer coordinates. For example, subproblem z = [i, j] in our example seeks the optimal parenthesization of A i... A j, where 1 i j N. The set of all subproblems induced by a problem instance P is called the domain of that instance, which we will denote by D. Individual subproblems z D are sometimes referred to as points because they are described by their coordinates. 3

Now that we can formally identify subproblems, we will show how to recursively compute the objective value V [z] of an optimal solution to a subproblem z D. Informally, the computation is for each initial choice, compute the value of an optimal solution that makes that choice, then keep the best solution overall. The set of initial choices was part of our first, informal algorithm description, while the value of an optimal solution that makes this choice was computed in the proof of its substructure property. Suppose there are C(z) possible initial choices for subproblem z, and let V [z k] be the value of an optimal solution given choice c k. Then, if the optimization is a minimization, V [z] = min V [z k]. 1 k C(z) (For a maximization, replace the min by a max.) The expression for each term of the minimization is given by its decomposition into subproblems. The resulting equation is a recurrence for computing V [z]. For our example, let V [i, j] be the cost of an optimal parenthesization of A i... A j. We have that V [i, j] = min V [i, j k], i k<j and that V [i, j k] = V [i, k] + V [k + 1, j] + p i q k q j. If you prefer, you can combine these two definitions, like this: V [i, j] = min i k<j (V [i, k] + V [k + 1, j] + p iq k q j ). (In more complex recurrences, the terms for the value of each choice may have different mathematical formulas. Consult your optimal substructure proof for guidance.) To compute any recurrence, we need it to bottom out at non-recursive base cases, that is points z D whose values V [z] are known a priori. In our example, V [i, i] = 0 for any i, since there is no cost to compute a product of one matrix. Note that, as we descend the levels of the recursion, distances between i and j shrink, so we will eventually reach these base cases. Finally, we need to identify a goal point z in the domain that represents the solution to our full problem instance. In this case, the point [1, N] corresponds to the full instance, so the value V [1, N] is the value of an optimal solution to the problem instance P. Note: while computing V [1, N] only gives us the objective value of an optimal solution, the solution itself is given by the choices made, i.e. the argmins of each minimization starting from the goal point and descending to the base cases. Each minimization indicates where to split a subchain of the original matrix chain. This relation between computing the optimal objective value and obtaining the actual solution holds for every recurrence. 3.2 Solving the Recurrence Efficiently A recurrence gives a formula for the quantity we wish to compute, but it does not fully specify how to evaluate the formula. Naively, we could evaluate the recurrence top-down by generating the subproblems of the goal point, solving these recursively in turn, and then combining the solutions as the recurrence directs. However, a top-down procedure is grossly inefficient. Even if each initial choice generated only one subproblem, the resulting computation would have a branching factor 4

equal to the number of initial choices, which is at least 2 for non-greedy algorithms. If the recursion depth is polynomial in the input size, the resulting algorithm requires time exponential in that size! The problem with a top-down solution is that it winds up repeatedly solving the same subproblems. For example, V [1, 3] and V [2, 4] in our example problem both require computation of V [2, 3]. Fortunately, the polyhedral view of our recurrence suggests a better approach. To compute our goal value V [z ], we may at worst need to evaluate the objective V at every point z D. Let s define an evaluation order on D that lets us solve each subproblem exactly once. As we solve each subproblem z, we will store its value V [[z] in a table so that we can look it up in O(1) time whenever we need it later. This bottom-up evaluation of a recurrence is called dynamic programming. Not every evaluation order on D is feasible. For example, we can t compute V [1, 3] in our matrix problem until we know both V [1, 2] and V [2, 3]. In general, each point z D has one or more dependencies on smaller subproblems, which are specified by the recurrence itself. To realize a dynamic programming computation, we must find an evaluation order for D that is consistent with these dependencies. For our example, observe that V [i, j] depends on the values of subproblems V [r, s], where i r s j and s r < j i. We can therefore satisfy the recurrence s dependencies by solving subproblems V [i, j] in increasing order of their difference d = j i. The following pseudocode shows one total ordering of subproblems that meets this criterion: for d = 1... N 1 for i = 1... N d j i + d V [i, j] min i k<j (V [i, k] + V [k + 1, j] + p i q k q j ) return V [1, N] In general, the cost of a dynamic programming algorithm is the domain size D times the cost to compute each point V [z] given its dependencies. In this case, we have a two-dimensional domain with i j running from 1 to N, so the domain has Θ(N 2 ) points. Each point performs a minimization over at most N initial choices and so requires worst-case time Θ(N) to compute. Hence, the total cost of the algorithm is Θ(N 3 ). 4 Advice for Your Homework To develop a dynamic programming algorithm for class purposes, you should do all of the following: 1. Describe your set of initial choices. 2. Prove optimality of your proposed algorithm by addressing each of the three required properties: (a) complete choice (b) inductive structure (c) optimal substructure 5

Please clearly divide your argument into the above three pieces, with suitable headings. You need not write down the full inductive template, though it may help you to do so the first couple of times to make sure you understand your proof obligations. Feel free to abbreviate your optimal substructure proofs in the common case as for greedy algorithms. 3. Develop a recurrence to precisely describe your algorithm. You must provide all of the following: (a) a definition of the domain (give the coordinates of a general subproblem, e.g. [i, j], and explain what they mean); (b) a formula for computing the value of a general subproblem from its dependencies; (c) one or more base cases sufficient to ground the recurrence; (d) identification of the goal point z. The hardest part of writing the recurrence is developing its formula, but you should have already done this work as part of your optimal substructure proof. 4. Give an evaluation order on D that satisfies all dependencies of the recurrence, evaluating each subproblem only once. There may be more than one correct order! 5. Compute the asymptotic complexity of your algorithm. This will be independent of your evaluation order. 6