Longest Common subsequence.pptx

76 views 29 slides Nov 30, 2023
Slide 1
Slide 1 of 29
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29

About This Presentation

The Longest Common Subsequence (LCS) programming problem is a fundamental challenge in computer science, finding wide applications in fields such as bioinformatics, text comparison, and version control systems. The essence of LCS lies in identifying the longest possible subsequence common to two giv...


Slide Content

Longest common subsequence (LCS)

T able of contents 01 03 02 04 Understanding the problem Recursion Memoization Tabulation

Understanding The Problem 01

Definations A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters. e.g. A = bacad (Total possible substring: 2 m ) Substrings: ad, ac, bac, acad, bacad, bcd. A common subsequence of two strings is a subsequence that is common to both strings. A = bacad B = accbadcb common subsequence : ad, ac, bac, acad. The longest common subsequence (LCS) of A and B: acad .

Problem Statement Given two strings s1 and s2 , return the length of their longest common subsequence. If there is no common subsequence, return 0. Example: Input: s1 = "abcde", s2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3.

Let’s try another example INPUT: two strings OUTPUT: longest common subsequence ACTGAACTCTGTGCACT TGACTCAGCACAAAAAC

Let’s try another example INPUT: two strings OUTPUT: longest common subsequence AC TGA ACTCTGTGCACT TGA CTCAGCACAAAAAC

Let’s try another example INPUT: two strings OUTPUT: longest common subsequence AC TGA A CTC TGTGCACT TGACTC AGCACAAAAAC

Let’s try another example INPUT: two strings OUTPUT: longest common subsequence AC TGA A CTC T G TGCACT TGACTC A G CACAAAAAC

Let’s try another example INPUT: two strings OUTPUT: longest common subsequence AC TGA A CTC T G TG CAC T TGACTC A GCAC AAAAAC lcs = TGACTCGCAC output = 10

Brute Force: For every subsequence of s1 , check whether it’s a subsequence of s2 . s1 has 2 m subsequences. Each subsequence takes Θ(n) time to check: scan s2 for first letter, for second, and so on. Time Complexity : Θ(n2 m ) . m = length of s1 n = length of s2

Reccursion 2

Alogrithm: Condition 1: Check if both character arrays are empty. If they are empty, return 0. Condition 2: If the character arrays are not empty, then check if the last character of both character arrays matches or not. If you get a match, then return 1 + LCS (Both character arrays with reduced size). Condition 3: If you don’t get a character match, move either one character in the first character array A or the second character array B. Pick whichever produces maximum value.

Recursion Tree let’s consider two string A = “XY” , length = 2 B = “XPYQ” , length = 4 XY X P Y Q

Memoization (Top down) 3

1. 2. 3.

Tabulation (Bottom up) 4

Refrences Videos Abdul Bari Articles Leetcode Algotree Simplelearn TakeUForward

Thanks!