dynamic programming
play

Dynamic Programming Chapter 15 Dictionary Definition Program - PowerPoint PPT Presentation

Dynamic Programming Chapter 15 Dictionary Definition Program (noun) \ pr - gram , - grm \ a sequence of coded instructions that can be inserted into a mechanism (such as a computer) Programming (noun) \ pr - gra- mi , - gr


  1. Dynamic Programming Chapter 15

  2. Dictionary Definition Program (noun) \ ˈprō - ˌgram , - grəm \ a sequence of coded instructions that can be inserted into a mechanism (such as a computer) Programming (noun) \ ˈprō - ˌ gra- miŋ , - grə -\ a plan of action to accomplish a specified end Synonyms progressing, plan out, arrange

  3. Rod Cutting Problem $13.10/ft $14.93/ft $21.10/ft $21.47/ft

  4. Rod Cutting Problem Given a rod of length 𝑜 inches and a table of prices 𝑞 𝑗 for 𝑗 = 1,2, … , 𝑜 , determine the maximum revenue 𝑠 𝑜 obtainable by cutting up the rod and selling the pieces. Naïve solution? Try all possibilities. Exponential!

  5. Recursive Solution Define 𝑠 𝑗 as the maximum possible revenue you can get for a rod of length 𝑗 We can express 𝑠 𝑗 as follows The value of the rod without cutting 0 𝑗 = 0 𝑞 𝑗 𝑠 𝑗 = ൞ max ൝ 𝑗 > 0 1≤𝑘<𝑗 𝑠 max 𝑘 + 𝑠 𝑗−𝑘 Final answer is 𝑠 Trying all 𝑜 possible cuts

  6. Optimal Substructure The problem is to find the optimal cut for a rod of length 𝑜 Assuming that we cut 𝑜 at position 𝑗 This leads to two subproblem, cutting two rods of lengths 𝑗 and 𝑜 − 𝑗 The optimal solution when 𝑜 is cut at position 𝑗 must include the optimal cuts of rods of lengths 𝑗 and 𝑜 − 𝑗 Proof by contradiction

  7. Recursive Solution (ruby) def rodcut(i,p) return 0 if i == 0 best_cut = p[i] for j in (1..i-1) value = rodcut(j,p) + rodcut(i-j,p) best_cut = [best_cut, value].max end return best_cut end

  8. Execution Recurrence Tree 𝑜 5 𝑜 4 𝑜 3 𝑜 2 𝑜 1 𝑜 3 𝑜 2 𝑜 1 𝑜 2 𝑜 1 𝑜 1 𝑜 2 𝑜 1 𝑜 1 𝑜 1

  9. Memoized Recursive Solution @best_cuts = [] def rodcut_memoized(i,p) return 0 if i == 0 return @best_cuts[i] if @best_cuts[i] best_cut = p[i] for j in (1..i-1) value = rodcut_memoized(j,p) + rodcut_memoized(i-j,p) best_cut = [best_cut, value].max end return @best_cuts[i] = best_cut end

  10. Iterative Bottom-up Solution def rodcut_bottomup(n,p) best_cuts = [] for i in (1..n) best_cuts[i] = p[i] for j in (1..i-1) value = best_cuts[j] + best_cuts[i-j] best_cuts[i] = [best_cuts[i], value].max end end return best_cuts[n] end

  11. Dynamic Programming You have a big problem You can break it down into smaller subproblems You don’t know the best way to split it So, you try all possibilities Identify similar subproblems that are solved many times Devise a better (polynomial) algorithm

  12. Matrix Chain Multiplication

  13. Example 5 50 50 100 5 A3 10 A1 10 Z 100 A2 𝐵 1 𝐵 2 𝐵 3 = 𝐵 1 𝐵 2 𝐵 3 Cost? 𝐵 1 𝐵 2 𝐵 3 = 𝐵 1 𝐵 2 𝐵 3

  14. Matrix Multiplication def multiply(a, b) raise "Incompatible sizes" if a.columns != b.rows c = Matrix.new(a.rows, b.columns) for i in (1..a.rows) for j in (1..b.columns) c[i][j] = 0 for k in (1..a.columns) c[i][j] += a[i][k] * b[k][j] end end end end

  15. Example 5 50 50 100 5 A3 10 A1 10 Z 100 A2 𝐷𝑝𝑡𝑢 𝐵 1 𝐵 2 𝐵 3 = 10 ∙ 100 ∙ 5 + 10 ∙ 5 ∙ 50 = 7,500 𝐷𝑝𝑡𝑢 𝐵 1 𝐵 2 𝐵 3 = 10 ∙ 100 ∙ 50 + 100 ∙ 5 ∙ 50 = 75,000

  16. Problem Given a sequence of (not necessarily square) matrices 𝐵 1 , 𝐵 2 , … , 𝐵 𝑜 where the dimensions of matrix 𝐵 𝑗 is 𝑞 𝑗−1 × 𝑞 𝑗 . We want to find the order of matrix multiplication operations to compute the product 𝐵 1 ∙ 𝐵 2 ∙ ⋯ ∙ 𝐵 𝑜 while minimizing the number of scalar multiplication operations. Hint: Number of scalar multiplication operations to computer 𝑌 ∙ 𝑍 with dimensions 𝑞 × 𝑟 and 𝑟 × 𝑠 , respectively, is 𝑞 ∙ 𝑟 ∙ 𝑠

  17. Recursive Implementation def matrix_chain_multiplication(p, i, j) return 0 if i == j # No cost for one matrix min_cost = Float::INFINITY for k in (i..j-1) # Cost of left chain cost = matrix_chain_multiplication(p, i, k)) + # Cost of right chain matrix_chain_multiplication(p, k+1, j) + # Cost of the final multiplication p[i-1] * p[k] * p[j] min_cost = [min_cost, cost].min end return min_cost end

  18. With Memoization @table = {} def matrix_chain_multiplication(p, i, j) return 0 if i == j # No cost for one matrix return @table[range] if @table[range] min_cost = Float::INFINITY for k in (i..j-1) # Cost of left chain cost = matrix_chain_multiplication(p, i, k)) + # Cost of right chain matrix_chain_multiplication(p, k+1, j) + # Cost of the final multiplication p[i-1] * p[k] * p[j] min_cost = [min_cost, cost].min end @table[range] = min_cost return min_cost end

  19. Recurrence Relation 𝑛[𝑗, 𝑘] : Minimum cost for multiplying matrices 𝑗 through 𝑘 ; 1 ≤ 𝑗 ≤ 𝑘 ≤ 𝑜 𝑛 𝑗, 𝑘 = 0 𝑗 = 𝑘 ቐ 𝑗≤𝑙<𝑘 𝑛 𝑗, 𝑙 + 𝑛 𝑙 + 1, 𝑘 + 𝑞 𝑗−1 𝑞 𝑙 𝑞 𝑘 min 𝑗 < 𝑘

  20. Bottom-up Solution def matrix_chain_multiplication_botoom_up(p) m = {}; n = p.size # Initialize the diagonal (bottom level) with zeros for i in (1..n) m[i..i] = 0 end for l in (2..n) # Level or chain Length for i in (1..(n-l+1)) j = i + l - 1 m[i..j] = Float::INFINITY for k in (i..(j-1)) q = m[i..k] + m[(k+1)..j] + p[i-1]*p[k]*p[j] m[i..j] = q if q < m[i..j] end end end return m[range] end

  21. Bottom-up Approach Final answer 𝑘 𝑗 Cost[ 𝐵 𝑗 .. 𝐵 𝑘 ] 0 0 0 0 0 0 … 𝐵 𝑜 𝐵 1 𝐵 2

  22. Longest Common Subsequence

  23. Definitions Sequence A C C G G T C G A G Subsequence A C G A A Subsequence G C T A A T Sequence G T C G T C G G A A T G C

  24. Definitions Sequence A C C G G T C G A G Common subsequence G G T C Sequence G T C G T C G G A A T G C

  25. Definitions Sequence A C C G G T C G A G Common subsequence G G T C Longer Common subsequence G G T C A G Sequence G T C G T C G G A A T G C

  26. Definitions Sequence A C C G G T C G A G Common subsequence G G T C Longer Common subsequence G G T C A G Longest Common subsequence G G T C G A G Sequence G T C G T C G G A A T G C

  27. Problem Definition Given two sequences 𝑌 and 𝑍 , we say that a sequence 𝑎 is a common subsequence of 𝑌 and 𝑍 if it is a subsequence of both 𝑌 and 𝑍 . For example, if 𝑌 = 𝐵, 𝐶, 𝐷, 𝐶, 𝐸, 𝐵, 𝐶 and 𝑍 = 𝐶, 𝐸, 𝐷, 𝐵, 𝐶, 𝐵 , the sequence 𝐶, 𝐷, 𝐵 is a common subsequence of 𝑌 and 𝑍 ; not a longest one though. The problem is to find a longest common subsequence of 𝑌 and 𝑍 .

  28. Optimal Substructure Let 𝑦 𝑛 and 𝑧 𝑜 bet the last two characters in the two sequences and 𝑨 𝑙 be the last character in the longest common subsequence 𝑦 𝑛 = 𝑧 𝑜 𝑦 𝑛 ≠ 𝑧 𝑜 𝑨 𝑙 = 𝑦 𝑛 ? 𝑨 𝑙 = 𝑧 𝑜 ?

  29. Recursive Algorithm LCS(X, Y) if X[m] == Y[n] return LCS(X[1..m-1], Y[1..n-1]) + 1 if X[m] != Y[n] return MAX(LCS(X[1..m], Y[1..n-1]), LCS(X[1..m-1], Y[1..n])

  30. Bottom-up Algorithm j B D C A B A i 0 1 2 3 4 5 6 0 0 0 0 0 0 0 0 A 1 0 0 0 B 2 0 C 3 0 B 4 0 D 5 0 A 6 0 B 7 0

  31. Bottom-up Algorithm j B D C A B A i 0 1 2 3 4 5 6 0 0 0 0 0 0 0 0 A 1 0 0 0 B 2 0 C 3 0 B 4 0 D 5 0 A 6 0 B 7 0

  32. Bottom-up Algorithm j B D C A B A i 0 1 2 3 4 5 6 0 0 0 0 0 0 0 0 A 1 0 0 0 0 1 B 2 0 C 3 0 B 4 0 D 5 0 A 6 0 B 7 0

  33. Bottom-up Algorithm j B D C A B A i 0 1 2 3 4 5 6 0 0 0 0 0 0 0 0 A 1 0 0 0 0 1 1 1 B 2 0 1 1 1 1 2 2 C 3 0 1 1 2 2 2 2 B 4 0 1 1 2 2 3 3 D 5 0 1 2 2 2 3 3 A 6 0 1 2 2 3 3 4 B 7 0 1 2 2 3 4 4

  34. Bottom-up Algorithm j B D C A B A i 0 1 2 3 4 5 6 0 0 0 0 0 0 0 0  0  0  0  1  1  1 A 1 0  1  1  1  1  2 B 2 0 2  1  1  2  2  2  2 C 3 0  1  1  2  2  3  3 B 4 0  1  2  2  2  3  3 D 5 0  1  2  2  3  3  4 A 6 0  1  2  2  3  4  4 B 7 0

  35. Bottom-up Algorithm j B D C A B A i 0 1 2 3 4 5 6 0 0 0 0 0 0 0 0  0  0  0  1  1  1 A 1 0  1  1  1  1  2 B 2 0 2  1  1  2  2  2  2 C 3 0  1  1  2  2  3  3 B 4 0  1  2  2  2  3  3 D 5 0  1  2  2  3  3  4 A 6 0  1  2  2  3  4  4 B 7 0

  36. Construction Algorithm

  37. Print-LCS

  38. Edit Distance

  39. Minimum Edit Distance How to measure the similarity of words or strings? Auto corrections: “ rationg ” - > {“rating”, “ration”} Alignment of DNA sequences

  40. Edit Distance Defined as the number of edit operations from string 𝑌 to a string 𝑍 . The edit operations are: Insertion: “ratio” → “ratio n ” Deletion: “ rationg ” → “ rationg ” Substitution: “ rationg ” → “ration s ” Example: Edit distance of “Mi c key” and “Mikey” is one deletion operation

  41. Minimum Edit Distance Given two strings 𝑌 and 𝑍 , find the minimum edit distance between them. That is, find the minimum number of edit operations that can be applied on 𝑌 to transform it to 𝑍 . Also, find this sequence of operations.

Recommend


More recommend