CS 58: Algorithm Design and Analysis Jeremiah Blocki Purdue University Spring 28 Announcement: Homework 3 due February 5 th at :59PM Midterm Exam: Wed, Feb 2 (8PM-PM) @ MTHW 2
Recap: Dynamic Programming Key Idea: Express optimal solution in terms of solutions to smaller sub problems Example : Weighted Interval Scheduling OPT(j) is optimal solution considering only jobs,,j OPT(j) = max{ v j + OPT(p(j)), OPT(j-)} Case : Optimal solution includes job j with value v j Add job j and eliminate incompatible jobs p(j)+,,j- Case 2: Optimal solution does not include job j Example 2: Segmented Least Squares Fit points to a sequence of several line segments oal: Minimize E+cL E squared error L number of lines OPT(j) is optimal solution considering only jobs,,j OPT(j) = min{e(i,j)+opt(i-)+c: i< j+} 2
6.4 Knapsack Problem
Knapsack Problem Knapsack problem. iven n objects and a "knapsack." Item i weighs w i > kilograms and has value v i >. Knapsack has capacity of W kilograms. oal: fill knapsack so as to maximize total value. Ex: { 3, 4 } has value 4. # value weight 2 6 2 3 4 8 22 5 6 W = 5 28 reedy: repeatedly add item with maximum ratio v i / w i. Ex: { 5, 2, } achieves only value = 35 greedy not optimal. 4
Dynamic Programming: False Start Def. OPT(i) = max profit subset of items,, i. Case : OPT does not select item i. OPT selects best of {, 2,, i- } Case 2: OPT selects item i. accepting item i does not immediately imply that we will have to reject other items without knowing what other items were selected before i, we don't even know if we have enough room for i Conclusion. Need more sub-problems! 5
Dynamic Programming: Adding a New Variable Def. OPT(i, w) = max profit subset of items,, i with weight limit w. Case : OPT does not select item i. OPT selects best of {, 2,, i- } using weight limit w Case 2: OPT selects item i. new weight limit = w w i OPT selects best of {, 2,, i } using this new weight limit if i OPT(i, w) OPT(i, w) if w i w maxopt(i, w), v i OPT(i, w w i ) otherwise 6
Knapsack Problem: Bottom-Up Knapsack. Fill up an n-by-w array. Input: n, W, w,,w N, v,,v N for w = to W M[, w] = for i = to n for w = to W if (w i > w) M[i, w] = M[i-, w] else M[i, w] = max {M[i-, w], v i + M[i-, w-w i ]} return M[n, W]
Knapsack Algorithm W + 2 3 4 5 6 8 9 { } n + {, 2 } {, 2, 3 } 6 6 8 9 24 25 25 25 25 {, 2, 3, 4 } 6 8 22 24 28 29 29 4 {, 2, 3, 4, 5 } 6 8 22 28 29 34 34 4 OPT: { 4, 3 } value = 22 + 8 = 4 Item Value Weight W = 2 3 6 2 8 5 4 22 6 5 28 8
Knapsack Problem: Running Time Running time. (n W). Not polynomial in input size! Only need log bits to encode each weight Problem can be encoded with log bits "Pseudo-polynomial." Decision version of Knapsack is NP-complete. [Chapter 8] Knapsack approximation algorithm. There exists a poly-time algorithm that produces a feasible solution that has value within.% of optimum. [Section.8] 9
6.5 RNA Secondary Structure
RNA Secondary Structure RNA. String B = b b 2 b n over alphabet { A, C,, U }. Secondary structure. RNA is single-stranded so it tends to loop back and form base pairs with itself. This structure is essential for understanding behavior of molecule. U C A A A C A U A U U A A C A A C U A U C A U C C C Ex: UCAUUACAAUUAACAACUCUACCAA complementary base pairs: A-U, C-
RNA Secondary Structure Secondary structure. A set of pairs S = { (b i, b j ) } that satisfy: [Watson-Crick.] S is a matching and each pair in S is a Watson-Crick complement: A-U, U-A, C-, or -C. [No sharp turns.] The ends of each pair are separated by at least 4 intervening bases. If (b i, b j ) S, then i < j - 4. [Non-crossing.] If (b i, b j ) and (b k, b l ) are two pairs in S, then we cannot have i < k < j < l. Free energy. Usual hypothesis is that an RNA molecule will form the secondary structure with the optimum total free energy. approximate by number of base pairs oal. iven an RNA molecule B = b b 2 b n, find a secondary structure S that maximizes the number of base pairs. 2
RNA Secondary Structure: Examples Examples. C C U C C C U U A U U A A U A base pair U A U A A U U C C A U A U C A U 4 A U U C C A U ok sharp turn crossing 3
RNA Secondary Structure: Subproblems First attempt. OPT(j) = maximum number of base pairs in a secondary structure of the substring b b 2 b j. match b t and b n t n Difficulty. Results in two sub-problems. Finding secondary structure in: b b 2 b t-. Finding secondary structure in: b t+ b t+2 b n-. OPT(t-) need more sub-problems 4
Dynamic Programming Over Intervals Notation. OPT(i, j) = maximum number of base pairs in a secondary structure of the substring b i b i+ b j. Case. If i j - 4. OPT(i, j) = by no-sharp turns condition. Case 2. Base b j is not involved in a pair. OPT(i, j) = OPT(i, j-) Case 3. Base b j pairs with b t for some i t < j - 4. non-crossing constraint decouples resulting sub-problems OPT(i, j) = + max t { OPT(i, t-) + OPT(t+, j-) } take max over t such that i t < j-4 and b t and b j are Watson-Crick complements Remark. Same core idea in CKY algorithm to parse contextfree grammars. 5
Bottom Up Dynamic Programming Over Intervals Q. What order to solve the sub-problems? A. Do shortest intervals first. RNA(b,,b n ) { for k = 5, 6,, n- for i =, 2,, n-k j = i + k Compute M[i, j] i 4 3 2 } return M[, n] using recurrence 6 8 9 j Running time. O(n 3 ). 6
Dynamic Programming Summary Recipe. Characterize structure of problem. Recursively define value of optimal solution. Compute value of optimal solution. Construct optimal solution from computed information. Dynamic programming techniques. Binary choice: weighted interval scheduling. Multi-way choice: segmented least squares. Adding a new variable: knapsack. Dynamic programming over intervals: RNA secondary structure. Viterbi algorithm for HMM also uses DP to optimize a maximum likelihood tradeoff between parsimony and accuracy CKY parsing algorithm for context-free grammar has similar structure Top-down vs. bottom-up: different people have different intuitions.
6.6 Sequence Alignment
String Similarity o c u r r a n c e - o c c u r r e n c e How similar are two strings? ocurrance occurrence 6 mismatches, gap o c - u r r a n c e o c c u r r e n c e mismatch, gap o c - u r r - a n c e o c c u r r e - n c e mismatches, 3 gaps 9
Edit Distance Applications. Basis for Unix diff. Speech recognition. Computational biology. Edit distance. [Levenshtein 966, Needleman-Wunsch 9] ap penalty ; mismatch penalty pq. Cost = sum of gap and mismatch penalties. C T A C C T A C C T - C T A C C T A C C T C C T A C T A C A T C C T A C - T A C A T TC + T + A + 2 CA 2 + CA 2
Sequence Alignment oal: iven two strings X = x x 2... x m and Y = y y 2... y n find alignment of minimum cost. Def. 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. Def. The pair x i -y j and x i' -y j' cross if i < i', but j > j'. cost(m ) xi y j (x i, y j ) M mismatch i : x i unmatched j : y j unmatched gap Ex: CTACC vs. TACAT. Sol: M = x 2 -y, x 3 -y 2, x 4 -y 3, x 5 -y 4, x 6 -y 6. x x 2 x 3 x 4 x 5 x 6 C T A C C - - T A C A T y y 2 y 3 y 4 y 5 y 6 2
Sequence Alignment: Problem Structure Def. OPT(i, j) = min cost of aligning strings x x 2... x i and y y 2... y j. Case : OPT matches x i -y j. pay mismatch for x i -y j + min cost of aligning two strings x x 2... x i- and y y 2... y j- Case 2a: OPT leaves x i unmatched. pay gap for x i and min cost of aligning x x 2... x i- and y y 2... y j Case 2b: OPT leaves y j unmatched. pay gap for y j and min cost of aligning x x 2... x i and y y 2... y j- OPT(i, j) j if i xi y j OPT(i, j ) min OPT(i, j) otherwise OPT(i, j ) i if j 22
Sequence Alignment: Algorithm Sequence-Alignment(m, n, x x 2...x m, y y 2...y n,, ) { for i = to m M[i, ] = i for j = to n M[, j] = j } for i = to m for j = to n M[i, j] = min([x i, y j ] + M[i-, j-], + M[i-, j], + M[i, j-]) return M[m, n] Analysis. (mn) time and space. English words or sentences: m, n. Computational biology: m = n =,. billions ops OK, but B array? 23
6. Sequence Alignment in Linear Space
Sequence Alignment: Linear Space Q. Can we avoid using quadratic space? Easy. Optimal value in O(m + n) space and O(mn) time. Compute OPT(i, ) from OPT(i-, ). No longer a simple way to recover alignment itself. Theorem. [Hirschberg 95] Optimal alignment in O(m + n) space and O(mn) time. Clever combination of divide-and-conquer and dynamic programming. Inspired by idea of Savitch from complexity theory. 25
Sequence Alignment: Linear Space Edit distance graph. Let f(i, j) be shortest path from (,) to (i, j). Observation: f(i, j) = OPT(i, j). y y 2 y 3 y 4 y 5 y 6 - x xi y j x 2 i-j x 3 m-n 26
Sequence Alignment: Linear Space Edit distance graph. Let f(i, j) be shortest path from (,) to (i, j). Observation: f(i, j) = OPT(i, j). y y 2 y 3 y 4 y 5 y 6 - x xi y j x 2 i-j x 3 m-n 2 x - - - - - 9 - y y 2 y 3 y 4 y 5 y 6 - - -
Sequence Alignment: Linear Space Edit distance graph. Let f(i, j) be shortest path from (,) to (i, j). Observation: f(i, j) = OPT(i, j). y y 2 y 3 y 4 y 5 y 6 - x xi y j x 2 i-j x 3 m-n 28 x 9 - - - - - - - - - y y 2 y 3 y 4 y 5 y 6
Sequence Alignment: Linear Space Edit distance graph. Let f(i, j) be shortest path from (,) to (i, j). Observation: f(i, j) = OPT(i, j). y y 2 y 3 y 4 y 5 y 6 - x xi y j x 2 i-j x 3 m-n x - - - - - 29, - - y y 2 y 3 y 4 y 5 y 6
Sequence Alignment: Linear Space Edit distance graph. Let f(i, j) be shortest path from (,) to (i, j). Can compute f (, j) for any j in O(mn) time and O(m + n) space. j y y 2 y 3 y 4 y 5 y 6 - x x 2 i-j x 3 m-n 3
Sequence Alignment: Linear Space Edit distance graph. Let g(i, j) be shortest path from (i, j) to (m, n). Can compute by reversing the edge orientations and inverting the roles of (, ) and (m, n) y y 2 y 3 y 4 y 5 y 6 - x i-j xi y j x 2 x 3 m-n 3
Sequence Alignment: Linear Space Edit distance graph. Let g(i, j) be shortest path from (i, j) to (m, n). Can compute g(, j) for any j in O(mn) time and O(m + n) space. j y y 2 y 3 y 4 y 5 y 6 - x i-j x 2 x 3 m-n 32
Sequence Alignment: Linear Space Observation. The cost of the shortest path that uses (i, j) is f(i, j) + g(i, j). y y 2 y 3 y 4 y 5 y 6 - x i-j x 2 x 3 m-n 33
Sequence Alignment: Linear Space Observation 2. let q be an index that minimizes f(q, n/2) + g(q, n/2). Then, the shortest path from (, ) to (m, n) uses (q, n/2). n / 2 y y 2 y 3 y 4 y 5 y 6 - x i-j q x 2 x 3 m-n 34
Sequence Alignment: Linear Space Divide: find index q that minimizes f(q, n/2) + g(q, n/2) using DP. Align x q and y n/2. Conquer: recursively compute optimal alignment in each piece. n / 2 y y 2 y 3 y 4 y 5 y 6 - x i-j q x 2 x 3 m-n 35
Sequence Alignment: Running Time Analysis Warmup Theorem. Let T(m, n) = max running time of algorithm on strings of length at most m and n. T(m, n) = O(mn log n). T(m, n) 2T(m, n/2) O(mn) T(m, n) O(mn logn) Remark. Analysis is not tight because two sub-problems are of size (q, n/2) and (m - q, n/2). In next slide, we save log n factor. 36
Sequence Alignment: Running Time Analysis Theorem. Let T(m, n) = max running time of algorithm on strings of length m and n. T(m, n) = O(mn). Pf. (by induction on n) O(mn) time to compute f(, n/2) and g (, n/2) and find index q. T(q, n/2) + T(m - q, n/2) time for two recursive calls. Choose constant c so that: Base cases: m = 2 or n = 2. Inductive hypothesis: T(m, n) 2cmn. T(m, 2) cm T(2, n) cn T(m, n) cmn T(q, n/2) T(m q, n/2) T ( m, n) T ( q, n / 2) T ( m q, n / 2) cmn 2cqn / 2 2c( m q) n / 2 cmn cqn cmn cqn cmn 2cmn 3