Dynamic Programming 1/15/2019 8:22 PM Dynamic Programming.

Slides:



Advertisements
Similar presentations
Dynamic Programming Introduction Prof. Muhammad Saeed.
Advertisements

RAIK 283: Data Structures & Algorithms
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Data Structures Lecture 10 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Dynamic Programming Reading Material: Chapter 7..
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design technique Dynamic Programming is a.
CSC401 – Analysis of Algorithms Lecture Notes 12 Dynamic Programming
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
UNC Chapel Hill Lin/Manocha/Foskey Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject.
Dynamic Programming1 Modified by: Daniel Gomez-Prado, University of Massachusetts Amherst.
Dynamic Programming Reading Material: Chapter 7 Sections and 6.
© 2004 Goodrich, Tamassia Dynamic Programming1. © 2004 Goodrich, Tamassia Dynamic Programming2 Matrix Chain-Products (not in book) Dynamic Programming.
Lecture 8: Dynamic Programming Shang-Hua Teng. First Example: n choose k Many combinatorial problems require the calculation of the binomial coefficient.
Dynamic Programming A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River,
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
Dynamic Programming – Part 2 Introduction to Algorithms Dynamic Programming – Part 2 CSE 680 Prof. Roger Crawfis.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Dynamic Programming UNC Chapel Hill Z. Guo.
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
Dynamic Programming Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by or formulated.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject to some constraints. (There may.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Chapter 7 Dynamic Programming 7.1 Introduction 7.2 The Longest Common Subsequence Problem 7.3 Matrix Chain Multiplication 7.4 The dynamic Programming Paradigm.
Dynamic Programming … Continued 0-1 Knapsack Problem.
Dynamic Programming Typically applied to optimization problems
Merge Sort 5/28/2018 9:55 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Seminar on Dynamic Programming.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Chapter 8 Dynamic Programming
Pattern Matching 9/14/2018 3:36 AM
Chapter 8 Dynamic Programming.
CS38 Introduction to Algorithms
CSCE 411 Design and Analysis of Algorithms
Dynamic Programming.
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Dynamic Programming Dr. Yingwu Zhu Chapter 15.
Chapter 8 Dynamic Programming
ICS 353: Design and Analysis of Algorithms
ICS 353: Design and Analysis of Algorithms
Merge Sort 1/12/2019 5:31 PM Dynamic Programming Dynamic Programming.
Dynamic Programming Dynamic Programming 1/15/ :41 PM
Dynamic Programming.
CS6045: Advanced Algorithms
Dynamic Programming.
Dynamic Programming Dynamic Programming 1/18/ :45 AM
Merge Sort 1/18/ :45 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Merge Sort 1/18/ :45 AM Spring 2007
Merge Sort 2/22/ :33 AM Dynamic Programming Dynamic Programming.
A Note on Useful Algorithmic Strategies
A Note on Useful Algorithmic Strategies
A Note on Useful Algorithmic Strategies
A Note on Useful Algorithmic Strategies
Dynamic Programming-- Longest Common Subsequence
Dynamic Programming.
CSC 413/513- Intro to Algorithms
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Matrix Chain Product 張智星 (Roger Jang)
Merge Sort 4/28/ :13 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Lecture 5 Dynamic Programming
A Note on Useful Algorithmic Strategies
Dynamic Programming Merge Sort 5/23/2019 6:18 PM Spring 2008
A Note on Useful Algorithmic Strategies
Seminar on Dynamic Programming.
Presentation transcript:

Dynamic Programming 1/15/2019 8:22 PM Dynamic Programming

Dynamic Programming is a general algorithm design technique for solving problems defined by or formulated as recurrences with overlapping subinstances. Invented by American mathematician Richard Bellman in the 1950s to solve optimization problems and later assimilated by CS “Programming” here means “planning” Main idea: set up a recurrence relating a solution to a larger instance to solutions of some smaller instances - solve smaller instances once save solutions in a table extract solution to the initial instance from that table

Sample problems solved by DP Computing binomial coefficients Matrix Chain-Product 0-1 Knapsack Problem Longest Common Subsequence (LCS)

Computing Binomial Coefficients

Matrix Chain-Products

Matrix Chain-Products C = A*B A is d × e and B is e × f O(def ) A C B d f e i j i,j

Matrix Chain-Products Compute A=A0*A1*…*An-1 Problem: How to parenthesize? Example B is 3 × 100 C is 100 × 5 D is 5 × 5 (B*C)*D takes 1500 + 75 = 1575 operations B*(C*D) takes 1500 + 2500 = 4000 operations

Enumeration Approach Matrix Chain-Product Alg.: Running time: Try all possible ways to parenthesize A=A0*A1*…*An-1 Calculate number of operations for each one Pick the one that is best Running time: The number of parenthesizations is equal to the number of binary trees with n nodes This is exponential! It is called the Catalan number, and it is almost 4n. This is a terrible algorithm!

Greedy Approach Idea #1: repeatedly select the product that uses the fewest operations. Counter-example: A is 101 × 11 B is 11 × 9 C is 9 × 100 D is 100 × 99 Greedy idea #1 gives A*((B*C)*D)), which takes 109989+9900+108900=228789 ops (A*B)*(C*D) takes 9999+89991+89100=189090 ops The greedy approach is not giving us the optimal value.

Recursive Approach Define subproblems: Find the best parenthesization of Ai*Ai+1*…*Aj. Let Ni,j denote the number of operations done by this subproblem. The optimal solution for the whole problem is N0,n-1. Subproblem optimality: The optimal solution can be defined in terms of optimal subproblems There has to be a final multiplication (root of the expression tree) for the optimal solution. Say, the final multiplication is at index i: (A0*…*Ai)*(Ai+1*…*An-1). Then the optimal solution N0,n-1 is the sum of two optimal subproblems, N0,i and Ni+1,n-1 plus the time for the last multiplication.

Characterizing Equation The global optimal has to be defined in terms of optimal subproblems, depending on where the final multiplication is at. Let us consider all possible places for that final multiplication: Recall that Ai is a di × di+1 dimensional matrix. So, a characterizing equation for Ni,j is the following: subproblems are overlapping. rows of 1st matrix columns of 2nd matrix rows of 2nd matrix

Subproblem Overlap Algorithm RecursiveMatrixChain(S, i, j): Input: sequence S of n matrices to be multiplied Output: number of operations in an optimal parenthesization of S if i=j then return 0 for k  i to j do Ni, j  min{Ni, j , RecursiveMatrixChain(S, i ,k) + RecursiveMatrixChain(S, k+1,j)+ di dk dj} return Ni,j

Subproblem Overlap 1..4 1..1 2..4 1..2 3..4 1..3 4..4 2..2 1..1 2..2 3..3 4..4 ... 3..4 2..3 4..4 3..3 4..4 2..2 3..3

Dynamic Programming Algorithm Algorithm matrixChain(S): Input: sequence S of n matrices to be multiplied Output: number of operations in an optimal parenthesization of S for i  1 to n - 1 do Ni,i  0 for b  1 to n - 1 do { b = j - i is the length of the problem } for i  0 to n - b - 1 do j  i + b Ni,j  + for k  i to j - 1 do Ni,j  min{Ni,j, Ni,k + Nk+1,j + di dk dj} return N0,n-1 Since subproblems overlap, we don’t use recursion. Instead, we construct optimal subproblems “bottom-up.” Ni,i’s are easy, so start with them Then do problems of “length” 2,3,… subproblems, and so on. Running time: O(n3)

Dynamic Programming Algorithm Visualization The bottom-up construction fills in the N array by diagonals Ni,j gets values from previous entries in i-th row and j-th column Filling in each entry in the N table takes O(n) time. Total run time: O(n3) Getting actual parenthesization can be done by remembering “k” for each N entry N 1 2 i j … n-1 1 … answer i j n-1 Dynamic Programming

Dynamic Programming Algorithm Visualization A0: 30 X 35; A1: 35 X15; A2: 15X5; A3: 5X10; A4: 10X20; A5: 20 X 25

The General Dynamic Programming Technique Applies to a problem that at first seems to require a lot of time (possibly exponential), provided we have: Subproblem optimality: the global optimum value can be defined in terms of optimal subproblems Subproblem overlap: the subproblems are not independent, but instead they overlap (hence, should be constructed bottom-up).

The 0/1 Knapsack Problem https://www.youtube.com/watch?v=8LusJS5-AGo

The 0/1 Knapsack Problem Given: A set S of n items, with each item i having wi - a positive weight bi - a positive benefit Goal: Choose items with maximum total benefit but with weight at most W. If we are not allowed to take fractional amounts, then this is the 0/1 knapsack problem. In this case, we let T denote the set of items we take Objective: maximize Constraint: Dynamic Programming

A 0/1 Knapsack Algorithm, First Attempt Sk: Set of items numbered 1 to k. Define B[k] = best selection from Sk. Problem: does not have subproblem optimality: Consider set S={(3,2),(5,4),(8,5),(4,3),(10,9)} of (benefit, weight) pairs and total weight W = 20 Best for S4: Best for S5: Dynamic Programming

A 0/1 Knapsack Algorithm Sk: Set of items numbered 1 to k. Define B[k,w] to be the best selection from Sk with weight at most w Good news: this does have subproblem optimality. the best subset of Sk with weight at most w is either the best subset of Sk-1 with weight at most w or the best subset of Sk-1 with weight at most w-wk plus item k Dynamic Programming

0/1 Knapsack Algorithm Consider set S={(1,1),(2,2),(4,3),(2,2),(5,5)} of (benefit, weight) pairs and total weight W = 10. 2D array holds the benefits.

0/1 Knapsack Algorithm Trace back to find the items picked

0/1 Knapsack Algorithm Each diagonal arrow corresponds to adding one item into the bag Pick items 2,3,5 {(2,2),(4,3),(5,5)} are what you will take away Dynamic Programming

0/1 Knapsack Algorithm Recall the definition of B[k,w] Algorithm 01Knapsack(S, W): Input: set S of n items with benefit bi and weight wi; maximum weight W Output: benefit of best subset of S with weight at most W let A and B be arrays of length W + 1 for w  0 to W do B[w]  0 for k  1 to n do copy array B into array A for w  wk to W do if A[w-wk] + bk > A[w] then B[w]  A[w-wk] + bk return B[W] Recall the definition of B[k,w] Since B[k,w] is defined in terms of B[k-1,*], we can use two arrays of instead of a matrix Running time: O(nW). n is the number of items. Not a polynomial-time algorithm since W may be large This is a pseudo-polynomial time algorithm Dynamic Programming

Longest Common Subsequence Dynamic Programming Longest Common Subsequence https://www.youtube.com/watch?v=NnD96abizww

Longest Common Subsequence (LCS) A subsequence of a sequence/string S is obtained by deleting zero or more symbols from S. For example, the following are some subsequences of “president”: pred, sdn, predent. In other words, the letters of a subsequence of S appear in order in S, but they are not required to be consecutive. The longest common subsequence problem is to find a maximum length common subsequence between two sequences.

LCS For instance, Sequence 1: president Sequence 2: providence Its LCS is priden. president providence

LCS Sequence 1: algorithm Sequence 2: alignment Another example: Sequence 1: algorithm Sequence 2: alignment One of its LCS is algm. a l g o r i t h m a l i g n m e n t

How to compute LCS? Let A=a1a2…am and B=b1b2…bn . len(i, j): the length of an LCS between a1a2…ai and b1b2…bj With proper initializations, len(i, j) can be computed as follows.

Running time : O(mn)

if the value of row and column are the same characters, increase the left up value by one. if up is bigger or equal to left value, take up value. else value is left cell

The backtracing algorithm