230 likes | 413 Views
This presentation on Longest Increasing Subsequence in Dynamic Programming will acquaint you with a clear understanding of the Longest Increasing Subsequence problem statement and solution implementation. In this Data Structure Tutorial, you will understand what is a longest increasing subsequence and how you can solve the same problem with less time complexity using DP. Finally, we will cover the implementation of the longest increasing subsequence in Dynamic Programming. <br><br>
E N D
What’s In It For You? What Is Longest Common Subsequence? Longest Common Subsequence Using Recursion LCS Implementation Using Dynamic Programming
What Is Common Subsequence? • A subsequence is a series of elements that occur in the same order but not necessarily contiguous. • A conceivable sequence of elements that appear in both provided strings is referred to as a common subsequence for those provided strings.
What Is Common Subsequence? S1 = “abcde” S2 = “cde” Common Subsequences: “c” , “d”, “e”, “cd”, “de”, “cde”
Understanding Longest Common Subsequence Problem Problem Statement: Given two sequences find the length of longest subsequence present in both items. String 1: x y z a b c d e f String 2: z a d f f d a z Longest Common Subsequence:
Understanding Longest Common Subsequence Problem Problem Statement: Given two sequences find the length of longest subsequence present in both items. String 1: x y z a b c d e f String 2: z y d b f y z f d b Longest Common Subsequence:
Understanding Longest Common Subsequence Problem Problem Statement: Given two sequences find the length of longest subsequence present in both items. String 1: x y z a b c d e f String 2: z y d b f f y z f d d Longest Common Subsequence:
Think and Answer Given two sequences find the length of longest common subsequence present in both items. String 1: a b d a c e String 2: b a b c e Longest Common Subsequence: babc bace bce
Longest Common Subsequence Using Recursion Create a program to find out the length of longest common sequence for strings A = XY, B = XPYQ.
Longest Common Subsequence Using Recursion B A 0 1 2 3 0 1 Let’s Create a Recursion tree to understand how our program works?
Let’s Create a Recursion tree to understand how our program works? A A[1] B[3] Y Q 0 1 B 0 1 2 3 A[1] B[2] Y Y A[0] B[3] X Q //LCS Function int LCS( char*A, char *B, int x, int y ) { if (x ==0 || y == 0) return 0; if (A[x-1] == B[y-1]) return 1 + LCS(A, B, x-1, y-1); else return max(LCS(A, B, x, y-1), LCS(A, B, x-1, y)); } A[0] B[1] X P A[0] B[2] X Y A[N] B[3] /0 Q 1 + A[0] B[0] X X A[N] B[1] /0 P A[0] B[1] X P 1 + A[N] B[N] /0 /0 A[0] B[0] X X 1 +
A A[1] B[3] Y Q 0 1 B 0 1 2 3 A[1] B[2] Y Y A[0] B[3] X Q //LCS Function int LCS( char*A, char *B, int x, int y ) { if (x ==0 || y == 0) return 0; if (A[x-1] == B[y-1]) return 1 + LCS(A, B, x-1, y-1); else return max(LCS(A, B, x, y-1), LCS(A, B, x-1, y)); } A[0] B[1] X P A[0] B[2] X Y A[N] B[3] /0 Q 1 + SUM = 2 SUM = 1+1 = 2 A[0] B[0] X X A[N] B[1] /0 P A[0] B[1] X P SUM = 1 SUM = 0 1 + A[N] B[N] /0 /0 A[0] B[0] X X 1 + SUM = 0
A A[1] B[3] Y Q 0 1 B 0 1 2 3 A[1] B[2] Y Y A[0] B[3] X Q SUM = 2 //LCS Function int LCS( char*A, char *B, int x, int y ) { if (x ==0 || y == 0) return 0; if (A[x-1] == B[y-1]) return 1 + LCS(A, B, x-1, y-1); else return max(LCS(A, B, x, y-1), LCS(A, B, x-1, y)); } A[0] B[1] X P A[0] B[2] X Y A[N] B[3] /0 Q A[0] B[0] X X A[N] B[1] /0 P A[0] B[1] X P A[N] B[N] /0 /0 A[0] B[0] X X SUM = 1 SUM = 0 1 +
Time Complexity Using Recursion Time O(n) = Value of n
Longest Common Subsequence Using Tabulation B A 0 1 2 3 0 1 Let’s Understand How DP(Tabulation) can reduce space complexity for given strings.
Let’s Understand How DP(Tabulation) can reduce space complexity for given strings. A 0 1 0 1 2 3 B 0 1 2 3 Y LCS P Q /0 X //LCS Function for(i=0; i<=x; i++) { for(j=0; j<=y; j++) { if(i==0|| j == 0) L[i][j] = 0; else if(A[i-1] == B[j-1]) L[i][j] = L[i-1][j-1] + 1; else L[i][j] = max(L[i-1][j], L[i][j-1]); } } /0 0 0 0 0 0 0 X 0 1 0 Y
A 0 1 0 1 2 3 B 0 1 2 3 Y LCS P Q /0 X //LCS Function for(i=0; i<=x; i++) { for(j=0; j<=y; j++) { if(i==0|| j == 0) L[i][j] = 0; else if(A[i-1] == B[j-1]) L[i][j] = L[i-1][j-1] + 1; else L[i][j] = max(L[i-1][j], L[i][j-1]); } } /0 0 0 0 0 0 0 1 1 X 1 1 0 1 2 2 1 1 0 Y
Let’s create a program for finding length of Longest Common Subsequence using Tabulation (DP).