3/28/18 Objec&ves • Dynamic Programming Ø Review Knapsack Ø Sequence Alignment Mar 28, 2018 CSCI211 - Sprenkle 1 Review • What is the knapsack problem? • What is our solu&on? Mar 28, 2018 CSCI211 - Sprenkle 2 1
3/28/18 Dynamic Programming: Adding a New Variable • Def. OPT(i, w ) = max profit subset of items 1, …, i with weight limit w Ø Case 1: OPT does not select item i • OPT selects best of { 1, 2, …, i-1 } using weight limit w Ø Case 2: OPT selects item i • new weight limit = w – w i • OPT selects best of { 1, 2, …, i–1 } using new weight limit, w – w i # 0 if i = 0 % OPT ( i , w ) = OPT ( i − 1, w ) if w i > w $ % max OPT ( i − 1, w ), v i + OPT ( i − 1, w − w i ) otherwise { } & Mar 28, 2018 CSCI211 - Sprenkle 3 Knapsack Problem: Bo^om-Up • Fill up an n-by-W array Input: W, N, w Input: W, N, w 1 ,…, ,…,w N, , v 1 ,…, ,…,v N for w = 0 for = 0 to to W W M[0, M[0, w] = 0 ] = 0 N # for all items # for all items for i = 1 for = 1 to to N W # for all possible weights # for all possible weights for for w = 1 w = 1 to to W : # item’s weight is more than available # item’s weight is more than available if if w i > > w : M[i M[i, , w] = M[i-1, ] = M[i-1, w] else else M[i M[i, , w] = max{ M[i-1, ] = max{ M[i-1, w], v ], v i i + M[i-1, + M[i-1, w-w w-w i ] } ] } return M[N, W] return M[N, W] Mar 28, 2018 CSCI211 - Sprenkle 4 2
3/28/18 Knapsack Input W = 11 Item Value Weight 1 1 1 2 6 2 3 18 5 4 22 6 5 28 7 Mar 28, 2018 CSCI211 - Sprenkle 5 Knapsack Algorithm W + 1 i = 4 0 1 2 3 4 5 6 7 8 9 10 11 φ 0 0 0 0 0 0 0 0 0 0 0 0 { 1 } 0 1 1 1 1 1 1 1 1 1 1 1 1 6 7 7 7 7 7 7 7 7 7 { 1, 2 } 0 n + 1 { 1, 2, 3 } 0 1 6 7 7 18 19 24 25 25 25 25 1 6 7 7 18 22 24 28 29 29 40 { 1, 2, 3, 4 } 0 { 1, 2, 3, 4, 5 } 0 Item Value Weight OPT: 1 1 1 Solution = 2 6 2 W = 11 3 18 5 4 22 6 5 28 7 Mar 28, 2018 CSCI211 - Sprenkle 6 3
3/28/18 Knapsack Algorithm W + 1 i = 5 0 1 2 3 4 5 6 7 8 9 10 11 φ 0 0 0 0 0 0 0 0 0 0 0 0 { 1 } 0 1 1 1 1 1 1 1 1 1 1 1 1 6 7 7 7 7 7 7 7 7 7 { 1, 2 } 0 n + 1 1 6 7 7 18 19 24 25 25 25 25 { 1, 2, 3 } 0 { 1, 2, 3, 4 } 0 1 6 7 7 18 22 24 28 29 29 40 1 6 7 7 18 22 28 29 34 35 40 { 1, 2, 3, 4, 5 } 0 Item Value Weight Observations? 1 1 1 Questions from last time? 2 6 2 3 18 5 W = 11 4 22 6 5 28 7 Mar 28, 2018 CSCI211 - Sprenkle 7 Knapsack Algorithm W + 1 i = 5 0 1 2 3 4 5 6 7 8 9 10 11 φ 0 0 0 0 0 0 0 0 0 0 0 0 { 1 } 0 1 1 1 1 1 1 1 1 1 1 1 1 6 7 7 7 7 7 7 7 7 7 { 1, 2 } 0 n + 1 { 1, 2, 3 } 0 1 6 7 7 18 19 24 25 25 25 25 1 6 7 7 18 22 24 28 29 29 40 { 1, 2, 3, 4 } 0 1 6 7 7 18 22 28 29 34 35 40 { 1, 2, 3, 4, 5 } 0 Item Value Weight OPT: 1 1 1 Solution = 2 6 2 W = 11 3 18 5 What is the optimal solution? 4 22 6 5 28 7 Mar 28, 2018 CSCI211 - Sprenkle 8 4
3/28/18 Knapsack Algorithm W + 1 0 1 2 3 4 5 6 7 8 9 10 11 φ 0 0 0 0 0 0 0 0 0 0 0 0 { 1 } 0 1 1 1 1 1 1 1 1 1 1 1 1 6 7 7 7 7 7 7 7 7 7 { 1, 2 } 0 n + 1 1 6 7 7 18 19 24 25 25 25 25 { 1, 2, 3 } 0 { 1, 2, 3, 4 } 0 1 6 7 7 18 22 24 28 29 29 40 1 6 7 7 18 22 28 29 34 35 40 { 1, 2, 3, 4, 5 } 0 Item Value Weight OPT: 40 = 22 + 18 1 1 1 Solution={4, 3} 2 6 2 3 18 5 W = 11 4 22 6 5 28 7 Mar 28, 2018 CSCI211 - Sprenkle 9 SEQUENCE ALIGNMENT Mar 28, 2018 CSCI211 - Sprenkle 10 5
3/28/18 String Similarity • How similar are two strings? o c u r r a n c e - Ø ocurrance o c c u r r e n c e Ø occurrence 6 mismatches, 1 gap • Measurements o c - u r r a n c e Ø Gap (-): add a le^er o c c u r r e n c e Ø Mismatch 1 mismatch, 1 gap o c - u r r - a n c e Which is the best alignment? o c c u r r e - n c e 0 mismatches, 3 gaps Mar 28, 2018 CSCI211 - Sprenkle 11 Edit Distance • [Levenshtein 1966, Needleman-Wunsch 1970] Ø Gap penalty: δ Parameters allow us Ø Mismatch penalty: α pq to tweak cost • If p and q are the same, then mismatch penalty is 0 Ø Cost = sum of gap and mismatch penal&es C T G A C C T A C C T - C T G A C C T A C C T C C T G A C T A C A T C C T G A C - T A C A T α TC + α GT + α AG + 2 α CA 2 δ + α CA Mar 28, 2018 CSCI211 - Sprenkle 12 6
3/28/18 Sequence Alignment • Goal: Given two strings X = x 1 x 2 . . . x m and Y = y 1 y 2 . . . y n find alignment of minimum cost • An alignment M is a set of ordered pairs x i -y j such that each item occurs in at most one pair and no crossings • The pair x i -y j and x i' -y j' cross if i < i', but j > j’. o c c u r e r n c e o c c u r e r n c e o c c u r r e n c e o c c u r r e n c e crossing 2 mismatches Mar 28, 2018 CSCI211 - Sprenkle 13 Sequence Alignment Example • X = CTACCG • Y = TACATG • Solu&on: M = x 2 -y 1 , x 3 -y 2 , x 4 -y 3 , x 5 -y 4 , x 6 -y 6 x 1 x 2 x 3 x 4 x 5 x 6 C T A C C - G - T A C A T G y 1 y 2 y 3 y 4 y 5 y 6 cost( M ) = ∑ α x i y j + ∑ δ + ∑ δ ( x i , y j ) ∈ M i : x i unmatched j : y j unmatched mismatch gap Recall: mismatch penalty is 0 if x i and y j are the same Mar 28, 2018 CSCI211 - Sprenkle 14 7
3/28/18 Sequence Alignment Case Analysis • Consider last character of the strings X and Y: x M and y N Ø M and N are not necessarily equal • i.e., strings are not necessarily the same length • What are the possibili&es for x M and y N in terms of the alignment? … x … y Mar 28, 2018 CSCI211 - Sprenkle 15 Sequence Alignment Case Analysis • Consider last character of strings X and Y: x M and y N Ø Case 1: x M and y N are aligned Ø Case 2: x M is not matched … x Ø Case 3: y N is not matched … y Formulate the optimal solution’s value Mar 28, 2018 CSCI211 - Sprenkle 16 8
3/28/18 Sequence Alignment Case Analysis • Consider last character of strings X and Y: x M and y N Ø Case 1: x M and y N are aligned Ø Case 2: x M is not matched What are the costs Ø Case 3: y N is not matched for these cases? x y • OPT(i, j) = min cost of aligning strings x 1 x 2 . . . x i and y 1 y 2 . . . y j Mar 28, 2018 CSCI211 - Sprenkle 17 Sequence Alignment Cost Analysis • Consider last character of strings X and Y: x M and y N Ø Case 1: x M and y N are aligned • Pay mismatch for x M -y N + min cost of aligning rest of strings • OPT(M, N) = α XmYn + OPT(M-1, N-1) Ø Case 2: x M is not matched • Pay gap for x M + min cost of aligning rest of strings • OPT(M, N) = δ + OPT(M-1, N) Ø Case 3: y N is not matched • Pay gap for y N + min cost of aligning rest of strings • OPT(M, N) = δ + OPT(M, N-1) Mar 28, 2018 CSCI211 - Sprenkle 18 9
3/28/18 Sequence Alignment Cost Analysis • Base costs? à i or j is 0 Ø What happens when we run out of le^ers in one string before the other? X = CTACCG Y = TACTG Mar 28, 2018 CSCI211 - Sprenkle 19 Sequence Alignment: Problem Structure Gaps for remainder of Y Ran out of 1 st string j δ " if i = 0 $ α x i y j + OPT ( i − 1, j − 1) " $ $ $ OPT ( i , j ) = δ + OPT ( i − 1, j ) min otherwise # # $ $ δ + OPT ( i , j − 1) % $ Ran out of 2 nd string $ i δ if j = 0 % Gaps for remainder of X Mar 28, 2018 CSCI211 - Sprenkle 20 10
3/28/18 Sequence Alignment: Algorithm Cost parameters Sequence- Sequence-Alignment(m Alignment(m, , n, x , x 1 x 2 ... ...x m , y , y 1 y 2 ... ...y n , , δ , , α ) ) for for i = 0 = 0 to to m M[ M[i, 0] = , 0] = i δ for for j = 0 = 0 to to n M[0, j] = j M[0, j] = j δ for for i = 1 = 1 to to m for for j = 1 = 1 to to n M[i M[i, , j] = ] = min( min( α [x [x i, , y j ] + M[i-1, j-1], ] + M[i-1, j-1], δ + M[i-1, + M[i-1, j], ], δ + + M[i M[i, j-1]) , j-1]) return return M[m M[m, , n] Mar 28, 2018 CSCI211 - Sprenkle 21 Example X = bait Y = boot α = 1, for vowel mismatch α = 2, for other mismatches j δ = 2 b a i t i b o o t Mar 28, 2018 CSCI211 - Sprenkle 22 11
3/28/18 Example X = bait Y = boot α = 1, for vowel mismatch α = 2, for other mismatches j δ = 2 b a i t i 0 2 4 6 8 b 2 o 4 o 6 t 8 Mar 28, 2018 CSCI211 - Sprenkle 23 Example X = bait Y = boot α = 1, for vowel mismatch α = 2, for other mismatches j δ = 2 b a i t i 0 2 4 6 8 b 2 0 2 4 6 o 4 o 6 t 8 Mar 28, 2018 CSCI211 - Sprenkle 24 12
3/28/18 Example X = bait Y = boot α = 1, for vowel mismatch α = 2, for other mismatches j δ = 2 b a i t i 0 2 4 6 8 b 2 0 2 4 6 o 4 2 1 3 5 o 6 t 8 Mar 28, 2018 CSCI211 - Sprenkle 25 Example X = bait Y = boot α = 1, for vowel mismatch α = 2, for other mismatches j δ = 2 b a i t i 0 2 4 6 8 b 2 0 2 4 6 o 4 2 1 3 5 o 6 4 3 2 4 t 8 Mar 28, 2018 CSCI211 - Sprenkle 26 13
3/28/18 What is the value for the problem? Example What is the solution? X = bait Y = boot α = 1, for vowel mismatch α = 2, for other mismatches j δ = 2 b a i t i 0 2 4 6 8 b 2 0 2 4 6 o 4 2 1 3 5 o 6 4 3 2 4 t 8 6 5 4 2 Mar 28, 2018 CSCI211 - Sprenkle 27 Example X = bait Y = boot α = 1, for vowel mismatch α = 2, for other mismatches j δ = 2 b a i t i 0 2 4 6 8 b 2 0 2 4 6 o 4 2 1 3 5 o 6 4 3 2 4 t 8 6 5 4 2 Mar 28, 2018 CSCI211 - Sprenkle 28 14
Recommend
More recommend