Skip to content

Longest Bitonic Subsequence #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Problem :-
=======
To find the longest Bitonic Subsequence(a bitonic subsequence is a sequence which strictly increases first then strictly decrases) in a given array of numbers.

Input :-
-----
The size of an array(n) and the contents of the array(arr).

Output :-
------
A single integer printing the maximum length of the bitonic subsequence present in the given array.

#### Language : `C++`

#### Algorithm Paradigm : `Dynamic Programming`

#### Time Complexity : `O(N^2)`

#### Space Complexity : `O(N)`

Working :-
-------
This problem is an application of the longest increasing subsequence problem also solved using dynamic programming.

For any `arr[i]` we are finding the longest increasing subsequence from left to right keeping the `arr[i]` the last element(lets call it `a`).Then we are finding the longest increasing subsequence from right to left keeping `arr[i]` the last element(lets call it `b`).Then we are finding `a+b-1` (subtracting `1` because `arr[i]` has been counted twice) which gives us the length of the bitonic subsequence with `arr[i]` as its peak element.
We are repeating above process for all the elements in the array and finding the max value of all `a+b-1` which gives us the length of the longest bitonic subsequence.

By using dynamic programming we are bringing the time complexity down from exponential(as in case of brute force solution) to polynomial.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//C++ program to find the longest bitonic sequence.
//A bitonic sequence is a sequence which increases first then decreases.
#include<bits/stdc++.h>

using namespace std;

int main()
{
int n;//n would store the size of the array.
cout<<"Enter the length of the array : ";
cin>>n;//Inputting the size of the array.
cout<<endl;

int arr[n];//arr array would store the input data given by the user.
for(int i=0;i<n;i++)
{
cout<<"Enter the value of element "<<i+1<<" : ";
cin>>arr[i];//Inputting the values in the array.
}

/*temp1[i] would store the longest increasing subsequence from left to right if arr[i] was the last element of the
subsequene.*/
int temp1[n];
for(int i=0;i<n;i++)
temp1[i]=1;//Initializing all values of temp1 with 1.

for(int i=1;i<n;i++)//Implementing the algorithm to find temp1[i].
{
for(int j=0;j<i;j++)
{
if(arr[i]>arr[j])
temp1[i]=max(temp1[i],temp1[j]+1);
}
}

/*temp2[i] would store the longest increasing subsequence from right to left if arr[i] was the last element of the
subsequence*/
int temp2[n];
for(int i=0;i<n;i++)
temp2[i]=1;//Initializing all values of temp2 with 1.

for(int i=n-2;i>=0;i--)//Implementing the algorithm to find temp2[i].
{
for(int j=n-1;j>i;j--)
{
if(arr[i]>arr[j])
temp2[i]=max(temp2[i],temp2[j]+1);
}
}

int max=-1;//max would store the maximum length of the longest bitonic subsequence.
for(int i=0;i<n;i++)//Implementing algorithm to find max.
{
if(max<temp1[i]+temp2[i]-1)
max=temp1[i]+temp2[i]-1;
}

cout<<endl;

cout<<"The longest Bitonic Subsequence is of length : "<<max<<endl;//Printing the longest length of such sequence.

return 0;
}