CSC 213 Lecture 19: Dynamic Programming and LCS. Subsequences (§ 11.5.1) A subsequence of a string x 0 x 1 x 2 …x n-1 is a string of the form x i 1 x.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms 6.046J/18.401J/SMA5503
Advertisements

Dynamic Programming.
Lecture 8: Dynamic Programming Shang-Hua Teng. Longest Common Subsequence Biologists need to measure how similar strands of DNA are to determine how closely.
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.
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 Reading Material: Chapter 7..
Dynamic Programming CIS 606 Spring 2010.
Dynamic Programming Dynamic Programming algorithms address problems whose solution is recursive in nature, but has the following property: The direct implementation.
CSC401 – Analysis of Algorithms Lecture Notes 12 Dynamic Programming
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)
1 Foundations of Software Design Lecture 26: Text Processing, Tries, and Dynamic Programming Marti Hearst & Fredrik Wallenberg Fall 2002.
1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
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:
Dynamic Programming Reading Material: Chapter 7 Sections and 6.
© 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.
Dynamic Programming 0-1 Knapsack These notes are taken from the notes by Dr. Steve Goddard at
1 Dynamic Programming Jose Rolim University of Geneva.
Lecture 7 Topics Dynamic Programming
Longest Common Subsequence
Dynamic Programming From An Excel Perspective. Dynamic Programming From An Excel Perspective Ranette Halverson, Richard Simpson, Catherine Stringfellow.
Dynamic Programming (16.0/15) The 3-d Paradigm 1st = Divide and Conquer 2nd = Greedy Algorithm Dynamic Programming = metatechnique (not a particular algorithm)
Text Processing 1 Last Update: July 31, Topics Notations & Terminology Pattern Matching – Brute Force – Boyer-Moore Algorithm – Knuth-Morris-Pratt.
ADA: 7. Dynamic Prog.1 Objective o introduce DP, its two hallmarks, and two major programming techniques o look at two examples: the fibonacci.
Lecture21: Dynamic Programming Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Greedy Methods and Backtracking Dr. Marina Gavrilova Computer Science University of Calgary Canada.
6/4/ ITCS 6114 Dynamic programming Longest Common Subsequence.
Introduction to Algorithms Jiafen Liu Sept
Dynamic Programming & Memoization. When to use? Problem has a recursive formulation Solutions are “ordered” –Earlier vs. later recursions.
2/19/ ITCS 6114 Dynamic programming 0-1 Knapsack problem.
Part 2 # 68 Longest Common Subsequence T.H. Cormen et al., Introduction to Algorithms, MIT press, 3/e, 2009, pp Example: X=abadcda, Y=acbacadb.
David Luebke 1 2/26/2016 CS 332: Algorithms Dynamic Programming.
Lab 6 Problem 1: DNA. DNA Given a string with length N, determine the number of occurrences of some given substrings (with length K) in that string. For.
Dynamic Programming Tutorial &Practice on Longest Common Sub-sequence.
Fundamental Data Structures and Algorithms Ananda Guna March 18, 2003 Dynamic Programming Part 1.
Dynamic Programming … Continued
Dynamic Programming Fundamental Data Structures and Algorithms Klaus Sutner March 30, 2004.
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
1 COMP9024: Data Structures and Algorithms Week Ten: Text Processing Hui Wu Session 1, 2016
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar.
Merge Sort 5/28/2018 9:55 AM Dynamic Programming Dynamic Programming.
Pattern Matching 9/14/2018 3:36 AM
Least common subsequence:
JinJu Lee & Beatrice Seifert CSE 5311 Fall 2005 Week 10 (Nov 1 & 3)
CS200: Algorithm Analysis
Chapter 8 Dynamic Programming.
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Dynamic Programming Dr. Yingwu Zhu Chapter 15.
ICS 353: Design and Analysis of Algorithms
Longest Common Subsequence
Lecture 8. Paradigm #6 Dynamic Programming
Dynamic Programming-- Longest Common Subsequence
Introduction to Algorithms: Dynamic Programming
Longest Common Subsequence
Longest common subsequence (LCS)
Lecture 5 Dynamic Programming
Analysis of Algorithms CS 477/677
Lecture 5 Dynamic Programming
Longest Common Subsequence
Longest Common Subsequence (LCS)
Presentation transcript:

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 i 2 …x i k, where i j < i j+1 This is not the same things as a substring! Subsequences can skip letters in the string Substrings must use consecutive letters Example string: ABCDEFGHIJK Subsequence (& substring): DEFGH Subsequence (& NOT substring): ACEFHJ Not subsequence or substring: DAGH

Longest Common Subsequence (LCS) Problem Given two strings X and Y, find longest subsequence in both X and Y Applications in DNA testing (  ={A,C,G,T}) Example the LCS for: ABCDEFG and XZACKDFWGH is ACDFG

Longest Common Subsequence (LCS) Problem Given two strings X and Y, find longest subsequence in both X and Y Applications in DNA testing (  ={A,C,G,T}) Example the LCS for: ABCDEFG and XZACKDFWGH is ACDFG

Dynamic Programming Some problems appear hard There does not seem to be a simple solutions Require a brute force approach --- evaluate every solution This means constantly reevaluating a lot of options Ultimately, this takes exponential time -- O(2 n ) For a class of problems, however, the solution is:

Dynamic Programming Works from problems with: Simple subproblems: can be defined using only a few simple variables Subproblem optimality: can define how to solve problem using the subproblem solutions Subproblem overlap: subproblems overlap such that the solution to a first subproblem can (help) solve later subproblems

How Not to Solve LCS in your Lifetime Brute-force solution: List all subsequences of X Check each subsequence to see if it is also a subsequence of Y Return the longest one of these Analysis: If X has length n, it has 2 n subsequences While waiting, you can not only get coffee, but could first fly to Columbia and pick the beans!

How to Solve LCS Quickly If X and Y are 1 character, LCS is 0 or 1 If we then add 1 character to X and Y, LCS increases by at most 1 Note that we do not need to rescan the first character X aa Y ba X abababab Y bdbdadad

Dynamic-Programming Solution Use an array L to hold solution to subproblems L[i,j] stores LCS of X[0..i] and Y[0..j] Define array to include an index of -1 L[-1, * ] computes LCS for X[0..-1] = “” L[ *,-1] computes LCS for Y[0..-1] = “” L[-1, * ] = 0 and L[ *, -1] = 0 since there are no characters to match!

Dynamic-Programming Solution Solve for remaining L[i,j] as follows: If x i = y j, then L[i,j] = L[i -1, j -1 ] +1  E.g., one more than previous solution If x i ≠ y j, then L[i,j] = max( L[i -1, j], L[i, j -1 ] )  E.g. use however good we did before Final result will be stored in L[n,m] Case 1:Case 2:

LCS Algorithm Algorithm LCS(String X, String Y): for i  1 to n-1 L[i,-1]  0 for j  0 to m-1 L[-1, j]  0 for i  0 to n-1 for j  0 to m-1 if x i = y j then L[i, j]  L[i-1, j-1] + 1 else L[i, j]  max(L[i-1, j], L[i, j-1]) return L

Visualizing the LCS Algorithm