Question:

Consider a table 𝑇, where the elements 𝑇[𝑖][𝑗], 0 ≀𝑖, 𝑗≀𝑛, 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:
𝑇[0][π‘˜] = 𝑇[π‘˜][0] = 1 for π‘˜= 0,1,2, … , 𝑛
𝑇[𝑖][𝑗] = 2𝑇[π‘–βˆ’1][𝑗] + 3𝑇[𝑖][π‘—βˆ’1] for 1 ≀𝑖, 𝑗≀𝑛
Consider the following two algorithms to compute entries of 𝑇. Assume that for
both the algorithms, for all 0 ≀𝑖, 𝑗≀𝑛, 𝑇[𝑖][𝑗] has been initialized to 1.
Algorithm π΅π‘˜, π‘˜βˆˆ{1,2} is said to be correct if and only if it calculates the correct
values of 𝑇[𝑖][𝑗], for all 0 ≀𝑖, 𝑗≀𝑛, (as per the recursive formulation) at the end
of the execution of the algorithm π΅π‘˜.
Which one of the following statements is true?

Show Hint

Both algorithms only ever use \(T[i-1][j]\) and \(T[i][j-1]\) after those entries are finalized -- B1 via row-major order, B2 via increasing diagonal sum \(i+j\) -- so both compute the table correctly.
Updated On: Jul 7, 2026
  • Both algorithms 𝐡1 and 𝐡2 are correct
  • Algorithm 𝐡1 is correct, but algorithm 𝐡2 is incorrect
  • Algorithm 𝐡2 is correct, but algorithm 𝐡1 is incorrect
  • Both algorithms 𝐡1 and 𝐡2 are incorrect Algorithm 𝐡2: For 𝑠= 2, 3, … , 2𝑛 For 𝑖= 1, 2, … , 𝑛 For 𝑗= 1, 2, … , 𝑛 If (𝑖+ 𝑗== 𝑠) 𝑇[𝑖][𝑗] = 2𝑇[π‘–βˆ’1][𝑗] + 3𝑇[𝑖][π‘—βˆ’1] Algorithm 𝐡1: For 𝑖= 1, 2, … , 𝑛 For 𝑗= 1, 2, … , 𝑛 𝑇[𝑖][𝑗] = 2𝑇[π‘–βˆ’1][𝑗] + 3𝑇[𝑖][π‘—βˆ’1]
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation

Step 1: The recurrence given is \(T[0][k]=T[k][0]=1\) for all \(k\), and \(T[i][j] = 2T[i-1][j] + 3T[i][j-1]\) for \(1 \leq i,j \leq n\). To compute \(T[i][j]\) correctly, both \(T[i-1][j]\) and \(T[i][j-1]\) must already hold their final correct values before \(T[i][j]\) is computed.
Step 2: Algorithm B1 uses a simple nested loop: outer loop over \(i = 1, \dots, n\), inner loop over \(j = 1, \dots, n\), computing \(T[i][j]\) directly. The computation order is \((1,1),(1,2),\dots,(1,n),(2,1),(2,2),\dots\). When \(T[i][j]\) is computed, \(T[i-1][j]\) was already computed in the previous outer iteration (\(i-1 < i\) in index terms), and \(T[i][j-1]\) was already computed earlier in the same outer iteration (\(j-1\) before \(j\)). So every dependency is satisfied before use, and B1 is correct.
Step 3: Algorithm B2 loops over diagonals \(s = i+j\) from \(2\) to \(2n\), and for each \(s\), scans all \((i,j)\) pairs with \(i+j = s\) and computes \(T[i][j]\). Both dependencies \(T[i-1][j]\) and \(T[i][j-1]\) have index sum \(s-1\), which is strictly smaller than \(s\), so they were fully computed in an earlier diagonal iteration before the current diagonal \(s\) is processed. Hence B2 is also correct.
Step 4: Both algorithms respect the natural dependency structure of the recurrence (each entry needs only entries with smaller row index or smaller column index), just processed in two different valid orders -- row-major order for B1 and anti-diagonal order for B2. Since both orders never use an entry before it is finalized, both compute every \(T[i][j]\) exactly as required by the recurrence.
Step 5: Therefore both B1 and B2 correctly compute the table \(T\).
Final Answer: \(\boxed{\text{Both algorithms } B_1 \text{ and } B_2 \text{ are correct}}\)
Was this answer helpful?
0
0

Top GATE CS Computer Science and IT Engineering Questions

View More Questions