0% found this document useful (0 votes)
20 views14 pages

Lis LCS MSCS

The document discusses the Longest Increasing Subsequence (LIS) and Longest Common Subsequence (LCS) problems, explaining their definitions, objectives, and methods for solving them using dynamic programming and recursion. It provides examples and processes for calculating LIS and LCS, as well as a brief mention of the Maximum Sum of Consecutive Subsequence (MSCS) problem with examples. The document emphasizes the importance of breaking down complex problems into simpler sub-problems and storing solutions for efficiency.

Uploaded by

A1Virus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views14 pages

Lis LCS MSCS

The document discusses the Longest Increasing Subsequence (LIS) and Longest Common Subsequence (LCS) problems, explaining their definitions, objectives, and methods for solving them using dynamic programming and recursion. It provides examples and processes for calculating LIS and LCS, as well as a brief mention of the Maximum Sum of Consecutive Subsequence (MSCS) problem with examples. The document emphasizes the importance of breaking down complex problems into simpler sub-problems and storing solutions for efficiency.

Uploaded by

A1Virus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Longest Increasing

Subsequence
Longest Common
Subsequence

LIS & LCS


Longest Increasing
Subsequence {LIS}
• Dynamic Programming:
Dynamic programming is a very powerful algorithmic paradigm in which a
problem is solved by identifying a collection of sub-problems and tackling them one
by one, smallest first, using the answers to small problems to help figure out larger
ones, until the whole lot of them is solved.
Dynamic programming (also known as dynamic optimization) is a method
for solving a complex problem by breaking it down into a collection of simpler sub-
problems, solving each of those sub-problems just once, and storing their solutions –
ideally, using a memory-based data structure.
The next time the same sub-problem occurs, instead of re-computing its
solution, one simply looks up the previously computed solution, thereby saving
computation time at the expense of a (hopefully) modest expenditure in storage space.
Longest Increasing
Subsequence {LIS}
• Subsequence:
A subsequence is an infinite ordered subset of a sequence.
OR,
A subsequence of a sequence ( a n ) is an infinite collection of numbers from ( a
n ) in the same order that they appear in that sequence.
The main theorem on subsequences is that every subsequence of a
convergent sequence ( a n ) converges to the same limit as ( a n ) .
• Examples:
(a1, a2, a3 , a4 , a5, a6, a7 ... ) is a sequence then
(a2 , a4 , a6 ) is a subsequence of (a1 , a2 , a3 , a4 , ... )
Longest Increasing
Subsequence
• Objec­tive:
The Longest Increasing Subsequence (LIS) problem is to find the length of the
longest subsequence of a given sequence such that all elements of the subsequence are
sorted in increasing order.
OR
In computer science, the longest increasing subsequence problem is to find
a subsequence of a given sequence in which the subsequence's elements are in sorted
order, lowest to highest, and in which the subsequence is as long as possible. This
subsequence is not necessarily contiguous, or unique.
• Exam­ple:
int[] A = { 1, 12, 7, 0, 23, 11, 52, 31, 61, 69, 70, 2 };
length of LIS is 7 and LIS is {1, 12, 23, 52, 61, 69, 70}.
Process of LIS

5 7 1 0 8 13 15 -3 17 14
1 1 1 1 1 1 1 1 1 1
2 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1
3 1 1 1 1 1
4 1 1 1 1
5 1 1 1
1 1 1
6 1
1
Final Result

5 7 1 0 8 13 15 -3 17 14
1 1 1 1 1 1 1 1 1 1
2 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1
3 1 1 1 1 1
4 1 1 1 1
5 1 1 1
1 1 1

So, The LIS of this Array is 6 66 1


1
And Sub-sequence is: {5,7,8,13,15,17}
Any
confusion
NO? ??
?
Than Move ON
Longest Common Subsequence
{LCS}
• A longest sub­se­quence is a sequence that appears in the same rel­a­
tive order, but not nec­es­sar­ily contiguous(not sub­string) in both the
string.

• Exam­ple:
String A = "acbaed";
String B = "abcadf";

Longest Common Subsequence - example


Longest Common Subsequence(LCS): a, c, a, d; Length: 4
Approach:
Recur­sion:
Start com­par­ing strings in reverse order one char­ac­ter at a time.
Now we have 2 cases -
1. Both char­ac­ters are same
add 1 to the result and remove the last char­ac­ter from both the strings
and make recur­sive call to the mod­i­fied strings.
2. Both char­ac­ters are different
Remove the last char­ac­ter of String 1 and make a recur­sive call and
remove the last char­ac­ter from String 2 and make a recur­sive and then return
the max from returns of both recur­sive calls.
Process
1. If data matched than Add 1 with Cardinal value
2. If not matched than
check Which data is larger [Left side or Upper side]
data comes from larger side
Else
If both side data are equal then check
C[i-1, j] >= C[i , j-1]
then,
data comes from Upper side
Else
data comes from Left Side
End ;
Process……

A B C D A
0 0 0 0 0 0
A 0 1 1 1 1 1
C 0 1 1 2 2 2
B 0 1 2 2 2 2
D 0 1 2 2 3 3
String A = “ABCDA"; E 0 1 2 2 3 3
String B = “ACBDEA"; A 0 1 2 2 3 4

Longest Common Subsequence - example


Longest Common Subsequence(LCS): A, C, D, A; Length: 4
Maximum Sum of Consecutive
Subsequence
MSCS (A,n,maxsum)
maxsum=suffixmax=0
For i=1 to n do
if suffixmax+A(i) > maxsum then
maxsum=suffixmax+A(i)
suffixmax=maxsum
elseif suffixmax+A(i) > 0 then
suffixmax=suffixmax+A(i)
else
suffixmax=0
endif
enddo
Simulation of the algorithm
n 1 2 3 4 5 6 7 8 9

A 2 -1 3 -6 5 2 -1 7 -8
Maxsum 0 2 2 4 4 5 7 7 13 13
Suffixmax 0 2 1 4 0 5 7 6 13 5

• Here The maximum consecutive subsequence is 5, 2, -1, 7 with sum 13

Example2:
• Consider the sequence 2, -3, 1.5, -1, 3, -2, -3, 3.
• The maximum consecutive subsequence is 1.5,-1, 3 with sum 3.5
Maximum Sum of Consecutive
Subsequence
Example3:
 Consider the sequence 2,-3,1,-5,1,2,-8,3,1,5,-1,6,-3, -2,4,9,-7,1,-2,3,6.
• The maximum consecutive subsequence is 3,1,5,-1,6,-3, -2,4,9,-7,1,-
2,3,6 with Maxsum = 23.
• Here starting point = 3 and ending point =6.

You might also like