61
Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer (DAC): subproblems are independent Dynamic Programming (DP): subproblems are not independent Overlapping subproblems: subproblems share sub- subproblems In solving problems with overlapping subproblems • A DAC algorithm does redundant work – Repeatedly solves common subproblems • A DP algorithm solves each problem just once – Saves its result in a table BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 1

Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Embed Size (px)

Citation preview

Page 1: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Dynamic Programming

• An algorithm design paradigm like divide-and-conquer• “Programming”: A tabular method (not writing computer

code)• Divide-and-Conquer (DAC): subproblems are independent• Dynamic Programming (DP): subproblems are not

independent• Overlapping subproblems: subproblems share sub-

subproblems• In solving problems with overlapping subproblems

• A DAC algorithm does redundant work– Repeatedly solves common subproblems

• A DP algorithm solves each problem just once– Saves its result in a table

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 1

Page 2: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Optimization Problems

• DP typically applied to optimization problems• In an optimization problem

– There are many possible solutions (feasible solutions)– Each solution has a value– Want to find an optimal solution to the problem

• A solution with the optimal value (min or max value)

– Wrong to say “the” optimal solution to the problem• There may be several solutions with the same optimal value

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 2

Page 3: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Development of a DP Algorithm

1. Characterize the structure of an optimal solution

2. Recursively define the value of an optimal solution

3. Compute the value of an optimal solution in a bottom-up fashion

4. Construct an optimal solution from the information computed in Step 3

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 3

Page 4: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Example: Matrix-chain Multiplication

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 4

Page 5: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Matrix-chain Multiplication: An ExampleParenthesization

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 5

Page 6: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Cost of Multiplying two Matrices

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 6

Page 7: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Matrix-chain Multiplication Problem

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 7

Page 8: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Counting the Number of Parenthesizations

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 8

Page 9: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Number of Parenthesizations

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 9

Page 10: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

The Structure of an Optimal Parenthesization

Step 1: Characterize the structure of an optimal solution

•Ai..j : matrix that results from evaluating the product Ai Ai+1 Ai+2 Aj

•An optimal parenthesization of the product A1A2An

– Splits the product between Ak and Ak1, for some 1≤k<n

(A1A2A3 Ak) · (Ak+1Ak+2 ... An)

– i.e., first compute A1..k and Ak+1..n and then multiply these two

•The cost of this optimal parenthesizationCost of computing A1..k

+ Cost of computing Ak+1..n

+ Cost of multiplying A1..k · Ak+1..n

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 10

Page 11: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Step 1: Characterize the Structure of an Optimal Solution

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 11

Page 12: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

The Structure of an Optimal Parenthesization

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 12

Page 13: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Step 2: Define Value of an Optimal Sol. Recursively(mij =?)

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 13

Page 14: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Step 2: Recursive Equation for mij

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 14

Page 15: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Step 2: mij = MIN{mik + mk+1, j +pi-1pk pj}

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 15

Page 16: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Computing the Optimal Cost (Matrix-Chain Multiplication)

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 16

Page 17: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Computing the Optimal Cost (Matrix-Chain Multiplication)

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 17

Page 18: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Algorithm for Computing the Optimal Costs

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 18

Page 19: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Algorithm for Computing the Optimal Costs

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 19

Page 20: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Algorithm for Computing the Optimal Costs

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 20

l 2for i 1 to n - 1m[i, i1] compute m[i, i1]for k i to i do {m[1, 2], m[2, 3], …, m[n-1, n]}

.

. (n-1) valuesl 3for i 1 to n - 2m[i, i2] compute m[i, i2]for k i to i1 do {m[1, 3], m[2, 4], …, m[n-2, n]}

.

. (n-2) valuesl 4for i 1 to n - 3m[i, i3] compute m[i, i3]for k i to i2 do {m[1, 4], m[2, 5], …, m[n-3, n]}

.

. (n-3) values

Page 21: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 21

Page 22: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 22

Page 23: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 23

Page 24: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 24

Page 25: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 25

Page 26: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 26

Page 27: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 27

Page 28: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Constructing an Optimal Solution

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 28

Page 29: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Constructing an Optimal Solution

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 29

Page 30: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Constructing an Optimal Solution

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 30

Page 31: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Example: Recursive Construction of an Optimal Solution

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 31

Page 32: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Example: Recursive Construction of an Optimal Solution

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 32

Page 33: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Example: Recursive Construction of an Optimal Solution

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 33

return A6

Page 34: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Elements of Dynamic Programming

• When should we look for a DP solution to an optimization problem?

• Two key ingredients for the problem

– Optimal substructure

– Overlapping subproblems

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 34

Page 35: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Optimal Substructure

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 35

Page 36: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Optimal Substructure

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 36

Page 37: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Optimal Substructure

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 37

Page 38: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Overlapping Subproblems

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 38

Page 39: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Overlapping Subproblems

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 39

Page 40: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Overlapping Subproblems

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 40

Page 41: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Recursive Matrix-chain Order

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 41

Page 42: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Running Time of RMC

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 42

Page 43: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 43

Page 44: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 44

Page 45: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Memoized Recursive Algorithm

• Offers the efficiency of the usual DP approach while maintaining top-down strategy

• Maintains an entry in a table for the soln to each subproblem

• Each table entry contains a special value to indicate that the entry has yet to be filled in

• When the subproblem is first encountered its solution is computed and then stored in the table

• Each subsequent time that the subproblem encountered the value stored in the table is simply looked up and returned

• The approach assumes that– The set of all possible subproblem parameters are known– The relation between the table positions and subproblems is

establishedBIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 45

Page 46: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Memoized Recursive Algorithm

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 46

Page 47: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Elements of Dynamic Programming: Summary

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 47

Page 48: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Elements of Dynamic Programming: Summary

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 48

Page 49: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Longest Common Subsequence

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 49

Page 50: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Longest Common Subsequence

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 50

Page 51: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Longest Common Subsequence

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 51

Page 52: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Characterizing a Longest Common Subsequence

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 52

Page 53: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Longest Common Subsequence Algorithm

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 53

Page 54: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

A Recursive Solution to Subproblems

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 54

Page 55: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

A Recursive Solution to Subproblems

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 55

Page 56: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Computing the Length of an LCS

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 56

Page 57: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Computing the Length of an LCS

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)57

Page 58: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Computing the Length of an LCS

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 58

Page 59: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Constructing an LCS

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 59

Page 60: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Constructing an LCS

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 60

Page 61: Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer

Longest Common Subsequence

BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 61

This improvement works if we only need the length of an LCS