1 / 24

Longest Common Subsequence Problem Using Dynamic Programming | Data Structures |

This presentation on Longest Common Subsequence Problem Using Dynamic Programming will acquaint you with a clear understanding of the LCS problem statement and solution implementation. In this Data Structure Tutorial, you will understand why a recursive solution for an LCS problem is not compatible and how you can solve the same problem with less time complexity using DP. Finally, we will cover the dynamic programming implementation of the Longest Common Subsequence Problem. <br><br>1. Introduction<br>2. What Is Longest Common Subsequence<br>3. Longest Common Subsequence Using Recursion<br>4. LCS Implementat

Simplilearn
Télécharger la présentation

Longest Common Subsequence Problem Using Dynamic Programming | Data Structures |

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. What’s In It For You? What Is Longest Common Subsequence? Longest Common Subsequence Using Recursion LCS Implementation Using Dynamic Programming

  2. What Is Longest Common Subsequence

  3. Click here to watch the video

  4. 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.

  5. What Is Common Subsequence? S1 = “abcde” S2 = “cde” Common Subsequences: “c” , “d”, “e”, “cd”, “de”, “cde”

  6. 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:

  7. 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:

  8. 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:

  9. 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

  10. Longest Common Subsequence Using Recursion

  11. Longest Common Subsequence Using Recursion Create a program to find out the length of longest common sequence for strings A = XY, B = XPYQ.

  12. 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?

  13. 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 +

  14. 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

  15. 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 +

  16. Time Complexity Using Recursion Time O(n) = Value of n

  17. LCS Implementation Using Dynamic Programming

  18. 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.

  19. 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

  20. 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

  21. Let’s create a program for finding length of Longest Common Subsequence using Tabulation (DP).

More Related