A subsequence is bitonic if it monotonically increases and then monotonically de- creases, or if it can be circularly shifted to monotonically increase and then monotonically decrease.
Given a sequence, how can I determine the longest bitonic subsequence efficiently?
edit: edited title to subsequence
here included a complete compilable example of the algorithm:
explanation:
first of all a binary search uses a complexity of O(logn), it is a given an i shall not explain this in this thread.
This method uses a dynamic programming version of LIS which uses a complexity of O(n*logn) (also a given that shall not be explained here) a dynamic LIS algorithm returns the length of the longest sub-array. with slight modification we save into an array the size of n the longest sub-array length up to and including that index.
so in each index we know "the max length up to index" next we do the same for LDS. this will give us the value of "the max length from index"
afterwards we cross-combine the values, this gives us the value of "the max length up to index + the max length from index"
now since the element in index is calculated twice we remove one. thus resulting in the formula:
lbs[i] = lis[i]+lds[n-i]-1for n>=1;as for complexity the following commands:
each work
O(n*logn)complexityand the for loop:
works in
O(n)complexity so the total isO(n*logn)+O(n*logn)+O(n) = O(n*logn)