Dynamic Programming II

Size: px
Start display at page:

Download "Dynamic Programming II"

Transcription

1 June 9, 214

2 DP: Longest common subsequence biologists often need to find out how similar are 2 DNA sequences DNA sequences are strings of bases: A, C, T and G how to define similarity?

3 DP: Longest common subsequence biologists often need to find out how similar are 2 DNA sequences DNA sequences are strings of bases: A, C, T and G how to define similarity? one is a substring of another

4 DP: Longest common subsequence biologists often need to find out how similar are 2 DNA sequences DNA sequences are strings of bases: A, C, T and G how to define similarity? one is a substring of another number of changes (mutations) needed to change one string to another

5 DP: Longest common subsequence biologists often need to find out how similar are 2 DNA sequences DNA sequences are strings of bases: A, C, T and G how to define similarity? one is a substring of another number of changes (mutations) needed to change one string to another the longest common subsequence of two strings S 1 and S 2 : a longest sequence S 3 appearing in each of S 1 and S 2 (in the same order, but necessarily consecutively) Definition. Z = z 1 z 2... z k is a subsequence of S = s 1 s 2... s n if there exists an increasing sequence of indexes: 1 i 1 < i 2 < < i k n such that z j = s ij

6 Example. S= G G C A C T G T A C Z= G C C A Z = GCCA is a subsequence of S = GGCACTGTAC Definition. Z is a common subsequence of X and Y if it is a subsequence of both X and Y. A longest such Z is called a longest common subsequence LCS.

7 Example. S= G G C A C T G T A C Z= G C C A Z = GCCA is a subsequence of S = GGCACTGTAC Definition. Z is a common subsequence of X and Y if it is a subsequence of both X and Y. A longest such Z is called a longest common subsequence LCS. Example. Consider X = GGCACTGTAC Y = CATGTCACGG Then ATAC and GCAG are a common subsequences of X and Y. The longest common subsequence is CATGTAC.

8 brute-force approach : list all subsequences of X and for each test if it s subsequence of Y If X has a length m, there are 2 m subsequences of X exponential time.

9 brute-force approach : list all subsequences of X and for each test if it s subsequence of Y If X has a length m, there are 2 m subsequences of X exponential time. We should apply dynamic programming approach.

10 Optimal substructure of LCS Claim. Let Z = z 1... z k be a LCS of X = x 1... x m and Y = y 1... y n. Then 1 if x m = y n, then z k = x m = y n and Z 1,k 1 is an LCS of X 1,m 1 and Y 1,n 1 ; 2 if x m y n and z k x m, then Z is an LCS of X 1,m 1 and Y ; 3 if x m y n and z k y n, then Z is an LCS of X and Y 1,n 1.

11 Proof. 1 if z k x m = y n, then Zx m is a common subsequence of X and Y longer than Z, a contradiction clearly, Z 1,k 1 is a common subsequence of X 1,m 1 and Y 1,n 1 if not a longest one: let W be an LCS of X 1,m 1 and Y 1,n 1 ; then Wz k is a common subsequence of X and Y, again a contradiction ( cut-and-paste ) 2 clearly, since z k x m, Z is a common subsequence of X 1,m 1 and Y ; if not a longest one: use cut-and-paste technique again 3 similarly as in case 2. Hence, an LCS of two sequences contains within it an LCS of prefixes of these two sequences: optimal substructure property.

12 A recursive solution To find an LCS of X = x 1... x m and Y = y 1... y n : if x m = y n, then find an LCS of X 1,m 1 and Y 1,n 1 and append x m = y n to it if x m y n, then find an LCS of X and Y 1,n 1 and an LCS of X 1,m 1 and Y, and take the longer of these two

13 A recursive solution To find an LCS of X = x 1... x m and Y = y 1... y n : if x m = y n, then find an LCS of X 1,m 1 and Y 1,n 1 and append x m = y n to it if x m y n, then find an LCS of X and Y 1,n 1 and an LCS of X 1,m 1 and Y, and take the longer of these two Let c[i, j] be the length of an LCS of X 1,i and Y 1,j recursive formula: if i = or j =, c[i, j] = c[i 1, j 1] + 1 if i, j > and x i = y j, max(c[i, j 1], c[i 1, j]) if i, j > and x i y j.

14 Computing A recursive algorithm based on recursive formula would be exponential, however there are only (m + 1)(n + 1) subproblems ( overlapping-subproblems property ) entries of table c[... m,... n] are filled in row-major order : the first row from left to right the second row from left to right etc table b[1... m, 1... n] - contains the information to construct the optimal solution (shows a direction from where we got the minimal value of the length of an LCS: c[i, j] = c[i, j 1], c[i, j] = c[i 1, j], or c[i, j] = c[i 1, j 1] + 1.

15 LCS-Length (X, Y ) 1. m := length[x ] 2. n := length[y ] 3. for i := 1 to m c[i, ] := 4. for i := 1 to n c[, i] := 5. for i := 1 to m 6. for j := 1 to n 7. if x i = y j 8. c[i, j] := c[i 1, j 1] b[i, j] := 1. if c[i 1, j] c[i, j 1] 11. c[i, j] := c[i 1, j] 12. b[i, j] := 13. else c[i, j] := c[i 1, j] 14. b[i, j] := 15. return c and b Time complexity: O(mn)

16 j i G G C A C T G T A C 1 C,,,1,1,1,1,1,1,1,1 2 A,,,1,2,2,2,2,2,2,2 3 T,,,1,2,2,3,3,3,3,3 4 G,1,1,1,2,2,3,4,4,4,4 5 T,1,1,1,2,2,3,4,5,5,5 6 C,1,1,2,2,3,3,4,5,5,6 7 A,1,1,2,3,3,3,4,5,6,6 8 C,1,1,2,3,4,4,4,5,6,7 9 G,1,2,2,3,4,4,5,5,6,7 1 G,1,2,2,3,4,4,5,5,6,7

17 C T G A C A y i x i 1 A C G C T A C

18 C T G A C A y i x i A C G C T PRING-LCS (b,x,y,i,j) 1. if i = or j = then return 2. if b[i, j] = then PRINT-LCS (b,x,y,i-1,j-1) print x i 3. else if b[i, j] = then PRINT-LCS (b,x,y,i-1,j) 6 A else PRINT-LCS(b,X,Y,i,j-1) 7 C

19 Exercise Matrix Multiplications Given: a chain of matrices (A 1, A 2,... A n ), with A i having dimension p i 1 p i. Goal: compute the product A 1 A 2 A n as fast as possible

20 Exercise Matrix Multiplications Given: a chain of matrices (A 1, A 2,... A n ), with A i having dimension p i 1 p i. Goal: compute the product A 1 A 2 A n as fast as possible Clearly, time to multiply two matrices depends on dimensions Does the order of multiplication (= parenthesization) matter? Example: n = 4. Possible orders: (A 1 (A 2 (A 3 A 4 ))) (A 1 ((A 2 A 3 )A 4 )) ((A 1 A 2 )(A 3 A 4 )) ((A 1 (A 2 A 3 ))A 4 ) (((A 1 A 2 )A 3 )A 4 )

21 Suppose A 1 is 1 1, A 2 is 1 5, A 3 is 5 5, and A 4 is 5 1 Assume that multiplication of a (p q)-matrix and a (q r)-matrix takes pqr steps (a straightforward algorithm)

22 Suppose A 1 is 1 1, A 2 is 1 5, A 3 is 5 5, and A 4 is 5 1 Assume that multiplication of a (p q)-matrix and a (q r)-matrix takes pqr steps (a straightforward algorithm) Order 2: (A 1 ((A 2 A 3 )A 4 )) = 85, Order 5: (((A 1 A 2 )A 3 )A 4 ) = 12, 5 Seems it might be a good idea to find a good order

23 How many orders are there? Can we just check all of them? ( we look only at fully parenthesized matrix products)

24 How many orders are there? Can we just check all of them? ( we look only at fully parenthesized matrix products) Let P(n) be the number of orders of a sequence of n matrices Clear, P(1) = 1 (only one matrix)

25 How many orders are there? Can we just check all of them? ( we look only at fully parenthesized matrix products) Let P(n) be the number of orders of a sequence of n matrices Clear, P(1) = 1 (only one matrix) If n 2, a matrix product is the product of two matrix sub-products. Split may occur between k-th and (k + 1)-st position, for any k = 1, 2,..., n 1 ( top-level multiplication )

26 How many orders are there? Can we just check all of them? ( we look only at fully parenthesized matrix products) Let P(n) be the number of orders of a sequence of n matrices Clear, P(1) = 1 (only one matrix) If n 2, a matrix product is the product of two matrix sub-products. Split may occur between k-th and (k + 1)-st position, for any k = 1, 2,..., n 1 ( top-level multiplication ) Thus { 1 if n = 1 P(n) = n 1 k=1 P(k) P(n k) if n 2 Unfortunately, P(n) = Ω(4 n /n 3/2 ), and thus (easier to see) P(n) = Ω(2 n ) Thus brute-force approach (check all parenthesization) is no good

27 We will use the Dynamic programming approach to optimally solve this problem. The four basic steps when designing Dynamic programming algorithm: 1 Characterize the structure of an optimal solution 2 Recursively define the value of an optimal solution 3 Compute the value of an optimal solution in a bottom-up fashion 4 Construct an optimal solution from computed information

28 1. Characterizing structure Let A i,j = A i A j for i j. If i < j, then any parenthesization of A i,j must split product at some k, i k < j, i.e., compute A i,k, A k+1,j, and then A i,k A k+1,j.

29 1. Characterizing structure Let A i,j = A i A j for i j. If i < j, then any parenthesization of A i,j must split product at some k, i k < j, i.e., compute A i,k, A k+1,j, and then A i,k A k+1,j. Hence, for some k, the cost of computing A i,j is the cost of computing A i,k plus the cost of computing A k+1,j plus the cost of multiplying A i,k and A k+1,j.

30 Optimal substructure: Suppose that optimal parenthesization of A i,j splits the product between A k and A k+1. Then, parenthesizations of A i,k and A k+1,j within this optimal parenthesization must be also optimal (otherwise, substitute the opt. parenthesization of A i,k (resp. A k+1,j ) to current parenthesization of A i,j and obtain a better solution contradiction) Use optimal substructure to construct an optimal solution: 1 split into two subproblems (choosing an optimal split), 2 find optimal solutions to subproblem, 3 combine optimal subproblem solutions.

31 A recursive solution Let m[i, j] denote minimum number of multiplications needed to compute A i,j = A i A i+1 A j (full problem: m[1, n]). Recursive definition of m[i, j]: if i = j, then m[i, j] = m[i, i] = (no multiplication needed)

32 A recursive solution Let m[i, j] denote minimum number of multiplications needed to compute A i,j = A i A i+1 A j (full problem: m[1, n]). Recursive definition of m[i, j]: if i = j, then m[i, j] = m[i, i] = (no multiplication needed) if i < j, assume optimal split at k, i k < j. Since each matrix A i is p i 1 p i, A i,k is p i 1 p k and A k+1,j is p k p j, m[i, j] = m[i, k] + m[k + 1, j] + p i 1 p k p j

33 A recursive solution Let m[i, j] denote minimum number of multiplications needed to compute A i,j = A i A i+1 A j (full problem: m[1, n]). Recursive definition of m[i, j]: if i = j, then m[i, j] = m[i, i] = (no multiplication needed) if i < j, assume optimal split at k, i k < j. Since each matrix A i is p i 1 p i, A i,k is p i 1 p k and A k+1,j is p k p j, m[i, j] = m[i, k] + m[k + 1, j] + p i 1 p k p j We do not know optimal value of k. There are j i possibilities, k = i, i + 1,..., j 1, hence if i = j m[i, j] = min i k<j {m[i, k] + m[k + 1, j] if i < j +p i 1 p k p j } We also keep track of optimal splits: s[i, j] = k m[i, j] = m[i, k] + m[k + 1, j] + p i 1 p k p j (s[i, j] is a value of k at which we split Dynamic the Programming product II A i,j to obtain

34 Computing the optimal costs Want to compute m[1, n], minimum cost for multiplying A 1 A 2 A n. Recursively, it would take Ω(2 n ) steps: the same subproblems are computed over and over again. However, if we compute in a bottom-up fashion, we can reduce running time to polynomial in n.

35 Computing the optimal costs Want to compute m[1, n], minimum cost for multiplying A 1 A 2 A n. Recursively, it would take Ω(2 n ) steps: the same subproblems are computed over and over again. However, if we compute in a bottom-up fashion, we can reduce running time to polynomial in n. The recursive equation shows that cost m[i, j] (product of j i + 1 matrices) depends only on smaller subproblems: for k = 1,..., j 1, A i,k is a product of k i + 1 < j i + 1 matrices, A k+1,j is a product of j k < j i + 1 matrices. Algorithm should fill table m in order of increasing lengths of chains.

36 Matrix-Chain-Order(p) 1. n := length[p] 1 2. for i := 1 to n 3. m[i, i] := 4. for l := 2 to n 4. for i := 1 to n l j := i + l 1 m[i, j] := 6. for k := i to j 1 7. q := m[i, k] + m[k + 1, j] + p i 1 p k p j 8. if q < m[i, j] 9. m[i, j] := q s[i, j] := k 1. return m and s

Lecture 8. Dynamic Programming

Lecture 8. Dynamic Programming Lecture 8. Dynamic Programming T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algorithms, 3rd Edition, MIT Press, 2009 Sungkyunkwan University Hyunseung Choo choo@skku.edu Copyright 2000-2018

More information

Dynamic Programming (Part #2)

Dynamic Programming (Part #2) Dynamic Programming (Part #) Introduction to Algorithms MIT Press (Chapter 5) Matrix-Chain Multiplication Problem: given a sequence A, A,, A n, compute the product: A A A n Matrix compatibility: C = A

More information

CS473-Algorithms I. Lecture 10. Dynamic Programming. Cevdet Aykanat - Bilkent University Computer Engineering Department

CS473-Algorithms I. Lecture 10. Dynamic Programming. Cevdet Aykanat - Bilkent University Computer Engineering Department CS473-Algorithms I Lecture 1 Dynamic Programming 1 Introduction An algorithm design paradigm like divide-and-conquer Programming : A tabular method (not writing computer code) Divide-and-Conquer (DAC):

More information

Lecture 13: Chain Matrix Multiplication

Lecture 13: Chain Matrix Multiplication Lecture 3: Chain Matrix Multiplication CLRS Section 5.2 Revised April 7, 2003 Outline of this Lecture Recalling matrix multiplication. The chain matrix multiplication problem. A dynamic programming algorithm

More information

Elements of Dynamic Programming. COSC 3101A - Design and Analysis of Algorithms 8. Discovering Optimal Substructure. Optimal Substructure - Examples

Elements of Dynamic Programming. COSC 3101A - Design and Analysis of Algorithms 8. Discovering Optimal Substructure. Optimal Substructure - Examples Elements of Dynamic Programming COSC 3A - Design and Analysis of Algorithms 8 Elements of DP Memoization Longest Common Subsequence Greedy Algorithms Many of these slides are taken from Monica Nicolescu,

More information

We ve done. Now. Next

We ve done. Now. Next We ve done Matroid Theory Task scheduling problem (another matroid example) Dijkstra s algorithm (another greedy example) Dynamic Programming Now Matrix Chain Multiplication Longest Common Subsequence

More information

Introduction to Algorithms

Introduction to Algorithms Introduction to Algorithms Dynamic Programming Well known algorithm design techniques: Brute-Force (iterative) ti algorithms Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic

More information

Chapter 3 Dynamic programming

Chapter 3 Dynamic programming Chapter 3 Dynamic programming 1 Dynamic programming also solve a problem by combining the solutions to subproblems. But dynamic programming considers the situation that some subproblems will be called

More information

Dynamic Programming. Design and Analysis of Algorithms. Entwurf und Analyse von Algorithmen. Irene Parada. Design and Analysis of Algorithms

Dynamic Programming. Design and Analysis of Algorithms. Entwurf und Analyse von Algorithmen. Irene Parada. Design and Analysis of Algorithms Entwurf und Analyse von Algorithmen Dynamic Programming Overview Introduction Example 1 When and how to apply this method Example 2 Final remarks Introduction: when recursion is inefficient Example: Calculation

More information

Chain Matrix Multiplication

Chain Matrix Multiplication Chain Matrix Multiplication Version of November 5, 2014 Version of November 5, 2014 Chain Matrix Multiplication 1 / 27 Outline Outline Review of matrix multiplication. The chain matrix multiplication problem.

More information

15.Dynamic Programming

15.Dynamic Programming 15.Dynamic Programming Dynamic Programming is an algorithm design technique for optimization problems: often minimizing or maximizing. Like divide and conquer, DP solves problems by combining solutions

More information

12 Dynamic Programming (2) Matrix-chain Multiplication Segmented Least Squares

12 Dynamic Programming (2) Matrix-chain Multiplication Segmented Least Squares 12 Dynamic Programming (2) Matrix-chain Multiplication Segmented Least Squares Optimal substructure Dynamic programming is typically applied to optimization problems. An optimal solution to the original

More information

/463 Algorithms - Fall 2013 Solution to Assignment 3

/463 Algorithms - Fall 2013 Solution to Assignment 3 600.363/463 Algorithms - Fall 2013 Solution to Assignment 3 (120 points) I (30 points) (Hint: This problem is similar to parenthesization in matrix-chain multiplication, except the special treatment on

More information

Algorithm Design Techniques part I

Algorithm Design Techniques part I Algorithm Design Techniques part I Divide-and-Conquer. Dynamic Programming DSA - lecture 8 - T.U.Cluj-Napoca - M. Joldos 1 Some Algorithm Design Techniques Top-Down Algorithms: Divide-and-Conquer Bottom-Up

More information

14 Dynamic. Matrix-chain multiplication. P.D. Dr. Alexander Souza. Winter term 11/12

14 Dynamic. Matrix-chain multiplication. P.D. Dr. Alexander Souza. Winter term 11/12 Algorithms Theory 14 Dynamic Programming (2) Matrix-chain multiplication P.D. Dr. Alexander Souza Optimal substructure Dynamic programming is typically applied to optimization problems. An optimal solution

More information

CMPS 2200 Fall Dynamic Programming. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

CMPS 2200 Fall Dynamic Programming. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk CMPS 00 Fall 04 Dynamic Programming Carola Wenk Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk 9/30/4 CMPS 00 Intro. to Algorithms Dynamic programming Algorithm design technique

More information

Data Structures and Algorithms Week 8

Data Structures and Algorithms Week 8 Data Structures and Algorithms Week 8 Dynamic programming Fibonacci numbers Optimization problems Matrix multiplication optimization Principles of dynamic programming Longest Common Subsequence Algorithm

More information

Introduction to Algorithms

Introduction to Algorithms Introduction to Algorithms 6.046J/18.401J LECTURE 12 Dynamic programming Longest common subsequence Optimal substructure Overlapping subproblems Prof. Charles E. Leiserson Dynamic programming Design technique,

More information

Write an algorithm to find the maximum value that can be obtained by an appropriate placement of parentheses in the expression

Write an algorithm to find the maximum value that can be obtained by an appropriate placement of parentheses in the expression Chapter 5 Dynamic Programming Exercise 5.1 Write an algorithm to find the maximum value that can be obtained by an appropriate placement of parentheses in the expression x 1 /x /x 3 /... x n 1 /x n, where

More information

ECE250: Algorithms and Data Structures Dynamic Programming Part B

ECE250: Algorithms and Data Structures Dynamic Programming Part B ECE250: Algorithms and Data Structures Dynamic Programming Part B Ladan Tahvildari, PEng, SMIEEE Associate Professor Software Technologies Applied Research (STAR) Group Dept. of Elect. & Comp. Eng. University

More information

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya CS60020: Foundations of Algorithm Design and Machine Learning Sourangshu Bhattacharya Dynamic programming Design technique, like divide-and-conquer. Example: Longest Common Subsequence (LCS) Given two

More information

So far... Finished looking at lower bounds and linear sorts.

So far... Finished looking at lower bounds and linear sorts. So far... Finished looking at lower bounds and linear sorts. Next: Memoization -- Optimization problems - Dynamic programming A scheduling problem Matrix multiplication optimization Longest Common Subsequence

More information

Dynamic Programming. An Enumeration Approach. Matrix Chain-Products. Matrix Chain-Products (not in book)

Dynamic Programming. An Enumeration Approach. Matrix Chain-Products. Matrix Chain-Products (not in book) Matrix Chain-Products (not in book) is a general algorithm design paradigm. Rather than give the general structure, let us first give a motivating example: Matrix Chain-Products Review: Matrix Multiplication.

More information

CMSC351 - Fall 2014, Homework #4

CMSC351 - Fall 2014, Homework #4 CMSC351 - Fall 2014, Homework #4 Due: November 14th at the start of class PRINT Name: Grades depend on neatness and clarity. Write your answers with enough detail about your approach and concepts used,

More information

Dynamic Programming part 2

Dynamic Programming part 2 Dynamic Programming part 2 Week 7 Objectives More dynamic programming examples - Matrix Multiplication Parenthesis - Longest Common Subsequence Subproblem Optimal structure Defining the dynamic recurrence

More information

y j LCS-Length(X,Y) Running time: O(st) set c[i,0] s and c[0,j] s to 0 for i=1 to s for j=1 to t if x i =y j then else if

y j LCS-Length(X,Y) Running time: O(st) set c[i,0] s and c[0,j] s to 0 for i=1 to s for j=1 to t if x i =y j then else if Recursive solution for finding LCS of X and Y if x s =y t, then find an LCS of X s-1 and Y t-1, and then append x s =y t to this LCS if x s y t, then solve two subproblems: (1) find an LCS of X s-1 and

More information

Lecture 4: Dynamic programming I

Lecture 4: Dynamic programming I Lecture : Dynamic programming I Dynamic programming is a powerful, tabular method that solves problems by combining solutions to subproblems. It was introduced by Bellman in the 950 s (when programming

More information

Algorithms: COMP3121/3821/9101/9801

Algorithms: COMP3121/3821/9101/9801 NEW SOUTH WALES Algorithms: COMP3121/3821/9101/9801 Aleks Ignjatović School of Computer Science and Engineering University of New South Wales TOPIC 5: DYNAMIC PROGRAMMING COMP3121/3821/9101/9801 1 / 38

More information

Dynamic Programming Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Dynamic Programming Shabsi Walfish NYU - Fundamental Algorithms Summer 2006 Dynamic Programming What is Dynamic Programming? Technique for avoiding redundant work in recursive algorithms Works best with optimization problems that have a nice underlying structure Can often be used

More information

Unit-5 Dynamic Programming 2016

Unit-5 Dynamic Programming 2016 5 Dynamic programming Overview, Applications - shortest path in graph, matrix multiplication, travelling salesman problem, Fibonacci Series. 20% 12 Origin: Richard Bellman, 1957 Programming referred to

More information

Longest Common Subsequence. Definitions

Longest Common Subsequence. Definitions Longest Common Subsequence LCS is an interesting variation on the classical string matching problem: the task is that of finding the common portion of two strings (more precise definition in a couple of

More information

Efficient Sequential Algorithms, Comp309. Problems. Part 1: Algorithmic Paradigms

Efficient Sequential Algorithms, Comp309. Problems. Part 1: Algorithmic Paradigms Efficient Sequential Algorithms, Comp309 Part 1: Algorithmic Paradigms University of Liverpool References: T. H. Cormen, C. E. Leiserson, R. L. Rivest Introduction to Algorithms, Second Edition. MIT Press

More information

15.4 Longest common subsequence

15.4 Longest common subsequence 15.4 Longest common subsequence Biological applications often need to compare the DNA of two (or more) different organisms A strand of DNA consists of a string of molecules called bases, where the possible

More information

Dynamic Programming Group Exercises

Dynamic Programming Group Exercises Name: Name: Name: Dynamic Programming Group Exercises Adapted from material by Cole Frederick Please work the following problems in groups of 2 or 3. Use additional paper as needed, and staple the sheets

More information

Algorithms: Dynamic Programming

Algorithms: Dynamic Programming Algorithms: Dynamic Programming Amotz Bar-Noy CUNY Spring 2012 Amotz Bar-Noy (CUNY) Dynamic Programming Spring 2012 1 / 58 Dynamic Programming General Strategy: Solve recursively the problem top-down based

More information

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Dynamic Programming

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Dynamic Programming Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 25 Dynamic Programming Terrible Fibonacci Computation Fibonacci sequence: f = f(n) 2

More information

Tutorial 6-7. Dynamic Programming and Greedy

Tutorial 6-7. Dynamic Programming and Greedy Tutorial 6-7 Dynamic Programming and Greedy Dynamic Programming Why DP? Natural Recursion may be expensive. For example, the Fibonacci: F(n)=F(n-1)+F(n-2) Recursive implementation memoryless : time= 1

More information

Longest Common Subsequences and Substrings

Longest Common Subsequences and Substrings Longest Common Subsequences and Substrings Version November 5, 2014 Version November 5, 2014 Longest Common Subsequences and Substrings 1 / 16 Longest Common Subsequence Given two sequences X = (x 1, x

More information

CS 231: Algorithmic Problem Solving

CS 231: Algorithmic Problem Solving CS 231: Algorithmic Problem Solving Naomi Nishimura Module 5 Date of this version: June 14, 2018 WARNING: Drafts of slides are made available prior to lecture for your convenience. After lecture, slides

More information

We augment RBTs to support operations on dynamic sets of intervals A closed interval is an ordered pair of real

We augment RBTs to support operations on dynamic sets of intervals A closed interval is an ordered pair of real 14.3 Interval trees We augment RBTs to support operations on dynamic sets of intervals A closed interval is an ordered pair of real numbers ], with Interval ]represents the set Open and half-open intervals

More information

Framework for Design of Dynamic Programming Algorithms

Framework for Design of Dynamic Programming Algorithms CSE 441T/541T Advanced Algorithms September 22, 2010 Framework for Design of Dynamic Programming Algorithms Dynamic programming algorithms for combinatorial optimization generalize the strategy we studied

More information

Unit 4: Dynamic Programming

Unit 4: Dynamic Programming Unit 4: Dynamic Programming Course contents: Assembly-line scheduling Matrix-chain multiplication Longest common subsequence Optimal binary search trees Applications: Cell flipping, rod cutting, optimal

More information

Subsequence Definition. CS 461, Lecture 8. Today s Outline. Example. Assume given sequence X = x 1, x 2,..., x m. Jared Saia University of New Mexico

Subsequence Definition. CS 461, Lecture 8. Today s Outline. Example. Assume given sequence X = x 1, x 2,..., x m. Jared Saia University of New Mexico Subsequence Definition CS 461, Lecture 8 Jared Saia University of New Mexico Assume given sequence X = x 1, x 2,..., x m Let Z = z 1, z 2,..., z l Then Z is a subsequence of X if there exists a strictly

More information

15.4 Longest common subsequence

15.4 Longest common subsequence 15.4 Longest common subsequence Biological applications often need to compare the DNA of two (or more) different organisms A strand of DNA consists of a string of molecules called bases, where the possible

More information

Dynamic Programming Matrix-chain Multiplication

Dynamic Programming Matrix-chain Multiplication 1 / 32 Dynamic Programming Matrix-chain Multiplication CS 584: Algorithm Design and Analysis Daniel Leblanc 1 1 Senior Adjunct Instructor Portland State University Maseeh College of Engineering and Computer

More information

Algorithms IV. Dynamic Programming. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Algorithms IV. Dynamic Programming. Guoqiang Li. School of Software, Shanghai Jiao Tong University Algorithms IV Dynamic Programming Guoqiang Li School of Software, Shanghai Jiao Tong University Dynamic Programming Shortest Paths in Dags, Revisited Shortest Paths in Dags, Revisited The special distinguishing

More information

Homework3: Dynamic Programming - Answers

Homework3: Dynamic Programming - Answers Most Exercises are from your textbook: Homework3: Dynamic Programming - Answers 1. For the Rod Cutting problem (covered in lecture) modify the given top-down memoized algorithm (includes two procedures)

More information

Partha Sarathi Manal

Partha Sarathi Manal MA 515: Introduction to Algorithms & MA353 : Design and Analysis of Algorithms [3-0-0-6] Lecture 29 http://www.iitg.ernet.in/psm/indexing_ma353/y09/index.html Partha Sarathi Manal psm@iitg.ernet.in Dept.

More information

Longest Common Subsequence, Knapsack, Independent Set Scribe: Wilbur Yang (2016), Mary Wootters (2017) Date: November 6, 2017

Longest Common Subsequence, Knapsack, Independent Set Scribe: Wilbur Yang (2016), Mary Wootters (2017) Date: November 6, 2017 CS161 Lecture 13 Longest Common Subsequence, Knapsack, Independent Set Scribe: Wilbur Yang (2016), Mary Wootters (2017) Date: November 6, 2017 1 Overview Last lecture, we talked about dynamic programming

More information

Module 27: Chained Matrix Multiplication and Bellman-Ford Shortest Path Algorithm

Module 27: Chained Matrix Multiplication and Bellman-Ford Shortest Path Algorithm Module 27: Chained Matrix Multiplication and Bellman-Ford Shortest Path Algorithm This module 27 focuses on introducing dynamic programming design strategy and applying it to problems like chained matrix

More information

Dynamic Programming. Nothing to do with dynamic and nothing to do with programming.

Dynamic Programming. Nothing to do with dynamic and nothing to do with programming. Dynamic Programming Deliverables Dynamic Programming basics Binomial Coefficients Weighted Interval Scheduling Matrix Multiplication /1 Knapsack Longest Common Subsequence 6/12/212 6:56 PM copyright @

More information

15-451/651: Design & Analysis of Algorithms January 26, 2015 Dynamic Programming I last changed: January 28, 2015

15-451/651: Design & Analysis of Algorithms January 26, 2015 Dynamic Programming I last changed: January 28, 2015 15-451/651: Design & Analysis of Algorithms January 26, 2015 Dynamic Programming I last changed: January 28, 2015 Dynamic Programming is a powerful technique that allows one to solve many different types

More information

CS173 Longest Increasing Substrings. Tandy Warnow

CS173 Longest Increasing Substrings. Tandy Warnow CS173 Longest Increasing Substrings Tandy Warnow CS 173 Longest Increasing Substrings Tandy Warnow Today s material The Longest Increasing Subsequence problem DP algorithm for finding a longest increasing

More information

1 i n (p i + r n i ) (Note that by allowing i to be n, we handle the case where the rod is not cut at all.)

1 i n (p i + r n i ) (Note that by allowing i to be n, we handle the case where the rod is not cut at all.) Dynamic programming is a problem solving method that is applicable to many different types of problems. I think it is best learned by example, so we will mostly do examples today. 1 Rod cutting Suppose

More information

CSE 101, Winter Design and Analysis of Algorithms. Lecture 11: Dynamic Programming, Part 2

CSE 101, Winter Design and Analysis of Algorithms. Lecture 11: Dynamic Programming, Part 2 CSE 101, Winter 2018 Design and Analysis of Algorithms Lecture 11: Dynamic Programming, Part 2 Class URL: http://vlsicad.ucsd.edu/courses/cse101-w18/ Goal: continue with DP (Knapsack, All-Pairs SPs, )

More information

IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech

IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech License CC BY-NC-SA 2.0 http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Outline Previously on IN101 Python s anatomy Functions,

More information

CS Algorithms. Dynamic programming and memoization. (Based on slides by Luebke, Lim, Wenbin)

CS Algorithms. Dynamic programming and memoization. (Based on slides by Luebke, Lim, Wenbin) CS 7 - lgorithms Dynamic programming and memoization (ased on slides by Luebke, Lim, Wenbin) When to Use Dynamic Programming Usually used to solve Optimization problems Usually the problem can be formulated

More information

Algorithms for Data Science

Algorithms for Data Science Algorithms for Data Science CSOR W4246 Eleni Drinea Computer Science Department Columbia University Thursday, October 1, 2015 Outline 1 Recap 2 Shortest paths in graphs with non-negative edge weights (Dijkstra

More information

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 15 Lecturer: Michael Jordan October 26, Notes 15 for CS 170

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 15 Lecturer: Michael Jordan October 26, Notes 15 for CS 170 UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 15 Lecturer: Michael Jordan October 26, 2005 Notes 15 for CS 170 1 Introduction to Dynamic Programming Consider the following algorithm

More information

ECE608 - Chapter 15 answers

ECE608 - Chapter 15 answers ¼ À ÈÌ Ê ½ ÈÊÇ Ä ÅË ½µ ½ º¾¹¾ ¾µ ½ º¾¹ µ ½ º¾¹ µ ½ º ¹½ µ ½ º ¹¾ µ ½ º ¹ µ ½ º ¹¾ µ ½ º ¹ µ ½ º ¹ ½¼µ ½ º ¹ ½½µ ½ ¹ ½ ECE608 - Chapter 15 answers (1) CLR 15.2-2 MATRIX CHAIN MULTIPLY(A, s, i, j) 1. if

More information

Dynamic Programming. Outline and Reading. Computing Fibonacci

Dynamic Programming. Outline and Reading. Computing Fibonacci Dynamic Programming Dynamic Programming version 1.2 1 Outline and Reading Matrix Chain-Product ( 5.3.1) The General Technique ( 5.3.2) -1 Knapsac Problem ( 5.3.3) Dynamic Programming version 1.2 2 Computing

More information

Efficient Sequential Algorithms, Comp309. Motivation. Longest Common Subsequence. Part 3. String Algorithms

Efficient Sequential Algorithms, Comp309. Motivation. Longest Common Subsequence. Part 3. String Algorithms Efficient Sequential Algorithms, Comp39 Part 3. String Algorithms University of Liverpool References: T. H. Cormen, C. E. Leiserson, R. L. Rivest Introduction to Algorithms, Second Edition. MIT Press (21).

More information

Greedy Algorithms. Algorithms

Greedy Algorithms. Algorithms Greedy Algorithms Algorithms Greedy Algorithms Many algorithms run from stage to stage At each stage, they make a decision based on the information available A Greedy algorithm makes decisions At each

More information

CSE 417 Dynamic Programming (pt 4) Sub-problems on Trees

CSE 417 Dynamic Programming (pt 4) Sub-problems on Trees CSE 417 Dynamic Programming (pt 4) Sub-problems on Trees Reminders > HW4 is due today > HW5 will be posted shortly Dynamic Programming Review > Apply the steps... 1. Describe solution in terms of solution

More information

CSED233: Data Structures (2017F) Lecture12: Strings and Dynamic Programming

CSED233: Data Structures (2017F) Lecture12: Strings and Dynamic Programming (2017F) Lecture12: Strings and Dynamic Programming Daijin Kim CSE, POSTECH dkim@postech.ac.kr Strings A string is a sequence of characters Examples of strings: Python program HTML document DNA sequence

More information

Dynamic Programming: 1D Optimization. Dynamic Programming: 2D Optimization. Fibonacci Sequence. Crazy 8 s. Edit Distance

Dynamic Programming: 1D Optimization. Dynamic Programming: 2D Optimization. Fibonacci Sequence. Crazy 8 s. Edit Distance Dynamic Programming: 1D Optimization Fibonacci Sequence To efficiently calculate F [x], the xth element of the Fibonacci sequence, we can construct the array F from left to right (or bottom up ). We start

More information

CMSC 451: Lecture 11 Dynamic Programming: Longest Common Subsequence Thursday, Oct 5, 2017

CMSC 451: Lecture 11 Dynamic Programming: Longest Common Subsequence Thursday, Oct 5, 2017 CMSC 451: Lecture 11 Dynamic Programming: Longest Common Subsequence Thursday, Oct 5, 217 Reading: This algorithm is not covered in KT or DPV. It is closely related to the Sequence lignment problem of

More information

COMP251: Greedy algorithms

COMP251: Greedy algorithms COMP251: Greedy algorithms Jérôme Waldispühl School of Computer Science McGill University Based on (Cormen et al., 2002) Based on slides from D. Plaisted (UNC) & (goodrich & Tamassia, 2009) Disjoint sets

More information

Memoization/Dynamic Programming. The String reconstruction problem. CS124 Lecture 11 Spring 2018

Memoization/Dynamic Programming. The String reconstruction problem. CS124 Lecture 11 Spring 2018 CS124 Lecture 11 Spring 2018 Memoization/Dynamic Programming Today s lecture discusses memoization, which is a method for speeding up algorithms based on recursion, by using additional memory to remember

More information

CS141: Intermediate Data Structures and Algorithms Dynamic Programming

CS141: Intermediate Data Structures and Algorithms Dynamic Programming CS141: Intermediate Data Structures and Algorithms Dynamic Programming Amr Magdy Programming? In this context, programming is a tabular method Other examples: Linear programing Integer programming 2 Rod

More information

Computer Sciences Department 1

Computer Sciences Department 1 1 Advanced Design and Analysis Techniques (15.1, 15.2, 15.3, 15.4 and 15.5) 3 Objectives Problem Formulation Examples The Basic Problem Principle of optimality Important techniques: dynamic programming

More information

Lecture 57 Dynamic Programming. (Refer Slide Time: 00:31)

Lecture 57 Dynamic Programming. (Refer Slide Time: 00:31) Programming, Data Structures and Algorithms Prof. N.S. Narayanaswamy Department of Computer Science and Engineering Indian Institution Technology, Madras Lecture 57 Dynamic Programming (Refer Slide Time:

More information

Recursive-Fib(n) if n=1 or n=2 then return 1 else return Recursive-Fib(n-1)+Recursive-Fib(n-2)

Recursive-Fib(n) if n=1 or n=2 then return 1 else return Recursive-Fib(n-1)+Recursive-Fib(n-2) Dynamic Programming Any recursive formula can be directly translated into recursive algorithms. However, sometimes the compiler will not implement the recursive algorithm very efficiently. When this is

More information

Dynamic Programming CS 445. Example: Floyd Warshll Algorithm: Computing all pairs shortest paths

Dynamic Programming CS 445. Example: Floyd Warshll Algorithm: Computing all pairs shortest paths CS 44 Dynamic Programming Some of the slides are courtesy of Charles Leiserson with small changes by Carola Wenk Example: Floyd Warshll lgorithm: Computing all pairs shortest paths Given G(V,E), with weight

More information

Dynamic Programming Algorithms

Dynamic Programming Algorithms Based on the notes for the U of Toronto course CSC 364 Dynamic Programming Algorithms The setting is as follows. We wish to find a solution to a given problem which optimizes some quantity Q of interest;

More information

Dynamic Programming 1

Dynamic Programming 1 Dynamic Programming 1 Jie Wang University of Massachusetts Lowell Department of Computer Science 1 I thank Prof. Zachary Kissel of Merrimack College for sharing his lecture notes with me; some of the examples

More information

Dynamic Programmming: Activity Selection

Dynamic Programmming: Activity Selection Dynamic Programmming: Activity Selection Select the maximum number of non-overlapping activities from a set of n activities A = {a 1,, a n } (sorted by finish times). Identify easier subproblems to solve.

More information

Dynamic Programming. CIS 110, Fall University of Pennsylvania

Dynamic Programming. CIS 110, Fall University of Pennsylvania Dynamic Programming CIS 110, Fall 2012 University of Pennsylvania Dynamic Programming Dynamic programming records saves computation for reuse later. Programming: in the optimization sense ( Linear Programming

More information

Lecture 22: Dynamic Programming

Lecture 22: Dynamic Programming Lecture 22: Dynamic Programming COSC242: Algorithms and Data Structures Brendan McCane Department of Computer Science, University of Otago Dynamic programming The iterative and memoised algorithms for

More information

1 Dynamic Programming

1 Dynamic Programming CS161 Lecture 13 Dynamic Programming and Greedy Algorithms Scribe by: Eric Huang Date: May 13, 2015 1 Dynamic Programming The idea of dynamic programming is to have a table of solutions of subproblems

More information

10/24/ Rotations. 2. // s left subtree s right subtree 3. if // link s parent to elseif == else 11. // put x on s left

10/24/ Rotations. 2. // s left subtree s right subtree 3. if // link s parent to elseif == else 11. // put x on s left 13.2 Rotations MAT-72006 AA+DS, Fall 2013 24-Oct-13 368 LEFT-ROTATE(, ) 1. // set 2. // s left subtree s right subtree 3. if 4. 5. // link s parent to 6. if == 7. 8. elseif == 9. 10. else 11. // put x

More information

F(0)=0 F(1)=1 F(n)=F(n-1)+F(n-2)

F(0)=0 F(1)=1 F(n)=F(n-1)+F(n-2) Algorithms Dana Shapira Lesson #4: Dynamic programming Fibonacci Series F()= F()= F(n)=F(n-)+F(n-) Write a Divide and Conquer Algorithm! What is its running time? Binomial Coefficients n! n = ( )! n! Recursive

More information

Greedy Algorithms. CLRS Chapters Introduction to greedy algorithms. Design of data-compression (Huffman) codes

Greedy Algorithms. CLRS Chapters Introduction to greedy algorithms. Design of data-compression (Huffman) codes Greedy Algorithms CLRS Chapters 16.1 16.3 Introduction to greedy algorithms Activity-selection problem Design of data-compression (Huffman) codes (Minimum spanning tree problem) (Shortest-path problem)

More information

Dynamic Programming. Lecture Overview Introduction

Dynamic Programming. Lecture Overview Introduction Lecture 12 Dynamic Programming 12.1 Overview Dynamic Programming is a powerful technique that allows one to solve many different types of problems in time O(n 2 ) or O(n 3 ) for which a naive approach

More information

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions U.C. Berkeley CS70 : Algorithms Midterm 2 Solutions Lecturers: Sanjam Garg and Prasad aghavra March 20, 207 Midterm 2 Solutions. (0 points) True/False Clearly put your answers in the answer box in front

More information

Algorithmic Paradigms. Chapter 6 Dynamic Programming. Steps in Dynamic Programming. Dynamic Programming. Dynamic Programming Applications

Algorithmic Paradigms. Chapter 6 Dynamic Programming. Steps in Dynamic Programming. Dynamic Programming. Dynamic Programming Applications lgorithmic Paradigms reed. Build up a solution incrementally, only optimizing some local criterion. hapter Dynamic Programming Divide-and-conquer. Break up a problem into two sub-problems, solve each sub-problem

More information

Dynamic Programming in Haskell

Dynamic Programming in Haskell Dynamic Programming in Haskell Thomas Sutton, Anchor 2015-05-27 Introduction Introduction This is a talk in two parts: 1. First I ll introduce dynamic programming and a framework for implementing DP algorithms

More information

Dynamic Programming Algorithms

Dynamic Programming Algorithms CSC 364S Notes University of Toronto, Fall 2003 Dynamic Programming Algorithms The setting is as follows. We wish to find a solution to a given problem which optimizes some quantity Q of interest; for

More information

Longest Common Subsequence

Longest Common Subsequence .. CSC 448 Bioinformatics Algorithms Alexander Dekhtyar.. Dynamic Programming for Bioinformatics... Longest Common Subsequence Subsequence. Given a string S = s 1 s 2... s n, a subsequence of S is any

More information

1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors

1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors 1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors on an EREW PRAM: See solution for the next problem. Omit the step where each processor sequentially computes the AND of

More information

Data Structure and Algorithm II Homework #2 Due: 13pm, Monday, October 31, === Homework submission instructions ===

Data Structure and Algorithm II Homework #2 Due: 13pm, Monday, October 31, === Homework submission instructions === Data Structure and Algorithm II Homework #2 Due: 13pm, Monday, October 31, 2011 === Homework submission instructions === Submit the answers for writing problems (including your programming report) through

More information

Design and Analysis of Algorithms 演算法設計與分析. Lecture 7 April 6, 2016 洪國寶

Design and Analysis of Algorithms 演算法設計與分析. Lecture 7 April 6, 2016 洪國寶 Design and Analysis of Algorithms 演算法設計與分析 Lecture 7 April 6, 2016 洪國寶 1 Course information (5/5) Grading (Tentative) Homework 25% (You may collaborate when solving the homework, however when writing up

More information

(Feodor F. Dragan) Department of Computer Science Kent State University. Advanced Algorithms, Feodor F. Dragan, Kent State University 1

(Feodor F. Dragan) Department of Computer Science Kent State University. Advanced Algorithms, Feodor F. Dragan, Kent State University 1 $GYDQFH $OJRULWKPV (Feodor F. Dragan) Department of Computer Science Kent State University Advanced Algorithms, Feodor F. Dragan, Kent State University Textbook: Thomas Cormen, Charles Leisterson, Ronald

More information

Optimization II: Dynamic Programming

Optimization II: Dynamic Programming Chapter 12 Optimization II: Dynamic Programming In the last chapter, we saw that greedy algorithms are efficient solutions to certain optimization problems. However, there are optimization problems for

More information

CMSC 451: Dynamic Programming

CMSC 451: Dynamic Programming CMSC 41: Dynamic Programming Slides By: Carl Kingsford Department of Computer Science University of Maryland, College Park Based on Sections 6.1&6.2 of Algorithm Design by Kleinberg & Tardos. Dynamic Programming

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 16 Dynamic Programming Least Common Subsequence Saving space Adam Smith Least Common Subsequence A.k.a. sequence alignment edit distance Longest Common Subsequence

More information

CSC 505, Spring 2005 Week 6 Lectures page 1 of 9

CSC 505, Spring 2005 Week 6 Lectures page 1 of 9 CSC 505, Spring 2005 Week 6 Lectures page 1 of 9 Objectives: learn general strategies for problems about order statistics learn how to find the median (or k-th largest) in linear average-case number of

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms Design and Analysis of Algorithms CSE 5311 Lecture 16 Greedy algorithms Junzhou Huang, Ph.D. Department of Computer Science and Engineering CSE5311 Design and Analysis of Algorithms 1 Overview A greedy

More information

Dynamic Programming on Plagiarism Detecting Application

Dynamic Programming on Plagiarism Detecting Application Dynamic Programming on Plagiarism Detecting Application Edwin Lunando/13509024 Program Studi Teknik Informatika Sekolah Teknik Elektro dan Informatika Institut Teknologi Bandung, Jl. Ganesha 10 Bandung

More information

Lectures 12 and 13 Dynamic programming: weighted interval scheduling

Lectures 12 and 13 Dynamic programming: weighted interval scheduling Lectures 12 and 13 Dynamic programming: weighted interval scheduling COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski Lectures 12-13: Dynamic Programming 1 Overview Last week: Graph

More information