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.

Slides:



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

Dynamic Programming.
Dynamic Programming Nithya Tarek. Dynamic Programming Dynamic programming solves problems by combining the solutions to sub problems. Paradigms: Divide.
Lecture 8: Dynamic Programming Shang-Hua Teng. Longest Common Subsequence Biologists need to measure how similar strands of DNA are to determine how closely.
Overview What is Dynamic Programming? A Sequence of 4 Steps
Algorithms Dynamic programming Longest Common Subsequence.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
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 14 Dynamic.
Dynamic Programming.
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.
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.
Sequence Alignment Variations Computing alignments using only O(m) space rather than O(mn) space. Computing alignments with bounded difference Exclusion.
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:
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.
Analysis of Algorithms
1 Dynamic Programming Jose Rolim University of Geneva.
Lecture 7 Topics Dynamic Programming
Longest Common Subsequence
First Ingredient of Dynamic Programming
October 21, Algorithms and Data Structures Lecture X Simonas Šaltenis Nykredit Center for Database Research Aalborg University
ADA: 7. Dynamic Prog.1 Objective o introduce DP, its two hallmarks, and two major programming techniques o look at two examples: the fibonacci.
Dynamic programming 叶德仕
Dynamic Programming Chapter 15 Highlights Charles Tappert Seidenberg School of CSIS, Pace University.
1 CPSC 320: Intermediate Algorithm Design and Analysis July 28, 2014.
6/4/ ITCS 6114 Dynamic programming Longest Common Subsequence.
Dynamic Programming (Ch. 15) Not a specific algorithm, but a technique (like divide- and-conquer). Developed back in the day when “programming” meant “tabular.
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2012 Lecture 16.
1 Chapter 6 Dynamic Programming. 2 Algorithmic Paradigms Greedy. Build up a solution incrementally, optimizing some local criterion. Divide-and-conquer.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 14.
Introduction to Algorithms Jiafen Liu Sept
CS 3343: Analysis of Algorithms Lecture 18: More Examples on Dynamic Programming.
Computer Sciences Department1.  Property 1: each node can have up to two successor nodes (children)  The predecessor node of a node is called its.
Chapter 7 Dynamic Programming 7.1 Introduction 7.2 The Longest Common Subsequence Problem 7.3 Matrix Chain Multiplication 7.4 The dynamic Programming Paradigm.
CS38 Introduction to Algorithms Lecture 10 May 1, 2014.
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.
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.
TU/e Algorithms (2IL15) – Lecture 4 1 DYNAMIC PROGRAMMING II
David Meredith Dynamic programming David Meredith
Advanced Algorithms Analysis and Design
Dynamic programming 叶德仕
Dynamic Programming CISC4080, Computer Algorithms CIS, Fordham Univ.
CS200: Algorithm Analysis
Dynamic Programming Several problems Principle of dynamic programming
Chapter 8 Dynamic Programming.
CSCE 411 Design and Analysis of Algorithms
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Dynamic Programming Dr. Yingwu Zhu Chapter 15.
CS 3343: Analysis of Algorithms
Longest Common Subsequence
Dynamic Programming Comp 122, Fall 2004.
Lecture 8. Paradigm #6 Dynamic Programming
Ch. 15: Dynamic Programming Ming-Te Chi
Dynamic Programming-- Longest Common Subsequence
Introduction to Algorithms: Dynamic Programming
Longest Common Subsequence
Dynamic Programming CISC4080, Computer Algorithms CIS, Fordham Univ.
Analysis of Algorithms CS 477/677
Longest Common Subsequence
Dynamic Programming.
Presentation transcript:

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 Programming Least Common Subsequence Saving space

Least Common Subsequence A.k.a. “sequence alignment” “edit distance” … 9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Longest Common Subsequence (LCS) Given two sequences x[1.. m] and y[1.. n], find a longest subsequence common to them both. x:x:ABCBDAB y:y:BDCABA “a” not “the” BCBA = LCS(x, y)

9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Longest Common Subsequence (LCS) Given two sequences x[1.. m] and y[1.. n], find a longest subsequence common to them both. x:x:ABCBDAB y:y:BDCABA “a” not “the” BCBA = LCS(x, y) BCAB

Motivation Approximate string matching [Levenshtein, 1966] –Search for “occurance”, get results for “occurrence” Computational biology [Needleman-Wunsch, 1970’s] –Simple measure of genome similarity cgtacgtacgtacgtacgtacgtatcgtacgt acgtacgtacgtacgtacgtacgtacgtacgt 9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

Motivation Approximate string matching [Levenshtein, 1966] –Search for “occurance”, get results for “occurrence” Computational biology [Needleman-Wunsch, 1970’s] –Simple measure of genome similarity acgtacgtacgtacgtcgtacgtatcgtacgt aacgtacgtacgtacgtcgtacgta cgtacgt n – length(LCS(x,y)) is called the “edit distance” 9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Brute-force LCS algorithm Check every subsequence of x[1.. m] to see if it is also a subsequence of y[1.. n]. Analysis Checking = O(n) time per subsequence. 2 m subsequences of x (each bit-vector of length m determines a distinct subsequence of x). Worst-case running time= O(n2 m ) = exponential time.

9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Dynamic programming algorithm Simplification: 1.Look at the length of a longest-common subsequence. 2.Extend the algorithm to find the LCS itself. Strategy: Consider prefixes of x and y. Define c[i, j] = | LCS(x[1.. i], y[1.. j]) |. Then, c[m, n] = | LCS(x, y) |. Notation: Denote the length of a sequence s by | s |.

9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Recursive formulation c[i, j] = c[i–1, j–1] + 1if x[i] = y[j], max { c[i–1, j], c[i, j–1] } otherwise. Base case: c[i,j]=0 if i=0 or j=0. Case x[i] = y[ j]: 12im 12 j n x:x: y:y: =   The second case is similar.

9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Recursive formulation c[i, j] = c[i–1, j–1] + 1if x[i] = y[j], max { c[i–1, j], c[i, j–1] } otherwise. Case x[i] ≠ y[ j]: best matching might use x[i] or y[j] (or neither) but not both. 12im 12 j n x:x: y:y:  

9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Dynamic-programming hallmark #1 Optimal substructure An optimal solution to a problem (instance) contains optimal solutions to subproblems. If z = LCS(x, y), then any prefix of z is an LCS of a prefix of x and a prefix of y.

9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Recursive algorithm for LCS LCS(x, y, i, j) // ignoring base cases if x[i] = y[ j] then c[i, j]  LCS(x, y, i–1, j–1) + 1 else c[i, j]  max { LCS(x, y, i–1, j), LCS(x, y, i, j–1) } return c[i, j] Worse case: x[i]  y[ j], in which case the algorithm evaluates two subproblems, each with only one parameter decremented.

9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Recursion tree Height = m + n  work potentially exponential. m = 7, n = 6: 7,6 6,6 7,5 6,5 5,5 6,4 6,5 5,5 6,4 5,6 4,6 5,5 7,4 6,4 7,3 m+nm+n

9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne same subproblem, but we’re solving subproblems already solved! Recursion tree Height = m + n  work potentially exponential. m = 7, n = 6: 7,6 6,6 7,5 6,5 5,5 6,4 6,5 5,5 6,4 5,6 4,6 5,5 7,4 6,4 7,3 m+nm+n

9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Dynamic-programming hallmark #2 Overlapping subproblems A recursive solution contains a “small” number of distinct subproblems repeated many times. The number of distinct LCS subproblems for two strings of lengths m and n is only m n.

9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Memoization algorithm Memoization: After computing a solution to a subproblem, store it in a table. Subsequent calls check the table to avoid redoing work. Time =  (m n) = constant work per table entry. Space =  (m n). LCS(x, y, i, j) if c[i, j] = NIL then if x[i] = y[j] then c[i, j]  LCS(x, y, i–1, j–1) + 1 else c[i, j]  max { LCS(x, y, i–1, j), LCS(x, y, i, j–1) } same as before

9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne D C A B A Dynamic-programming algorithm I DEA : Compute the table bottom-up. ABCBDB B A

9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne D C A B A Dynamic-programming algorithm I DEA : Compute the table bottom-up. ABCBDB B A Time =  (m n).

9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne D C A B A Dynamic-programming algorithm I DEA : Compute the table bottom-up. ABCBDB B A Time =  (m n). Reconstruct LCS by tracing backwards. 0 A 4 0 B B 1 C C 2 B B 3 A A D 1 A 2 D 3 B 4

9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne D C A B A Dynamic-programming algorithm I DEA : Compute the table bottom-up. ABCBDB B A Time =  (m n). Reconstruct LCS by tracing backwards. Multiple solutions are possible

9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne D C A B A Dynamic-programming algorithm I DEA : Compute the table bottom-up. ABCBDB B A Time =  (m n). Reconstruct LCS by tracing backwards. 0 A 4 0 B B 1 C C 2 B B 3 A A D 1 A 2 D 3 B 4 Space =  (m n). Section 6.7: O(min{m, n})

Saving space Why is space important? –Cost of storage –Cache misses Suppose we only want to compute the cost of the optimal solution –Fill the table left to right –Remember only the previous column –O(m) space in addition to input 9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne D C A B AABCBDB B A A 4 0 B B 1 C C 2 B B 3 A A D 1 A 2 D 3 B 4

Computing the optimum View the matrix as a directed acyclic graph: our goal is to find a longest path from (0,0) to (m,n) –Every path must pass through column n/2 Algorithm 1.Find longest path lengths from (0,0) to all points in column n/2, i.e. (0,n/2),(1,n/2),…,(m,n/2) 2.Find longest path lengths from all of column n/2 to (m,n) 3.Find a point q where optimal path crosses column n/2. Add (q,n/2) to output list 4.Recursively solve subproblems (0,0) -> (q,n/2) and (q,n/2) to (m,n) How much time and space? 9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne