Question

In: Computer Science

The following two questions can be solved by dynamic programming. For each question, please describe optimal...

The following two questions can be solved by dynamic programming. For each question, please describe optimal substructure and express recurrence relation. Give pseudo-code and analyze time and space complexity of your algorithm.

1. Longest palindrome subsequence A palindrome is a nonempty string over some alphabet that reads the same forward and backward. Examples of palindromes are all strings of length 1, “civic”, “racecar”, and “aibohphobia” (fear of palindromes). Design a dynamic programming algorithm to find the longest palindrome that is a subsequence of a given input string. For example, given the input “character”, your algorithm should return “carac”.

2. Coin changing Design a dynamic programming algorithm to find the change of n cents using the fewest number of coins given any set of k different coin denominations. For example, coins in America are in denominations of 1, 5, 10, and 25 cents. However, this question does not have any specific denomination. It only assumes that each coin’s value is an integer and one of the coins is a penny.

Solutions

Expert Solution

PROBLEM 1

Dynamic Programming Solution

We can solve this problem using the solution to another similar problem - the Longest Common Subsequence (LCS) problem. The idea is:

  • Reverse the given sequence. Let’s call the original sequence original. Let’s call the reversed sequence reverse.
  • Use the LCS algorithm to find the longest common subsequence between original and reverse. Let LCS(original, reverse) be a function that returns the longest common subsequence between the pair of strings.
  • The answer from LCS will in fact be the longest palindromic subsequence.

More specifically, consider the illustration below:

The last characters match for both original and reverse in the figure above, hence we recur on both of them excluding the last character.

Next, consider the case illustrated below:

In the case where the last characters do not match, we have to solve two subproblems:

  1. Ignore the last character of reverse, and recur on original and updated reverse again.

  2. Ignore the last character of original and recur on the updated original and reverse again.

Select the best result from these two subproblems.

Optimal Substructure:-

The solution to this problem satisfies the overlapping subproblem property, as shown in the diagram below. The diagram is a partial LCS of "PXYM" and "PYQX":

The boxes in purple above are the same subproblem - this means we do not need to solve it twice. Using memoization, overlapping subproblems are solved only once and their result is stored to be used if the subproblem shows up again.

For this problem, a two dimensional array LCS can be made as a memo, where LCS[i][j] represents the length of the longest common subsequence between original with length i and reverse, with length j. After populating the memo using the above algorithm, the longest palindromic subsequence can be reconstructed using backtracking on the memo.

Problem 2

Given a value N, if we want to make change for N cents, and we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins, how many ways can we make the change? The order of coins doesn’t matter.

For example, for N = 4 and S = {1,2,3}, there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}. So output should be 4. For N = 10 and S = {2, 5, 3, 6}, there are five solutions: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. So the output should be 5.

1) Optimal Substructure
To count the total number of solutions, we can divide all set solutions into two sets.
1) Solutions that do not contain mth coin (or S[m]).
2) Solutions that contain at least one S[m].
Let Rec(S[], m, N) be the function to count the number of solutions, then it can be written as sum of Rec(S[], m-1, N) and Rec(S[], m, N-S[m]).

Therefore, the problem has optimal substructure property as the problem can be solved using solutions to subproblems.

Algorithm and pseudo code

We can approach the problem by thinking of a recursive solution and then convert it to dp. We can break this into 2 parts for a particular coin:

  1. we use this coin.
  2. we do not use this coin.

Let us define the function as

int Rec(S[], m, N)

S[] is the set of coins.
m is current coin which you will either include or avoid.
N is the sum you want to make.

The possible states to go from Rec are
Case 1 : You include the coin -> Rec(S[], m, N-S[m])
Case 2 : You avoid the coin -> Rec(S[], m-1, N)

Base Cases are:

  1. N = 0, m >= 0 Since the sum is already been made return 1.
  2. N > 0, m = 0 Sum cannot be made as no coins left return 0.
  3. N < 0, We have overshot the required sum return 0.

Lets say we can make the sum N - S[i] in X ways. Then if we have a coin of value S[i] we can also make a sum of N in X ways. We can memoize the number of ways in which we can make all the sums < N. This can be done by keeping a count array for all sums less than N which gives us the expected space complexity of O(N). A sum of 0 is always possible as we can pick no coins, so the base case will be count[0] = 1
Remember to avoid counting a way more than once. This can be done by choosing the coins in a particular order.


Related Solutions

Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the...
Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the Weightlifting Walrus is training for a contest where it will have to lift 1000 kg. Wallace has some weight plates lying around, possibly of different weights, and its goal is to add some of the plates to a bar so that it can train with a weight as close as possible to 1000 kg. In case there exist two such numbers which are equally...
Using dynamic programming, find an optimal parenthesization of a matrix-chain product of 4 matrices whose dimensions...
Using dynamic programming, find an optimal parenthesization of a matrix-chain product of 4 matrices whose dimensions are p = { 1, 10, 5, 20, 2}. Show your work.
Build a Dynamic Programming algorithm in Python for the following problem. Given two sequences of vowels,...
Build a Dynamic Programming algorithm in Python for the following problem. Given two sequences of vowels, find the length of longest subsequence present in both of them. NOTE: A subsequence is a sequence that appears in the same order, but not necessarily contiguous. For example, “aei”, “aeo”, “eio”, “aiu”, ... are subsequences of “aeiou”. Sample Input 1: aeiou aiu Sample Output 1: 3 Sample Input 2: aeaiueoiuaeeoeooaauoi aeuioauuuoaeieeaueuiouaiieuiuuuaoueueauaeiauuo Sample Output 2: 16 Sample Input 3: iioioieiaiauaoeoiioiiue iuuueauiaieooaoaaouaaaae Sample Output 3:...
Write 5 questions about Object Oriented Programming in Python. Each question should have 7 options. Please...
Write 5 questions about Object Oriented Programming in Python. Each question should have 7 options. Please provide 7 answer options for EACH question and the select answer for EACH question
a) What is double counting problem? How can it be solved? b) Describe the two sector,...
a) What is double counting problem? How can it be solved? b) Describe the two sector, three sector and four sector circular flow of GDP.
Your experts solved my question: Describe steps that can be taken to develop a healthy menu...
Your experts solved my question: Describe steps that can be taken to develop a healthy menu for the following groups: Infants Toddlers Children Be sure to mention any food safety issues as well as listing special menu items for diverse cultures and those children with special dietary concerns. I would appreciate it if you would send the resources used to answer the questions.
The following questions are to be solved using the table above
  net income $92,600 dividends paid (cash) 50,500 depreciation expense 12,000 Net decrease in current assets 21,400 Issued new notes payable for cash 61,200 Paid cash for building 279,000 Net decrease in current liabilities 5,300 Sold investment for cash 350,000 The following questions are to be solved using the table above 1. What was the net cash flow from operating activities for the year? 2. What was the cash flow from (or used for) investing activities for the year? 3....
QUESTION 1 (10 Marks- 5 each)- This question contains two sub questions a and b. Describe...
QUESTION 1 (10 Marks- 5 each)- This question contains two sub questions a and b. Describe five requirements of the COBIT framework. Compare the followings in terms of capacity, expenses, processing power, size and users they support: Supercomputers Mainframe Computers Comparison Element Supercomputers Mainframe Computers
When can multiple optimal solutions can occur in linear programming problems? Explain.
When can multiple optimal solutions can occur in linear programming problems? Explain.
Please use at least 200 words to describe each of the following questions: To minimize the...
Please use at least 200 words to describe each of the following questions: To minimize the uncertainty in a new instrument for measuring distance, would it be better to calibrate the new instrument with the PDT or the Ultra-sonic Sensor? Also, if possible, discuss how to use the value of the range, total uncertainties, and full-scale error of PDT and The Ultra-Sonic Sensor to select a sensor for a specific application
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT