CS 580: Algorithm Design and Analysis

Similar documents
CSE 202 Dynamic Programming II

Chapter 6. Dynamic Programming. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Dynamic Programming. Weighted Interval Scheduling. Algorithmic Paradigms. Dynamic Programming

Chapter 6. Weighted Interval Scheduling. Dynamic Programming. Algorithmic Paradigms. Dynamic Programming Applications

Areas. ! Bioinformatics. ! Control theory. ! Information theory. ! Operations research. ! Computer science: theory, graphics, AI, systems,.

6.6 Sequence Alignment

Copyright 2000, Kevin Wayne 1

Chapter 6. Dynamic Programming. CS 350: Winter 2018

CSE 421 Weighted Interval Scheduling, Knapsack, RNA Secondary Structure

Dynamic Programming 1

6. DYNAMIC PROGRAMMING I

6. DYNAMIC PROGRAMMING II

Dynamic Programming: Interval Scheduling and Knapsack

Chapter 6. Dynamic Programming. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Copyright 2000, Kevin Wayne 1

Objec&ves. Review. Dynamic Programming. What is the knapsack problem? What is our solu&on? Ø Review Knapsack Ø Sequence Alignment 3/28/18

CS 580: Algorithm Design and Analysis

6. DYNAMIC PROGRAMMING I

Chapter 6. Dynamic Programming. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

6. DYNAMIC PROGRAMMING I

CSE 421 Dynamic Programming

Dynamic Programming. Cormen et. al. IV 15

RNA Secondary Structure. CSE 417 W.L. Ruzzo

Outline DP paradigm Discrete optimisation Viterbi algorithm DP: 0 1 Knapsack. Dynamic Programming. Georgy Gimel farb

Algoritmi e strutture di dati 2

CS Lunch. Dynamic Programming Recipe. 2 Midterm 2! Slides17 - Segmented Least Squares.key - November 16, 2016

Aside: Golden Ratio. Golden Ratio: A universal law. Golden ratio φ = lim n = 1+ b n = a n 1. a n+1 = a n + b n, a n+b n a n

CSE 431/531: Analysis of Algorithms. Dynamic Programming. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

The Double Helix. CSE 417: Algorithms and Computational Complexity! The Central Dogma of Molecular Biology! DNA! RNA! Protein! Protein!

Matching Residents to Hospitals

More Dynamic Programming

Lecture 2: Divide and conquer and Dynamic programming

CS2223 Algorithms D Term 2009 Exam 3 Solutions

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

More Dynamic Programming

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Lecture 2: Pairwise Alignment. CG Ron Shamir

Algorithm Design and Analysis

CS 580: Algorithm Design and Analysis

Dynamic Programming( Weighted Interval Scheduling)

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 11. Approximation Algorithms. Slides by Kevin Wayne Pearson-Addison Wesley. All rights reserved.

CSE 591 Foundations of Algorithms Homework 4 Sample Solution Outlines. Problem 1

CS325: Analysis of Algorithms, Fall Final Exam

Pair Hidden Markov Models

CS Analysis of Recursive Algorithms and Brute Force

CS 580: Algorithm Design and Analysis. Jeremiah Blocki Purdue University Spring 2018

Algorithms and Theory of Computation. Lecture 9: Dynamic Programming

Algorithm Design and Analysis

Chapter 11. Approximation Algorithms. Slides by Kevin Wayne Pearson-Addison Wesley. All rights reserved.

CS 6901 (Applied Algorithms) Lecture 3

Algorithms: COMP3121/3821/9101/9801

This means that we can assume each list ) is

Final exam study sheet for CS3719 Turing machines and decidability.

CSE 421. Dynamic Programming Shortest Paths with Negative Weights Yin Tat Lee

Algoritmiek, bijeenkomst 3

CSE 202 Homework 4 Matthias Springer, A

Do not turn this page until you have received the signal to start. Please fill out the identification section above. Good Luck!

CS483 Design and Analysis of Algorithms

Algorithms Exam TIN093 /DIT602

Dynamic programming. Curs 2015

Pairwise Alignment. Guan-Shieng Huang. Dept. of CSIE, NCNU. Pairwise Alignment p.1/55

Algorithms: Lecture 12. Chalmers University of Technology

Dynamic programming. Curs 2017

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Foundations of Natural Language Processing Lecture 6 Spelling correction, edit distance, and EM

Computational Models

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Dynamic Programming II Date: 10/12/17

Approximation Algorithms

0-1 Knapsack Problem in parallel Progetto del corso di Calcolo Parallelo AA

Algorithm Design and Analysis

(tree searching technique) (Boolean formulas) satisfying assignment: (X 1, X 2 )

Dynamic Programming. Reading: CLRS Chapter 15 & Section CSE 2331 Algorithms Steve Lai

Design and Analysis of Algorithms

a 1 a 2 a 3 a 4 v i c i c(a 1, a 3 ) = 3

Pairwise alignment, Gunnar Klau, November 9, 2005, 16:

CS 580: Algorithm Design and Analysis

Dynamic Programming. Reading: CLRS Chapter 15 & Section CSE 6331: Algorithms Steve Lai

CS 580: Algorithm Design and Analysis

Bio nformatics. Lecture 3. Saad Mneimneh

Greedy vs Dynamic Programming Approach

CS60007 Algorithm Design and Analysis 2018 Assignment 1

Lecture 13. More dynamic programming! Longest Common Subsequences, Knapsack, and (if time) independent sets in trees.

General Methods for Algorithm Design

Midterm 2 for CS 170

CS 6783 (Applied Algorithms) Lecture 3

Outline. Approximation: Theory and Algorithms. Motivation. Outline. The String Edit Distance. Nikolaus Augsten. Unit 2 March 6, 2009

4/12/2011. Chapter 8. NP and Computational Intractability. Directed Hamiltonian Cycle. Traveling Salesman Problem. Directed Hamiltonian Cycle

More NP-Complete Problems

CS173 Lecture B, November 3, 2015

Lecture 9. Greedy Algorithm

University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016

8. INTRACTABILITY I. Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley. Last updated on 2/6/18 2:16 AM

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Lecture 11 October 7, 2013

NP and Computational Intractability

Q = Set of states, IE661: Scheduling Theory (Fall 2003) Primer to Complexity Theory Satyaki Ghosh Dastidar

Sequence Comparison. mouse human

Algorithms. NP -Complete Problems. Dong Kyue Kim Hanyang University

Transcription:

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