Question

In: Computer Science

The subset-sum problem is defined as follows. Given a set of n positive integers, S =...

The subset-sum problem is defined as follows. Given a set of n positive integers, S = {a1, a2, a3, ..., an} and positive integer W, is there a subset of S whose elements sum to W? Design a dynamic program to solve the problem. (Hint: uses a 2-dimensional Boolean array X, with n rows and W+1 columns, i.e., X[i, j] = 1,1 <= i <= n, 0 <= j <= W, if and only if there is a subset of {a1, a2, ..., ai} whose elements sum to j). Justify your answer

PLEASE HELP

Solutions

Expert Solution

The function returns true if a subset exists and false if subset doesn't exist

bool isSubsetSum(int set[], int n, int sum)
{
// The value of subset[i][j] will be true if there is a
// subset of set[0..j-1] with sum equal to i
bool subset[n+1][sum+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 <= sum; 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 <= sum; 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]];
}
}
return subset[n][sum];
}


Related Solutions

Given an array of positive integers a, your task is to calculate the sum of every...
Given an array of positive integers a, your task is to calculate the sum of every possible a[i] ∘a[j], where a[i]∘a[j] is the concatenation of the string representations of a[i] and a[j] respectively. Example For a = [10, 2], the output should be concatenationsSum(a) = 1344. a[0] ∘a[0] = 10 ∘10 = 1010, a[0] ∘a[1] = 10 ∘2 = 102, a[1] ∘a[0] = 2 ∘10 = 210, a[1] ∘a[1] = 2 ∘2 = 22. So the sum is equal to...
Let Dn be the set of positive integers that divide evenly into n. List the elements...
Let Dn be the set of positive integers that divide evenly into n. List the elements of each of the sets D6, D16, D12, and D30
1. Write MIPS assembly code to sum “n” positive integers. Forexample, n=5, read 5 numbers...
1. Write MIPS assembly code to sum “n” positive integers. For example, n=5, read 5 numbers in memory (“.data” section) and add them together. 2. Write MIPS assembly code to calculate N-factorial. Read n from the memory. (Hint: use “.data” to define the content in a memory location)
The question is correct. Let X be an n-element set of positive integers each of whose...
The question is correct. Let X be an n-element set of positive integers each of whose elements is at most (2n - 2)/n. Use the pigeonhole principle to show that X has 2 distinct nonempty subsets A ≠ B with the property that the sum of the elements in A is equal to the sum of the elements in B.
C++ program which partitions n positive integers into two disjoint sets with the same sum. Consider...
C++ program which partitions n positive integers into two disjoint sets with the same sum. Consider all possible subsets of the input numbers. All in one C++ file. This is the sample Input 1 6 3 5 20 7 1 14 Output 1 Equal Set: 1 3 7 14 This is the sample Input 2 5 10 8 6 4 2 Output 2 Equal Set: 0
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether we can use numbers from c[0...n − 1] to make a sum of v, allowing any particular number in the list to be used multiple times. Or, mathematically, this means that there exists non-negative integer coefficients, x0, x1, ..., xn−1, such that v = x0c[0] + x1c[1] + ...xn−1c[n − 1]. For example, given c[0...3] = {2, 4, 6, 10}, and v = 17,...
Let S{a, b, c, d} be a set of four positive integers. If pairs of distinct...
Let S{a, b, c, d} be a set of four positive integers. If pairs of distinct elements of S are added, the following six sums are obtained:5,10, 11,13,14,19. Determine the values of a, b, c, and d. (There are two possibilities. )
Consider the relation R defined on the set Z as follows: ∀m, n ∈ Z, (m,...
Consider the relation R defined on the set Z as follows: ∀m, n ∈ Z, (m, n) ∈ R if and only if m + n = 2k for some integer k. For example, (3, 11) is in R because 3 + 11 = 14 = 2(7). (a) Is the relation reflexive? Prove or disprove. (b) Is the relation symmetric? Prove or disprove. (c) Is the relation transitive? Prove or disprove. (d) Is it an equivalence relation? Explain.
Statement: For a given integer N, print all the squares of positive integers where the square...
Statement: For a given integer N, print all the squares of positive integers where the square is less than or equal to N, in ascending order. Programming Tasks: Prompt the user to input the value of N Output to the screen all squares of positive integers <= N Tests: Item Test 1 Test 2 Test 3 Inputs: 50 9 100 Outputs: 1 4 9 16 25 36 49 1 4 9 1 4 9 16 25 36 49 64 81...
Envision an algorithm that when given any positive integer n, it will print out the sum...
Envision an algorithm that when given any positive integer n, it will print out the sum of the squares from 1 to n. E.g. given 4 the algorithm would print 30 (because 1 + 4 + 9 + 16 = 30) You can use multiplication denoted as * in your solution and you do not have to define it (e.g. 2*2=4) Write pseudocode for this algorithm using iteration (looping). Create a flow chart Implement solution from flowchart in Python at...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT