Question:hard

Consider a table \(T\), where the elements \(T[i][j]\), \(0 \leq i, j \leq n\), represent the cost of the optimal solutions of different subproblems of a problem that is being solved using a dynamic programming algorithm. The recursive formulation to compute the table entries is as follows:

\[ T[0][k] = T[k][0] = 1 \quad \text{for } k = 0,1,2,\dots,n \] \[ T[i][j] = 2T[i-1][j] + 3T[i][j-1] \quad \text{for } 1 \leq i,j \leq n \]
Consider the following two algorithms to compute entries of \(T\). Assume that for both the algorithms, for all \(0 \leq i,j \leq n\), \(T[i][j]\) has been initialized to 1.

Algorithm \(B_1\):
For i = 1, 2, ..., n
    For j = 1, 2, ..., n
        T[i][j] = 2*T[i-1][j] + 3*T[i][j-1]

Algorithm \(B_2\):
For s = 2, 3, ..., 2n
    For i = 1, 2, ..., n
        For j = 1, 2, ..., n
            If (i + j == s)
                T[i][j] = 2*T[i-1][j] + 3*T[i][j-1]

Algorithm \(B_k\), \(k \in \{1,2\}\) is said to be correct if and only if it calculates the correct values of \(T[i][j]\), for all \(0 \leq i,j \leq n\), (as per the recursive formulation) at the end of the execution of the algorithm \(B_k\).

Which one of the following statements is true?

Show Hint

Check whether each algorithm always computes \(T[i-1][j]\) and \(T[i][j-1]\) before it computes \(T[i][j]\); B1 goes row by row and B2 goes diagonal by diagonal (\(i+j\) = constant).
Updated On: Jul 22, 2026
  • Both algorithms \(B_1\) and \(B_2\) are correct
  • Algorithm \(B_1\) is correct, but algorithm \(B_2\) is incorrect
  • Algorithm \(B_2\) is correct, but algorithm \(B_1\) is incorrect
  • Both algorithms \(B_1\) and \(B_2\) are incorrect
Show Solution

The Correct Option is A

Solution and Explanation

Step 1: Observe the dependency graph.
Each entry \(T[i][j]\) depends only on the two neighboring entries directly above and to the left, namely \(T[i-1][j]\) and \(T[i][j-1]\). Therefore, any traversal that guarantees these two predecessor cells are computed before visiting \(T[i][j]\) will correctly evaluate the entire table.

Step 2: Analyze Algorithm \(B_1\).
Algorithm \(B_1\) traverses the table from the first row to the last and, within each row, from the first column to the last. As a result, whenever \(T[i][j]\) is processed, the cell above it belongs to a row that has already been completed, while the cell to its left was computed earlier in the current row. Since both prerequisite entries are available, the recurrence can always be applied correctly. Thus, \(B_1\) produces the correct values for every table entry.

Step 3: Analyze Algorithm \(B_2\).
Algorithm \(B_2\) groups cells according to the value of \(i+j\), processing one diagonal at a time. Every predecessor of \(T[i][j]\) has index sum \(i+j-1\), meaning both required cells belong to the immediately preceding diagonal. Since diagonals are processed in increasing order, the entire previous diagonal is completed before the current one begins. Consequently, every dependency is satisfied before a cell is evaluated, making \(B_2\) correct as well.

Step 4: Verify the boundary conditions.
The algorithms update only the entries with \(1 \le i,j \le n\). The initialized boundary values \(T[0][k]=T[k][0]=1\) are never modified, so the recurrence always starts from the correct base cases.

Step 5: Draw the conclusion.
Both traversals respect the dependency structure of the dynamic programming recurrence. Neither algorithm attempts to compute a cell before its required predecessor entries are available. Therefore, each algorithm correctly constructs the entire table \(T\).

Final Answer:
Both algorithms satisfy the dependency requirements of the recurrence and compute the table correctly. \[ \boxed{\text{Both algorithms } B_1 \text{ and } B_2 \text{ are correct (Option A).}} \]
Was this answer helpful?
0

Questions Asked in GATE CS exam