0% found this document useful (0 votes)
6 views12 pages

Lecture-13 - DP-Longest Increasing Subsequance - LIS

The document discusses the Longest Increasing Subsequence (LIS) problem, which involves finding the length of the longest subsequence in a given sequence where all elements are sorted in increasing order. It provides examples and a naive approach to calculate the LIS using dynamic programming. Additionally, it includes pseudocode for implementing the LIS algorithm.

Uploaded by

Moh Edi Wibowo
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)
6 views12 pages

Lecture-13 - DP-Longest Increasing Subsequance - LIS

The document discusses the Longest Increasing Subsequence (LIS) problem, which involves finding the length of the longest subsequence in a given sequence where all elements are sorted in increasing order. It provides examples and a naive approach to calculate the LIS using dynamic programming. Additionally, it includes pseudocode for implementing the LIS algorithm.

Uploaded by

Moh Edi Wibowo
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/ 12

CSE214: Algorithms

Dynamic Programming: LIS

Afsara Tasneem Misha


Lecturer, CSE
Department of CSE, DIU
1
Dynamic Programming

Part - 3 : Longest increasing subsequence(LIS)


Longest increasing subsequence(LIS)
• 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.

• For example,
{10, 22, 9, 33, 21, 50, 41, 60, 80}

LIS is {10, 22, 33, 50, 60, 80}


Length of LIS = 6
Increasing subsequences
You have given the following array

9 2 5 3 7 11 8 10 13 6

Now Find The increasing subsequences.


Increasing subsequences
9 2 5 3 7 11 8 10 13 6
2 3 7
5 7 10 13 are increasing subsequences.

2 5 7 8 10 13
2 3 7 8 10 13

We want to find the longest one

So LIS = 2 3 7 8 10 13 OR 2 5 7 8 10 13
And Length of LIS = 6
9 7 11
5 3 11 13
are not increasing subsequences.
5
More Example

• Input : arr[] = {3, 10, 2, 1, 20}


• Output : Length of LIS = 3
• The longest increasing subsequence is 3, 10, 20

• Input : arr[] = {3, 2}


• Output : Length of LIS = 1
• The longest increasing subsequences are {3} and {2}

• Input : arr[] = {50, 3, 10, 7, 40, 80}


• Output : Length of LIS = 4
• The longest increasing subsequence is {3, 7, 40, 80}
6
Longest Increasing subsequences
You have given the following array

9 2 5 3 7 11 8 10 13 6

Now Find The longest increasing subsequences.


A naive approach for LIS

LIS[i] = 1 + max j = 0..i-1{L[j] | aj < ai}


i = 1; j = 0 to i-1

Index 0 1 2 3 4 5 6 7 8 9
Input 9 2 5 3 7 11 8 10 13 6

Length

Prev
A naive approach for LIS

Index 0 1 2 3 4 5 6 7 8 9
Input 9 2 5 3 7 11 8 10 13 6

Length 1 1 2 2 3 4 4 5 6 3

Prev -1 -1 1 1 2 4 4 6 7 2

Length of LIS = 6
The longest increasing subsequence 2, 5, 7, 8, 10, 13
Exercise

• Find the Length of LIS


• Find out the the longest increasing subsequence

Index 0 1 2 3 4 5
Input 19 20 50 3 17 110
Psudocode or LIS
LIS( int arr[], int n )
• for (i = 0; i < n; i++ )
• length [i] = 1;

/* Compute optimized LIS values in bottom up manner */


• for (i = 1; i < n; i++ )
• for (j = 0; j < i; j++ )
• if ( arr [ i ] > arr [ j ] && length [ i ] < (length [ j ] + 1) )
• length [i] = length [j] + 1;

/* Pick maximum of all LIS values */


• for (i = 0; i < n; i++ )
• if (max < length [i])
• max = length [i];
• return max;
Thank you!

You might also like