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

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.
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. )
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...
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.
1. "Give an example of a function that is defined on the set of integers that...
1. "Give an example of a function that is defined on the set of integers that is not a one-to-one function." Keep in mind that the above domain must be the set of integers. Identify what your codomain is, too. 2. "Give an example of a function that is defined on the set of rational numbers that is not an onto function." The above domain must be the set of rational numbers. Identify what your codomain is, too. This is...
Find two positive integers such that the sum of the first number and four times the...
Find two positive integers such that the sum of the first number and four times the second number is 100 and the product of the numbers is as large as possible. please double check answer
Prove that the following is true for all positive integers n: n is odd if and...
Prove that the following is true for all positive integers n: n is odd if and only if n2 is odd.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT