Skip to content
Official PYQ · 2008

GATE CS 2008 Q80 — Algorithm Design: Dynamic Programming

Explain with AI

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

Which of the following is valid for 2≤i≤n2 \leq i \leq n, and ai≤j≤Wa_i \leq j \leq W?

Choose one option
View worked solution

Answer: B

To find the recurrence relation for X[i,j]X[i, j], we analyze the choices available for the element aia_i when trying to form a subset of the first ii elements, {a1,a2,…,ai}\{a_1, a_2, \dots, a_i\}, that sums to jj:

  1. Exclude aia_i: If we do not include aia_i in the subset, we must form the sum jj using only the first i−1i-1 elements. This is possible if and only if:
    X[i−1,j]=TRUEX[i-1, j] = \text{TRUE}

  2. Include aia_i: If we include aia_i in the subset (which is allowed since ai≤ja_i \le j), the remaining sum to be formed is j−aij - a_i. This remaining sum must be formed using the first i−1i-1 elements. This is possible if and only if:
    X[i−1,j−ai]=TRUEX[i-1, j-a_i] = \text{TRUE}

Since the subset-sum problem allows each element to be chosen at most once, and the overall problem is solvable if either of the two choices is successful, we combine them using the logical OR (∨\vee) operator:

X[i,j]=X[i−1,j]∨X[i−1,j−ai]X[i, j] = X[i-1, j] \vee X[i-1, j-a_i]

Thus, Option B is the correct recurrence relation.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Algorithm Design: Dynamic Programming questions