Dynamic Programming. Prof. S.J. Soni

Similar documents
Dynamic Programming. p. 1/43

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Dynamic Programming ACM Seminar in Algorithmics

Introduction. I Dynamic programming is a technique for solving optimization problems. I Key element: Decompose a problem into subproblems, solve them

CS473 - Algorithms I

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

Partha Sarathi Mandal

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

Maximum sum contiguous subsequence Longest common subsequence Matrix chain multiplication All pair shortest path Kna. Dynamic Programming

CS 470/570 Dynamic Programming. Format of Dynamic Programming algorithms:

Chapter 8 Dynamic Programming

Matrices: 2.1 Operations with Matrices

DAA Unit- II Greedy and Dynamic Programming. By Mrs. B.A. Khivsara Asst. Professor Department of Computer Engineering SNJB s KBJ COE, Chandwad

General Methods for Algorithm Design

Chapter 8 Dynamic Programming

Dynamic Programming. Credits: Many of these slides were originally authored by Jeff Edmonds, York University. Thanks Jeff!

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

Week 7 Solution. The two implementations are 1. Approach 1. int fib(int n) { if (n <= 1) return n; return fib(n 1) + fib(n 2); } 2.

Algorithm Design and Analysis

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

CS483 Design and Analysis of Algorithms

CSE 202 Dynamic Programming II

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

CSC 1700 Analysis of Algorithms: Warshall s and Floyd s algorithms

Lecture 4: An FPTAS for Knapsack, and K-Center

Activity selection. Goal: Select the largest possible set of nonoverlapping (mutually compatible) activities.

Determining an Optimal Parenthesization of a Matrix Chain Product using Dynamic Programming

Algorithms Design & Analysis. Dynamic Programming

CS60007 Algorithm Design and Analysis 2018 Assignment 1

Dynamic Programming. Cormen et. al. IV 15

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

Dynamic Programming (CLRS )

Lecture 9. Greedy Algorithm

Graduate Algorithms CS F-09 Dynamic Programming

A Review of Matrix Analysis

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

INF4130: Dynamic Programming September 2, 2014 DRAFT version

Lecture 13: Dynamic Programming Part 2 10:00 AM, Feb 23, 2018

Algorithms and Theory of Computation. Lecture 9: Dynamic Programming

Dynamic programming. Curs 2015

Lecture 2: Pairwise Alignment. CG Ron Shamir

After linear functions, the next simplest functions are polynomials. Examples are

CS325: Analysis of Algorithms, Fall Final Exam

Courses 12-13: Dynamic programming

Dynamic Programming: Matrix chain multiplication (CLRS 15.2)

Greedy Algorithms My T. UF

More Dynamic Programming

Stage-structured Populations

More Dynamic Programming

Note that M i,j depends on two entries in row (i 1). If we proceed in a row major order, these two entries will be available when we are ready to comp

ME751 Advanced Computational Multibody Dynamics

Dynamic Programming. Shuang Zhao. Microsoft Research Asia September 5, Dynamic Programming. Shuang Zhao. Outline. Introduction.

CSE 421 Dynamic Programming

Analysis of Algorithms I: All-Pairs Shortest Paths

Design and Analysis of Algorithms

Chapter 6. Properties of Regular Languages

Vector, Matrix, and Tensor Derivatives

1 Assembly Line Scheduling in Manufacturing Sector

Phys 201. Matrices and Determinants

BSc (Hons) in Computer Games Development. vi Calculate the components a, b and c of a non-zero vector that is orthogonal to

Complexity Theory of Polynomial-Time Problems

Lecture #8: We now take up the concept of dynamic programming again using examples.

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

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 20: Dynamic Programming

Outline / Reading. Greedy Method as a fundamental algorithm design technique

CS 580: Algorithm Design and Analysis

Approximation: Theory and Algorithms

A primer on matrices

Data Structures and Algorithms

CS173 Running Time and Big-O. Tandy Warnow

All-Pairs Shortest Paths

Lecture 2: Divide and conquer and Dynamic programming

Assignment 4. CSci 3110: Introduction to Algorithms. Sample Solutions

CSE 421 Weighted Interval Scheduling, Knapsack, RNA Secondary Structure

What is Dynamic Programming

Question Paper Code :

Notes on vectors and matrices

Similarity Search. The String Edit Distance. Nikolaus Augsten. Free University of Bozen-Bolzano Faculty of Computer Science DIS. Unit 2 March 8, 2012

Matrices. Chapter Definitions and Notations

Copyright 2000, Kevin Wayne 1

CSE 202 Homework 4 Matthias Springer, A

Mathematics for Graphics and Vision

Linear Algebra. Matrices Operations. Consider, for example, a system of equations such as x + 2y z + 4w = 0, 3x 4y + 2z 6w = 0, x 3y 2z + w = 0.

Advanced Engineering Mathematics Prof. Pratima Panigrahi Department of Mathematics Indian Institute of Technology, Kharagpur

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

Data Structures in Java

Similarity Search. The String Edit Distance. Nikolaus Augsten.

Find: a multiset M { 1,..., n } so that. i M w i W and. i M v i is maximized. Find: a set S { 1,..., n } so that. i S w i W and. i S v i is maximized.

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

Regular Expressions and Language Properties

CS583 Lecture 11. Many slides here are based on D. Luebke slides. Review: Dynamic Programming

1 Matrices and matrix algebra

Chapter 6. Dynamic Programming. CS 350: Winter 2018

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

CS 470/570 Divide-and-Conquer. Format of Divide-and-Conquer algorithms: Master Recurrence Theorem (simpler version)

2.6 Complexity Theory for Map-Reduce. Star Joins 2.6. COMPLEXITY THEORY FOR MAP-REDUCE 51

An Introduction to NeRDS (Nearly Rank Deficient Systems)

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

MCR3U Unit 7 Lesson Notes

Transcription:

Dynamic Programming Prof. S.J. Soni

Idea is Very Simple.. Introduction void calculating the same thing twice, usually by keeping a table of known results that fills up as subinstances are solved. Dynamic Programming is a bottom-up technique. We usually start with the smallest, and hence the simplest, subinstances. y combining their solutions, we obtain the answers to subinstances of increasing size, until finally we arrive at the solution of the original instance.

alculating the binomial coefficient onsider the problem of calculating the binomial coefficient 3

alculating the binomial coefficient Function (n,k) if k= or k=n then return 1 else return (n-1,k-1) + (n-1,k) alculate for (5,3) (4,), (4,3) (3,1), (3,) (3,), (3,3) (,), (,1), (,1), (,) (,1), (,), (,), (,3) This algorithm calculates same values again & again, so that it is inefficient algorithm. 4

Solution using Dynamic Programming Pascal s Triangle : 1 1: 1 1 : 1 1 3: 1 3 3 1 4: 1 4 6 4 1 5: 1 5 1 1 5 1 6: 1 6 15 15 6 1 7: 1 7 1 35 35 1 7 1 8: 1 8 8 56 7 56 8 8 1 5

Solution using Dynamic Programming Pascal s Triangle 1 3 4 5. k-1 k : 1 1: 1 1 : 1 1 3: 1 3 3 1 4: 1 4 6 4 1 5: 1 5 1 1 5 1 n-1: (n-1,k-1) (n-1,k) n: (n,k) 6

Making hange () Problem There are several instances of problem in which greedy algorithm fails to generate answer. For example, There are coins for 1,4 and 6 units. If we have to make change for 8 units, the greedy algorithm will propose doing so using one 6-unit coin and two 1-unit coins, for a total of three coins. However it is clearly possible to do better than this: we can give the customer his change using just two 4-unit coins. lthough the greedy algorithm does not find this solution, it is easily obtained using dynamic programming. 7

Solution using Dynamic Programming To solve this problem using DP, we set up a table c [1 n, N], with one row for each available denomination and one column for each amount from to N units. In this table c [i,j] will be the minimum number of coins required to pay an amount of j units, <= j <=N. c [i,j] = min (c[i-1,j], 1+c[i, j - di]) 8

Solution using Dynamic Programming mount 1 3 4 5 6 7 8 d1=1 1 1 3 4 5 6 7 8 d=4 1 3 1 3 4 d3=6 3 1 3 1 1 c [i,j] = min (c[i-1,j], 1+c[i, j - di]) c [,1] = min (c[1,1], 1+c[, 1-4]) = min(1,inf) =1 c [,] = min (c[1,], 1+c[, - 4]) = min(,inf) = c [,4] = min (c[1,4], 1+c[, 4-4]) = min(4,1) =1 c [3,8] = min (c[,8], 1+c[3, 8-6]) = min(,3) = 9

Solution using Dynamic Programming Which oins? mount 1 3 4 5 6 7 8 d1=1 1 1 3 4 5 6 7 8 d=4 1 3 1 3 4 d3=6 3 1 3 1 1 If c[i,j]=c[i-1,j], we move up to c[i-1, j] c[3,8] = c[,8], we move on to c[,8] Otherwise check c[i,j]=1+c[i,j-di] so check c[,8]=1+c[,8-4] = 1+c[,4]= [select coin] gain check c[,4]<>c[1,4] so, c[,4]=1+c[,4-4]=1 [select coin] 1

Knapsack Problem () We are given number of objects and knapsack. This time, however, we suppose that the objects may not be broken into smaller pieces, we may decide either to take an object or to leave it behind, but we may not take a fraction of an object. So it s called Non-fractional knapsack problem or /1 knapsack problem. 11

Knapsack Problem () Example for which greedy method cannot work. Object 1 3 Weight 6 5 5 Value 8 5 5 W=1 Greedy method generates the value 8 which is not optimal. 1

Solution using Dynamic Programming To solve this problem using DP, we set up a table V [1 n, W], with one row for each available object and one column for each weight from to W. In this table V [i,j] will be the maximum value of the objects, <= j <=W & 1<= i <=n V [i,j] = max (V[i-1,j], V[i-1, j - wi]+vi) 13

Knapsack Problem () Example Objects 1 3 4 5 Weight 1 5 6 7 Value 1 6 18 8 Vi/Wi 1 3 3.6 3.67 4 W=11 14

Solution using Dynamic Programming Weight Limit 1 3 4 5 6 7 8 9 1 11 w1=1, v1=1 1 1 1 1 1 1 1 1 1 1 1 w= v=6 1 6 7 7 7 7 7 7 7 7 7 w3=5, v3=18 1 6 7 7 18 19 4 5 5 5 5 w4=6, v4= 1 6 7 7 18 4 8 9 9 4 w5=7, v5=8 1 6 7 7 18 8 9 34 35 4 V [i,j] = max (V[i-1,j], V[i-1, j - wi]+vi) V [5,11] = max (V[4,11], V[4,11-7] +8 )= max(4,35)=4 15

Solution using Dynamic Programming Weight Limit 1 3 4 5 6 7 8 9 1 11 w1=1, v1=1 1 1 1 1 1 1 1 1 1 1 1 w= v=6 1 6 7 7 7 7 7 7 7 7 7 w3=5, v3=18 1 6 7 7 18 19 4 5 5 5 5 w4=6, v4= 1 6 7 7 18 4 8 9 9 4 w5=7, v5=8 1 6 7 7 18 8 9 34 35 4 Which Objects? If V[i,j]=V[i-1,j], we move up to V[i-1, j] V[5,11] = V[4,11], we move on to V[4,11] Otherwise check V[i,j]=V[i-1,j-wi]+vi so check V[4,11]=V[3,11-6]+ = V[3,5]+=4 [select object 4] now check V[3,5]=V[,5-5]+18=V[,]+18=18 [select object 3] 16

Practice Examples Solve following knapsack problem using dynamic programming algorithm with given capacity W=5, Weight and Value are as follows : (,1),(1,1),(3,),(,15). Solve the following Knapsack Problem using Dynamic Programming Method. Write the equation for solving above problem. n = 5, W = 1 Object 1 3 4 5 Weight (w) 1 3 4 5 Value (v) 3 66 4 6 17

Shortest Paths Let G = <N,> be a directed graph; N is a set of nodes and is a set of edges. We want to calculate the length of the shortest path between each pair of nodes. Suppose the nodes of G are numbered from 1 to n, so N=[1,, n] and suppose matrix L gives the length of each edge, with L[i,j]= for i=1,,..n. L[i,j] >= for all i and j, and L[i,j]= if the edge (i,j) does not exist. 18

Shortest Paths The principle of optimality: If k is a node on the shortest path from i to j, then the part of the path from i to k, and the part from k to j, must also be optimal. We construct matrix D that gives the length of the shortest path between each pair of nodes. D k [i,j] = min(d k-1 [i,j], D k-1 [i,k]+d k-1 [k,j]) 19

Floyd s lgorithm 1 4 5 5 3 15 3 15 5 5 15 D k [i,j] = min(d k-1 [i,j], D k-1 [i,k]+d k-1 [k,j]) D 1 = D = L = 5 5 15 5 3 15 15 5 5 5 15 5 3 35 15 15 5 D 1 [3,] = min(d [3,], D [3,1]+D [1,]) = min(inf, 3+5) = 35

Floyd s lgorithm D = L = 5 5 15 5 3 15 15 5 D 1 = 5 5 15 5 3 35 15 15 5 D k [i,j] = min(d k-1 [i,j], D k-1 [i,k]+d k-1 [k,j]) D 1 [1,1] = min(d [1,1], D [1,1]+D [1,1]) = min(, + ) = D 1 [1,] = min(d [1,], D [1,1]+D [1,]) = min(5, + 5) = 5 D 1 [,1] = min(d [,1], D [,1]+D [1,1]) = min(5, 5+ ) = 5 D 1 [3,] = min(d [3,], D [3,1]+D [1,]) = min(inf, 3+5) = 35 1

Floyd s lgorithm D = L = 5 5 15 5 3 15 15 5 D 1 = 5 5 15 5 3 35 15 15 5 D = 5 1 5 15 5 3 35 15 15 5 D 3 = 5 1 45 15 5 3 35 15 15 5 D 4 = 5 15 1 15 5 3 35 15 15 5 D k [i,j] = min(d k-1 [i,j], D k-1 [i,k]+d k-1 [k,j])

Practice Example Write the equation for finding out shortest path using Floyd s algorithm. Use Floyd s method to find shortest path for below mentions all pairs. 3 7 1 6 3

hained Matrix Multiplication Recall that the product of a p x q matrix and a q x r matrix is the p x r matrix given by q ij = a ik b kj k=1 1<=i<=p, 1<=j<=r It is clear that a total of pqr scalar multiplications are required to calculate the matrix product using this algorithm. e.g. (3X5) (5X4) => (3X4) [3X5X4=6 multiplications] 4

hained Matrix Multiplication Suppose now we want to calculate the product of more than two matrices. Matrix multiplication is associative, so we can compute the matrix product M = M 1 M. M n in a number of ways, which all gives the same answer. M = (((M 1 M ) M 3 ). M n ) = ((M 1 M ) (M 3. M n )) = ((M 1 (M M 3 )). M n )) and so on. However matrix multiplication is not commutative, so we are not allowed to change the order of the matrices in these arrangements. 5

MM - Example Suppose for example, we want to calculate the product D of four matrices, where is 13X5, is 5X89, is 89X3 and D is 3X34 Instances (())D => =5785, ()=3471, (())D=136 => 5785+3471+136 = 158 scalar multpls ()(D) = 541 (()D) = 455 (())D = 856 ((D)) = 6418 The most efficient method is almost 19 times faster than the slowest. 6

MM Example Number of combinations N 1 3 4 5 1. 15 T(n) 1 1 5 14 483 67444 The values of T(n) are called the atalan numbers. 7

hained Matrix Multiplication Suppose the dimensions of the matrices are given by a vector d[..n] such that the matrix M i, 1<= i<=n, is of dimension d i-1 X d i We fill the table m ij using the following rules for s=,1,, n-1. s= : m ii = i=1,,.,n s=1 : m i,i+1 = d i-1 d i d i+1 i=1,,.,n-1 1<s<n: m i,i+s = min (m ik +m k+1,i+s +d i-1 d k d i+s ) i<= k < i+s i=1,,,n-s 8

hained Matrix Multiplication Suppose for example, we want to calculate the product D of four matrices, where is 13X5, is 5X89, is 89X3 and D is 3X34 do d1 d d3 d4 The vector d is therefore (13, 5, 89, 3, 34). s=1 : m i,i+1 = d i-1 d i d i+1 i=1,,.,n-1 For s=1, we find m1 = d d1 d = 13X5X89 = 5785 m3 = d1 d d3 = 5X89X3 = 1335 m34 = d d3 d4 = 89X3X34 = 978 9

hained Matrix Multiplication 1<s<n: m i,i+s = min (m ik +m k+1,i+s +d i-1 d k d i+s ) i<= k < i+s For s=, we obtain, [where 1<s<n=1<<4 and i = 1,..,4-, so i=1,] [ i<=k<i+s = 1<=k<3, so k=1 & k=] m13 = min (m11+m3+13x5x3, // for k=1 m1+m33+13x89x3) // for k= = min (153,956) = 153 [ i<=k<i+s = <=k<4, so k= & k=3] m4 = min (m+m34+5x89x34, // for k= m3+m44+5x3x34) // for k=3 = min (48,1845) = 1845 i=1,,,n-s 3

hained Matrix Multiplication 1<s<n: m i,i+s = min (m ik +m k+1,i+s +d i-1 d k d i+s ) i<= k < i+s Finally for s=3, we obtain, [where 1<s<n=1<3<4 and i = 1,..,4-3, so i=1 only] [ i<=k<i+s = 1<=k<4, so k=1, k=, k=3] m14 = min (m11+m4+13x5x34, // for k=1 m1+m34+13x89x34, // for k= m13+m14+13x3x34) // for k=3 = min (455,541,856) = 856 i=1,,,n-s 31

hained Matrix Multiplication j=1 3 4 i=1 5785 153 856 1335 1845 3 978 4 s=3 s= s=1 s= Solution: (( ( )) D) is 13X5, is 5X89, is 89X3 and D is 3X34 [()=1335 + ()=195 + (())D=136] = 856 3

GTU Practice Examples Given the four matrix find out optimal sequence for multiplication D=<5,4,6,,7> Using algorithm find an optimal parenthesization of a matrix chain product whose sequence of dimension is 5,1,3,1,5,5,6 (use dynamic programming). 33

Longest ommon Subsequence Problem using Dynamic Programming 34

Dynamic programming It is used, when the solution can be recursively described in terms of solutions to subproblems (optimal substructure) lgorithm finds solutions to subproblems and stores them in memory for later use More efficient than brute-force methods, which solve the same subproblems over and over again 35

Longest ommon Subsequence (LS) pplication: comparison of two DN strings Ex: X= { D }, Y= { D } Longest ommon Subsequence: X = D Y = D rute force algorithm would compare each subsequence of X with the symbols in Y 36

LS lgorithm if X = m, Y = n, then there are m subsequences of x; we must compare each with Y (n comparisons) So the running time of the brute-force algorithm is O(n m ) Notice that the LS problem has optimal substructure: solutions of subproblems are parts of the final solution. Subproblems: find LS of pairs of prefixes of X and Y 37

LS lgorithm First we ll find the length of LS. Later we ll modify the algorithm to find LS itself. Define X i, Y j to be the prefixes of X and Y of length i and j respectively Define c[i,j] to be the length of LS of X i and Y j Then the length of LS of X and Y will be c[m,n] c[ i, j] c[ i 1, j 1] 1 max( c[ i, j 1], c[ i 1, j]) if x[ i] y[ otherwise j], 38

LS recursive solution c[ i, j] c[ i 1, j 1] 1 max( c[ i, j 1], c[ i 1, j]) if x[ i] y[ otherwise j], We start with i = j = (empty substrings of x and y) Since X and Y are empty strings, their LS is always empty (i.e. c[,] = ) LS of empty string and any other string is empty, so for every i and j: c[, j] = c[i,] = 39

LS recursive solution c[ i, j] c[ i 1, j 1] 1 max( c[ i, j 1], c[ i 1, j]) if x[ i] y[ otherwise j], 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 LS X i and Y j equals to the length of LS of smaller strings X i-1 and Y i-1, plus 1 4

LS recursive solution c[ i, j] c[ i 1, j 1] 1 max( c[ i, j 1], c[ i 1, j]) if x[ i] y[ otherwise j], Second case: x[i]!= y[j] s symbols don t match, our solution is not improved, and the length of LS(X i, Y j ) is the same as before (i.e. maximum of LS(X i, Y j-1 ) and LS(X i-1,y j ) 41

LS Example We ll see how LS algorithm works on the following example: X = Y = D What is the Longest ommon Subsequence of X and Y? LS(X, Y) = X = Y = D 4

LS Example () j 1 3 4 5 D i Yj D Xi 1 3 4 X = ; m = X = 4 Y = D; n = Y = 5 llocate array c[4,5] 43

LS Example (1) j 1 3 4 5 D i Yj D Xi 1 3 4 for i = 1 to m c[i,] = for j = 1 to n c[,j] = 44

LS Example () j 1 3 4 5 D i Yj D Xi 1 3 4 if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 45

LS Example (3) j 1 3 4 5 D i Yj D Xi 1 3 4 if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 46

LS Example (4) j 1 3 4 5 D i Yj D Xi 1 1 3 4 if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 47

LS Example (5) j 1 3 4 5 D i Yj D Xi 1 1 1 3 4 if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 48

LS Example (6) j 1 3 4 5 D i Yj D Xi 1 1 1 1 3 4 if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 49

LS Example (7) j 1 3 4 5 D i Yj D Xi 1 1 1 1 1 1 1 3 4 if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 5

LS Example (8) j 1 3 4 5 D i Yj D Xi 1 1 1 1 1 1 1 3 4 if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 51

LS Example (1) j 1 3 4 5 D i Yj D Xi 1 1 1 1 1 1 1 3 1 1 4 if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 5

LS Example (11) j 1 3 4 5 D i Yj D Xi 1 1 1 1 1 1 1 3 1 1 4 if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 53

LS Example (1) j 1 3 4 5 D i Yj D Xi 1 1 1 1 1 1 1 3 1 1 4 if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 54

LS Example (13) j 1 3 4 5 D i Yj D Xi 1 1 1 1 1 1 1 3 1 1 4 1 if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 55

LS Example (14) j 1 3 4 5 D i Yj D Xi 1 1 1 1 1 1 1 3 1 1 4 1 1 if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 56

LS Example (15) j 1 3 4 5 D i Yj D Xi 1 1 1 1 1 1 1 3 1 1 4 1 1 3 if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 57

LS lgorithm Running Time LS algorithm calculates the values of each entry of the array c[m,n] So what is the running time? O(m*n) since each c[i,j] is calculated in constant time, and there are m*n elements in the array 58

How to find actual LS So far, we have just found the length of LS, but not LS itself. We want to modify this algorithm to make it output Longest ommon Subsequence of X and Y Each c[i,j] depends on c[i-1,j] and c[i,j-1] or c[i-1, j-1] For each c[i,j] we can say how it was acquired: 3 For example, here c[i,j] = c[i-1,j-1] +1 = +1=3 59

How to find actual LS - continued Remember that c[ i, j] c[ i 1, j 1] 1 max( c[ i, j 1], c[ i 1, j]) if x[ i] y[ otherwise j], So we can start from c[m,n] and go backwards Whenever c[i,j] = c[i-1, j-1]+1, remember x[i] (because x[i] is a part of LS) When i= or j= (i.e. we reached the beginning), output remembered letters in reverse order 6

Finding LS j 1 3 4 5 i Yj D Xi 1 1 1 1 1 1 1 3 1 1 4 1 1 3 61

Finding LS () j 1 3 4 5 i Yj D Xi 1 1 1 1 1 1 1 3 1 1 4 1 1 3 LS (reversed order): LS (straight order): (this string turned Prof. S.J. Soni out - SPE, to Visnagar be a palindrome) 6

GTU Practice Examples Given two sequences of characters, P=<MLNOM> Q=<MNOM> Obtain the longest common subsequence. Find Longest ommon Subsequence using Dynamic Programming Technique with illustration X={,,,,D,,} Y={,D,,,,} Using algorithm determine an Longest ommon Sequence of,,,d,,,,d,f and,,,f (use dynamic programming). Explain how to find out Longest ommon Subsequence of two strings using Dynamic Programming method.find any one Longest ommon Subsequence of given two strings using Dynamic Programming. S1=abbacdcba S=bcdbbcaac 63