Longest Bitonic Sequence - Algorithms and Problem SolvingAlgorithms and Problem Solving



Longest Bitonic Sequence - Algorithms and Problem SolvingAlgorithms and Problem Solving

Given a sequence as as array of positive integers. Find the length of longest bitonic subsequence.

A bitonic subsequence is a subsequence that is first increasing up to a peak value and then decreasing from the peak value. For example, A=[1, 11, 2, 10, 4, 5, 2, 1] the longest bitonic sequence is 1, 2, 4, 5, 2, 1 of length 6. For A=[0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15] longest bitonic sequence is 0, 8, 12, 14, 13, 11, 7 of length 7.

Note that a bitoic sequence starting from a value reaches a peak value in a strict increasing order of values. Then it start decreasing monotonically. So, we can easily perceive that a bitonic sequence consists of a increasing subsequence and a decreasing subsequence. So, a longest bitonic subsequence would be subsequence that consists of a longest increasing subsequence (LIS) ending at peak and a longest decreasing subsequence (LDS) starting at peak. For example longest bitonic subsequence of A=[0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15] can be represented as follows.

  LIS     peak    LDS     |       |       |     |       v       |     |      14       |     v   12    13    v       8         11     0             7  

So, the longest bitonic subsequence with peak at a position i would consists of longest increasing subsequence that ends at i and a longest decreasing subsequence starting at i. We need to construct two arrays LIS[] and LDS[] such that for each position i –

   	LIS[i] : length of the Longest Increasing subsequence ending at arr[i].   	LDS[i]:  length of the longest Decreasing subsequence starting from arr[i].                   LIS[i]+LDS[i]-1 : the length Longest Bitonic Subsequence with peak at i.  

We need to find the position i with maximum value of LIS[i]+LDS[i]-1. We can easily solve this problem by solving the Longest Increasing Subsequence (LIS) problem.

Longest Increasing Subsequence (LIS) – Dynamic Programming
First of all, lets focus how to compute longest increasing subsequence. Let's consider LIS(i) be the length of longest increasing subsequence of the sequence at position i i.e. A[0..i] where A is the input sequence.

For example, A=[0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15] –

LIS(0) = 1  [0]  ---> a singe number is an increasing subsequence itself  LIS(1) = 2  [0, 8]  LIS(2) = 2  [0,4]  LIS(3) = 3  [0, 8, 12] or [0, 4, 12]  LIS(4) = 2  [0, 2]  LIS(5) = 3  [0, 8, 10] or [0, 4, 10] or [0, 2, 10]  LIS(6) = 3  [0, 4, 6] or [0, 2, 6]  LIS(7) = 4  [0, 8, 12, 14] or [0, 4, 12, 14] or [0, 8, 10, 14] or [0, 4, 10, 14] or [0, 4, 6, 14] or [0, 2, 6, 14]  ....  ....  

Note that, LIS(7) is constructed by appending A[7] to the solutions of LIS(3), LIS(5), and LIS(6). If we look closely to other solutions then we will se that LIS(i) is constructed by appending A[i] to the maximum length solutions LIS(j) for all j < i and A[j] < A[i]. So, we can construct a recurrence relation LIS(i) = max{1+LIS(j)} for all j < i and A[j] < A[i] . The recursion tree for the above recurrence for i = 4 is as follows –

                           lis(4)                              /       |      \           lis(3)      lis(2)    lis(1)            /     \        /             lis(2)  lis(1)   lis(1)     /      lis(1)   

So, the LIS problem has an optimal substructure LIS(i) = max{1+LIS(j)} for all j < i and the subproblems are repeating. So, we can use a Dynamic Programming (DP) table to cache the result and reuse computations.

Longest Decreasing Subsequence (LDS) can be computed similar to LIS with the recurrence relation LDS(i) = max{1+LDS(j)} for all j < i and A[j] > A[i] . Using these two relations we can now easily compute Longest Bitonic Sequence with peak at i by LIS(i)+LDS(i)-1. We need to find a peak position i such that the value of LIS(i)+LDS(i)-1 is maximized. Following is a simple implementation of the idea which runs in O(n^2) time and O(n) auxiliary space.


Read full article from Longest Bitonic Sequence - Algorithms and Problem SolvingAlgorithms and Problem Solving


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts