Question

In: Computer Science

5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, ....

5. Design a dynamic programming algorithm to solve the following problem.

Input: An array A[1, . . . , n] of positive integers, an integer K.

Decide: Are there integers in A such that their sum is K. (Return T RUE or F ALSE)

Example: The answer is TRUE for the array A = [1, 2, 3] and 5, since 2 + 3 = 5. The answer is FALSE for A = [2, 3, 4] and 8. Note that you have to solve this problem for any array of size n and any integer K. Do not just solve the problem for the sample inputs above.

Explain

• what subproblems one must solve for this problem,

• what the recursive solution is, and

• the algorithm in pseudocode format. Explain your work in detail. Answers without explanation will not receive full credit. (20 pt)

Solutions

Expert Solution

Here's a code in C:

// A Dynamic Programming solution
// for subset sum problem
#include <stdio.h>

// Returns true if there is a subset of set[]
// with sum equal to given k
bool isSubsetSum(int set[], int n, int k)
{
   // The value of subset[i][j] will be true if
   // there is a subset of set[0..j-1] with k
   // equal to i
   bool subset[n + 1][k + 1];

   // If sum is 0, then answer is true
   for (int i = 0; i <= n; i++)
       subset[i][0] = true;

   // If sum is not 0 and set is empty,
   // then answer is false
   for (int i = 1; i <= k; i++)
       subset[0][i] = false;

   // Fill the subset table in botton up manner
   for (int i = 1; i <= n; i++) {
       for (int j = 1; j <= k; j++) {
           if (j < set[i - 1])
               subset[i][j] = subset[i - 1][j];
           if (j >= set[i - 1])
               subset[i][j] = subset[i - 1][j]
                           || subset[i - 1][j - set[i - 1]];
       }
   }

   /* // uncomment this code to print table
   for (int i = 0; i <= n; i++)
   {
   for (int j = 0; j <= k; j++)
       printf ("%4d", subset[i][j]);
   printf("\n");
   }*/

   return subset[n][k];
}

// Driver program to test above function
int main()
{
   int set[] = { 3, 34, 4, 12, 5, 2 };
   int k = 9;
   int n = sizeof(set) / sizeof(set[0]);
   if (isSubsetSum(set, n, k) == true)
       printf("TRUE");
   else
       printf("FALSE");
   return 0;
}

i hope you understood the concept well. if you did, please click on the 'like' button.

Also if you have any doubt regarding any concept taught above, do mention in the comments. i will try to answer it as soon as possible.


Related Solutions

Problem 2. Purpose: practice algorithm design using dynamic programming. A subsequence is palindromic if it is...
Problem 2. Purpose: practice algorithm design using dynamic programming. A subsequence is palindromic if it is the same whether read left to right or right to left. For instance, the sequence A,C,G,T,G,T,C,A,A,A,A,T,C,G has many palindromic subsequences, including A,C,G,C,A and A,A,A,A (on the other hand, the subsequence A,C,T is not palindromic). Assume you are given a sequence x[1...n] of characters. Denote L(i,j) the length of the longest palindrome in the substring x[i,...,j]. The goal of the Maximum Palindromic Subsequence Problem (MPSP)...
Design and analyze asymptotically an O(nlgn) transform-conquer algorithm for the following problem: input: an array A[lo..hi]...
Design and analyze asymptotically an O(nlgn) transform-conquer algorithm for the following problem: input: an array A[lo..hi] of n real values; output: true iff the array contains two elements (at different indices) whose sum is 2020.
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:...
Recall the dynamic programming algorithm we saw in class for solving the 0/1 knapsack problem for...
Recall the dynamic programming algorithm we saw in class for solving the 0/1 knapsack problem for n objects with a knapsack capacity of K. In particular, we characterized our recurrence OPT(j, W) to be following quantity: OPT(j, W) := The maximum profit that can be obtained when selecting from objects 1, 2, . . . , j with a knapsack capacity of W , where (after filling in our dynamic programming table), we return the value stored at OPT(n, K)...
Solve the following problem by Dynamic Programming: Maximize z = (y1 + 2)^2 + y2 *...
Solve the following problem by Dynamic Programming: Maximize z = (y1 + 2)^2 + y2 * y3 + (y4 - 5)^2 subject to y1 + y2 + y3 + y4 <= 5 yi >= 0 and integer, i = 1, 2, 3, 4
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++....
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output examples for duplicate-free arrays...
C programming problem I have to design an efficient algorithm which solves this problem. Also, it...
C programming problem I have to design an efficient algorithm which solves this problem. Also, it needs to include time and space complexities of this algorithm. There is a matrix (N times N) of integers which rows and columns are sorted in non-decreasing order. A sorted matrix and integer M will be given and we need to find the position(row,column) of M in this matrix. it's okay to report only one position if there are more than one answers. when...
(C programming) Use a one-dimensional array to solve the following problem. Read in 20 numbers, each...
(C programming) Use a one-dimensional array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it’s not a duplicate of a number already read. Provide for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve this problem. Your solution must include a function called isUnique() that returns 1 (true) if the number input is...
give an algorithm where given as input an array A[1...n] withthe following property. There exists...
give an algorithm where given as input an array A[1...n] with the following property. There exists an index I such that if we append A[1...i−1] after A[i...n], we get an array in sorted increasing order. For simplicity you can assume that n is a power of 2.Give an efficient algorithm that returns the smallest element in A. Analyze the time complexity of your algorithm.Hint: you may want to compare A[1] and A[n/2].
Programming assignment 9 Write a function sortWords(array) that does the following: 1. Takes as an input...
Programming assignment 9 Write a function sortWords(array) that does the following: 1. Takes as an input a cell array of strings consisting of letters only. (1 pts) 2. Returns a cell array with all the strings in alphabetical order. (5 pts) 3. The function should ignore whether letters are lower case or upper case. (2 pts) Test your function with the following: (2 pts) >> sorted=sortWords({’Hello’,’hell’,’abc’,’aa’,’aza’,’aab’,’AaBb’,’a’}) sorted = ’a’ ’aa’ ’aab’ ’AaBb’ ’abc’ ’aza’ ’hell’ ’Hello’ Note: Your function may...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT