Dynamic Programming Chapter 15 Dictionary Definition Program - - PowerPoint PPT Presentation

dynamic programming
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Dynamic Programming

Chapter 15

slide-2
SLIDE 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

slide-3
SLIDE 3

Rod Cutting Problem

$13.10/ft $14.93/ft $21.10/ft $21.47/ft

slide-4
SLIDE 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!

slide-5
SLIDE 5

Recursive Solution

Define 𝑠𝑗 as the maximum possible revenue you can get for a rod of length 𝑗 We can express 𝑠𝑗 as follows 𝑠𝑗 = ൞ 𝑗 = 0 max ൝ 𝑞𝑗 max

1≤𝑘<𝑗 𝑠 𝑘 + 𝑠𝑗−𝑘

𝑗 > 0 Final answer is 𝑠

𝑜

The value of the rod without cutting Trying all possible cuts

slide-6
SLIDE 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

slide-7
SLIDE 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

slide-8
SLIDE 8

Execution Recurrence Tree

𝑜5 𝑜4 𝑜3 𝑜2 𝑜1 𝑜3 𝑜2 𝑜1 𝑜2 𝑜1 𝑜1 𝑜1 𝑜2 𝑜1 𝑜1

slide-9
SLIDE 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

slide-10
SLIDE 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

slide-11
SLIDE 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

slide-12
SLIDE 12

Matrix Chain Multiplication

slide-13
SLIDE 13

Example

A1 10 100 A2 100 5 A3 50 5 Z 10 50

𝐵1𝐵2𝐵3 = 𝐵1𝐵2 𝐵3 𝐵1𝐵2𝐵3 = 𝐵1 𝐵2𝐵3

Cost?

slide-14
SLIDE 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

slide-15
SLIDE 15

Example

A1 10 100 A2 100 5 A3 50 5 Z 10 50

𝐷𝑝𝑡𝑢 𝐵1𝐵2 𝐵3 = 10 ∙ 100 ∙ 5 + 10 ∙ 5 ∙ 50 = 7,500 𝐷𝑝𝑡𝑢 𝐵1 𝐵2𝐵3 = 10 ∙ 100 ∙ 50 + 100 ∙ 5 ∙ 50 = 75,000

slide-16
SLIDE 16

Problem

Given a sequence of (not necessarily square) matrices 𝐵1, 𝐵2, … , 𝐵𝑜 where the dimensions

  • f matrix 𝐵𝑗 is 𝑞𝑗−1 × 𝑞𝑗. We want to find the
  • rder of matrix multiplication operations to

compute the product 𝐵1 ∙ 𝐵2 ∙ ⋯ ∙ 𝐵𝑜 while minimizing the number of scalar multiplication

  • perations.

Hint: Number of scalar multiplication operations to computer 𝑌 ∙ 𝑍 with dimensions 𝑞 × 𝑟 and 𝑟 × 𝑠, respectively, is 𝑞 ∙ 𝑟 ∙ 𝑠

slide-17
SLIDE 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

slide-18
SLIDE 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

slide-19
SLIDE 19

Recurrence Relation

𝑛[𝑗, 𝑘]: Minimum cost for multiplying matrices 𝑗 through 𝑘; 1 ≤ 𝑗 ≤ 𝑘 ≤ 𝑜 𝑛 𝑗, 𝑘 = ቐ 𝑗 = 𝑘 min

𝑗≤𝑙<𝑘 𝑛 𝑗, 𝑙 + 𝑛 𝑙 + 1, 𝑘 + 𝑞𝑗−1𝑞𝑙𝑞𝑘

𝑗 < 𝑘

slide-20
SLIDE 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

slide-21
SLIDE 21

Bottom-up Approach

𝐵1 𝐵2 … 𝐵𝑜

Cost[𝐵𝑗 .. 𝐵𝑘]

0 0 0 0 0 0

𝑘 𝑗

Final answer

slide-22
SLIDE 22

Longest Common Subsequence

slide-23
SLIDE 23

Definitions

Sequence A C C G G T C G A G Subsequence A C G A A Sequence G T C G T C G G A A T G C Subsequence G C T A A T

slide-24
SLIDE 24

Definitions

Sequence A C C G G T C G A G Sequence G T C G T C G G A A T G C Common subsequence G G T C

slide-25
SLIDE 25

Definitions

Sequence A C C G G T C G A G Sequence G T C G T C G G A A T G C Common subsequence G G T C Longer Common subsequence G G T C A G

slide-26
SLIDE 26

Definitions

Sequence A C C G G T C G A G Sequence G T C G T C G G A A T G C 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

slide-27
SLIDE 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 𝑍.

slide-28
SLIDE 28

Optimal Substructure

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

𝑨𝑙 = 𝑦𝑛? 𝑨𝑙 = 𝑧𝑜?

slide-29
SLIDE 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])

slide-30
SLIDE 30

Bottom-up Algorithm

j i B 1 D 2 C 3 A 4 B 5 A 6 A 1 B 2 C 3 B 4 D 5 A 6 B 7

slide-31
SLIDE 31

Bottom-up Algorithm

j i B 1 D 2 C 3 A 4 B 5 A 6 A 1 B 2 C 3 B 4 D 5 A 6 B 7

slide-32
SLIDE 32

Bottom-up Algorithm

j i B 1 D 2 C 3 A 4 B 5 A 6 A 1 1 B 2 C 3 B 4 D 5 A 6 B 7

slide-33
SLIDE 33

Bottom-up Algorithm

j i B 1 D 2 C 3 A 4 B 5 A 6 A 1 1 1 1 B 2 1 1 1 1 2 2 C 3 1 1 2 2 2 2 B 4 1 1 2 2 3 3 D 5 1 2 2 2 3 3 A 6 1 2 2 3 3 4 B 7 1 2 2 3 4 4

slide-34
SLIDE 34

Bottom-up Algorithm

j i B 1 D 2 C 3 A 4 B 5 A 6 A 1  0  0  0  1  1  1 B 2  1  1  1  1  2 2 C 3  1  1  2  2  2  2 B 4  1  1  2  2  3  3 D 5  1  2  2  2  3  3 A 6  1  2  2  3  3  4 B 7  1  2  2  3  4  4

slide-35
SLIDE 35

Bottom-up Algorithm

j i B 1 D 2 C 3 A 4 B 5 A 6 A 1  0  0  0  1  1  1 B 2  1  1  1  1  2 2 C 3  1  1  2  2  2  2 B 4  1  1  2  2  3  3 D 5  1  2  2  2  3  3 A 6  1  2  2  3  3  4 B 7  1  2  2  3  4  4

slide-36
SLIDE 36

Construction Algorithm

slide-37
SLIDE 37

Print-LCS

slide-38
SLIDE 38

Edit Distance

slide-39
SLIDE 39

Minimum Edit Distance

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

slide-40
SLIDE 40

Edit Distance

Defined as the number of edit operations from string 𝑌 to a string 𝑍. The edit operations are:

Insertion: “ratio” → “ration” Deletion: “rationg” → “rationg” Substitution: “rationg” → “rations”

Example:

Edit distance of “Mickey” and “Mikey” is one deletion operation

slide-41
SLIDE 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.

slide-42
SLIDE 42

Subproblem Formulation

G O B L I N G O L D E N

slide-43
SLIDE 43

Subproblem Formulation

G O B L I N G O L D E N If the last character in both strings is the same, then we know that the optimal solution can copy this value. Why? The subproblem is formulated by removing the last character of both

slide-44
SLIDE 44

Subproblem Formulation

G O B L I G O L D E If the last character is different, then we do not really know what would be the optimal edit If we do not know the optimal edit, the next best thing is to try everything

slide-45
SLIDE 45

Subproblem Formulation

G O B L I G O L D E

𝑄

G O B L I G O L D

𝑌 𝑍

G O B L G O L D E G O B L G O L D

𝑄

1 𝑌[1. . 𝑜] 𝑍[1. . 𝑛 − 1]

Insert last character in 𝑍

𝑄2

𝑌[1. . 𝑜 − 1] 𝑍[1. . 𝑛]

Delete last character in 𝑌

𝑄

3 𝑌[1. . 𝑜 − 1] 𝑍[1. . 𝑛 − 1]

Substitute the last characters

slide-46
SLIDE 46

Recurrence Relation

𝐸𝑗,𝑘: The cost of transforming 𝑌[1. . 𝑗] to 𝑍[1. . 𝑘]

𝐸𝑗𝑘 = max 𝑗, 𝑘 ; 𝑗 = 0 ∨ 𝑘 = 0 𝐸𝑗−1,𝑘−1 ; 𝑗 > 0 ∧ 𝑘 > 0 ∧ 𝑦𝑗 = 𝑧𝑘 min 𝐸𝑗,𝑘−1 + 𝑗𝑜𝑡𝑓𝑠𝑢𝑗𝑝𝑜 𝑑𝑝𝑡𝑢 𝑧𝑘 𝐸𝑗−1,𝑘 + 𝑒𝑓𝑚𝑓𝑢𝑗𝑝𝑜 𝑑𝑝𝑡𝑢 𝑦𝑗 𝐸𝑗−1,𝑘−1 + 𝑡𝑣𝑐𝑡𝑢𝑗𝑢𝑣𝑢𝑗𝑝𝑜 𝑑𝑝𝑡𝑢 𝑦𝑗, 𝑧𝑘 ; 𝑗 > 0 ∧ 𝑘 > 0 ∧ 𝑦𝑗 ≠ 𝑦𝑘

𝑡𝑣𝑐𝑡𝑢𝑗𝑢𝑣𝑢𝑗𝑝𝑜 𝑑𝑝𝑡𝑢 𝑏, 𝑐 = ቊ0 ; 𝑏 = 𝑐 1 ; 𝑏 ≠ 𝑐

𝐸𝑗,𝑘 = max 𝑗, 𝑘 ; 𝑗 = 0 ∨ 𝑘 = 0 min 𝐸𝑗,𝑘−1 + 𝑗𝑜𝑡𝑓𝑠𝑢𝑗𝑝𝑜 𝑑𝑝𝑡𝑢 𝑧𝑘 𝐸𝑗−1,𝑘 + 𝑒𝑓𝑚𝑓𝑢𝑗𝑝𝑜 𝑑𝑝𝑡𝑢 𝑦𝑗 𝐸𝑗−1,𝑘−1 + 𝑡𝑣𝑐𝑡𝑢𝑗𝑢𝑣𝑢𝑗𝑝𝑜 𝑑𝑝𝑡𝑢 𝑦𝑗, 𝑧𝑘 ; 𝑗 > 0 ∧ 𝑘 > 0

slide-47
SLIDE 47

Bottom-up Algorithm

j i G 1 O 2 L 3 D 4 E 5 N 6 G 1 O 2 B 3 L 4 I 5 N 6