Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Longest Bitonic Subsequence
A sequence is said to be bitonic if it is first increasing and then decreasing. In this problem, an array of all positive integers is given. We have to find a subsequence which is increasing first and then decreasing.
To solve this problem, we will define two subsequences, they are the Longest Increasing Subsequence and the Longest Decreasing Subsequence. The LIS array will hold the length of increasing subsequence ending with array[i]. The LDS array will store the length of decreasing subsequence starting from array[i]. Using these two arrays, we can get the length of longest bitonic subsequence.
Input and Output
Input:
A sequence of numbers. {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}
Output:
The longest bitonic subsequence length. Here it is 7.
Algorithm
longBitonicSub(array, size)
Input: The array, the size of an array.
Output − Max length of longest bitonic subsequence.
Begin define incSubSeq of size same as the array size initially fill all entries to 1 for incSubSeq for i := 1 to size -1, do for j := 0 to i-1, do if array[i] > array[j] and incSubSeq[i] array[j] and decSubSeq[i] max, then max := incSubSeq[i] + decSubSeq[i] – 1 done return max End
Example
#includeusing namespace std; int longBitonicSub( int arr[], int size ) { int *increasingSubSeq = new int[size]; //create increasing sub sequence array for (int i = 0; i arr[j] && increasingSubSeq[i] = 0; i--) //compute values from left ot right for (int j = size-1; j > i; j--) if (arr[i] > arr[j] && decreasingSubSeq[i] max) max = increasingSubSeq[i] + decreasingSubSeq[i] - 1; return max; } int main() { int arr[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}; int n = 16; cout Output
Length of longest bitonic subsequence is 7
Advertisements
