Lecture 7 Topics Dynamic Programming

Slides:



Advertisements
Similar presentations
Lecture 8: Dynamic Programming Shang-Hua Teng. Longest Common Subsequence Biologists need to measure how similar strands of DNA are to determine how closely.
Advertisements

CPSC 335 Dynamic Programming Dr. Marina Gavrilova Computer Science University of Calgary Canada.
Overview What is Dynamic Programming? A Sequence of 4 Steps
Algorithms Dynamic programming Longest Common Subsequence.
COMP8620 Lecture 8 Dynamic Programming.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS420 Lecture 9 Dynamic Programming. Optimization Problems In optimization problems a set of choices are to be made to arrive at an optimum, and sub problems.
David Luebke 1 5/4/2015 CS 332: Algorithms Dynamic Programming Greedy Algorithms.
Comp 122, Fall 2004 Dynamic Programming. dynprog - 2 Lin / Devi Comp 122, Spring 2004 Longest Common Subsequence  Problem: Given 2 sequences, X =  x.
1 Dynamic Programming (DP) Like divide-and-conquer, solve problem by combining the solutions to sub-problems. Differences between divide-and-conquer and.
1 Longest Common Subsequence (LCS) Problem: Given sequences x[1..m] and y[1..n], find a longest common subsequence of both. Example: x=ABCBDAB and y=BDCABA,
Data Structures Lecture 10 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2006 Lecture 1 (Part 3) Design Patterns for Optimization Problems.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2002 Lecture 2 Tuesday, 2/5/02 Dynamic Programming.
Dynamic Programming CIS 606 Spring 2010.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2009 Design Patterns for Optimization Problems Dynamic Programming.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2002 Lecture 1 (Part 3) Tuesday, 9/3/02 Design Patterns for Optimization.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Analysis of Algorithms CS 477/677
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2008 Design Patterns for Optimization Problems Dynamic Programming.
November 7, 2005Copyright © by Erik D. Demaine and Charles E. Leiserson Dynamic programming Design technique, like divide-and-conquer. Example:
© 2004 Goodrich, Tamassia Dynamic Programming1. © 2004 Goodrich, Tamassia Dynamic Programming2 Matrix Chain-Products (not in book) Dynamic Programming.
Lecture 8: Dynamic Programming Shang-Hua Teng. First Example: n choose k Many combinatorial problems require the calculation of the binomial coefficient.
Analysis of Algorithms
1 Dynamic Programming Jose Rolim University of Geneva.
Longest Common Subsequence
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2005 Design Patterns for Optimization Problems Dynamic Programming.
Dynamic Programming – Part 2 Introduction to Algorithms Dynamic Programming – Part 2 CSE 680 Prof. Roger Crawfis.
Algorithms and Data Structures Lecture X
Dynamic Programming UNC Chapel Hill Z. Guo.
October 21, Algorithms and Data Structures Lecture X Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
ADA: 7. Dynamic Prog.1 Objective o introduce DP, its two hallmarks, and two major programming techniques o look at two examples: the fibonacci.
CS 8833 Algorithms Algorithms Dynamic Programming.
Greedy Methods and Backtracking Dr. Marina Gavrilova Computer Science University of Calgary Canada.
6/4/ ITCS 6114 Dynamic programming Longest Common Subsequence.
Dynamic Programming continued David Kauchak cs302 Spring 2012.
Dynamic Programming (Ch. 15) Not a specific algorithm, but a technique (like divide- and-conquer). Developed back in the day when “programming” meant “tabular.
Dynamic Programming David Kauchak cs302 Spring 2013.
COSC 3101A - Design and Analysis of Algorithms 8 Elements of DP Memoization Longest Common Subsequence Greedy Algorithms Many of these slides are taken.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 14.
Introduction to Algorithms Jiafen Liu Sept
Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP.
1 Dynamic Programming Topic 07 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.
Dynamic Programming David Kauchak cs161 Summer 2009.
CSC 213 Lecture 19: Dynamic Programming and LCS. Subsequences (§ ) A subsequence of a string x 0 x 1 x 2 …x n-1 is a string of the form x i 1 x.
9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 16 Dynamic.
TU/e Algorithms (2IL15) – Lecture 4 1 DYNAMIC PROGRAMMING II
CS583 Lecture 12 Jana Kosecka Dynamic Programming Longest Common Subsequence Matrix Chain Multiplication Greedy Algorithms Many slides here are based on.
Dynamic Programming Csc 487/687 Computing for Bioinformatics.
Dynamic Programming Typically applied to optimization problems
David Meredith Dynamic programming David Meredith
Advanced Algorithms Analysis and Design
Least common subsequence:
CS200: Algorithm Analysis
Chapter 8 Dynamic Programming.
Dynamic Programming.
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Dynamic Programming Dr. Yingwu Zhu Chapter 15.
CS 3343: Analysis of Algorithms
CS6045: Advanced Algorithms
Longest Common Subsequence
Lecture 8. Paradigm #6 Dynamic Programming
Introduction to Algorithms: Dynamic Programming
Longest Common Subsequence
Longest common subsequence (LCS)
Analysis of Algorithms CS 477/677
Longest Common Subsequence
Longest Common Subsequence
Algorithm Course Dr. Aref Rashad
Presentation transcript:

Lecture 7 Topics Dynamic Programming Reference: Introduction to Algorithm by Cormen Chapter 15: Dynamic Programming Data Structure and Algorithm

Longest Common Subsequence (LCS) Biologists need to measure similarity between DNA and thus determine how closely related an organism is to another. They do this by considering DNA as strings of letters A,C,G,T and then comparing similarities in the strings. Formally, they look at common subsequences in the strings. Example X = ABCBDAB, Y=BDCABA Subsequences may be: ABA, BCA, BCB, BBA BCBA BDAB etc. But the Longest Common Subsequences (LCS) are BCBA and BDAB. How to find LCS efficiently? Data Structure and Algorithm

Longest Common Subsequence ( Brute Force Approach ) if |X| = m, |Y| = n, then there are 2m subsequences of X; we must compare each with Y (n comparisons) So the running time of the brute-force algorithm is O(n 2m) Making it impractical for long sequences. LCS problem has optimal substructure: solutions of subproblems are parts of the final solution. Subproblems: “find LCS of pairs of prefixes of X and Y” Data Structure and Algorithm

LCS: Optimal Substructure Data Structure and Algorithm

LCS: Setup for Dynamic Programming First we’ll find the length of LCS, along the way we will leave “clues” on finding subsequence. Define Xi, Yj to be the prefixes of X and Y of length i and j respectively. Define c[i,j] to be the length of LCS of Xi and Yj Then the length of LCS of X and Y will be c[m,n] Data Structure and Algorithm

LCS recurrence Notice the issues…This recurrence is exponential. We don’t know max ahead of time. The subproblems overlap, to find LCS we need to find LCS of c[i, j-1] and of c[i-1, j] Data Structure and Algorithm

LCS Algorithm First we’ll find the length of LCS. Later we’ll modify the algorithm to find LCS itself. Recall we want to let Xi, Yj to be the prefixes of X and Y of length i and j respectively And that Define c[i,j] to be the length of LCS of Xi and Yj Then the length of LCS of X and Y will be c[m,n] Data Structure and Algorithm

LCS Recursive Solution We start with i = j = 0 (empty substrings of x and y) Since X0 and Y0 are empty strings, their LCS is always empty (i.e. c[0,0] = 0) LCS of empty string and any other string is empty, so for every i and j: c[0, j] = c[i,0] = 0 Data Structure and Algorithm

LCS Recursive Solution When we calculate c[i,j], we consider two cases: First case: x[i]=y[j]: one more symbol in strings X and Y matches, so the length of LCS Xi and Yj equals to the length of LCS of smaller strings Xi-1 and Yi-1 , plus 1 Data Structure and Algorithm

LCS Recursive Solution Second case: x[i] != y[j] As symbols don’t match, our solution is not improved, and the length of LCS(Xi , Yj) is the same as before (i.e. maximum of LCS(Xi, Yj-1) and LCS(Xi-1,Yj) Data Structure and Algorithm

LCS Example We’ll see how LCS algorithm works on the following X = ABCB Y = BDCAB What is the Longest Common Subsequence of X and Y? LCS(X, Y) = BCB X = A B C B Y = B D C A B Data Structure and Algorithm

LCS Example (0) X = ABCB; m = |X| = 4 Y = BDCAB; n = |Y| = 5 j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 B 2 3 C 4 B X = ABCB; m = |X| = 4 Y = BDCAB; n = |Y| = 5 Allocate array c[6,5] Data Structure and Algorithm

LCS Example (1) for i = 1 to m c[i,0] = 0 j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 B 2 3 C 4 B for i = 1 to m c[i,0] = 0 Data Structure and Algorithm

LCS Example (2) for j = 0 to n c[0,j] = 0 j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 B 2 3 C 4 B for j = 0 to n c[0,j] = 0 Data Structure and Algorithm

LCS Example (3) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 B 2 3 C 4 B A 1 B 2 3 C 4 B case i=1 and j=1 A != B but, c[0,1]>=c[1,0] so c[1,1] = c[0,1], and b[1,1] = Data Structure and Algorithm

LCS Example (4) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 B 2 3 C 4 B A 1 B 2 3 C 4 B case i=1 and j=2 A != D but, c[0,2]>=c[1,1] so c[1,2] = c[0,2], and b[1,2] = Data Structure and Algorithm

LCS Example (5) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 B 2 3 C 4 B A 1 B 2 3 C 4 B case i=1 and j=3 A != C but, c[0,3]>=c[1,2] so c[1,3] = c[0,3], and b[1,3] = Data Structure and Algorithm

LCS Example (6) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 B 2 3 C 4 B A 1 1 B 2 3 C 4 B case i=1 and j=4 A = A so c[1,4] = c[0,3]+1, and b[1,4] = Data Structure and Algorithm

LCS Example (7) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 3 C 4 B A 1 1 1 B 2 3 C 4 B case i=1 and j=5 A != B this time c[0,5]<c[1,4] so c[1,5] = c[1, 4], and b[1,5] = Data Structure and Algorithm

LCS Example (8) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 3 C 4 B A 1 1 1 B 2 1 3 C 4 B case i=2 and j=1 B = B so c[2, 1] = c[1, 0]+1, and b[2, 1] = Data Structure and Algorithm

LCS Example (9) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 1 3 C 4 A 1 1 1 B 2 1 1 3 C 4 B case i=2 and j=2 B != D and c[1, 2] < c[2, 1] so c[2, 2] = c[2, 1] and b[2, 2] = Data Structure and Algorithm

LCS Example (10) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 1 1 3 C A 1 1 1 B 2 1 1 1 3 C 4 B case i=2 and j=3 B != D and c[1, 3] < c[2, 2] so c[2, 3] = c[2, 2] and b[2, 3] = Data Structure and Algorithm

LCS Example (11) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 1 1 1 3 A 1 1 1 B 2 1 1 1 1 3 C 4 B case i=2 and j=4 B != A and c[1, 4] = c[2, 3] so c[2, 4] = c[1, 4] and b[2, 2] = Data Structure and Algorithm

LCS Example (12) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 1 1 1 2 A 1 1 1 B 2 1 1 1 1 2 3 C 4 B case i=2 and j=5 B = B so c[2, 5] = c[1, 4]+1 and b[2, 5] = Data Structure and Algorithm

LCS Example (13) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 1 1 1 2 A 1 1 1 B 2 1 1 1 1 2 3 C 1 4 B case i=3 and j=1 C != B and c[2, 1] > c[3,0] so c[3, 1] = c[2, 1] and b[3, 1] = Data Structure and Algorithm

LCS Example (14) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 1 1 1 2 A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 4 B case i=3 and j= 2 C != D and c[2, 2] = c[3, 1] so c[3, 2] = c[2, 2] and b[3, 2] = Data Structure and Algorithm

LCS Example (15) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 1 1 1 2 A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 4 B case i=3 and j= 3 C = C so c[3, 3] = c[2, 2]+1 and b[3, 3] = Data Structure and Algorithm

LCS Example (16) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 1 1 1 2 A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 4 B case i=3 and j= 4 C != A c[2, 4] < c[3, 3] so c[3, 4] = c[3, 3] and b[3, 3] = Data Structure and Algorithm

LCS Example (17) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 1 1 1 2 A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 4 B case i=3 and j= 5 C != B c[2, 5] = c[3, 4] so c[3, 5] = c[2, 5] and b[3, 5] = Data Structure and Algorithm

LCS Example (18) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 1 1 1 2 A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 4 B 1 case i=4 and j=1 B = B so c[4, 1] = c[3, 0]+1 and b[4, 1] = Data Structure and Algorithm

LCS Example (19) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 1 1 1 2 A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 4 B 1 1 case i=4 and j=2 B != D c[3, 2] = c[4, 1] so c[4, 2] = c[3, 2] and b[4, 2] = Data Structure and Algorithm

LCS Example (20) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 1 1 1 2 A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 4 B 1 1 2 case i=4 and j= 3 B != C c[3, 3] > c[4, 2] so c[4, 3] = c[3, 3] and b[4, 3] = Data Structure and Algorithm

LCS Example (21) j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 1 1 1 2 A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 4 B 1 1 2 2 case i=4 and j=4 B != A c[3, 4] = c[4, 3] so c[4, 4] = c[3, 4] and b[3, 5] = Data Structure and Algorithm

LCS Example (22) 3 j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 1 1 1 A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 4 B 3 1 1 2 2 case i=4 and j=5 B= B so c[4, 5] = c[3, 4]+1 and b[4, 5] = Data Structure and Algorithm

LCS Algorithm LCS-Length(X, Y) m = length(X), n = length(Y) for i = 1 to m do c[i, 0] = 0 for j = 0 to n do c[0, j] = 0 for j = 1 to n do if ( xi = = yj ) then c[i, j] = c[i - 1, j - 1] + 1 else if c[i - 1, j]>=c[i, j - 1] then c[i, j] = c[i - 1, j] else c[i, j] = c[i, j - 1] return c and b Data Structure and Algorithm

LCS Algorithm Running Time LCS algorithm calculates the values of each entry of the array c[m,n] So the running time is clearly O(mn) as each entry is done in 3 steps. Now how to get at the solution? We use the arrows we created to guide us. We simply follow arrows back to base case 0 Data Structure and Algorithm

3 Finding LCS j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B 2 1 1 1 1 2 3 A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 3 4 B 1 1 2 2 Data Structure and Algorithm

3 Finding LCS (2) LCS: B C B j 0 1 2 3 4 5 i Yj B D C A B Xi A 1 1 1 B A 1 1 1 B 2 1 1 1 1 2 3 C 1 1 2 2 2 3 4 B 1 1 2 2 LCS: B C B Data Structure and Algorithm

Finding LCS (3) Print_LCS (X, i, j) if i = 0 or j = 0 then return if b[i, j] = “ “ then Print_LCS (X, i-1, j-1) Print X[i] elseif b[i, j] = “ “ then Print_LCS (X, i-1, j) else Print_LCS (X, i, j-1) Cost: O(m+n) Data Structure and Algorithm

Element of Dynamic Programming Optimal Substructure Overlapping Subproblems Data Structure and Algorithm

Optimal Substructure A problem exhibits optimal substructure if an optimal solution contains optimal solutions to its subproblems. Build an optimal solution from optimal solutions to subproblems solutions of subproblems are parts of the final solution. Example :Longest Common Subsequence - An LCS contains within it optimal solutions to the prefixes of the two input sequences. Common with Greedy Solution Data Structure and Algorithm

Overlapping Subproblems Divide-and-Conquer is suitable when generating brand-new problems at each step of the recursion. Dynamic-programming algorithms take advantage of overlapping subproblems by solving each subproblem once and then storing the solution in a table where it can be looked up when needed, using constant time per lookup Data Structure and Algorithm

Dynamic VS Greedy Dynamic programming uses optimal substructure in a bottom-up fashion First find optimal solutions to subproblems and, having solved the subproblems, we find an optimal solution to the problem Greedy algorithms use optimal substructure in a top-down fashion First make a choice – the choice that looks best at the time – and then solving the resulting subproblem Data Structure and Algorithm