Dynamic Programming An algorithm design paradigm like divideandconquer

  • Slides: 61
Download presentation
Dynamic Programming • • • An algorithm design paradigm like divide-and-conquer “Programming”: A tabular

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 BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 1

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

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 BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 2

Development of a DP Algorithm 1. Characterize the structure of an optimal solution 2.

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 BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 3

Example: Matrix-chain Multiplication BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)

Example: Matrix-chain Multiplication BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 4

Matrix-chain Multiplication: An Example Parenthesization BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma

Matrix-chain Multiplication: An Example Parenthesization BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 5

Cost of Multiplying two Matrices BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma

Cost of Multiplying two Matrices BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 6

Matrix-chain Multiplication Problem BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)

Matrix-chain Multiplication Problem BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 7

Counting the Number of Parenthesizations BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma

Counting the Number of Parenthesizations BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 8

Number of Parenthesizations BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)

Number of Parenthesizations BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 9

The Structure of an Optimal Parenthesization Step 1: Characterize the structure of an optimal

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 A 1 A 2. . . An – Splits the product between Ak and Ak+1, for some 1≤k<n (A 1 A 2 A 3. . . Ak) · (Ak+1 Ak+2. . . An) – i. e. , first compute A 1. . k and Ak+1. . n and then multiply these two • The cost of this optimal parenthesization Cost of computing A 1. . k + Cost of computing Ak+1. . n + Cost of multiplying A 1. . k · Ak+1. . n BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 10

Step 1: Characterize the Structure of an Optimal Solution BIL 741: Advanced Analysis of

Step 1: Characterize the Structure of an Optimal Solution BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 11

The Structure of an Optimal Parenthesization BIL 741: Advanced Analysis of Algorithms I (İleri

The Structure of an Optimal Parenthesization BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 12

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

Step 2: Define Value of an Optimal Sol. Recursively(mij =? ) BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 13

Step 2: Recursive Equation for mij BIL 741: Advanced Analysis of Algorithms I (İleri

Step 2: Recursive Equation for mij BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 14

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

Step 2: mij = MIN{mik + mk+1, j +pi-1 pk pj} BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 15

Computing the Optimal Cost (Matrix-Chain Multiplication) BIL 741: Advanced Analysis of Algorithms I (İleri

Computing the Optimal Cost (Matrix-Chain Multiplication) BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 16

Computing the Optimal Cost (Matrix-Chain Multiplication) BIL 741: Advanced Analysis of Algorithms I (İleri

Computing the Optimal Cost (Matrix-Chain Multiplication) BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 17

Algorithm for Computing the Optimal Costs BIL 741: Advanced Analysis of Algorithms I (İleri

Algorithm for Computing the Optimal Costs BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 18

Algorithm for Computing the Optimal Costs BIL 741: Advanced Analysis of Algorithms I (İleri

Algorithm for Computing the Optimal Costs BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 19

Algorithm for Computing the Optimal Costs l=2 for i = 1 to n -

Algorithm for Computing the Optimal Costs l=2 for i = 1 to n - 1 m[i, i+1] = for k = i to i do. . l=3 for i = 1 to n - 2 m[i, i+2] = for k = i to i+1 do. . l=4 for i = 1 to n - 3 m[i, i+3] = for k = i to i+2 do. . compute m[i, i+1] {m[1, 2], m[2, 3], …, m[n-1, n]} (n-1) values compute m[i, i+2] {m[1, 3], m[2, 4], …, m[n-2, n]} (n-2) values compute m[i, i+3] {m[1, 4], m[2, 5], …, m[n-3, n]} (n-3) values BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 20

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Constructing an Optimal Solution BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme

Constructing an Optimal Solution BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 28

Constructing an Optimal Solution BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme

Constructing an Optimal Solution BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 29

Constructing an Optimal Solution BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme

Constructing an Optimal Solution BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 30

Example: Recursive Construction of an Optimal Solution BIL 741: Advanced Analysis of Algorithms I

Example: Recursive Construction of an Optimal Solution BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 31

Example: Recursive Construction of an Optimal Solution BIL 741: Advanced Analysis of Algorithms I

Example: Recursive Construction of an Optimal Solution BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 32

Example: Recursive Construction of an Optimal Solution return A 6 BIL 741: Advanced Analysis

Example: Recursive Construction of an Optimal Solution return A 6 BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 33

Elements of Dynamic Programming • When should we look for a DP solution to

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 BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 34

Optimal Substructure BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 35

Optimal Substructure BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 35

Optimal Substructure BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 36

Optimal Substructure BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 36

Optimal Substructure BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 37

Optimal Substructure BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 37

Overlapping Subproblems BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 38

Overlapping Subproblems BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 38

Overlapping Subproblems BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 39

Overlapping Subproblems BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 39

Overlapping Subproblems BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 40

Overlapping Subproblems BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 40

Recursive Matrix-chain Order BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)

Recursive Matrix-chain Order BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 41

Running Time of RMC BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme

Running Time of RMC BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 42

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

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

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

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

Memoized Recursive Algorithm • Offers the efficiency of the usual DP approach while maintaining

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 established BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 45

Memoized Recursive Algorithm BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)

Memoized Recursive Algorithm BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 46

Elements of Dynamic Programming: Summary BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma

Elements of Dynamic Programming: Summary BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 47

Elements of Dynamic Programming: Summary BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma

Elements of Dynamic Programming: Summary BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 48

Longest Common Subsequence BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)

Longest Common Subsequence BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 49

Longest Common Subsequence BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)

Longest Common Subsequence BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 50

Longest Common Subsequence BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)

Longest Common Subsequence BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 51

Characterizing a Longest Common Subsequence BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma

Characterizing a Longest Common Subsequence BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 52

Longest Common Subsequence Algorithm BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme

Longest Common Subsequence Algorithm BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 53

A Recursive Solution to Subproblems BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma

A Recursive Solution to Subproblems BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 54

A Recursive Solution to Subproblems BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma

A Recursive Solution to Subproblems BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 55

Computing the Length of an LCS BIL 741: Advanced Analysis of Algorithms I (İleri

Computing the Length of an LCS BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 56

Computing the Length of an LCS BIL 741: Advanced Analysis of Algorithms I (İleri

Computing the Length of an LCS BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 57

Computing the Length of an LCS BIL 741: Advanced Analysis of Algorithms I (İleri

Computing the Length of an LCS BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 58

Constructing an LCS BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)

Constructing an LCS BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 59

Constructing an LCS BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)

Constructing an LCS BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 60

Longest Common Subsequence This improvement works if we only need the length of an

Longest Common Subsequence This improvement works if we only need the length of an LCS BIL 741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I) 61