Longest Palindrome SubsequenceIt is a sequence of characters in a string that can be spelled and read the same both ways, forward and backward. Words like civic, redivider, deified, radar, level, madam, rotor, refer, kayak, racecar, and reviver. But in palindromic subsequence, a sequence can but not necessarily appear in the same relative order, but the chance of being necessarily contiguous and palindromic in nature is negligible. Dynamic Programming Solution Example  We have been given a sequence as "BDBADBDCBDCADB." Then the longest palindrome will be eleven  "BDABDCDBADB", this is the longest palindromic subsequence here. 'BBABB,' 'DAD,' 'BBDBB' and many more are also palindromic subsequences of the given sequence, but they are not the longest. In simpler words, sequences generate subsequences, then we compare their length and find which palindromic subsequence is the longest. We follow these steps to achieve the most extended palindrome sequence using Dynamic Programming Solution  First, we reverse the sequence. Then we use the LCS algorithm (Longest Common Subsequence) to find the longest common subsequence among the original sequence and reversed sequence. Here original LCS and reverse LCS are a function that returns the longest common subsequence between the pair of strings, now the answer from LCS will be the longest palindromic subsequence. Let LP (a, b) = Length of longest palindromic subsequence in array Z from index a to b LP (a, b) = LP (a+1, b1) + 2: if Z [a] = X [b] = max [LP (a+1, b), LP (a, b1)]: if Z [a]! = Z [b] = 1 if a = b = 1 if a = b  1 and Z [a]! = Z [b] = 2 if a = b  1 and Z [a] = Z [b] Code  OUTPUT  The length of the longest palindrome subsequence is: 11 Now, if we were to combine all the above cases into a mathematical equation: We call the original sequence X = (x1x2 ... xm ) and reverse as Y = ( y1y2 ... ym). Here, the prefixes of X are X1, 2, 3 ... m and the prefixes of Y are Y1, 2, 3 ... m. Let LCS (XI, Yj) represent the set of the longest common subsequence of prefixes Xi and Yj. Then: LCS (Xi , Yj) = ∅ ; if i = 0 or j = 0 LCS (Xi , Yj) = LCS(Xi1, Yj1) ^ xi ; if i > 0, j > 0 & xi = yj LCS (Xi , Yj) = max{ LCS (Xi, Yj1), LCS (Xi1, Yj) } ; if i > 0, j > 0 & xi ≠ yj If the last characters match, then the sequence LCS (Xi1 , Yj1) is extended by that matching character xi. Otherwise, the best result from LCS (Xi , Yj1) and LCS (Xi1 , Yj) is used. In the recursive method, we compute some subproblem, divide it, and repeatedly perform this kind of task. So it's a simple but very tedious method. The time complexity in recursive solution is more. The worstcase time complexity is exponential O (2^n), and auxiliary space used by the program is O (1). In X, if the last and first characters are the same  X (0, n  1) = X (1, n  2) + 2 If not, then X (0, n  1) = MAX (X (1, n  1), X (0, n  2)). CODE  (IN JAVA) OUTPUT  The length of the Longest Palindromic Subsequence is 11 The Longest Palindromic Subsequence is BDADBCBDADB Now, if we were to combine all the above cases into a mathematical equation: We call the original sequence X = (x1x2 ... xm ) and reverse as Y = ( y1y2 ... ym). Here, the prefixes of X are X1, 2, 3 ... m and the prefixes of Y are Y1, 2, 3 ... m. Let LCS (XI, Yj) represent the set of the longest common subsequence of prefixes Xi and Yj. Then: LCS (Xi , Yj) = ∅ ; if i = 0 or j = 0 LCS (Xi , Yj) = LCS(Xi1, Yj1) ^ xi ; if i > 0, j > 0 & xi = yj LCS (Xi , Yj) = max{ LCS (Xi, Yj1), LCS (Xi1, Yj) } ; if i > 0, j > 0 & xi ≠ yj If the last characters match, then the sequence LCS (Xi1 , Yj1) is extended by that matching character xi. Otherwise, the best result from LCS (Xi , Yj1) and LCS (Xi1 , Yj) is used. In the recursive method, we compute some subproblem, divide it, and repeatedly perform this kind of task. So it's a simple but very tedious method. The time complexity in recursive solution is more. The worstcase time complexity is exponential O (2^n), and auxiliary space used by the program is O (1). In X, if the last and first characters are the same  X (0, n  1) = X (1, n  2) + 2 If not, then X (0, n  1) = MAX (X (1, n  1), X (0, n  2)). CODE  (IN JAVA) OUTPUT  The length of the Longest Palindromic Subsequence is 11 The Longest Palindromic Subsequence is BDADBCBDADB Optimal Substructure  It satisfies overlapping subproblem properties. In a two dimensional array, Longest Common Subsequence can be made as a memo, where LCS[X][Y] represents the length between original with length X and reverse, with length. The longest palindromic subsequence can be generated by backtracking technique, after filling and using the above algorithm
Next TopicLongest Increasing Subsequence
