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
• Objective:
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.
• Example:
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 subsequence is a sequence that appears in the same rela
tive order, but not necessarily contiguous(not substring) in both the
string.
• Example:
String A = "acbaed";
String B = "abcadf";
Longest Common Subsequence - example
Longest Common Subsequence(LCS): a, c, a, d; Length: 4
Approach:
Recursion:
Start comparing strings in reverse order one character at a time.
Now we have 2 cases -
1. Both characters are same
add 1 to the result and remove the last character from both the strings
and make recursive call to the modified strings.
2. Both characters are different
Remove the last character of String 1 and make a recursive call and
remove the last character from String 2 and make a recursive and then return
the max from returns of both recursive 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.