Question

In: Computer Science

(General guidance: in responding to the following four questions, do not simply provide pseudo-code. Rather explain...

(General guidance: in responding to the following four questions, do not simply provide pseudo-code. Rather explain the main insight in such a way that an experienced programmer could devise an implementation of the algorithm on the basis of your explanation. Indeed, pseudo-code may not be necessary at all.)

Give an algorithm that takes as input a string and returns the length of its longest palindromic subsequence.

Solutions

Expert Solution

Solution:

The naive solution would be to generate all possible subsequence string with the given string and then checking wether that subsequence string is palindrome or not.If it is a palindrome then we should compare it's length with the longest palindromic string encountered till now and update the maximum length if the length of current subsequence is greater than previous longest palindromic subsequence ecountered.

So, if the length of the string is n then there are 2n subsequences and we have to check them all. Therefore using the naive approach time compexity will be exponentail.

Let arr[0..n-1] be the input sequence of length n and len(0, n-1) be the length of the longest palindromic subsequence of arr[0..n-1].

If first and last characters of arr are same

len(0, n-1) = len(1, n-2) + 2.
Else

L(0, n-1) = MAX (len(1, n-1), len(0, n-2)).

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

(Handling all cases of the problem)

// Every single character is a palindrome of length 1

len(i, i) = 1 for all indexes i in given sequence   

// IF first and last characters are not same

If (arr[i] != arr[j]) len(i, j) = max{len(i + 1, j), len(i, j - 1)}

// If there are only 2 characters and both are same

Else if (j == i + 1) len(i, j) = 2

// If there are more than two characters, and first and last

// characters are same

Else len(i, j) = len(i + 1, j - 1) + 2

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

We can further optimise the solution using Dynamic Programming approach as there are many repetitive recursive call which are made. Recomputations of same subproblems can be avoided by constructing a temporary array len[][] in a bottom-up manner.


Related Solutions

(General guidance: in responding to the following four questions, do not simply provide pseudo-code. Rather explain...
(General guidance: in responding to the following four questions, do not simply provide pseudo-code. Rather explain the main insight in such a way that an experienced programmer could devise an implementation of the algorithm on the basis of your explanation. Indeed, pseudo-code may not be necessary at all.) Give an O(n) algorithm that determines (1) the number of subsequences of an input vector that sum to an even number and (2) ) the number of subsequences of an input vector...
Write pseudo-code to solve the following problem using MapReduce and explain how it works. Each line...
Write pseudo-code to solve the following problem using MapReduce and explain how it works. Each line in the file lists a user ID, the ID of the movie the user watched, the rating the user gave for the movie, and the timestamp. For example line 1 indicates that the user’s ID is 196, the movie ID is 242, the user gave this movie a rating of 3, and the timestamp is 881250949. Given the file, find out the top similar...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar questions. Please post in a format that can be directly copied. Reasoning on answers would be most helpful but not required. Thank you in advance for your help. 1. List the following functions according to their order of growth from the lowest to the highest: (n−2)!, 5lg(n+100)10, 22n, 0.001n4 +3n3 +1, ln2 n, √3 n, 3n. 2. The range of afinite nonempty set of...
Use pseudo code to plan a programming solution for the following: A GUI interface to ensure...
Use pseudo code to plan a programming solution for the following: A GUI interface to ensure a user is old enough to play a game. See additional resources above for additional resources. Properly formatted prompts to input name, address, phone number, and age. Instructions to ensure that the information is displayed back to the user. Also, the user must be 21. So, include instructions to display an appropriate message about whether or not the user can continue. Instructions that will...
Develop the pseudo code for, desk check and test the following programs. Allow the user to...
Develop the pseudo code for, desk check and test the following programs. Allow the user to provide the number of elements, and initialise the array according to their input. Use functions where appropriate to ensure modularity. 1. Find the average of a list of numbers 2. Find the smallest element in a list of numbers
For the following two questions you need to provide a code in C 1. Build a...
For the following two questions you need to provide a code in C 1. Build a double linked list with 5 in the first node following the instructions: Insert a node with 3 at the top of the list Insert a node with 10 at the bottom of the list Insert a node with 7 between nodes with 5 and 10 2. Start deleting nodes from the list in the following order; Delete the node with 7 Delete the node...
What will be the expected output of the following pseudo code? Write exactly what would display...
What will be the expected output of the following pseudo code? Write exactly what would display when you execute the statements. Module main() Declare Integer a = 5 Declare Integer b = 2 Declare Integer c = 3 Declare Integer result = 0 Display "The value of result is" Display result Set result = a + b * c - a Display "Changed value is: ", result End Module
(Artificial Intelligence) Write a pseudo code for the following: Regular Hill Climbing with steepest ascent
(Artificial Intelligence) Write a pseudo code for the following: Regular Hill Climbing with steepest ascent
For the following program descriptions, write step by step pseudo code that shows you understand the...
For the following program descriptions, write step by step pseudo code that shows you understand the problem and what it takes to solve it. The first one is done for you as an example. Please answer the questions in the same format as the example problem below so it is the same. Example #1 Problem A customer is purchasing five items. Design a program where you collect the amount of each item, calculate the subTotal of the items, the tax...
In 100-200 entry responding to these questions. 1. Do you use credit in the form of...
In 100-200 entry responding to these questions. 1. Do you use credit in the form of credit cards or other forms of lending? Why or why not? 2. What personal convictions do you have about the use of credit? 3. What personal challenges do you see in yourself in the use of credit?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT