The Gateway to Computer Science Excellence

First time here? Checkout the FAQ!

x

+21 votes

The subset-sum problem is defined as follows. Given a set of $n$ positive integers, $S = \{ a_1, a_2, a_3, \dots , a_n \}$, and positive integer $W$, is there a subset of $S$ whose elements sum to $W$? A dynamic program for solving this problem uses a $\text{2-dimensional}$ Boolean array, $X$, with $n$ rows and $W+1$ columns. $X[i, j], 1 \leq i \leq n, 0 \leq j \leq W$, is TRUE, if and only if there is a subset of $\{a_1, a_2, \dots, a_i\}$ whose elements sum to $j$.

Which of the following is valid for $2 \leq i \leq n$, and $a_i \leq j \leq W$?

- $X[i, j] = X[i-1, j] \vee X[i, j-a_i]$
- $X[i, j] = X[i-1, j] \vee X[i-1, j-a_i]$
- $X[i, j] = X[i-1, j] \wedge X[i, j-a_i]$
- $X[i, j] = X[i-1, j] \wedge X[i-1, j-a_i]$

+9 votes

Best answer

This is analogous to the dynamic programming solution to $0/1$ knapsack problem.

Consider the capacity of the knapsack, i.e., $W$ to be analogous to $J$(the total sum here).

The solution **exploits the optimal substructure **of the problem.

At each stage we can have $2$ options:

Case $(1)$: Either we take an item(in this question either we consider the element $A_i$) along with the total solution to previous sub-problem(total solution here means the total sum obtained till previous sub-problem)

in which case we choose $A[i-1]$$\left [ j-a_{i} \right ]$

**$A[i-1]$** indicates we are considering solution to previous subproblem and

**$A[j-a_i]$** means we have considered element $a_i$ and now remaining sum is $J-a_i$ which has to be further considered.

Case $(2)$: Or we do not consider the item(in this case the element $a_{i}$) in which case we only consider the solution to previous subproblem, which is,

$A[i-1][J]$

Since the whole solution to this subset-sum problem is Logical OR(+) of cases 1 and 2, we eliminate options $C$ and $D$ because both are considering the Logical AND of the two parts of the solution.

Now, since here in the given question we are given a boolean array $X[n][W+1]$

So, an entry $X[i][j]$ is true only if sum $j$ is possible with array elements from $0$ to $i$.

So, for Each element of array, $a_{i}$, we consider two possibilites:

(1) EIther we can ignore it and still get our possible sum, which is,

$X[i-1][j]$

OR

($2$) We could include element $a_i$ and then get our required sum, which is,

$X[i-1][j-a_i]$

And finally, to get $X[i][j]$, we take logical or of the above two cases.

Hence, **answer is option B.**

Reference :

By using the analogy of the problem and solution between subset-sum problem and $0/1$ knapsack problem, the above video clearly explains the how the solution to the problem is structured .

+8 votes

I think answers are

(B)

$X[i][j] = X[i-1][j] \cup X[i-1][j-a_i]$

$\text{ We calculate the value of } X[i][j] \text{ as if we include the value }$

$ a_i \text{ in the subset } X[i-1][j-a_i] \text{ or we do not include the value in the subset} X[i-1][j].$

(C)

// Returns true if there is a subset of set[] with sun equal to given sum 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[sum+1][n+1]; // If sum is 0, then answer is true for (int i = 0; i <= n; i++) subset[0][i] = true; // If sum is not 0 and set is empty, then answer is false for (int i = 1; i <= sum; i++) subset[i][0] = false; // Fill the subset table in botton up manner for (int i = 1; i <= sum; i++) { for (int j = 1; j <= n; j++) { subset[i][j] = subset[i][j-1]; if (i >= set[j-1]) subset[i][j] = subset[i][j] || subset[i - set[j-1]][j-1]; } } /* // uncomment this code to print table for (int i = 0; i <= sum; i++) { for (int j = 0; j <= n; j++) printf ("%4d", subset[i][j]); printf("\n"); } */ return subset[sum][n]; }

- All categories
- General Aptitude 1.4k
- Engineering Mathematics 6k
- Digital Logic 2.3k
- Programming & DS 4.3k
- Algorithms 3.7k
- Theory of Computation 4.6k
- Compiler Design 1.7k
- Databases 3.4k
- CO & Architecture 2.9k
- Computer Networks 3.4k
- Non GATE 1.2k
- Others 1.3k
- Admissions 506
- Exam Queries 482
- Tier 1 Placement Questions 22
- Job Queries 64
- Projects 16

40,976 questions

47,609 answers

146,779 comments

62,342 users