Dynamic Programming
![Question 80, GATE 2008: Given positive integers S={a₁,…,aₙ} and target W>0, a subset-sum dynamic program uses Boolean X with n rows and W+1 columns. X[i,j] is true iff some subset of {a₁,…,aᵢ} sums to j, for 1≤i≤n and 0≤j≤W. Which recurrence is valid for 2≤i≤n and aᵢ≤j≤W?
(A) X[i,j]=X[i−1,j]∨X[i,j−aᵢ]
(B) X[i,j]=X[i−1,j]∨X[i−1,j−aᵢ]
(C) X[i,j]=X[i−1,j]∧X[i,j−aᵢ]
(D) X[i,j]=X[i−1,j]∧X[i−1,j−aᵢ]](../../assets/questions/CS-2008-Q80-1.webp)
![Question 80, GATE 2008: Given positive integers S={a₁,…,aₙ} and target W>0, a subset-sum dynamic program uses Boolean X with n rows and W+1 columns. X[i,j] is true iff some subset of {a₁,…,aᵢ} sums to j, for 1≤i≤n and 0≤j≤W. Which recurrence is valid for 2≤i≤n and aᵢ≤j≤W?
(A) X[i,j]=X[i−1,j]∨X[i,j−aᵢ]
(B) X[i,j]=X[i−1,j]∨X[i−1,j−aᵢ]
(C) X[i,j]=X[i−1,j]∧X[i,j−aᵢ]
(D) X[i,j]=X[i−1,j]∧X[i−1,j−aᵢ]](../../assets/questions/CS-2008-Q80-2.webp)
Your answer
Answer & solution
Answer: B (no official key was published)
1. Consider whether the chosen subset contains aᵢ. If not, the first i−1 elements alone must sum to j, represented by X[i−1,j].
2. If aᵢ is included, remove it once; the remaining subset of the first i−1 elements must sum to j−aᵢ, represented by X[i−1,j−aᵢ].
3. Either alternative suffices, so combine with OR, giving B. Using row i in the second term allows repeated copies of aᵢ; AND incorrectly requires both choices.