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.
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...
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].
(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...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array of signed integers. Write a program that creates an Array container of size 100 and fills it with random numbers in the range [1..999]. (Use std::rand() with std::srand(1).) When building the array, if the random number is evenly divisible by 3 or 5, store it as a negative number. Within your main.cpp source code file, write a function for each of the following processes....
1. Purpose: Apply various algorithm design strategies to solve a problem, practice formulating and analyzing algorithms,...
1. Purpose: Apply various algorithm design strategies to solve a problem, practice formulating and analyzing algorithms, implement an algorithm. In the US, coins are minted with denominations of 50, 25, 10, 5, and 1 cent. An algorithm for making change using the smallest possible number of coins repeatedly returns the biggest coin smaller than the amount to be changed until it is zero. For example, 17 cents will result in the series 10 cents, 5 cents, 1 cent, and 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT